Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 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 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 19 | // Below are the header files we want to test. |
| 20 | #include <grp.h> |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 22 | |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
| 26 | #include <sys/types.h> |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 29 | #include <set> |
| 30 | #include <vector> |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 31 | |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 32 | #include <android-base/file.h> |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 33 | #include <android-base/strings.h> |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
| 35 | |
Tom Cherry | 5c94143 | 2018-10-09 11:01:28 -0700 | [diff] [blame] | 36 | #if defined(__BIONIC__) |
Tom Cherry | 9da8ff1 | 2019-02-19 13:23:49 -0800 | [diff] [blame] | 37 | #include <android/api-level.h> |
Tom Cherry | 5c94143 | 2018-10-09 11:01:28 -0700 | [diff] [blame] | 38 | #include <android-base/properties.h> |
| 39 | #endif |
| 40 | |
Elliott Hughes | 3f6eee9 | 2016-12-13 23:47:25 +0000 | [diff] [blame] | 41 | // Generated android_ids array |
| 42 | #include "generated_android_ids.h" |
| 43 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 44 | using android::base::Join; |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 45 | using android::base::ReadFileToString; |
| 46 | using android::base::Split; |
| 47 | using android::base::StartsWith; |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 48 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 49 | using namespace std::literals; |
| 50 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 51 | enum uid_type_t { |
Tom Cherry | fa5f61c | 2018-09-27 13:19:02 -0700 | [diff] [blame] | 52 | TYPE_APP, |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 53 | TYPE_SYSTEM, |
Tom Cherry | fa5f61c | 2018-09-27 13:19:02 -0700 | [diff] [blame] | 54 | TYPE_VENDOR, |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 55 | }; |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 56 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 57 | #if defined(__BIONIC__) |
| 58 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 59 | static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type, |
| 60 | bool check_username) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(pwd != nullptr); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 62 | if (check_username) { |
| 63 | EXPECT_STREQ(username, pwd->pw_name); |
| 64 | } |
Tom Cherry | 2c05c0f | 2017-11-10 10:57:21 -0800 | [diff] [blame] | 65 | EXPECT_EQ(uid, pwd->pw_uid); |
| 66 | EXPECT_EQ(uid, pwd->pw_gid); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 67 | EXPECT_EQ(nullptr, pwd->pw_passwd); |
Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 68 | #ifdef __LP64__ |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 69 | EXPECT_EQ(nullptr, pwd->pw_gecos); |
Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 70 | #endif |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 71 | |
Tom Cherry | fa5f61c | 2018-09-27 13:19:02 -0700 | [diff] [blame] | 72 | if (uid_type == TYPE_APP) { |
Tom Cherry | 2c05c0f | 2017-11-10 10:57:21 -0800 | [diff] [blame] | 73 | EXPECT_STREQ("/data", pwd->pw_dir); |
Tom Cherry | fa5f61c | 2018-09-27 13:19:02 -0700 | [diff] [blame] | 74 | } else { |
| 75 | EXPECT_STREQ("/", pwd->pw_dir); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 76 | } |
Tom Cherry | fa5f61c | 2018-09-27 13:19:02 -0700 | [diff] [blame] | 77 | |
| 78 | if (uid_type == TYPE_VENDOR) { |
| 79 | EXPECT_STREQ("/vendor/bin/sh", pwd->pw_shell); |
| 80 | } else { |
| 81 | EXPECT_STREQ("/system/bin/sh", pwd->pw_shell); |
| 82 | } |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 83 | } |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 84 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 85 | static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type, |
| 86 | bool check_username) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 87 | errno = 0; |
| 88 | passwd* pwd = getpwuid(uid); |
| 89 | ASSERT_EQ(0, errno); |
| 90 | SCOPED_TRACE("getpwuid"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 91 | check_passwd(pwd, username, uid, uid_type, check_username); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 94 | static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type, |
| 95 | bool check_username) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 96 | errno = 0; |
| 97 | passwd* pwd = getpwnam(username); |
| 98 | ASSERT_EQ(0, errno); |
| 99 | SCOPED_TRACE("getpwnam"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 100 | check_passwd(pwd, username, uid, uid_type, check_username); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 103 | static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type, |
| 104 | bool check_username) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 105 | passwd pwd_storage; |
| 106 | char buf[512]; |
| 107 | int result; |
| 108 | |
| 109 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 110 | passwd* pwd = nullptr; |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 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"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 115 | check_passwd(pwd, username, uid, uid_type, check_username); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 118 | static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type, |
| 119 | bool check_username) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 120 | passwd pwd_storage; |
| 121 | char buf[512]; |
| 122 | int result; |
| 123 | |
| 124 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 125 | passwd* pwd = nullptr; |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 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"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 130 | check_passwd(pwd, username, uid, uid_type, check_username); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 133 | static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type, |
| 134 | bool check_username = true) { |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 135 | SCOPED_TRACE("username '"s + username + "'"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 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); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 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 | |
| 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 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 170 | #else // !defined(__BIONIC__) |
| 171 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 172 | static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */, |
| 173 | bool /* check_username */) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 174 | GTEST_SKIP() << "bionic-only test"; |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 177 | static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 178 | GTEST_SKIP() << "bionic-only test"; |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 181 | static void expect_no_passwd_id(uid_t /* uid */) { |
| 182 | GTEST_SKIP() << "bionic-only test"; |
| 183 | } |
| 184 | |
| 185 | static void expect_no_passwd_name(const char* /* username */) { |
| 186 | GTEST_SKIP() << "bionic-only test"; |
| 187 | } |
| 188 | |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 189 | #endif |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 190 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 191 | TEST(pwd, getpwnam_platform_ids) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 192 | check_get_passwd("root", 0, TYPE_SYSTEM); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 193 | check_get_passwd("daemon", 1, TYPE_SYSTEM); |
| 194 | check_get_passwd("bin", 2, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 195 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 196 | check_get_passwd("system", 1000, TYPE_SYSTEM); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 197 | check_get_passwd("radio", 1001, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 198 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 199 | check_get_passwd("shell", 2000, TYPE_SYSTEM); |
Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 200 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 201 | check_get_passwd("nobody", 9999, TYPE_SYSTEM); |
Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 231 | TEST(pwd, getpwnam_u0_app_ids) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 232 | check_get_passwd("u0_a0", 10000, TYPE_APP); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 233 | check_get_passwd("u0_a1234", 11234, TYPE_APP); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 234 | check_get_passwd("u0_a9999", 19999, TYPE_APP); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 235 | |
Martijn Coenen | f9d2299 | 2019-01-16 16:25:40 +0100 | [diff] [blame] | 236 | check_get_passwd("u0_i1", 90001, TYPE_APP); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 237 | check_get_passwd("u0_i4545", 94545, TYPE_APP); |
| 238 | check_get_passwd("u0_i9999", 99999, TYPE_APP); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 241 | TEST(pwd, getpwnam_app_id_u1_ids) { |
| 242 | check_get_passwd("u1_system", 101000, TYPE_SYSTEM); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 243 | check_get_passwd("u1_radio", 101001, TYPE_SYSTEM); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 244 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 245 | check_get_passwd("u1_a0", 110000, TYPE_APP); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 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); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 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); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 299 | } |
Tom Cherry | c57c5bd | 2019-05-14 17:02:28 -0700 | [diff] [blame] | 300 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
Tom Cherry | 5c94143 | 2018-10-09 11:01:28 -0700 | [diff] [blame] | 383 | #if defined(__BIONIC__) |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 384 | template <typename T> |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 385 | static void expect_ids(const T& ids, bool is_group) { |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 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); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 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 | } |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 410 | expect_range(AID_ISOLATED_START, AID_ISOLATED_END); |
| 411 | |
Tom Cherry | 9da8ff1 | 2019-02-19 13:23:49 -0800 | [diff] [blame] | 412 | // TODO(73062966): We still don't have a good way to create vendor AIDs in the system or other |
| 413 | // non-vendor partitions, therefore we keep this check disabled. |
| 414 | if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= __ANDROID_API_Q__) { |
Tom Cherry | 5c94143 | 2018-10-09 11:01:28 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 418 | // Ensure that no other ids were returned. |
| 419 | auto return_differences = [&ids, &expected_ids] { |
| 420 | std::vector<typename T::key_type> missing_from_ids; |
| 421 | std::set_difference(expected_ids.begin(), expected_ids.end(), ids.begin(), ids.end(), |
| 422 | std::inserter(missing_from_ids, missing_from_ids.begin())); |
| 423 | std::vector<typename T::key_type> extra_in_ids; |
| 424 | std::set_difference(ids.begin(), ids.end(), expected_ids.begin(), expected_ids.end(), |
| 425 | std::inserter(extra_in_ids, extra_in_ids.begin())); |
| 426 | std::string result; |
| 427 | if (!missing_from_ids.empty()) { |
| 428 | result += "Missing ids from results: " + Join(missing_from_ids, " "); |
| 429 | } |
| 430 | if (!extra_in_ids.empty()) { |
| 431 | if (!result.empty()) result += ", "; |
| 432 | result += "Extra ids in results: " + Join(extra_in_ids, " "); |
| 433 | } |
| 434 | return result; |
| 435 | }; |
| 436 | EXPECT_EQ(expected_ids, ids) << return_differences(); |
| 437 | } |
Tom Cherry | 5c94143 | 2018-10-09 11:01:28 -0700 | [diff] [blame] | 438 | #endif |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 439 | |
Elliott Hughes | 5367d1b | 2016-12-12 17:32:14 -0800 | [diff] [blame] | 440 | TEST(pwd, getpwent_iterate) { |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 441 | #if defined(__BIONIC__) |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 442 | passwd* pwd; |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 443 | std::set<uid_t> uids; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 444 | |
| 445 | setpwent(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 446 | while ((pwd = getpwent()) != nullptr) { |
| 447 | ASSERT_TRUE(nullptr != pwd->pw_name); |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 448 | |
Tom Cherry | 2c05c0f | 2017-11-10 10:57:21 -0800 | [diff] [blame] | 449 | EXPECT_EQ(pwd->pw_gid, pwd->pw_uid) << "pwd->pw_uid: " << pwd->pw_uid; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 450 | EXPECT_EQ(nullptr, pwd->pw_passwd) << "pwd->pw_uid: " << pwd->pw_uid; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 451 | #ifdef __LP64__ |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 452 | EXPECT_TRUE(nullptr == pwd->pw_gecos) << "pwd->pw_uid: " << pwd->pw_uid; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 453 | #endif |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 454 | EXPECT_TRUE(nullptr != pwd->pw_shell); |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 455 | if (pwd->pw_uid < AID_APP_START || pwd->pw_uid == AID_OVERFLOWUID) { |
Tom Cherry | 2c05c0f | 2017-11-10 10:57:21 -0800 | [diff] [blame] | 456 | EXPECT_STREQ("/", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid; |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 457 | } else { |
| 458 | EXPECT_STREQ("/data", pwd->pw_dir) << "pwd->pw_uid: " << pwd->pw_uid; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 459 | } |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 460 | |
Tom Cherry | 4cddb00 | 2018-06-29 10:39:43 -0700 | [diff] [blame] | 461 | // TODO(b/27999086): fix this check with the OEM range |
| 462 | // If OEMs add their own AIDs to private/android_filesystem_config.h, this check will fail. |
| 463 | // Long term we want to create a better solution for OEMs adding AIDs, but we're not there |
| 464 | // yet, so therefore we do not check for uid's in the OEM range. |
| 465 | if (!(pwd->pw_uid >= 2900 && pwd->pw_uid <= 2999) && |
| 466 | !(pwd->pw_uid >= 5000 && pwd->pw_uid <= 5999)) { |
| 467 | EXPECT_EQ(0U, uids.count(pwd->pw_uid)) << "pwd->pw_uid: " << pwd->pw_uid; |
| 468 | } |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 469 | uids.emplace(pwd->pw_uid); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 470 | } |
| 471 | endpwent(); |
| 472 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 473 | expect_ids(uids, false); |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 474 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 475 | GTEST_SKIP() << "bionic-only test"; |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 476 | #endif |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 479 | static void check_group(const group* grp, const char* group_name, gid_t gid, |
| 480 | bool check_groupname = true) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 481 | ASSERT_TRUE(grp != nullptr); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 482 | if (check_groupname) { |
| 483 | EXPECT_STREQ(group_name, grp->gr_name); |
| 484 | } |
Tom Cherry | 2c05c0f | 2017-11-10 10:57:21 -0800 | [diff] [blame] | 485 | EXPECT_EQ(gid, grp->gr_gid); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 486 | ASSERT_TRUE(grp->gr_mem != nullptr); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 487 | if (check_groupname) { |
| 488 | EXPECT_STREQ(group_name, grp->gr_mem[0]); |
| 489 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 490 | EXPECT_TRUE(grp->gr_mem[1] == nullptr); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 493 | #if defined(__BIONIC__) |
| 494 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 495 | static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 496 | errno = 0; |
| 497 | group* grp = getgrgid(gid); |
| 498 | ASSERT_EQ(0, errno); |
| 499 | SCOPED_TRACE("getgrgid"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 500 | check_group(grp, group_name, gid, check_groupname); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 503 | static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 504 | errno = 0; |
| 505 | group* grp = getgrnam(group_name); |
| 506 | ASSERT_EQ(0, errno); |
| 507 | SCOPED_TRACE("getgrnam"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 508 | check_group(grp, group_name, gid, check_groupname); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 511 | static void check_getgrgid_r(const char* group_name, gid_t gid, bool check_groupname) { |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 512 | group grp_storage; |
| 513 | char buf[512]; |
| 514 | group* grp; |
| 515 | |
| 516 | errno = 0; |
| 517 | int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp); |
| 518 | ASSERT_EQ(0, result); |
| 519 | ASSERT_EQ(0, errno); |
| 520 | SCOPED_TRACE("getgrgid_r"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 521 | check_group(grp, group_name, gid, check_groupname); |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 524 | static void check_getgrnam_r(const char* group_name, gid_t gid, bool check_groupname) { |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 525 | group grp_storage; |
| 526 | char buf[512]; |
| 527 | group* grp; |
| 528 | |
| 529 | errno = 0; |
| 530 | int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp); |
| 531 | ASSERT_EQ(0, result); |
| 532 | ASSERT_EQ(0, errno); |
| 533 | SCOPED_TRACE("getgrnam_r"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 534 | check_group(grp, group_name, gid, check_groupname); |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 537 | static void check_get_group(const char* group_name, gid_t gid, bool check_groupname = true) { |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 538 | SCOPED_TRACE("groupname '"s + group_name + "'"); |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 539 | check_getgrgid(group_name, gid, check_groupname); |
| 540 | check_getgrnam(group_name, gid, check_groupname); |
| 541 | check_getgrgid_r(group_name, gid, check_groupname); |
| 542 | check_getgrnam_r(group_name, gid, check_groupname); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 545 | static void expect_no_group_id(gid_t gid) { |
| 546 | SCOPED_TRACE("gid '" + std::to_string(gid) + "'"); |
| 547 | errno = 0; |
| 548 | group* group = nullptr; |
| 549 | group = getgrgid(gid); |
| 550 | EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'"; |
| 551 | EXPECT_EQ(ENOENT, errno); |
| 552 | |
| 553 | struct group group_storage; |
| 554 | char buf[512]; |
| 555 | EXPECT_EQ(ENOENT, getgrgid_r(gid, &group_storage, buf, sizeof(buf), &group)); |
| 556 | EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'"; |
| 557 | } |
| 558 | |
| 559 | static void expect_no_group_name(const char* groupname) { |
| 560 | SCOPED_TRACE("groupname '"s + groupname + "'"); |
| 561 | errno = 0; |
| 562 | group* group = nullptr; |
| 563 | group = getgrnam(groupname); |
| 564 | EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'"; |
| 565 | EXPECT_EQ(ENOENT, errno); |
| 566 | |
| 567 | struct group group_storage; |
| 568 | char buf[512]; |
| 569 | EXPECT_EQ(ENOENT, getgrnam_r(groupname, &group_storage, buf, sizeof(buf), &group)); |
| 570 | EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'"; |
| 571 | } |
| 572 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 573 | #else // !defined(__BIONIC__) |
| 574 | |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 575 | static void check_get_group(const char*, gid_t, bool) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 576 | GTEST_SKIP() << "bionic-only test"; |
Tom Cherry | b4c25c8 | 2018-04-04 15:02:55 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 579 | static void check_get_group(const char*, gid_t) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 580 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 583 | static void expect_no_group_id(gid_t /* gid */) { |
| 584 | GTEST_SKIP() << "bionic-only test"; |
| 585 | } |
| 586 | |
| 587 | static void expect_no_group_name(const char* /* groupname */) { |
| 588 | GTEST_SKIP() << "bionic-only test"; |
| 589 | } |
| 590 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 591 | #endif |
| 592 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 593 | TEST(grp, getgrnam_platform_ids) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 594 | check_get_group("root", 0); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 595 | check_get_group("daemon", 1); |
| 596 | check_get_group("bin", 2); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 597 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 598 | check_get_group("system", 1000); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 599 | check_get_group("radio", 1001); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 600 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 601 | check_get_group("shell", 2000); |
Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 602 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 603 | check_get_group("nobody", 9999); |
| 604 | } |
| 605 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 606 | TEST(grp, getgrnam_oem_ids) { |
| 607 | check_get_group("oem_2900", 2900, false); |
| 608 | check_get_group("oem_2945", 2945, false); |
| 609 | check_get_group("oem_2999", 2999, false); |
| 610 | check_get_group("oem_5000", 5000, false); |
| 611 | check_get_group("oem_5454", 5454, false); |
| 612 | check_get_group("oem_5999", 5999, false); |
| 613 | } |
| 614 | |
| 615 | TEST(grp, getgrnam_non_exist) { |
| 616 | expect_no_passwd_id(999); // End of the system reserved range, unallocated. |
| 617 | expect_no_passwd_id(1999); // End of the system reserved range, unallocated. |
| 618 | expect_no_passwd_id(2899); // End of the system reserved range, unallocated. |
| 619 | } |
| 620 | |
| 621 | TEST(grp, getgrnam_u0_app_ids) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 622 | check_get_group("u0_a0", 10000); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 623 | check_get_group("u0_a1234", 11234); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 624 | check_get_group("u0_a9999", 19999); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 625 | |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 626 | check_get_group("u0_a0_cache", 20000); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 627 | check_get_group("u0_a1234_cache", 21234); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 628 | check_get_group("u0_a9999_cache", 29999); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 629 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 630 | check_get_group("u0_a0_ext", 30000); |
| 631 | check_get_group("u0_a4545_ext", 34545); |
| 632 | check_get_group("u0_a9999_ext", 39999); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 633 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 634 | check_get_group("u0_a0_ext_cache", 40000); |
| 635 | check_get_group("u0_a4545_ext_cache", 44545); |
| 636 | check_get_group("u0_a9999_ext_cache", 49999); |
| 637 | |
| 638 | check_get_group("all_a0", 50000); |
| 639 | check_get_group("all_a4545", 54545); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 640 | check_get_group("all_a9999", 59999); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 641 | |
Martijn Coenen | f9d2299 | 2019-01-16 16:25:40 +0100 | [diff] [blame] | 642 | check_get_group("u0_i1", 90001); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 645 | TEST(grp, getgrnam_u1_app_ids) { |
| 646 | check_get_group("u1_system", 101000); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 647 | check_get_group("u1_radio", 101001); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 648 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 649 | check_get_group("u1_a0", 110000); |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 650 | check_get_group("u1_a1234", 111234); |
| 651 | check_get_group("u1_a9999", 119999); |
| 652 | |
| 653 | check_get_group("u1_a0_cache", 120000); |
| 654 | check_get_group("u1_a1234_cache", 121234); |
| 655 | check_get_group("u1_a9999_cache", 129999); |
| 656 | |
| 657 | check_get_group("u1_a0_ext", 130000); |
| 658 | check_get_group("u1_a4545_ext", 134545); |
| 659 | check_get_group("u1_a9999_ext", 139999); |
| 660 | |
| 661 | check_get_group("u1_a0_ext_cache", 140000); |
| 662 | check_get_group("u1_a4545_ext_cache", 144545); |
| 663 | check_get_group("u1_a9999_ext_cache", 149999); |
| 664 | |
| 665 | check_get_group("u1_i1", 190001); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 668 | TEST(grp, getgrnam_u31_app_ids) { |
| 669 | check_get_group("u31_system", 3101000); |
| 670 | check_get_group("u31_radio", 3101001); |
| 671 | |
| 672 | check_get_group("u31_a0", 3110000); |
| 673 | check_get_group("u31_a1234", 3111234); |
| 674 | check_get_group("u31_a9999", 3119999); |
| 675 | |
| 676 | check_get_group("u31_a0_cache", 3120000); |
| 677 | check_get_group("u31_a1234_cache", 3121234); |
| 678 | check_get_group("u31_a9999_cache", 3129999); |
| 679 | |
| 680 | check_get_group("u31_a0_cache", 3120000); |
| 681 | check_get_group("u31_a1234_cache", 3121234); |
| 682 | check_get_group("u31_a9999_cache", 3129999); |
| 683 | |
| 684 | check_get_group("u31_a0_ext", 3130000); |
| 685 | check_get_group("u31_a4545_ext", 3134545); |
| 686 | check_get_group("u31_a9999_ext", 3139999); |
| 687 | |
| 688 | check_get_group("u31_a0_ext_cache", 3140000); |
| 689 | check_get_group("u31_a4545_ext_cache", 3144545); |
| 690 | check_get_group("u31_a9999_ext_cache", 3149999); |
| 691 | |
| 692 | check_get_group("u31_i1", 3190001); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 695 | TEST(grp, getpgram_app_id_not_allowed_platform) { |
| 696 | expect_no_group_name("u1_root"); |
| 697 | expect_no_group_name("u1_debuggerd"); |
| 698 | |
| 699 | expect_no_group_name("u31_root"); |
| 700 | expect_no_group_name("u31_debuggerd"); |
| 701 | } |
| 702 | |
| 703 | TEST(grp, getgrgid_app_id_u1_non_exist) { |
| 704 | expect_no_group_id(100000); // There is no 'root' for secondary users. |
| 705 | expect_no_group_id(101999); // End of the system reserved range, unallocated. |
| 706 | expect_no_group_id(102900); // The OEM ranges were never allocated to secondary users. |
| 707 | expect_no_group_id(105000); // The OEM ranges were never allocated to secondary users. |
| 708 | |
| 709 | // The shared range is shared among users, and therefore doesn't exist for secondary users. |
| 710 | expect_no_group_id(150000); |
| 711 | } |
| 712 | |
| 713 | TEST(grp, getgrgid_app_id_u31_non_exist) { |
| 714 | expect_no_group_id(3100000); // There is no 'root' for secondary users. |
| 715 | expect_no_group_id(3101999); // End of the system reserved range, unallocated. |
| 716 | expect_no_group_id(3102900); // The OEM ranges were never allocated to secondary users. |
| 717 | expect_no_group_id(3105000); // The OEM ranges were never allocated to secondary users. |
| 718 | |
| 719 | // The shared range is shared among users, and therefore doesn't exist for secondary users. |
| 720 | expect_no_group_id(3150000); |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 721 | } |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 722 | |
Tom Cherry | c57c5bd | 2019-05-14 17:02:28 -0700 | [diff] [blame] | 723 | TEST(grp, getgrnam_r_alignment) { |
| 724 | #if defined(__BIONIC__) |
| 725 | group grp_storage; |
| 726 | alignas(16) char buf[512]; |
| 727 | group* grp; |
| 728 | int result = getgrnam_r("root", &grp_storage, buf + 1, sizeof(buf) - 1, &grp); |
| 729 | ASSERT_EQ(0, result); |
| 730 | check_group(grp, "root", 0); |
| 731 | #else |
| 732 | GTEST_SKIP() << "bionic-only test"; |
| 733 | #endif |
| 734 | } |
| 735 | |
| 736 | TEST(grp, getgrgid_r_alignment) { |
| 737 | #if defined(__BIONIC__) |
| 738 | group grp_storage; |
| 739 | alignas(16) char buf[512]; |
| 740 | group* grp; |
| 741 | int result = getgrgid_r(0, &grp_storage, buf + 1, sizeof(buf) - 1, &grp); |
| 742 | ASSERT_EQ(0, result); |
| 743 | check_group(grp, "root", 0); |
| 744 | #else |
| 745 | GTEST_SKIP() << "bionic-only test"; |
| 746 | #endif |
| 747 | } |
| 748 | |
Elliott Hughes | 5367d1b | 2016-12-12 17:32:14 -0800 | [diff] [blame] | 749 | TEST(grp, getgrnam_r_reentrancy) { |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 750 | #if defined(__BIONIC__) |
| 751 | group grp_storage[2]; |
| 752 | char buf[2][512]; |
| 753 | group* grp[3]; |
| 754 | int result = getgrnam_r("root", &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]); |
| 755 | ASSERT_EQ(0, result); |
| 756 | check_group(grp[0], "root", 0); |
| 757 | grp[1] = getgrnam("system"); |
| 758 | check_group(grp[1], "system", 1000); |
| 759 | result = getgrnam_r("radio", &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]); |
| 760 | ASSERT_EQ(0, result); |
| 761 | check_group(grp[2], "radio", 1001); |
| 762 | check_group(grp[0], "root", 0); |
| 763 | check_group(grp[1], "system", 1000); |
| 764 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 765 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 766 | #endif |
| 767 | } |
| 768 | |
Elliott Hughes | 5367d1b | 2016-12-12 17:32:14 -0800 | [diff] [blame] | 769 | TEST(grp, getgrgid_r_reentrancy) { |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 770 | #if defined(__BIONIC__) |
| 771 | group grp_storage[2]; |
| 772 | char buf[2][512]; |
| 773 | group* grp[3]; |
| 774 | int result = getgrgid_r(0, &grp_storage[0], buf[0], sizeof(buf[0]), &grp[0]); |
| 775 | ASSERT_EQ(0, result); |
| 776 | check_group(grp[0], "root", 0); |
| 777 | grp[1] = getgrgid(1000); |
| 778 | check_group(grp[1], "system", 1000); |
| 779 | result = getgrgid_r(1001, &grp_storage[1], buf[1], sizeof(buf[1]), &grp[2]); |
| 780 | ASSERT_EQ(0, result); |
| 781 | check_group(grp[2], "radio", 1001); |
| 782 | check_group(grp[0], "root", 0); |
| 783 | check_group(grp[1], "system", 1000); |
| 784 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 785 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 786 | #endif |
| 787 | } |
| 788 | |
Elliott Hughes | 5367d1b | 2016-12-12 17:32:14 -0800 | [diff] [blame] | 789 | TEST(grp, getgrnam_r_large_enough_suggested_buffer_size) { |
Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 790 | long size = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 791 | ASSERT_GT(size, 0); |
| 792 | char buf[size]; |
| 793 | group grp_storage; |
| 794 | group* grp; |
| 795 | ASSERT_EQ(0, getgrnam_r("root", &grp_storage, buf, size, &grp)); |
| 796 | check_group(grp, "root", 0); |
| 797 | } |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 798 | |
Elliott Hughes | 5367d1b | 2016-12-12 17:32:14 -0800 | [diff] [blame] | 799 | TEST(grp, getgrent_iterate) { |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 800 | #if defined(__BIONIC__) |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 801 | group* grp; |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 802 | std::set<gid_t> gids; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 803 | |
| 804 | setgrent(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 805 | while ((grp = getgrent()) != nullptr) { |
| 806 | ASSERT_TRUE(grp->gr_name != nullptr) << "grp->gr_gid: " << grp->gr_gid; |
| 807 | ASSERT_TRUE(grp->gr_mem != nullptr) << "grp->gr_gid: " << grp->gr_gid; |
Tom Cherry | 2c05c0f | 2017-11-10 10:57:21 -0800 | [diff] [blame] | 808 | EXPECT_STREQ(grp->gr_name, grp->gr_mem[0]) << "grp->gr_gid: " << grp->gr_gid; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 809 | EXPECT_TRUE(grp->gr_mem[1] == nullptr) << "grp->gr_gid: " << grp->gr_gid; |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 810 | |
Tom Cherry | 4cddb00 | 2018-06-29 10:39:43 -0700 | [diff] [blame] | 811 | // TODO(b/27999086): fix this check with the OEM range |
| 812 | // If OEMs add their own AIDs to private/android_filesystem_config.h, this check will fail. |
| 813 | // Long term we want to create a better solution for OEMs adding AIDs, but we're not there |
| 814 | // yet, so therefore we do not check for gid's in the OEM range. |
| 815 | if (!(grp->gr_gid >= 2900 && grp->gr_gid <= 2999) && |
| 816 | !(grp->gr_gid >= 5000 && grp->gr_gid <= 5999)) { |
| 817 | EXPECT_EQ(0U, gids.count(grp->gr_gid)) << "grp->gr_gid: " << grp->gr_gid; |
| 818 | } |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 819 | gids.emplace(grp->gr_gid); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 820 | } |
| 821 | endgrent(); |
| 822 | |
Tom Cherry | 6b116d1 | 2019-04-25 10:34:07 -0700 | [diff] [blame] | 823 | expect_ids(gids, true); |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 824 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 825 | GTEST_SKIP() << "bionic-only test"; |
Josh Gao | 2fe1034 | 2018-02-27 14:05:53 -0800 | [diff] [blame] | 826 | #endif |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 827 | } |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 828 | |
| 829 | #if defined(__BIONIC__) |
| 830 | static void TestAidNamePrefix(const std::string& file_path) { |
| 831 | std::string file_contents; |
| 832 | if (!ReadFileToString(file_path, &file_contents)) { |
| 833 | // If we cannot read this file, then there are no vendor defind AID names, in which case this |
| 834 | // test passes by default. |
| 835 | return; |
| 836 | } |
| 837 | auto lines = Split(file_contents, "\n"); |
| 838 | for (const auto& line : lines) { |
| 839 | if (line.empty()) continue; |
| 840 | auto name = Split(line, ":")[0]; |
| 841 | EXPECT_TRUE(StartsWith(name, "vendor_")); |
| 842 | } |
| 843 | } |
| 844 | #endif |
| 845 | |
| 846 | TEST(pwd, vendor_prefix_users) { |
| 847 | #if defined(__BIONIC__) |
Chuwei Xu | 5d9312b | 2018-10-23 13:50:04 +0800 | [diff] [blame] | 848 | if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) { |
| 849 | return; |
| 850 | } |
| 851 | |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 852 | TestAidNamePrefix("/vendor/etc/passwd"); |
| 853 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 854 | GTEST_SKIP() << "bionic-only test"; |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 855 | #endif |
| 856 | } |
| 857 | |
| 858 | TEST(pwd, vendor_prefix_groups) { |
| 859 | #if defined(__BIONIC__) |
Chuwei Xu | 5d9312b | 2018-10-23 13:50:04 +0800 | [diff] [blame] | 860 | if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) { |
| 861 | return; |
| 862 | } |
| 863 | |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 864 | TestAidNamePrefix("/vendor/etc/group"); |
| 865 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 866 | GTEST_SKIP() << "bionic-only test"; |
Tom Cherry | e88b408 | 2018-05-24 14:44:10 -0700 | [diff] [blame] | 867 | #endif |
| 868 | } |