File: syscall_wrappers_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 9,816 bytes parent folder | download | duplicates (9)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "sandbox/linux/services/syscall_wrappers.h"

#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "base/logging.h"
#include "base/memory/page_size.h"
#include "base/posix/eintr_wrapper.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#include "sandbox/linux/system_headers/linux_signal.h"
#include "sandbox/linux/system_headers/linux_stat.h"
#include "sandbox/linux/tests/scoped_temporary_file.h"
#include "sandbox/linux/tests/test_utils.h"
#include "sandbox/linux/tests/unit_tests.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace sandbox {

namespace {

TEST(SyscallWrappers, BasicSyscalls) {
  EXPECT_EQ(getpid(), sys_getpid());
}

TEST(SyscallWrappers, CloneBasic) {
  pid_t child = sys_clone(SIGCHLD);
  TestUtils::HandlePostForkReturn(child);
  EXPECT_LT(0, child);
}

TEST(SyscallWrappers, CloneParentSettid) {
  pid_t ptid = 0;
  pid_t child = sys_clone(CLONE_PARENT_SETTID | SIGCHLD, nullptr, &ptid,
                          nullptr, nullptr);
  TestUtils::HandlePostForkReturn(child);
  EXPECT_LT(0, child);
  EXPECT_EQ(child, ptid);
}

TEST(SyscallWrappers, CloneChildSettid) {
  pid_t ctid = 0;
  pid_t pid =
      sys_clone(CLONE_CHILD_SETTID | SIGCHLD, nullptr, nullptr, &ctid, nullptr);

  const int kSuccessExit = 0;
  if (0 == pid) {
    // In child.
    if (sys_getpid() == ctid)
      _exit(kSuccessExit);
    _exit(1);
  }

  ASSERT_NE(-1, pid);
  int status = 0;
  ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0)));
  ASSERT_TRUE(WIFEXITED(status));
  EXPECT_EQ(kSuccessExit, WEXITSTATUS(status));
}

TEST(SyscallWrappers, GetRESUid) {
  uid_t ruid, euid, suid;
  uid_t sys_ruid, sys_euid, sys_suid;
  ASSERT_EQ(0, getresuid(&ruid, &euid, &suid));
  ASSERT_EQ(0, sys_getresuid(&sys_ruid, &sys_euid, &sys_suid));
  EXPECT_EQ(ruid, sys_ruid);
  EXPECT_EQ(euid, sys_euid);
  EXPECT_EQ(suid, sys_suid);
}

TEST(SyscallWrappers, GetRESGid) {
  gid_t rgid, egid, sgid;
  gid_t sys_rgid, sys_egid, sys_sgid;
  ASSERT_EQ(0, getresgid(&rgid, &egid, &sgid));
  ASSERT_EQ(0, sys_getresgid(&sys_rgid, &sys_egid, &sys_sgid));
  EXPECT_EQ(rgid, sys_rgid);
  EXPECT_EQ(egid, sys_egid);
  EXPECT_EQ(sgid, sys_sgid);
}

TEST(SyscallWrappers, LinuxSigSet) {
  sigset_t sigset;
  ASSERT_EQ(0, sigemptyset(&sigset));
  ASSERT_EQ(0, sigaddset(&sigset, LINUX_SIGSEGV));
  ASSERT_EQ(0, sigaddset(&sigset, LINUX_SIGBUS));
  uint64_t linux_sigset = 0;
  std::memcpy(&linux_sigset, &sigset,
              std::min(sizeof(sigset), sizeof(linux_sigset)));
  EXPECT_EQ((1ULL << (LINUX_SIGSEGV - 1)) | (1ULL << (LINUX_SIGBUS - 1)),
            linux_sigset);
}

TEST(SyscallWrappers, Stat) {
  // Create a file to stat, with 12 bytes of data.
  ScopedTemporaryFile tmp_file;
  EXPECT_EQ(12, write(tmp_file.fd(), "blahblahblah", 12));

  // To test we have the correct stat structures for each kernel/platform, we
  // will right-align them on a page, with a guard page after.
  char* two_pages = static_cast<char*>(TestUtils::MapPagesOrDie(2));
  TestUtils::MprotectLastPageOrDie(two_pages, 2);
  char* page1_end = two_pages + base::GetPageSize();

  // First, check that calling stat with |stat_buf| pointing to the last byte on
  // a page causes EFAULT.
  int res = sys_stat(tmp_file.full_file_name(),
                     reinterpret_cast<struct kernel_stat*>(page1_end - 1));
  ASSERT_EQ(res, -1);
  if (res < 0 && errno == EOVERFLOW) {
    GTEST_SKIP();
  }
  ASSERT_EQ(errno, EFAULT);

  // Now, check that we have the correctly sized stat structure.
  struct kernel_stat* sb = reinterpret_cast<struct kernel_stat*>(
      page1_end - sizeof(struct kernel_stat));
  // Memset to c's so we can check the kernel zero'd the padding...
  memset(sb, 'c', sizeof(struct kernel_stat));
  res = sys_stat(tmp_file.full_file_name(), sb);
  ASSERT_EQ(res, 0);

  // Following fields may never be consistent but should be non-zero.
  // Don't trust the platform to define fields with any particular sign.
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_dev));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_ino));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_mode));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_blksize));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_blocks));

// We are the ones that made the file.
// Note: normally gid and uid overflow on backwards-compatible 32-bit systems
// and we end up with dummy uids and gids in place here.
#if defined(ARCH_CPU_64_BITS)
  EXPECT_EQ(geteuid(), sb->st_uid);
  EXPECT_EQ(getegid(), sb->st_gid);
#endif

  // Wrote 12 bytes above which should fit in one block.
  EXPECT_EQ(12u, sb->st_size);

  // Can't go backwards in time, 1500000000 was some time ago.
  EXPECT_LT(1500000000u, static_cast<unsigned int>(sb->st_atime_));
  EXPECT_LT(1500000000u, static_cast<unsigned int>(sb->st_mtime_));
  EXPECT_LT(1500000000u, static_cast<unsigned int>(sb->st_ctime_));

  // Checking the padding for good measure.
#if defined(__x86_64__)
  EXPECT_EQ(0u, sb->__pad0);
  EXPECT_EQ(0u, sb->__unused4[0]);
  EXPECT_EQ(0u, sb->__unused4[1]);
  EXPECT_EQ(0u, sb->__unused4[2]);
#elif defined(__aarch64__)
  EXPECT_EQ(0u, sb->__pad1);
  EXPECT_EQ(0, sb->__pad2);
  EXPECT_EQ(0u, sb->__unused4);
  EXPECT_EQ(0u, sb->__unused5);
#endif
}

#if defined(__NR_fstatat64)
TEST(SyscallWrappers, Stat64) {
  static_assert(sizeof(struct kernel_stat64) == sizeof(default_stat_struct),
                "This test only works on systems where the default_stat_struct "
                "is kernel_stat64");
  // Create a file to stat, with 12 bytes of data.
  ScopedTemporaryFile tmp_file;
  EXPECT_EQ(12, write(tmp_file.fd(), "blahblahblah", 12));

  // To test we have the correct stat structures for each kernel/platform, we
  // will right-align them on a page, with a guard page after.
  char* two_pages = static_cast<char*>(TestUtils::MapPagesOrDie(2));
  TestUtils::MprotectLastPageOrDie(two_pages, 2);
  char* page1_end = two_pages + base::GetPageSize();

  // First, check that calling stat with |stat_buf| pointing to the last byte on
  // a page causes EFAULT.
  int res =
      sys_fstatat64(AT_FDCWD, tmp_file.full_file_name(),
                    reinterpret_cast<struct kernel_stat64*>(page1_end - 1), 0);
  ASSERT_EQ(res, -1);
  ASSERT_EQ(errno, EFAULT);

  // Now, check that we have the correctly sized stat structure.
  struct kernel_stat64* sb = reinterpret_cast<struct kernel_stat64*>(
      page1_end - sizeof(struct kernel_stat64));
  memset(sb, 0, sizeof(struct kernel_stat64));
  res = sys_fstatat64(AT_FDCWD, tmp_file.full_file_name(), sb, 0);
  ASSERT_EQ(res, 0);

  // Following fields may never be consistent but should be non-zero.
  // Don't trust the platform to define fields with any particular sign.
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_dev));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_ino));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_mode));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_blksize));
  EXPECT_NE(0u, static_cast<unsigned int>(sb->st_blocks));

  // We are the ones that made the file.
  EXPECT_EQ(geteuid(), sb->st_uid);
  EXPECT_EQ(getegid(), sb->st_gid);

  // Wrote 12 bytes above which should fit in one block.
  EXPECT_EQ(12, sb->st_size);

  // Can't go backwards in time, 1500000000 was some time ago.
  EXPECT_LT(1500000000u, static_cast<unsigned int>(sb->st_atime_));
  EXPECT_LT(1500000000u, static_cast<unsigned int>(sb->st_mtime_));
  EXPECT_LT(1500000000u, static_cast<unsigned int>(sb->st_ctime_));
}
#endif  // defined(__NR_fstatat64)

TEST(SyscallWrappers, LStat) {
  // Create a file to stat, with 12 bytes of data.
  ScopedTemporaryFile tmp_file;
  EXPECT_EQ(12, write(tmp_file.fd(), "blahblahblah", 12));

  // Also create a symlink.
  std::string symlink_name;
  {
    ScopedTemporaryFile tmp_file2;
    symlink_name = tmp_file2.full_file_name();
  }
  int rc = symlink(tmp_file.full_file_name(), symlink_name.c_str());
  if (rc != 0) {
    PLOG(ERROR) << "Couldn't symlink " << symlink_name << " to target "
                << tmp_file.full_file_name();
    GTEST_FAIL();
  }

  struct kernel_stat lstat_info;
  rc = sys_lstat(symlink_name.c_str(), &lstat_info);
  if (rc < 0 && errno == EOVERFLOW) {
    GTEST_SKIP();
  }
  if (rc != 0) {
    PLOG(ERROR) << "Couldn't sys_lstat " << symlink_name;
    GTEST_FAIL();
  }

  struct kernel_stat tmp_file_stat_info;
  rc = sys_stat(tmp_file.full_file_name(), &tmp_file_stat_info);
  if (rc < 0 && errno == EOVERFLOW) {
    GTEST_SKIP();
  }
  if (rc != 0) {
    PLOG(ERROR) << "Couldn't sys_stat " << tmp_file.full_file_name();
    GTEST_FAIL();
  }

  // lstat should produce information about a symlink.
  ASSERT_TRUE(S_ISLNK(lstat_info.st_mode));

// /tmp is mounted with nosymfollow on ChromeOS so calling
// sys_stat leads to an error.
#if BUILDFLAG(IS_CHROMEOS)
  if (base::SysInfo::IsRunningOnChromeOS()) {
    GTEST_SKIP();
  }
#endif

  struct kernel_stat stat_info;
  rc = sys_stat(symlink_name.c_str(), &stat_info);
  if (rc < 0 && errno == EOVERFLOW) {
    GTEST_SKIP();
  }
  if (rc != 0) {
    PLOG(ERROR) << "Couldn't sys_stat " << symlink_name;
    GTEST_FAIL();
  }

  // stat-ing symlink_name and tmp_file should produce the same inode.
  ASSERT_EQ(stat_info.st_ino, tmp_file_stat_info.st_ino);

  // lstat-ing symlink_name should give a different inode than stat-ing
  // symlink_name.
  ASSERT_NE(stat_info.st_ino, lstat_info.st_ino);
}

}  // namespace

}  // namespace sandbox