summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/uidgid.cc
blob: d95a3e010afca52749345a13c189f952c0d74716 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// 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 <grp.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>

#include "gtest/gtest.h"
#include "absl/flags/flag.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "test/util/capability_util.h"
#include "test/util/cleanup.h"
#include "test/util/multiprocess_util.h"
#include "test/util/posix_error.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
#include "test/util/uid_util.h"

ABSL_FLAG(int32_t, scratch_uid1, 65534, "first scratch UID");
ABSL_FLAG(int32_t, scratch_uid2, 65533, "second scratch UID");
ABSL_FLAG(int32_t, scratch_gid1, 65534, "first scratch GID");
ABSL_FLAG(int32_t, scratch_gid2, 65533, "second scratch GID");

// Force use of syscall instead of glibc set*id() wrappers because we want to
// apply to the current task only. libc sets all threads in a process because
// "POSIX requires that all threads in a process share the same credentials."
#define setuid USE_SYSCALL_INSTEAD
#define setgid USE_SYSCALL_INSTEAD
#define setreuid USE_SYSCALL_INSTEAD
#define setregid USE_SYSCALL_INSTEAD
#define setresuid USE_SYSCALL_INSTEAD
#define setresgid USE_SYSCALL_INSTEAD

using ::testing::UnorderedElementsAreArray;

namespace gvisor {
namespace testing {

namespace {

TEST(UidGidTest, Getuid) {
  uid_t ruid, euid, suid;
  EXPECT_THAT(getresuid(&ruid, &euid, &suid), SyscallSucceeds());
  EXPECT_THAT(getuid(), SyscallSucceedsWithValue(ruid));
  EXPECT_THAT(geteuid(), SyscallSucceedsWithValue(euid));
}

TEST(UidGidTest, Getgid) {
  gid_t rgid, egid, sgid;
  EXPECT_THAT(getresgid(&rgid, &egid, &sgid), SyscallSucceeds());
  EXPECT_THAT(getgid(), SyscallSucceedsWithValue(rgid));
  EXPECT_THAT(getegid(), SyscallSucceedsWithValue(egid));
}

TEST(UidGidTest, Getgroups) {
  // "If size is zero, list is not modified, but the total number of
  // supplementary group IDs for the process is returned." - getgroups(2)
  int nr_groups;
  ASSERT_THAT(nr_groups = getgroups(0, nullptr), SyscallSucceeds());
  std::vector<gid_t> list(nr_groups);
  EXPECT_THAT(getgroups(list.size(), list.data()), SyscallSucceeds());

  // "EINVAL: size is less than the number of supplementary group IDs, but is
  // not zero."
  EXPECT_THAT(getgroups(-1, nullptr), SyscallFailsWithErrno(EINVAL));

  // Testing for EFAULT requires actually having groups, which isn't guaranteed
  // here; see the setgroups test below.
}

// Checks that the calling process' real/effective/saved user IDs are
// ruid/euid/suid respectively.
PosixError CheckUIDs(uid_t ruid, uid_t euid, uid_t suid) {
  uid_t actual_ruid, actual_euid, actual_suid;
  int rc = getresuid(&actual_ruid, &actual_euid, &actual_suid);
  MaybeSave();
  if (rc < 0) {
    return PosixError(errno, "getresuid");
  }
  if (ruid != actual_ruid || euid != actual_euid || suid != actual_suid) {
    return PosixError(
        EPERM, absl::StrCat(
                   "incorrect user IDs: got (",
                   absl::StrJoin({actual_ruid, actual_euid, actual_suid}, ", "),
                   ", wanted (", absl::StrJoin({ruid, euid, suid}, ", "), ")"));
  }
  return NoError();
}

PosixError CheckGIDs(gid_t rgid, gid_t egid, gid_t sgid) {
  gid_t actual_rgid, actual_egid, actual_sgid;
  int rc = getresgid(&actual_rgid, &actual_egid, &actual_sgid);
  MaybeSave();
  if (rc < 0) {
    return PosixError(errno, "getresgid");
  }
  if (rgid != actual_rgid || egid != actual_egid || sgid != actual_sgid) {
    return PosixError(
        EPERM, absl::StrCat(
                   "incorrect group IDs: got (",
                   absl::StrJoin({actual_rgid, actual_egid, actual_sgid}, ", "),
                   ", wanted (", absl::StrJoin({rgid, egid, sgid}, ", "), ")"));
  }
  return NoError();
}

// N.B. These tests may break horribly unless run via a gVisor test runner,
// because changing UID in one test may forfeit permissions required by other
// tests. (The test runner runs each test in a separate process.)

TEST(UidGidRootTest, Setuid) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  // Do setuid in a separate thread so that after finishing this test, the
  // process can still open files the test harness created before starting this
  // test. Otherwise, the files are created by root (UID before the test), but
  // cannot be opened by the `uid` set below after the test. After calling
  // setuid(non-zero-UID), there is no way to get root privileges back.
  ScopedThread([&] {
    // Use syscall instead of glibc setuid wrapper because we want this setuid
    // call to only apply to this task. POSIX threads, however, require that all
    // threads have the same UIDs, so using the setuid wrapper sets all threads'
    // real UID.
    EXPECT_THAT(syscall(SYS_setuid, -1), SyscallFailsWithErrno(EINVAL));

    const uid_t uid = absl::GetFlag(FLAGS_scratch_uid1);
    EXPECT_THAT(syscall(SYS_setuid, uid), SyscallSucceeds());
    // "If the effective UID of the caller is root (more precisely: if the
    // caller has the CAP_SETUID capability), the real UID and saved set-user-ID
    // are also set." - setuid(2)
    EXPECT_NO_ERRNO(CheckUIDs(uid, uid, uid));
  });
}

TEST(UidGidRootTest, Setgid) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  EXPECT_THAT(syscall(SYS_setgid, -1), SyscallFailsWithErrno(EINVAL));

  ScopedThread([&] {
    const gid_t gid = absl::GetFlag(FLAGS_scratch_gid1);
    EXPECT_THAT(syscall(SYS_setgid, gid), SyscallSucceeds());
    EXPECT_NO_ERRNO(CheckGIDs(gid, gid, gid));
  });
}

TEST(UidGidRootTest, SetgidNotFromThreadGroupLeader) {
#pragma push_macro("allow_setgid")
#undef setgid

  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  int old_gid = getgid();
  auto clean = Cleanup([old_gid] { setgid(old_gid); });

  const gid_t gid = absl::GetFlag(FLAGS_scratch_gid1);
  // NOTE(b/64676707): Do setgid in a separate thread so that we can test if
  // info.si_pid is set correctly.
  ScopedThread thread =
      ScopedThread([gid] { ASSERT_THAT(setgid(gid), SyscallSucceeds()); });
  thread.Join();
  EXPECT_NO_ERRNO(CheckGIDs(gid, gid, gid));

#pragma pop_macro("allow_setgid")
}

TEST(UidGidRootTest, Setreuid) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  // "Supplying a value of -1 for either the real or effective user ID forces
  // the system to leave that ID unchanged." - setreuid(2)
  EXPECT_THAT(syscall(SYS_setreuid, -1, -1), SyscallSucceeds());

  EXPECT_NO_ERRNO(CheckUIDs(0, 0, 0));

  // Do setuid in a separate thread so that after finishing this test, the
  // process can still open files the test harness created before starting
  // this test. Otherwise, the files are created by root (UID before the
  // test), but cannot be opened by the `uid` set below after the test. After
  // calling setuid(non-zero-UID), there is no way to get root privileges
  // back.
  ScopedThread([&] {
    const uid_t ruid = absl::GetFlag(FLAGS_scratch_uid1);
    const uid_t euid = absl::GetFlag(FLAGS_scratch_uid2);

    EXPECT_THAT(syscall(SYS_setreuid, ruid, euid), SyscallSucceeds());

    // "If the real user ID is set or the effective user ID is set to a value
    // not equal to the previous real user ID, the saved set-user-ID will be
    // set to the new effective user ID." - setreuid(2)
    EXPECT_NO_ERRNO(CheckUIDs(ruid, euid, euid));
  });
}

TEST(UidGidRootTest, Setregid) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  EXPECT_THAT(syscall(SYS_setregid, -1, -1), SyscallSucceeds());
  EXPECT_NO_ERRNO(CheckGIDs(0, 0, 0));

  ScopedThread([&] {
    const gid_t rgid = absl::GetFlag(FLAGS_scratch_gid1);
    const gid_t egid = absl::GetFlag(FLAGS_scratch_gid2);
    ASSERT_THAT(syscall(SYS_setregid, rgid, egid), SyscallSucceeds());
    EXPECT_NO_ERRNO(CheckGIDs(rgid, egid, egid));
  });
}

TEST(UidGidRootTest, Setresuid) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  // "If one of the arguments equals -1, the corresponding value is not
  // changed." - setresuid(2)
  EXPECT_THAT(syscall(SYS_setresuid, -1, -1, -1), SyscallSucceeds());
  EXPECT_NO_ERRNO(CheckUIDs(0, 0, 0));

  // Do setuid in a separate thread so that after finishing this test, the
  // process can still open files the test harness created before starting
  // this test. Otherwise, the files are created by root (UID before the
  // test), but cannot be opened by the `uid` set below after the test. After
  // calling setuid(non-zero-UID), there is no way to get root privileges
  // back.
  ScopedThread([&] {
    const uid_t ruid = 12345;
    const uid_t euid = 23456;
    const uid_t suid = 34567;

    // Use syscall instead of glibc setuid wrapper because we want this setuid
    // call to only apply to this task. posix threads, however, require that
    // all threads have the same UIDs, so using the setuid wrapper sets all
    // threads' real UID.
    EXPECT_THAT(syscall(SYS_setresuid, ruid, euid, suid), SyscallSucceeds());
    EXPECT_NO_ERRNO(CheckUIDs(ruid, euid, suid));
  });
}

TEST(UidGidRootTest, Setresgid) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  EXPECT_THAT(syscall(SYS_setresgid, -1, -1, -1), SyscallSucceeds());
  EXPECT_NO_ERRNO(CheckGIDs(0, 0, 0));

  ScopedThread([&] {
    const gid_t rgid = 12345;
    const gid_t egid = 23456;
    const gid_t sgid = 34567;
    ASSERT_THAT(syscall(SYS_setresgid, rgid, egid, sgid), SyscallSucceeds());
    EXPECT_NO_ERRNO(CheckGIDs(rgid, egid, sgid));
  });
}

TEST(UidGidRootTest, Setgroups) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  std::vector<gid_t> list = {123, 500};
  ASSERT_THAT(setgroups(list.size(), list.data()), SyscallSucceeds());
  std::vector<gid_t> list2(list.size());
  ASSERT_THAT(getgroups(list2.size(), list2.data()), SyscallSucceeds());
  EXPECT_THAT(list, UnorderedElementsAreArray(list2));

  // "EFAULT: list has an invalid address."
  EXPECT_THAT(getgroups(100, reinterpret_cast<gid_t*>(-1)),
              SyscallFailsWithErrno(EFAULT));
}

TEST(UidGidRootTest, Setuid_prlimit) {
  SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(IsRoot()));

  // Do seteuid in a separate thread so that after finishing this test, the
  // process can still open files the test harness created before starting
  // this test. Otherwise, the files are created by root (UID before the
  // test), but cannot be opened by the `uid` set below after the test.
  ScopedThread([&] {
    // Use syscall instead of glibc setuid wrapper because we want this
    // seteuid call to only apply to this task. POSIX threads, however,
    // require that all threads have the same UIDs, so using the seteuid
    // wrapper sets all threads' UID.
    EXPECT_THAT(syscall(SYS_setreuid, -1, 65534), SyscallSucceeds());

    // Despite the UID change, we should be able to get our own limits.
    struct rlimit rl = {};
    EXPECT_THAT(prlimit(0, RLIMIT_NOFILE, NULL, &rl), SyscallSucceeds());
  });
}

}  // namespace

}  // namespace testing
}  // namespace gvisor