|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include "nix/util/file-descriptor.hh" |
| 4 | +#include "nix/util/file-system.hh" |
| 5 | +#include "nix/util/fs-sink.hh" |
| 6 | +#include "nix/util/processes.hh" |
| 7 | + |
| 8 | +#ifdef __linux__ |
| 9 | +# include "nix/util/linux-namespaces.hh" |
| 10 | + |
| 11 | +# include <sys/mount.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +#include <cstring> |
| 15 | + |
| 16 | +namespace nix { |
| 17 | + |
| 18 | +using namespace nix::unix; |
| 19 | + |
| 20 | +/* ---------------------------------------------------------------------------- |
| 21 | + * openFileEnsureBeneathNoSymlinks |
| 22 | + * --------------------------------------------------------------------------*/ |
| 23 | + |
| 24 | +TEST(openFileEnsureBeneathNoSymlinks, works) |
| 25 | +{ |
| 26 | + std::filesystem::path tmpDir = nix::createTempDir(); |
| 27 | + nix::AutoDelete delTmpDir(tmpDir, /*recursive=*/true); |
| 28 | + |
| 29 | + { |
| 30 | + RestoreSink sink(/*startFsync=*/false); |
| 31 | + sink.dstPath = tmpDir; |
| 32 | + sink.dirFd = openDirectory(tmpDir); |
| 33 | + sink.createDirectory(CanonPath("a")); |
| 34 | + sink.createDirectory(CanonPath("c")); |
| 35 | + sink.createDirectory(CanonPath("c/d")); |
| 36 | + sink.createRegularFile(CanonPath("c/d/regular"), [](CreateRegularFileSink & crf) { crf("some contents"); }); |
| 37 | + sink.createSymlink(CanonPath("a/absolute_symlink"), tmpDir.string()); |
| 38 | + sink.createSymlink(CanonPath("a/relative_symlink"), "../."); |
| 39 | + sink.createSymlink(CanonPath("a/broken_symlink"), "./nonexistent"); |
| 40 | + sink.createDirectory(CanonPath("a/b"), [](FileSystemObjectSink & dirSink, const CanonPath & relPath) { |
| 41 | + dirSink.createDirectory(CanonPath("d")); |
| 42 | + dirSink.createSymlink(CanonPath("c"), "./d"); |
| 43 | + }); |
| 44 | + sink.createDirectory(CanonPath("a/b/c/e")); // FIXME: This still follows symlinks |
| 45 | + ASSERT_THROW( |
| 46 | + sink.createDirectory( |
| 47 | + CanonPath("a/b/c/f"), [](FileSystemObjectSink & dirSink, const CanonPath & relPath) {}), |
| 48 | + SymlinkNotAllowed); |
| 49 | + ASSERT_THROW( |
| 50 | + sink.createRegularFile( |
| 51 | + CanonPath("a/b/c/regular"), [](CreateRegularFileSink & crf) { crf("some contents"); }), |
| 52 | + SymlinkNotAllowed); |
| 53 | + } |
| 54 | + |
| 55 | + AutoCloseFD dirFd = openDirectory(tmpDir); |
| 56 | + |
| 57 | + auto open = [&](std::string_view path, int flags, mode_t mode = 0) { |
| 58 | + return openFileEnsureBeneathNoSymlinks(dirFd.get(), CanonPath(path), flags, mode); |
| 59 | + }; |
| 60 | + |
| 61 | + EXPECT_THROW(open("a/absolute_symlink", O_RDONLY), SymlinkNotAllowed); |
| 62 | + EXPECT_THROW(open("a/relative_symlink", O_RDONLY), SymlinkNotAllowed); |
| 63 | + EXPECT_THROW(open("a/absolute_symlink/a", O_RDONLY), SymlinkNotAllowed); |
| 64 | + EXPECT_THROW(open("a/absolute_symlink/c/d", O_RDONLY), SymlinkNotAllowed); |
| 65 | + EXPECT_THROW(open("a/relative_symlink/c", O_RDONLY), SymlinkNotAllowed); |
| 66 | + EXPECT_THROW(open("a/b/c/d", O_RDONLY), SymlinkNotAllowed); |
| 67 | + EXPECT_EQ(open("a/broken_symlink", O_CREAT | O_WRONLY | O_EXCL, 0666), INVALID_DESCRIPTOR); |
| 68 | + /* Sanity check, no symlink shenanigans and behaves the same as regular openat with O_EXCL | O_CREAT. */ |
| 69 | + EXPECT_EQ(errno, EEXIST); |
| 70 | + EXPECT_THROW(open("a/absolute_symlink/broken_symlink", O_CREAT | O_WRONLY | O_EXCL, 0666), SymlinkNotAllowed); |
| 71 | + EXPECT_EQ(open("c/d/regular/a", O_RDONLY), INVALID_DESCRIPTOR); |
| 72 | + EXPECT_EQ(open("c/d/regular", O_RDONLY | O_DIRECTORY), INVALID_DESCRIPTOR); |
| 73 | + EXPECT_TRUE(AutoCloseFD{open("c/d/regular", O_RDONLY)}); |
| 74 | + EXPECT_TRUE(AutoCloseFD{open("a/regular", O_CREAT | O_WRONLY | O_EXCL, 0666)}); |
| 75 | +} |
| 76 | + |
| 77 | +/* ---------------------------------------------------------------------------- |
| 78 | + * fchmodatTryNoFollow |
| 79 | + * --------------------------------------------------------------------------*/ |
| 80 | + |
| 81 | +TEST(fchmodatTryNoFollow, works) |
| 82 | +{ |
| 83 | + std::filesystem::path tmpDir = nix::createTempDir(); |
| 84 | + nix::AutoDelete delTmpDir(tmpDir, /*recursive=*/true); |
| 85 | + |
| 86 | + { |
| 87 | + RestoreSink sink(/*startFsync=*/false); |
| 88 | + sink.dstPath = tmpDir; |
| 89 | + sink.dirFd = openDirectory(tmpDir); |
| 90 | + sink.createRegularFile(CanonPath("file"), [](CreateRegularFileSink & crf) {}); |
| 91 | + sink.createDirectory(CanonPath("dir")); |
| 92 | + sink.createSymlink(CanonPath("filelink"), "file"); |
| 93 | + sink.createSymlink(CanonPath("dirlink"), "dir"); |
| 94 | + } |
| 95 | + |
| 96 | + ASSERT_EQ(chmod((tmpDir / "file").c_str(), 0644), 0); |
| 97 | + ASSERT_EQ(chmod((tmpDir / "dir").c_str(), 0755), 0); |
| 98 | + |
| 99 | + AutoCloseFD dirFd = openDirectory(tmpDir); |
| 100 | + ASSERT_TRUE(dirFd); |
| 101 | + |
| 102 | + struct ::stat st; |
| 103 | + |
| 104 | + /* Check that symlinks are not followed and targets are not changed. */ |
| 105 | + |
| 106 | + EXPECT_NO_THROW( |
| 107 | + try { fchmodatTryNoFollow(dirFd.get(), CanonPath("filelink"), 0777); } catch (SysError & e) { |
| 108 | + if (e.errNo != EOPNOTSUPP) |
| 109 | + throw; |
| 110 | + }); |
| 111 | + ASSERT_EQ(stat((tmpDir / "file").c_str(), &st), 0); |
| 112 | + EXPECT_EQ(st.st_mode & 0777, 0644); |
| 113 | + |
| 114 | + EXPECT_NO_THROW( |
| 115 | + try { fchmodatTryNoFollow(dirFd.get(), CanonPath("dirlink"), 0777); } catch (SysError & e) { |
| 116 | + if (e.errNo != EOPNOTSUPP) |
| 117 | + throw; |
| 118 | + }); |
| 119 | + ASSERT_EQ(stat((tmpDir / "dir").c_str(), &st), 0); |
| 120 | + EXPECT_EQ(st.st_mode & 0777, 0755); |
| 121 | + |
| 122 | + /* Check fchmodatTryNoFollow works on regular files and directories. */ |
| 123 | + |
| 124 | + EXPECT_NO_THROW(fchmodatTryNoFollow(dirFd.get(), CanonPath("file"), 0600)); |
| 125 | + ASSERT_EQ(stat((tmpDir / "file").c_str(), &st), 0); |
| 126 | + EXPECT_EQ(st.st_mode & 0777, 0600); |
| 127 | + |
| 128 | + EXPECT_NO_THROW((fchmodatTryNoFollow(dirFd.get(), CanonPath("dir"), 0700), 0)); |
| 129 | + ASSERT_EQ(stat((tmpDir / "dir").c_str(), &st), 0); |
| 130 | + EXPECT_EQ(st.st_mode & 0777, 0700); |
| 131 | +} |
| 132 | + |
| 133 | +#ifdef __linux__ |
| 134 | + |
| 135 | +TEST(fchmodatTryNoFollow, fallbackWithoutProc) |
| 136 | +{ |
| 137 | + if (!userNamespacesSupported()) |
| 138 | + GTEST_SKIP() << "User namespaces not supported"; |
| 139 | + |
| 140 | + std::filesystem::path tmpDir = nix::createTempDir(); |
| 141 | + nix::AutoDelete delTmpDir(tmpDir, /*recursive=*/true); |
| 142 | + |
| 143 | + { |
| 144 | + RestoreSink sink(/*startFsync=*/false); |
| 145 | + sink.dstPath = tmpDir; |
| 146 | + sink.dirFd = openDirectory(tmpDir); |
| 147 | + sink.createRegularFile(CanonPath("file"), [](CreateRegularFileSink & crf) {}); |
| 148 | + sink.createSymlink(CanonPath("link"), "file"); |
| 149 | + } |
| 150 | + |
| 151 | + ASSERT_EQ(chmod((tmpDir / "file").c_str(), 0644), 0); |
| 152 | + |
| 153 | + Pid pid = startProcess( |
| 154 | + [&] { |
| 155 | + if (unshare(CLONE_NEWNS) == -1) |
| 156 | + _exit(1); |
| 157 | + |
| 158 | + if (mount(0, "/", 0, MS_PRIVATE | MS_REC, 0) == -1) |
| 159 | + _exit(1); |
| 160 | + |
| 161 | + if (mount("tmpfs", "/proc", "tmpfs", 0, 0) == -1) |
| 162 | + _exit(1); |
| 163 | + |
| 164 | + AutoCloseFD dirFd = openDirectory(tmpDir); |
| 165 | + if (!dirFd) |
| 166 | + exit(1); |
| 167 | + |
| 168 | + try { |
| 169 | + fchmodatTryNoFollow(dirFd.get(), CanonPath("file"), 0600); |
| 170 | + } catch (SysError & e) { |
| 171 | + _exit(1); |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + fchmodatTryNoFollow(dirFd.get(), CanonPath("link"), 0777); |
| 176 | + } catch (SysError & e) { |
| 177 | + if (e.errNo == EOPNOTSUPP) |
| 178 | + _exit(0); /* Success. */ |
| 179 | + } |
| 180 | + |
| 181 | + _exit(1); /* Didn't throw the expected exception. */ |
| 182 | + }, |
| 183 | + {.cloneFlags = CLONE_NEWUSER}); |
| 184 | + |
| 185 | + int status = pid.wait(); |
| 186 | + ASSERT_TRUE(statusOk(status)); |
| 187 | + |
| 188 | + struct ::stat st; |
| 189 | + ASSERT_EQ(stat((tmpDir / "file").c_str(), &st), 0); |
| 190 | + EXPECT_EQ(st.st_mode & 0777, 0600); |
| 191 | +} |
| 192 | +#endif |
| 193 | + |
| 194 | +} // namespace nix |
0 commit comments