Skip to content

Commit 70765d7

Browse files
kralickywbpcode
andauthored
filesystem: allow access to /dev/shm/* (#44102)
Commit Message: filesystem: allow access to /dev/shm/* Additional Description: This adds `/dev/shm/*` as an allowed path prefix for the posix filesystem impl. I ran into this issue while running integration tests while having the `--sandbox_base=/dev/shm` bazel flag set, which prevented the tests from accessing any files in their own sandbox. Risk Level: low/moderate. Testing: Updated existing tests. The new assertions only run if /dev/shm actually exists, so the code is #ifdef'd out on macos in case anyone out there is running macos coverage builds. Docs Changes: Release Notes: Platform Specific Features: [Optional Runtime guard:] [Optional Fixes #Issue] [Optional Fixes commit #PR or SHA] [Optional Deprecated:] [Optional [API Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):] --------- Signed-off-by: Joe Kralicky <joekralicky@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: code <wbphub@gmail.com>
1 parent 1424668 commit 70765d7

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

source/common/filesystem/posix/filesystem_impl.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ bool InstanceImplPosix::illegalPath(const std::string& path) {
367367
absl::StartsWith(canonical_path.return_value_, "/proc/") ||
368368
canonical_path.return_value_ == "/dev" || canonical_path.return_value_ == "/sys" ||
369369
canonical_path.return_value_ == "/proc") {
370+
371+
#ifdef __linux__
372+
// Allow /dev/shm/*, which is a de-facto standard tmpfs location on linux. A common use case is
373+
// to set the bazel sandbox_base to /dev/shm, since /tmp is not always backed by memory. Some
374+
// tests may then need to access files in bazel sandboxes under this directory.
375+
if (absl::StartsWith(canonical_path.return_value_, "/dev/shm/") ||
376+
canonical_path.return_value_ == "/dev/shm") {
377+
return false;
378+
}
379+
#endif
380+
370381
return true;
371382
}
372383
return false;

test/common/filesystem/filesystem_impl_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ TEST_F(FileSystemImplTest, IllegalPath) {
230230
EXPECT_TRUE(file_system_.illegalPath("/dev/"));
231231
// Exception to allow opening from file descriptors. See #7258.
232232
EXPECT_FALSE(file_system_.illegalPath("/dev/fd/0"));
233+
234+
if (file_system_.directoryExists("/dev/shm")) {
235+
// Exception for /dev/shm/*
236+
EXPECT_FALSE(file_system_.illegalPath("/dev/shm/"));
237+
EXPECT_FALSE(file_system_.illegalPath("/dev/shm"));
238+
EXPECT_TRUE(file_system_.illegalPath("/dev/shm/_some_non_existent_file"));
239+
// This is not the same special case as /dev/fd; paths will actually need to exist here to be
240+
// considered valid.
241+
std::string shmTempFile = "/dev/shm/envoy_test_XXXXXX";
242+
auto fd = mkstemp(shmTempFile.data());
243+
ASSERT_NE(fd, -1);
244+
auto res = file_system_.illegalPath(shmTempFile);
245+
close(fd);
246+
unlink(shmTempFile.c_str());
247+
EXPECT_FALSE(res);
248+
}
249+
233250
EXPECT_TRUE(file_system_.illegalPath("/proc"));
234251
EXPECT_TRUE(file_system_.illegalPath("/proc/"));
235252
EXPECT_TRUE(file_system_.illegalPath("/sys"));

tools/spelling/spelling_dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ timestamps
15181518
timeval
15191519
tmp
15201520
tmpfile
1521+
tmpfs
15211522
token
15221523
tokenize
15231524
tokenized

0 commit comments

Comments
 (0)