summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/symlink.cc
blob: 494072a9b27c1e0744f97dc72831142c3b207b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include <string>

#include "gtest/gtest.h"
#include "test/util/capability_util.h"
#include "test/util/file_descriptor.h"
#include "test/util/fs_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"

namespace gvisor {
namespace testing {

namespace {

mode_t FilePermission(const std::string& path) {
  struct stat buf = {0};
  TEST_CHECK(lstat(path.c_str(), &buf) == 0);
  return buf.st_mode & 0777;
}

// Test that name collisions are checked on the new link path, not the source
// path.
TEST(SymlinkTest, CanCreateSymlinkWithCachedSourceDirent) {
  const std::string srcname = NewTempAbsPath();
  const std::string newname = NewTempAbsPath();
  const std::string basedir = std::string(Dirname(srcname));
  ASSERT_EQ(basedir, Dirname(newname));

  ASSERT_THAT(chdir(basedir.c_str()), SyscallSucceeds());

  // Open the source node to cause the underlying dirent to be cached. It will
  // remain cached while we have the file open.
  int fd;
  ASSERT_THAT(fd = open(srcname.c_str(), O_CREAT | O_RDWR, 0666),
              SyscallSucceeds());
  FileDescriptor fd_closer(fd);

  // Attempt to create a symlink. If the bug exists, this will fail since the
  // dirent link creation code will check for a name collision on the source
  // link name.
  EXPECT_THAT(symlink(std::string(Basename(srcname)).c_str(),
                      std::string(Basename(newname)).c_str()),
              SyscallSucceeds());
}

TEST(SymlinkTest, CanCreateSymlinkFile) {
  const std::string oldname = NewTempAbsPath();
  const std::string newname = NewTempAbsPath();

  int fd;
  ASSERT_THAT(fd = open(oldname.c_str(), O_CREAT | O_RDWR, 0666),
              SyscallSucceeds());
  EXPECT_THAT(close(fd), SyscallSucceeds());

  EXPECT_THAT(symlink(oldname.c_str(), newname.c_str()), SyscallSucceeds());
  EXPECT_EQ(FilePermission(newname), 0777);

  auto link = ASSERT_NO_ERRNO_AND_VALUE(ReadLink(newname));
  EXPECT_EQ(oldname, link);

  EXPECT_THAT(unlink(newname.c_str()), SyscallSucceeds());
  EXPECT_THAT(unlink(oldname.c_str()), SyscallSucceeds());
}

TEST(SymlinkTest, CanCreateSymlinkDir) {
  const std::string olddir = NewTempAbsPath();
  const std::string newdir = NewTempAbsPath();

  EXPECT_THAT(mkdir(olddir.c_str(), 0777), SyscallSucceeds());
  EXPECT_THAT(symlink(olddir.c_str(), newdir.c_str()), SyscallSucceeds());
  EXPECT_EQ(FilePermission(newdir), 0777);

  auto link = ASSERT_NO_ERRNO_AND_VALUE(ReadLink(newdir));
  EXPECT_EQ(olddir, link);

  EXPECT_THAT(unlink(newdir.c_str()), SyscallSucceeds());

  ASSERT_THAT(rmdir(olddir.c_str()), SyscallSucceeds());
}

TEST(SymlinkTest, CannotCreateSymlinkInReadOnlyDir) {
  // Drop capabilities that allow us to override file and directory permissions.
  ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
  ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));

  const std::string olddir = NewTempAbsPath();
  ASSERT_THAT(mkdir(olddir.c_str(), 0444), SyscallSucceeds());

  const std::string newdir = NewTempAbsPathInDir(olddir);
  EXPECT_THAT(symlink(olddir.c_str(), newdir.c_str()),
              SyscallFailsWithErrno(EACCES));

  ASSERT_THAT(rmdir(olddir.c_str()), SyscallSucceeds());
}

TEST(SymlinkTest, CannotSymlinkOverExistingFile) {
  const auto oldfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  const auto newfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());

  EXPECT_THAT(symlink(oldfile.path().c_str(), newfile.path().c_str()),
              SyscallFailsWithErrno(EEXIST));
}

TEST(SymlinkTest, CannotSymlinkOverExistingDir) {
  const auto oldfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  const auto newdir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  EXPECT_THAT(symlink(oldfile.path().c_str(), newdir.path().c_str()),
              SyscallFailsWithErrno(EEXIST));
}

TEST(SymlinkTest, OldnameIsEmpty) {
  const std::string newname = NewTempAbsPath();
  EXPECT_THAT(symlink("", newname.c_str()), SyscallFailsWithErrno(ENOENT));
}

TEST(SymlinkTest, OldnameIsDangling) {
  const std::string newname = NewTempAbsPath();
  EXPECT_THAT(symlink("/dangling", newname.c_str()), SyscallSucceeds());

  // This is required for S/R random save tests, which pre-run this test
  // in the same TEST_TMPDIR, which means that we need to clean it for any
  // operations exclusively creating files, like symlink above.
  EXPECT_THAT(unlink(newname.c_str()), SyscallSucceeds());
}

TEST(SymlinkTest, NewnameCannotExist) {
  const std::string newname =
      JoinPath(GetAbsoluteTestTmpdir(), "thisdoesnotexist", "foo");
  EXPECT_THAT(symlink("/thisdoesnotmatter", newname.c_str()),
              SyscallFailsWithErrno(ENOENT));
}

TEST(SymlinkTest, CanEvaluateLink) {
  const auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());

  // We are going to assert that the symlink inode id is the same as the linked
  // file's inode id. In order for the inode id to be stable across
  // save/restore, it must be kept open. The FileDescriptor type will do that
  // for us automatically.
  auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR));
  struct stat file_st;
  EXPECT_THAT(fstat(fd.get(), &file_st), SyscallSucceeds());

  const std::string link = NewTempAbsPath();
  EXPECT_THAT(symlink(file.path().c_str(), link.c_str()), SyscallSucceeds());
  EXPECT_EQ(FilePermission(link), 0777);

  auto linkfd = ASSERT_NO_ERRNO_AND_VALUE(Open(link.c_str(), O_RDWR));
  struct stat link_st;
  EXPECT_THAT(fstat(linkfd.get(), &link_st), SyscallSucceeds());

  // Check that in fact newname points to the file we expect.
  EXPECT_EQ(file_st.st_dev, link_st.st_dev);
  EXPECT_EQ(file_st.st_ino, link_st.st_ino);
}

TEST(SymlinkTest, TargetIsNotMapped) {
  const std::string oldname = NewTempAbsPath();
  const std::string newname = NewTempAbsPath();

  int fd;
  // Create the target so that when we read the link, it exists.
  ASSERT_THAT(fd = open(oldname.c_str(), O_CREAT | O_RDWR, 0666),
              SyscallSucceeds());
  EXPECT_THAT(close(fd), SyscallSucceeds());

  // Create a symlink called newname that points to oldname.
  EXPECT_THAT(symlink(oldname.c_str(), newname.c_str()), SyscallSucceeds());

  std::vector<char> buf(1024);
  int linksize;
  // Read the link and assert that the oldname is still the same.
  EXPECT_THAT(linksize = readlink(newname.c_str(), buf.data(), 1024),
              SyscallSucceeds());
  EXPECT_EQ(0, strncmp(oldname.c_str(), buf.data(), linksize));

  EXPECT_THAT(unlink(newname.c_str()), SyscallSucceeds());
  EXPECT_THAT(unlink(oldname.c_str()), SyscallSucceeds());
}

TEST(SymlinkTest, PreadFromSymlink) {
  std::string name = NewTempAbsPath();
  int fd;
  ASSERT_THAT(fd = open(name.c_str(), O_CREAT, 0644), SyscallSucceeds());
  ASSERT_THAT(close(fd), SyscallSucceeds());

  std::string linkname = NewTempAbsPath();
  ASSERT_THAT(symlink(name.c_str(), linkname.c_str()), SyscallSucceeds());

  ASSERT_THAT(fd = open(linkname.c_str(), O_RDONLY), SyscallSucceeds());

  char buf[1024];
  EXPECT_THAT(pread64(fd, buf, 1024, 0), SyscallSucceeds());
  EXPECT_THAT(close(fd), SyscallSucceeds());

  EXPECT_THAT(unlink(name.c_str()), SyscallSucceeds());
  EXPECT_THAT(unlink(linkname.c_str()), SyscallSucceeds());
}

TEST(SymlinkTest, SymlinkAtDegradedPermissions_NoRandomSave) {
  // Drop capabilities that allow us to override file and directory permissions.
  ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
  ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));

  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir.path()));

  int dirfd;
  ASSERT_THAT(dirfd = open(dir.path().c_str(), O_DIRECTORY, 0),
              SyscallSucceeds());

  const DisableSave ds;  // Permissions are dropped.
  EXPECT_THAT(fchmod(dirfd, 0), SyscallSucceeds());

  std::string basename = std::string(Basename(file.path()));
  EXPECT_THAT(symlinkat("/dangling", dirfd, basename.c_str()),
              SyscallFailsWithErrno(EACCES));
  EXPECT_THAT(close(dirfd), SyscallSucceeds());
}

TEST(SymlinkTest, ReadlinkAtDegradedPermissions_NoRandomSave) {
  // Drop capabilities that allow us to override file and directory permissions.
  ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
  ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));

  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  const std::string oldpath = NewTempAbsPathInDir(dir.path());
  const std::string oldbase = std::string(Basename(oldpath));
  ASSERT_THAT(symlink("/dangling", oldpath.c_str()), SyscallSucceeds());

  int dirfd;
  EXPECT_THAT(dirfd = open(dir.path().c_str(), O_DIRECTORY, 0),
              SyscallSucceeds());

  const DisableSave ds;  // Permissions are dropped.
  EXPECT_THAT(fchmod(dirfd, 0), SyscallSucceeds());

  char buf[1024];
  int linksize;
  EXPECT_THAT(linksize = readlinkat(dirfd, oldbase.c_str(), buf, 1024),
              SyscallFailsWithErrno(EACCES));
  EXPECT_THAT(close(dirfd), SyscallSucceeds());
}

TEST(SymlinkTest, ChmodSymlink) {
  auto target = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  const std::string newpath = NewTempAbsPath();
  ASSERT_THAT(symlink(target.path().c_str(), newpath.c_str()),
              SyscallSucceeds());
  EXPECT_EQ(FilePermission(newpath), 0777);
  EXPECT_THAT(chmod(newpath.c_str(), 0666), SyscallSucceeds());
  EXPECT_EQ(FilePermission(newpath), 0777);
}

}  // namespace

}  // namespace testing
}  // namespace gvisor