1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 // Below are the header files we want to test.
20 #include <grp.h>
21 #include <pwd.h>
22
23 #include <errno.h>
24 #include <limits.h>
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <set>
30 #include <vector>
31
32 #include <android-base/file.h>
33 #include <android-base/strings.h>
34 #include <private/android_filesystem_config.h>
35
36 #if defined(__BIONIC__)
37 #include <android/api-level.h>
38 #include <android-base/properties.h>
39 #endif
40
41 // Generated android_ids array
42 #include "generated_android_ids.h"
43
44 using android::base::Join;
45 using android::base::ReadFileToString;
46 using android::base::Split;
47 using android::base::StartsWith;
48
49 using namespace std::literals;
50
51 enum uid_type_t {
52 TYPE_APP,
53 TYPE_SYSTEM,
54 TYPE_VENDOR,
55 };
56
57 #if defined(__BIONIC__)
58
check_passwd(const passwd * pwd,const char * username,uid_t uid,uid_type_t uid_type,bool check_username)59 static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type,
60 bool check_username) {
61 ASSERT_TRUE(pwd != nullptr);
62 if (check_username) {
63 EXPECT_STREQ(username, pwd->pw_name);
64 }
65 EXPECT_EQ(uid, pwd->pw_uid);
66 EXPECT_EQ(uid, pwd->pw_gid);
67 EXPECT_EQ(nullptr, pwd->pw_passwd);
68 #ifdef __LP64__
69 EXPECT_EQ(nullptr, pwd->pw_gecos);
70 #endif
71
72 if (uid_type == TYPE_APP) {
73 EXPECT_STREQ("/data", pwd->pw_dir);
74 } else {
75 EXPECT_STREQ("/", pwd->pw_dir);
76 }
77
78 // This has changed over time and that causes new GSI + old vendor images testing to fail.
79 // This parameter doesn't matter on Android, so simply ignore its value for older vendor images.
80 if (android::base::GetIntProperty("ro.product.first_api_level", 0) >= 30) {
81 EXPECT_STREQ("/bin/sh", pwd->pw_shell);
82 }
83 }
84
check_getpwuid(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)85 static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type,
86 bool check_username) {
87 errno = 0;
88 passwd* pwd = getpwuid(uid);
89 ASSERT_EQ(0, errno);
90 SCOPED_TRACE("getpwuid");
91 check_passwd(pwd, username, uid, uid_type, check_username);
92 }
93
check_getpwnam(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)94 static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type,
95 bool check_username) {
96 errno = 0;
97 passwd* pwd = getpwnam(username);
98 ASSERT_EQ(0, errno);
99 SCOPED_TRACE("getpwnam");
100 check_passwd(pwd, username, uid, uid_type, check_username);
101 }
102
check_getpwuid_r(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)103 static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type,
104 bool check_username) {
105 passwd pwd_storage;
106 char buf[512];
107 int result;
108
109 errno = 0;
110 passwd* pwd = nullptr;
111 result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
112 ASSERT_EQ(0, result);
113 ASSERT_EQ(0, errno);
114 SCOPED_TRACE("getpwuid_r");
115 check_passwd(pwd, username, uid, uid_type, check_username);
116 }
117
check_getpwnam_r(const char * username,uid_t uid,uid_type_t uid_type,bool check_username)118 static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type,
119 bool check_username) {
120 passwd pwd_storage;
121 char buf[512];
122 int result;
123
124 errno = 0;
125 passwd* pwd = nullptr;
126 result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
127 ASSERT_EQ(0, result);
128 ASSERT_EQ(0, errno);
129 SCOPED_TRACE("getpwnam_r");
130 check_passwd(pwd, username, uid, uid_type, check_username);
131 }
132
check_get_passwd(const char * username,uid_t uid,uid_type_t uid_type,bool check_username=true)133 static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type,
134 bool check_username = true) {
135 SCOPED_TRACE("username '"s + username + "'");
136 check_getpwuid(username, uid, uid_type, check_username);
137 check_getpwnam(username, uid, uid_type, check_username);
138 check_getpwuid_r(username, uid, uid_type, check_username);
139 check_getpwnam_r(username, uid, uid_type, check_username);
140 }
141
expect_no_passwd_id(uid_t uid)142 static void expect_no_passwd_id(uid_t uid) {
143 SCOPED_TRACE("uid '" + std::to_string(uid) + "'");
144 errno = 0;
145 passwd* passwd = nullptr;
146 passwd = getpwuid(uid);
147 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
148 EXPECT_EQ(ENOENT, errno);
149
150 struct passwd passwd_storage;
151 char buf[512];
152 EXPECT_EQ(ENOENT, getpwuid_r(uid, &passwd_storage, buf, sizeof(buf), &passwd));
153 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
154 }
155
expect_no_passwd_name(const char * username)156 static void expect_no_passwd_name(const char* username) {
157 SCOPED_TRACE("username '"s + username + "'");
158 errno = 0;
159 passwd* passwd = nullptr;
160 passwd = getpwnam(username);
161 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
162 EXPECT_EQ(ENOENT, errno);
163
164 struct passwd passwd_storage;
165 char buf[512];
166 EXPECT_EQ(ENOENT, getpwnam_r(username, &passwd_storage, buf, sizeof(buf), &passwd));
167 EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
168 }
169
170 #else // !defined(__BIONIC__)
171
check_get_passwd(const char *,uid_t,uid_type_t,bool)172 static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */,
173 bool /* check_username */) {
174 GTEST_SKIP() << "bionic-only test";
175 }
176
check_get_passwd(const char *,uid_t,uid_type_t)177 static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
178 GTEST_SKIP() << "bionic-only test";
179 }
180
expect_no_passwd_id(uid_t)181 static void expect_no_passwd_id(uid_t /* uid */) {
182 GTEST_SKIP() << "bionic-only test";
183 }
184
expect_no_passwd_name(const char *)185 static void expect_no_passwd_name(const char* /* username */) {
186 GTEST_SKIP() << "bionic-only test";
187 }
188
189 #endif
190
TEST(pwd,getpwnam_platform_ids)191 TEST(pwd, getpwnam_platform_ids) {
192 check_get_passwd("root", 0, TYPE_SYSTEM);
193 check_get_passwd("daemon", 1, TYPE_SYSTEM);
194 check_get_passwd("bin", 2, TYPE_SYSTEM);
195
196 check_get_passwd("system", 1000, TYPE_SYSTEM);
197 check_get_passwd("radio", 1001, TYPE_SYSTEM);
198
199 check_get_passwd("shell", 2000, TYPE_SYSTEM);
200
201 check_get_passwd("nobody", 9999, TYPE_SYSTEM);
202 }
203
TEST(pwd,getpwnam_oem_ids)204 TEST(pwd, getpwnam_oem_ids) {
205 check_get_passwd("oem_2900", 2900, TYPE_VENDOR, false);
206 check_get_passwd("oem_2945", 2945, TYPE_VENDOR, false);
207 check_get_passwd("oem_2999", 2999, TYPE_VENDOR, false);
208 check_get_passwd("oem_5000", 5000, TYPE_VENDOR, false);
209 check_get_passwd("oem_5454", 5454, TYPE_VENDOR, false);
210 check_get_passwd("oem_5999", 5999, TYPE_VENDOR, false);
211 }
212
TEST(pwd,getpwnam_non_exist)213 TEST(pwd, getpwnam_non_exist) {
214 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
215 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
216 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
217
218 // These ranges are for GIDs only.
219 expect_no_passwd_id(20000);
220 expect_no_passwd_id(30000);
221 expect_no_passwd_id(40000);
222 expect_no_passwd_id(50000);
223
224 // These should not be parsed as users, only as groups.
225 expect_no_passwd_name("u0_a9999_cache");
226 expect_no_passwd_name("u0_a9999_ext");
227 expect_no_passwd_name("u0_a9999_ext_cache");
228 expect_no_passwd_name("all_a9999");
229 }
230
TEST(pwd,getpwnam_u0_app_ids)231 TEST(pwd, getpwnam_u0_app_ids) {
232 check_get_passwd("u0_a0", 10000, TYPE_APP);
233 check_get_passwd("u0_a1234", 11234, TYPE_APP);
234 check_get_passwd("u0_a9999", 19999, TYPE_APP);
235
236 check_get_passwd("u0_i1", 90001, TYPE_APP);
237 check_get_passwd("u0_i4545", 94545, TYPE_APP);
238 check_get_passwd("u0_i9999", 99999, TYPE_APP);
239 }
240
TEST(pwd,getpwnam_app_id_u1_ids)241 TEST(pwd, getpwnam_app_id_u1_ids) {
242 check_get_passwd("u1_system", 101000, TYPE_SYSTEM);
243 check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
244
245 check_get_passwd("u1_a0", 110000, TYPE_APP);
246 check_get_passwd("u1_a1234", 111234, TYPE_APP);
247 check_get_passwd("u1_a9999", 119999, TYPE_APP);
248
249 check_get_passwd("u1_i1", 190001, TYPE_APP);
250 check_get_passwd("u1_i4545", 194545, TYPE_APP);
251 check_get_passwd("u1_i9999", 199999, TYPE_APP);
252 }
253
TEST(pwd,getpwnam_app_id_u31_ids)254 TEST(pwd, getpwnam_app_id_u31_ids) {
255 check_get_passwd("u31_system", 3101000, TYPE_SYSTEM);
256 check_get_passwd("u31_radio", 3101001, TYPE_SYSTEM);
257
258 check_get_passwd("u31_a0", 3110000, TYPE_APP);
259 check_get_passwd("u31_a1234", 3111234, TYPE_APP);
260 check_get_passwd("u31_a9999", 3119999, TYPE_APP);
261
262 check_get_passwd("u31_i1", 3190001, TYPE_APP);
263 check_get_passwd("u31_i4545", 3194545, TYPE_APP);
264 check_get_passwd("u31_i9999", 3199999, TYPE_APP);
265 }
266
TEST(pwd,getpwnam_app_id_not_allowed_platform)267 TEST(pwd, getpwnam_app_id_not_allowed_platform) {
268 expect_no_passwd_name("u1_root");
269 expect_no_passwd_name("u1_debuggerd");
270
271 expect_no_passwd_name("u31_root");
272 expect_no_passwd_name("u31_debuggerd");
273 }
274
TEST(pwd,getpwuid_app_id_u1_non_exist)275 TEST(pwd, getpwuid_app_id_u1_non_exist) {
276 expect_no_passwd_id(100000); // There is no 'root' for secondary users.
277 expect_no_passwd_id(101999); // End of the system reserved range, unallocated.
278 expect_no_passwd_id(102900); // The OEM ranges were never allocated to secondary users.
279 expect_no_passwd_id(105000); // The OEM ranges were never allocated to secondary users.
280
281 // These ranges are for GIDs only.
282 expect_no_passwd_id(120000);
283 expect_no_passwd_id(130000);
284 expect_no_passwd_id(140000);
285 expect_no_passwd_id(150000);
286 }
287
TEST(pwd,getpwuid_app_id_u31_non_exist)288 TEST(pwd, getpwuid_app_id_u31_non_exist) {
289 expect_no_passwd_id(3100000); // There is no 'root' for secondary users.
290 expect_no_passwd_id(3101999); // End of the system reserved range, unallocated.
291 expect_no_passwd_id(3102900); // The OEM ranges were never allocated to secondary users.
292 expect_no_passwd_id(3105000); // The OEM ranges were never allocated to secondary users.
293
294 // These ranges are for GIDs only.
295 expect_no_passwd_id(3120000);
296 expect_no_passwd_id(3130000);
297 expect_no_passwd_id(3140000);
298 expect_no_passwd_id(3150000);
299 }
300
TEST(pwd,getpwnam_r_alignment)301 TEST(pwd, getpwnam_r_alignment) {
302 #if defined(__BIONIC__)
303 passwd pwd_storage;
304 alignas(16) char buf[512];
305 passwd* pwd;
306 int result = getpwnam_r("root", &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
307 ASSERT_EQ(0, result);
308 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
309 #else
310 GTEST_SKIP() << "bionic-only test";
311 #endif
312 }
313
TEST(pwd,getpwuid_r_alignment)314 TEST(pwd, getpwuid_r_alignment) {
315 #if defined(__BIONIC__)
316 passwd pwd_storage;
317 alignas(16) char buf[512];
318 passwd* pwd;
319 int result = getpwuid_r(0, &pwd_storage, buf + 1, sizeof(buf) - 1, &pwd);
320 ASSERT_EQ(0, result);
321 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
322 #else
323 GTEST_SKIP() << "bionic-only test";
324 #endif
325 }
326
TEST(pwd,getpwnam_r_reentrancy)327 TEST(pwd, getpwnam_r_reentrancy) {
328 #if defined(__BIONIC__)
329 passwd pwd_storage[2];
330 char buf[2][512];
331 passwd* pwd[3];
332 int result = getpwnam_r("root", &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
333 ASSERT_EQ(0, result);
334 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
335 pwd[1] = getpwnam("system");
336 ASSERT_NE(nullptr, pwd[1]);
337 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
338 result = getpwnam_r("radio", &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
339 ASSERT_EQ(0, result);
340 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
341 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
342 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
343 #else
344 GTEST_SKIP() << "bionic-only test";
345 #endif
346 }
347
TEST(pwd,getpwuid_r_reentrancy)348 TEST(pwd, getpwuid_r_reentrancy) {
349 #if defined(__BIONIC__)
350 passwd pwd_storage[2];
351 char buf[2][512];
352 passwd* pwd[3];
353 int result = getpwuid_r(0, &pwd_storage[0], buf[0], sizeof(buf[0]), &pwd[0]);
354 ASSERT_EQ(0, result);
355 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
356 pwd[1] = getpwuid(1000);
357 ASSERT_NE(nullptr, pwd[1]);
358 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
359 result = getpwuid_r(1001, &pwd_storage[1], buf[1], sizeof(buf[1]), &pwd[2]);
360 ASSERT_EQ(0, result);
361 check_passwd(pwd[2], "radio", 1001, TYPE_SYSTEM, true);
362 check_passwd(pwd[0], "root", 0, TYPE_SYSTEM, true);
363 check_passwd(pwd[1], "system", 1000, TYPE_SYSTEM, true);
364 #else
365 GTEST_SKIP() << "bionic-only test";
366 #endif
367 }
368
TEST(pwd,getpwnam_r_large_enough_suggested_buffer_size)369 TEST(pwd, getpwnam_r_large_enough_suggested_buffer_size) {
370 #if defined(__BIONIC__)
371 long size = sysconf(_SC_GETPW_R_SIZE_MAX);
372 ASSERT_GT(size, 0);
373 char buf[size];
374 passwd pwd_storage;
375 passwd* pwd;
376 ASSERT_EQ(0, getpwnam_r("root", &pwd_storage, buf, size, &pwd));
377 check_passwd(pwd, "root", 0, TYPE_SYSTEM, true);
378 #else
379 GTEST_SKIP() << "bionic-only test";
380 #endif
381 }
382
383 #if defined(__BIONIC__)
384 template <typename T>
expect_ids(T ids,bool is_group)385 static void expect_ids(T ids, bool is_group) {
386 std::set<typename T::key_type> expected_ids;
387 // Ensure that all android_ids are iterated through.
388 for (size_t n = 0; n < android_id_count; ++n) {
389 EXPECT_EQ(1U, ids.count(android_ids[n].aid)) << "android_ids[n].aid: " << android_ids[n].aid;
390 expected_ids.emplace(android_ids[n].aid);
391 }
392
393 auto expect_range = [&ids, &expected_ids](uid_t start, uid_t end) {
394 for (size_t n = start; n <= end; ++n) {
395 EXPECT_EQ(1U, ids.count(n)) << "n: " << n;
396 expected_ids.emplace(n);
397 }
398 };
399
400 // Ensure that all reserved ranges are iterated through.
401 expect_range(AID_OEM_RESERVED_START, AID_OEM_RESERVED_END);
402 expect_range(AID_OEM_RESERVED_2_START, AID_OEM_RESERVED_2_END);
403 expect_range(AID_APP_START, AID_APP_END);
404 if (is_group) {
405 expect_range(AID_CACHE_GID_START, AID_CACHE_GID_END);
406 expect_range(AID_EXT_GID_START, AID_EXT_GID_END);
407 expect_range(AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END);
408 expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
409 }
410 expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
411
412 // Prior to R, we didn't have a mechanism to create vendor AIDs in the system or other non-vendor
413 // partitions, therefore we disabled the rest of these checks for older API levels.
414 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 29) {
415 return;
416 }
417
418 auto allow_range = [&ids](uid_t start, uid_t end) {
419 for (size_t n = start; n <= end; ++n) {
420 ids.erase(n);
421 }
422 };
423
424 allow_range(AID_SYSTEM_RESERVED_START, AID_SYSTEM_EXT_RESERVED_END);
425
426 // Ensure that no other ids were returned.
427 auto return_differences = [&ids, &expected_ids] {
428 std::vector<typename T::key_type> missing_from_ids;
429 std::set_difference(expected_ids.begin(), expected_ids.end(), ids.begin(), ids.end(),
430 std::inserter(missing_from_ids, missing_from_ids.begin()));
431 std::vector<typename T::key_type> extra_in_ids;
432 std::set_difference(ids.begin(), ids.end(), expected_ids.begin(), expected_ids.end(),
433 std::inserter(extra_in_ids, extra_in_ids.begin()));
434 std::string result;
435 if (!missing_from_ids.empty()) {
436 result += "Missing ids from results: " + Join(missing_from_ids, " ");
437 }
438 if (!extra_in_ids.empty()) {
439 if (!result.empty()) result += ", ";
440 result += "Extra ids in results: " + Join(extra_in_ids, " ");
441 }
442 return result;
443 };
444
445 // AID_PRNG_SEEDER (1092) was added in TM-QPR2, but CTS is shared
446 // across Android 13 versions so we may or may not find it in this
447 // test (b/253185870).
448 if (android::base::GetIntProperty("ro.build.version.sdk", 0) == __ANDROID_API_T__) {
449 #ifndef AID_PRNG_SEEDER
450 #define AID_PRNG_SEEDER 1092
451 #endif
452 ids.erase(AID_PRNG_SEEDER);
453 expected_ids.erase(AID_PRNG_SEEDER);
454 }
455 EXPECT_EQ(expected_ids, ids) << return_differences();
456 }
457 #endif
458
TEST(pwd,getpwent_iterate)459 TEST(pwd, getpwent_iterate) {
460 #if defined(__BIONIC__)
461 passwd* pwd;
462 std::set<uid_t> uids;
463
464 setpwent();
465 while ((pwd = getpwent()) != nullptr) {
466 ASSERT_TRUE(nullptr != pwd->pw_name);
467
468 EXPECT_EQ(pwd->pw_gid, pwd->pw_uid) << "pwd->pw_uid: " << pwd->pw_uid;
469 EXPECT_EQ(nullptr, pwd->pw_passwd) << "pwd->pw_uid: " << pwd->pw_uid;
470 #ifdef __LP64__
471 EXPECT_TRUE(nullptr == pwd->pw_gecos) << "pwd->pw_uid: " << pwd->pw_uid;
472 #endif
473 EXPECT_TRUE(nullptr != pwd->pw_shell);
474 if (pwd->pw_uid < AID_APP_START || pwd->pw_uid == AID_OVERFLOWUID) {
475 EXPECT_STREQ("/", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
476 } else {
477 EXPECT_STREQ("/data", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid;
478 }
479
480 EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid;
481 uids.emplace(pwd->pw_uid);
482 }
483 endpwent();
484
485 expect_ids(uids, false);
486 #else
487 GTEST_SKIP() << "bionic-only test";
488 #endif
489 }
490
check_group(const group * grp,const char * group_name,gid_t gid,bool check_groupname=true)491 static void check_group(const group* grp, const char* group_name, gid_t gid,
492 bool check_groupname = true) {
493 ASSERT_TRUE(grp != nullptr);
494 if (check_groupname) {
495 EXPECT_STREQ(group_name, grp->gr_name);
496 }
497 EXPECT_EQ(gid, grp->gr_gid);
498 ASSERT_TRUE(grp->gr_mem != nullptr);
499 if (check_groupname) {
500 EXPECT_STREQ(group_name, grp->gr_mem[0]);
501 }
502 EXPECT_TRUE(grp->gr_mem[1] == nullptr);
503 }
504
505 #if defined(__BIONIC__)
506
check_getgrgid(const char * group_name,gid_t gid,bool check_groupname)507 static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
508 errno = 0;
509 group* grp = getgrgid(gid);
510 ASSERT_EQ(0, errno);
511 SCOPED_TRACE("getgrgid");
512 check_group(grp, group_name, gid, check_groupname);
513 }
514
check_getgrnam(const char * group_name,gid_t gid,bool check_groupname)515 static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
516 errno = 0;
517 group* grp = getgrnam(group_name);
518 ASSERT_EQ(0, errno);
519 SCOPED_TRACE("getgrnam");
520 check_group(grp, group_name, gid, check_groupname);
521 }
522
check_getgrgid_r(const char * group_name,gid_t gid,bool check_groupname)523 static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) {
524 group grp_storage;
525 char buf[512];
526 group* grp;
527
528 errno = 0;
529 int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
530 ASSERT_EQ(0, result);
531 ASSERT_EQ(0, errno);
532 SCOPED_TRACE("getgrgid_r");
533 check_group(grp, group_name, gid, check_groupname);
534 }
535
check_getgrnam_r(const char * group_name,gid_t gid,bool check_groupname)536 static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) {
537 group grp_storage;
538 char buf[512];
539 group* grp;
540
541 errno = 0;
542 int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
543 ASSERT_EQ(0, result);
544 ASSERT_EQ(0, errno);
545 SCOPED_TRACE("getgrnam_r");
546 check_group(grp, group_name, gid, check_groupname);
547 }
548
check_get_group(const char * group_name,gid_t gid,bool check_groupname=true)549 static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) {
550 SCOPED_TRACE("groupname '"s + group_name + "'");
551 check_getgrgid(group_name, gid, check_groupname);
552 check_getgrnam(group_name, gid, check_groupname);
553 check_getgrgid_r(group_name, gid, check_groupname);
554 check_getgrnam_r(group_name, gid, check_groupname);
555 }
556
expect_no_group_id(gid_t gid)557 static void expect_no_group_id(gid_t gid) {
558 SCOPED_TRACE("gid '" + std::to_string(gid) + "'");
559 errno = 0;
560 group* group = nullptr;
561 group = getgrgid(gid);
562 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
563 EXPECT_EQ(ENOENT, errno);
564
565 struct group group_storage;
566 char buf[512];
567 EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group));
568 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
569 }
570
expect_no_group_name(const char * groupname)571 static void expect_no_group_name(const char* groupname) {
572 SCOPED_TRACE("groupname '"s + groupname + "'");
573 errno = 0;
574 group* group = nullptr;
575 group = getgrnam(groupname);
576 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
577 EXPECT_EQ(ENOENT, errno);
578
579 struct group group_storage;
580 char buf[512];
581 EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group));
582 EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
583 }
584
585 #else // !defined(__BIONIC__)
586
check_get_group(const char *,gid_t,bool)587 static void check_get_group(const char*, gid_t, bool) {
588 GTEST_SKIP() << "bionic-only test";
589 }
590
check_get_group(const char *,gid_t)591 static void check_get_group(const char*, gid_t) {
592 GTEST_SKIP() << "bionic-only test";
593 }
594
expect_no_group_id(gid_t)595 static void expect_no_group_id(gid_t /* gid */) {
596 GTEST_SKIP() << "bionic-only test";
597 }
598
expect_no_group_name(const char *)599 static void expect_no_group_name(const char* /* groupname */) {
600 GTEST_SKIP() << "bionic-only test";
601 }
602
603 #endif
604
TEST(grp,getgrnam_platform_ids)605 TEST(grp, getgrnam_platform_ids) {
606 check_get_group("root", 0);
607 check_get_group("daemon", 1);
608 check_get_group("bin", 2);
609
610 check_get_group("system", 1000);
611 check_get_group("radio", 1001);
612
613 check_get_group("shell", 2000);
614
615 check_get_group("nobody", 9999);
616 }
617
TEST(grp,getgrnam_oem_ids)618 TEST(grp, getgrnam_oem_ids) {
619 check_get_group("oem_2900", 2900, false);
620 check_get_group("oem_2945", 2945, false);
621 check_get_group("oem_2999", 2999, false);
622 check_get_group("oem_5000", 5000, false);
623 check_get_group("oem_5454", 5454, false);
624 check_get_group("oem_5999", 5999, false);
625 }
626
TEST(grp,getgrnam_non_exist)627 TEST(grp, getgrnam_non_exist) {
628 expect_no_passwd_id(999); // End of the system reserved range, unallocated.
629 expect_no_passwd_id(1999); // End of the system reserved range, unallocated.
630 expect_no_passwd_id(2899); // End of the system reserved range, unallocated.
631 }
632
TEST(grp,getgrnam_u0_app_ids)633 TEST(grp, getgrnam_u0_app_ids) {
634 check_get_group("u0_a0", 10000);
635 check_get_group("u0_a1234", 11234);
636 check_get_group("u0_a9999", 19999);
637
638 check_get_group("u0_a0_cache", 20000);
639 check_get_group("u0_a1234_cache", 21234);
640 check_get_group("u0_a9999_cache", 29999);
641
642 check_get_group("u0_a0_ext", 30000);
643 check_get_group("u0_a4545_ext", 34545);
644 check_get_group("u0_a9999_ext", 39999);
645
646 check_get_group("u0_a0_ext_cache", 40000);
647 check_get_group("u0_a4545_ext_cache", 44545);
648 check_get_group("u0_a9999_ext_cache", 49999);
649
650 check_get_group("all_a0", 50000);
651 check_get_group("all_a4545", 54545);
652 check_get_group("all_a9999", 59999);
653
654 check_get_group("u0_i1", 90001);
655 }
656
TEST(grp,getgrnam_u1_app_ids)657 TEST(grp, getgrnam_u1_app_ids) {
658 check_get_group("u1_system", 101000);
659 check_get_group("u1_radio", 101001);
660
661 check_get_group("u1_a0", 110000);
662 check_get_group("u1_a1234", 111234);
663 check_get_group("u1_a9999", 119999);
664
665 check_get_group("u1_a0_cache", 120000);
666 check_get_group("u1_a1234_cache", 121234);
667 check_get_group("u1_a9999_cache", 129999);
668
669 check_get_group("u1_a0_ext", 130000);
670 check_get_group("u1_a4545_ext", 134545);
671 check_get_group("u1_a9999_ext", 139999);
672
673 check_get_group("u1_a0_ext_cache", 140000);
674 check_get_group("u1_a4545_ext_cache", 144545);
675 check_get_group("u1_a9999_ext_cache", 149999);
676
677 check_get_group("u1_i1", 190001);
678 }
679
TEST(grp,getgrnam_u31_app_ids)680 TEST(grp, getgrnam_u31_app_ids) {
681 check_get_group("u31_system", 3101000);
682 check_get_group("u31_radio", 3101001);
683
684 check_get_group("u31_a0", 3110000);
685 check_get_group("u31_a1234", 3111234);
686 check_get_group("u31_a9999", 3119999);
687
688 check_get_group("u31_a0_cache", 3120000);
689 check_get_group("u31_a1234_cache", 3121234);
690 check_get_group("u31_a9999_cache", 3129999);
691
692 check_get_group("u31_a0_cache", 3120000);
693 check_get_group("u31_a1234_cache", 3121234);
694 check_get_group("u31_a9999_cache", 3129999);
695
696 check_get_group("u31_a0_ext", 3130000);
697 check_get_group("u31_a4545_ext", 3134545);
698 check_get_group("u31_a9999_ext", 3139999);
699
700 check_get_group("u31_a0_ext_cache", 3140000);
701 check_get_group("u31_a4545_ext_cache", 3144545);
702 check_get_group("u31_a9999_ext_cache", 3149999);
703
704 check_get_group("u31_i1", 3190001);
705 }
706
TEST(grp,getpgram_app_id_not_allowed_platform)707 TEST(grp, getpgram_app_id_not_allowed_platform) {
708 expect_no_group_name("u1_root");
709 expect_no_group_name("u1_debuggerd");
710
711 expect_no_group_name("u31_root");
712 expect_no_group_name("u31_debuggerd");
713 }
714
TEST(grp,getgrgid_app_id_u1_non_exist)715 TEST(grp, getgrgid_app_id_u1_non_exist) {
716 expect_no_group_id(100000); // There is no 'root' for secondary users.
717 expect_no_group_id(101999); // End of the system reserved range, unallocated.
718 expect_no_group_id(102900); // The OEM ranges were never allocated to secondary users.
719 expect_no_group_id(105000); // The OEM ranges were never allocated to secondary users.
720
721 // The shared range is shared among users, and therefore doesn't exist for secondary users.
722 expect_no_group_id(150000);
723 }
724
TEST(grp,getgrgid_app_id_u31_non_exist)725 TEST(grp, getgrgid_app_id_u31_non_exist) {
726 expect_no_group_id(3100000); // There is no 'root' for secondary users.
727 expect_no_group_id(3101999); // End of the system reserved range, unallocated.
728 expect_no_group_id(3102900); // The OEM ranges were never allocated to secondary users.
729 expect_no_group_id(3105000); // The OEM ranges were never allocated to secondary users.
730
731 // The shared range is shared among users, and therefore doesn't exist for secondary users.
732 expect_no_group_id(3150000);
733 }
734
TEST(grp,getgrnam_r_alignment)735 TEST(grp, getgrnam_r_alignment) {
736 #if defined(__BIONIC__)
737 group grp_storage;
738 alignas(16) char buf[512];
739 group* grp;
740 int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
741 ASSERT_EQ(0, result);
742 check_group(grp, "root", 0);
743 #else
744 GTEST_SKIP() << "bionic-only test";
745 #endif
746 }
747
TEST(grp,getgrgid_r_alignment)748 TEST(grp, getgrgid_r_alignment) {
749 #if defined(__BIONIC__)
750 group grp_storage;
751 alignas(16) char buf[512];
752 group* grp;
753 int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp);
754 ASSERT_EQ(0, result);
755 check_group(grp, "root", 0);
756 #else
757 GTEST_SKIP() << "bionic-only test";
758 #endif
759 }
760
TEST(grp,getgrnam_r_reentrancy)761 TEST(grp, getgrnam_r_reentrancy) {
762 #if defined(__BIONIC__)
763 group grp_storage[2];
764 char buf[2][512];
765 group* grp[3];
766 int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
767 ASSERT_EQ(0, result);
768 check_group(grp[0], "root", 0);
769 grp[1] = getgrnam("system");
770 check_group(grp[1], "system", 1000);
771 result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
772 ASSERT_EQ(0, result);
773 check_group(grp[2], "radio", 1001);
774 check_group(grp[0], "root", 0);
775 check_group(grp[1], "system", 1000);
776 #else
777 GTEST_SKIP() << "bionic-only test";
778 #endif
779 }
780
TEST(grp,getgrgid_r_reentrancy)781 TEST(grp, getgrgid_r_reentrancy) {
782 #if defined(__BIONIC__)
783 group grp_storage[2];
784 char buf[2][512];
785 group* grp[3];
786 int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]);
787 ASSERT_EQ(0, result);
788 check_group(grp[0], "root", 0);
789 grp[1] = getgrgid(1000);
790 check_group(grp[1], "system", 1000);
791 result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]);
792 ASSERT_EQ(0, result);
793 check_group(grp[2], "radio", 1001);
794 check_group(grp[0], "root", 0);
795 check_group(grp[1], "system", 1000);
796 #else
797 GTEST_SKIP() << "bionic-only test";
798 #endif
799 }
800
TEST(grp,getgrnam_r_large_enough_suggested_buffer_size)801 TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) {
802 long size = sysconf(_SC_GETGR_R_SIZE_MAX);
803 ASSERT_GT(size, 0);
804 char buf[size];
805 group grp_storage;
806 group* grp;
807 ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp));
808 check_group(grp, "root", 0);
809 }
810
TEST(grp,getgrent_iterate)811 TEST(grp, getgrent_iterate) {
812 #if defined(__BIONIC__)
813 group* grp;
814 std::set<gid_t> gids;
815
816 setgrent();
817 while ((grp = getgrent()) != nullptr) {
818 ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid;
819 ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid;
820 EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid;
821 EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid;
822
823 EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid;
824 gids.emplace(grp->gr_gid);
825 }
826 endgrent();
827
828 expect_ids(gids, true);
829 #else
830 GTEST_SKIP() << "bionic-only test";
831 #endif
832 }
833
TEST(grp,getgrouplist)834 TEST(grp, getgrouplist) {
835 #if defined(__BIONIC__)
836 // Query the number of groups.
837 int ngroups = 0;
838 ASSERT_EQ(-1, getgrouplist("root", 123, nullptr, &ngroups));
839 ASSERT_EQ(1, ngroups);
840
841 // Query the specific groups (just the one you pass in on Android).
842 ngroups = 8;
843 gid_t groups[ngroups];
844 ASSERT_EQ(1, getgrouplist("root", 123, groups, &ngroups));
845 ASSERT_EQ(1, ngroups);
846 ASSERT_EQ(123u, groups[0]);
847 #else
848 GTEST_SKIP() << "bionic-only test (groups too unpredictable)";
849 #endif
850 }
851
852 #if defined(__BIONIC__)
TestAidNamePrefix(const std::string & file_path)853 static void TestAidNamePrefix(const std::string& file_path) {
854 std::string file_contents;
855 if (!ReadFileToString(file_path, &file_contents)) {
856 // If we cannot read this file, then there are no vendor defind AID names, in which case this
857 // test passes by default.
858 return;
859 }
860 auto lines = Split(file_contents, "\n");
861 for (const auto& line : lines) {
862 if (line.empty()) continue;
863 auto name = Split(line, ":")[0];
864 EXPECT_TRUE(StartsWith(name, "vendor_"));
865 }
866 }
867 #endif
868
TEST(pwd,vendor_prefix_users)869 TEST(pwd, vendor_prefix_users) {
870 #if defined(__BIONIC__)
871 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
872 return;
873 }
874
875 TestAidNamePrefix("/vendor/etc/passwd");
876 #else
877 GTEST_SKIP() << "bionic-only test";
878 #endif
879 }
880
TEST(pwd,vendor_prefix_groups)881 TEST(pwd, vendor_prefix_groups) {
882 #if defined(__BIONIC__)
883 if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
884 return;
885 }
886
887 TestAidNamePrefix("/vendor/etc/group");
888 #else
889 GTEST_SKIP() << "bionic-only test";
890 #endif
891 }
892