blob: bd5c27dd1d7eb8167ca8be728f9b6a8042e36b66 [file] [log] [blame]
Mark Salyzynb38347a2016-04-05 09:09:46 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Tom Cherryb368a0b2019-04-24 11:12:59 -070029#include <android/api-level.h>
Mark Salyzynb38347a2016-04-05 09:09:46 -070030#include <ctype.h>
31#include <errno.h>
32#include <grp.h>
33#include <mntent.h>
34#include <pthread.h>
35#include <pwd.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
Tom Cherryb368a0b2019-04-24 11:12:59 -070039#include <sys/system_properties.h>
Mark Salyzynb38347a2016-04-05 09:09:46 -070040#include <unistd.h>
41
42#include "private/android_filesystem_config.h"
43#include "private/bionic_macros.h"
Josh Gao5e2285d2017-02-22 12:19:05 -080044#include "private/grp_pwd.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070045#include "private/ErrnoRestorer.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070046
Elliott Hughes3f6eee92016-12-13 23:47:25 +000047// Generated android_ids array
48#include "generated_android_ids.h"
Tom Cherry6034ef82018-02-02 16:10:07 -080049#include "grp_pwd_file.h"
50
Tom Cherryc2b9fec2018-05-10 13:33:06 -070051static PasswdFile vendor_passwd("/vendor/etc/passwd", "vendor_");
52static GroupFile vendor_group("/vendor/etc/group", "vendor_");
Elliott Hughes3f6eee92016-12-13 23:47:25 +000053
Mark Salyzynb38347a2016-04-05 09:09:46 -070054// POSIX seems to envisage an implementation where the <pwd.h> functions are
55// implemented by brute-force searching with getpwent(3), and the <grp.h>
56// functions are implemented similarly with getgrent(3). This means that it's
57// okay for all the <grp.h> functions to share state, and all the <passwd.h>
58// functions to share state, but <grp.h> functions can't clobber <passwd.h>
59// functions' state and vice versa.
Josh Gao5e2285d2017-02-22 12:19:05 -080060#include "bionic/pthread_internal.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070061
62static void init_group_state(group_state_t* state) {
Mark Salyzyn722ab052016-04-06 10:35:48 -070063 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
Tom Cherryc57c5bd2019-05-14 17:02:28 -070064 state->group_.gr_name = state->group_name_buffer_;
Mark Salyzynb38347a2016-04-05 09:09:46 -070065 state->group_.gr_mem = state->group_members_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -070066 state->group_.gr_mem[0] = state->group_.gr_name;
Mark Salyzynb38347a2016-04-05 09:09:46 -070067}
68
Tom Cherryc57c5bd2019-05-14 17:02:28 -070069static group_state_t* get_group_tls_buffer() {
70 auto result = &__get_bionic_tls().group;
71 init_group_state(result);
Mark Salyzynb38347a2016-04-05 09:09:46 -070072 return result;
73}
74
Tom Cherryc57c5bd2019-05-14 17:02:28 -070075static void init_passwd_state(passwd_state_t* state) {
76 memset(state, 0, sizeof(passwd_state_t) - sizeof(state->getpwent_idx));
77 state->passwd_.pw_name = state->name_buffer_;
78 state->passwd_.pw_dir = state->dir_buffer_;
79 state->passwd_.pw_shell = state->sh_buffer_;
Mark Salyzynb38347a2016-04-05 09:09:46 -070080}
81
Tom Cherryc57c5bd2019-05-14 17:02:28 -070082static passwd_state_t* get_passwd_tls_buffer() {
83 auto result = &__get_bionic_tls().passwd;
84 init_passwd_state(result);
85 return result;
Mark Salyzynb38347a2016-04-05 09:09:46 -070086}
87
88static passwd* android_iinfo_to_passwd(passwd_state_t* state,
89 const android_id_info* iinfo) {
90 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
91 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
92 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
93
94 passwd* pw = &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -070095 pw->pw_uid = iinfo->aid;
96 pw->pw_gid = iinfo->aid;
Mark Salyzynb38347a2016-04-05 09:09:46 -070097 return pw;
98}
99
100static group* android_iinfo_to_group(group_state_t* state,
101 const android_id_info* iinfo) {
102 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
103
104 group* gr = &state->group_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700105 gr->gr_gid = iinfo->aid;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700106 return gr;
107}
108
Tom Cherry5fb07632019-04-24 13:35:39 -0700109static const android_id_info* find_android_id_info(unsigned id) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700110 for (size_t n = 0; n < android_id_count; ++n) {
111 if (android_ids[n].aid == id) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700112 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700113 }
114 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700115 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700116}
117
Tom Cherry5fb07632019-04-24 13:35:39 -0700118static const android_id_info* find_android_id_info(const char* name) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700119 for (size_t n = 0; n < android_id_count; ++n) {
120 if (!strcmp(android_ids[n].name, name)) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700121 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700122 }
123 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700124 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700125}
126
Tom Cherry4362f892017-11-14 08:50:43 -0800127// These are a list of the reserved app ranges, and should never contain anything below
128// AID_APP_START. They exist per user, so a given uid/gid modulo AID_USER_OFFSET will map
129// to these ranges.
130static constexpr struct {
131 uid_t start;
132 uid_t end;
133} user_ranges[] = {
134 { AID_APP_START, AID_APP_END },
135 { AID_CACHE_GID_START, AID_CACHE_GID_END },
136 { AID_EXT_GID_START, AID_EXT_GID_END },
137 { AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END },
138 { AID_SHARED_GID_START, AID_SHARED_GID_END },
139 { AID_ISOLATED_START, AID_ISOLATED_END },
140};
141
142static constexpr bool verify_user_ranges_ascending() {
143 auto array_size = arraysize(user_ranges);
144 if (array_size < 2) return false;
145
146 if (user_ranges[0].start > user_ranges[0].end) return false;
147
148 for (size_t i = 1; i < array_size; ++i) {
149 if (user_ranges[i].start > user_ranges[i].end) return false;
150 if (user_ranges[i - 1].end > user_ranges[i].start) return false;
151 }
152 return true;
153}
154
155static_assert(verify_user_ranges_ascending(), "user_ranges must have ascending ranges");
156
157static bool is_valid_app_id(id_t id) {
158 id_t appid = id % AID_USER_OFFSET;
159
160 // AID_OVERFLOWUID is never a valid app id, so we explicitly return false to ensure this.
161 // This is true across all users, as there is no reason to ever map this id into any user range.
162 if (appid == AID_OVERFLOWUID) {
163 return false;
164 }
165
166 // If we've resolved to something before app_start, we have already checked against
167 // android_ids, so no need to check again.
168 if (appid < user_ranges[0].start) {
169 return true;
170 }
171
172 // Otherwise check that the appid is in one of the reserved ranges.
173 for (size_t i = 0; i < arraysize(user_ranges); ++i) {
174 if (appid >= user_ranges[i].start && appid <= user_ranges[i].end) {
175 return true;
176 }
177 }
178
179 return false;
180}
181
182// This provides an iterater for app_ids within the first user's app id's.
183static uid_t get_next_app_id(uid_t current_id) {
184 // If current_id is below the first of the user_ranges, then we're uninitialized, and return the
185 // first valid id.
186 if (current_id < user_ranges[0].start) {
187 return user_ranges[0].start;
188 }
189
190 uid_t incremented_id = current_id + 1;
191
192 // Check to see if our incremented_id is between two ranges, and if so, return the beginning of
193 // the next valid range.
194 for (size_t i = 1; i < arraysize(user_ranges); ++i) {
195 if (incremented_id > user_ranges[i - 1].end && incremented_id < user_ranges[i].start) {
196 return user_ranges[i].start;
197 }
198 }
199
200 // Check to see if our incremented_id is above final range, and return -1 to indicate that we've
201 // completed if so.
202 if (incremented_id > user_ranges[arraysize(user_ranges) - 1].end) {
203 return -1;
204 }
205
206 // Otherwise the incremented_id is valid, so return it.
207 return incremented_id;
208}
209
Mark Salyzynb38347a2016-04-05 09:09:46 -0700210// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700211// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
212// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
213// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
214// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
215// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700216// returns 0 and sets errno to ENOENT in case of error.
217static id_t app_id_from_name(const char* name, bool is_group) {
218 char* end;
219 unsigned long userid;
220 bool is_shared_gid = false;
221
222 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
223 end = const_cast<char*>(name+3);
224 userid = 0;
225 is_shared_gid = true;
226 } else if (name[0] == 'u' && isdigit(name[1])) {
227 userid = strtoul(name+1, &end, 10);
228 } else {
229 errno = ENOENT;
230 return 0;
231 }
232
233 if (end[0] != '_' || end[1] == 0) {
234 errno = ENOENT;
235 return 0;
236 }
237
238 unsigned long appid = 0;
239 if (end[1] == 'a' && isdigit(end[2])) {
240 if (is_shared_gid) {
241 // end will point to \0 if the strtoul below succeeds.
242 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
243 if (appid > AID_SHARED_GID_END) {
244 errno = ENOENT;
245 return 0;
246 }
247 } else {
248 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700249 appid = strtoul(end+2, &end, 10);
250 if (is_group && !strcmp(end, "_cache")) {
251 end += 6;
252 appid += AID_CACHE_GID_START;
253 } else {
254 appid += AID_APP_START;
255 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700256 }
257 } else if (end[1] == 'i' && isdigit(end[2])) {
258 // end will point to \0 if the strtoul below succeeds.
259 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
Tom Cherry5fb07632019-04-24 13:35:39 -0700260 } else if (auto* android_id_info = find_android_id_info(end + 1); android_id_info != nullptr) {
261 appid = android_id_info->aid;
262 end += strlen(android_id_info->name) + 1;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700263 }
264
265 // Check that the entire string was consumed by one of the 3 cases above.
266 if (end[0] != 0) {
267 errno = ENOENT;
268 return 0;
269 }
270
271 // Check that user id won't overflow.
272 if (userid > 1000) {
273 errno = ENOENT;
274 return 0;
275 }
276
277 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700278 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700279 errno = ENOENT;
280 return 0;
281 }
282
Jeff Sharkey934bc862016-12-13 14:03:19 -0700283 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700284}
285
286static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700287 const uid_t appid = uid % AID_USER_OFFSET;
288 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700289 if (appid >= AID_ISOLATED_START) {
290 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700291 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700292 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
293 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700294 }
295 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700296 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700297 }
298}
299
300static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700301 const uid_t appid = gid % AID_USER_OFFSET;
302 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700303 if (appid >= AID_ISOLATED_START) {
304 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
305 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
306 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700307 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
308 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
309 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700310 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
311 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700312 }
313 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700314 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700315 }
316}
317
Tom Cherryb368a0b2019-04-24 11:12:59 -0700318static bool device_launched_before_api_29() {
319 // Check if ro.product.first_api_level is set to a value > 0 and < 29, if so, this device was
320 // launched before API 29 (Q). Any other value is considered to be either in development or
321 // launched after.
322 // Cache the value as __system_property_get() is expensive and this may be called often.
323 static bool result = [] {
324 char value[PROP_VALUE_MAX] = { 0 };
325 if (__system_property_get("ro.product.first_api_level", value) == 0) {
326 return false;
327 }
328 int value_int = atoi(value);
329 return value_int != 0 && value_int < 29;
330 }();
331 return result;
332}
333
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700334// oem_XXXX -> uid
335// Supported ranges:
336// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
337// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
338// Check OEM id is within range.
339static bool is_oem_id(id_t id) {
Tom Cherryb368a0b2019-04-24 11:12:59 -0700340 // Upgrading devices launched before API level 29 may not comply with the below check.
341 // Due to the difficulty in changing uids after launch, it is waived for these devices.
342 // The legacy range:
343 // AID_OEM_RESERVED_START to AID_EVERYBODY (2900-9996), excluding builtin AIDs.
344 if (device_launched_before_api_29() && id >= AID_OEM_RESERVED_START && id < AID_EVERYBODY &&
345 find_android_id_info(id) == nullptr) {
346 return true;
347 }
348
349 return (id >= AID_OEM_RESERVED_START && id <= AID_OEM_RESERVED_END) ||
350 (id >= AID_OEM_RESERVED_2_START && id <= AID_OEM_RESERVED_2_END);
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700351}
352
Mark Salyzynb38347a2016-04-05 09:09:46 -0700353// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700354static id_t oem_id_from_name(const char* name) {
355 unsigned int id;
356 if (sscanf(name, "oem_%u", &id) != 1) {
357 return 0;
358 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700359 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700360 return 0;
361 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700362 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700363}
364
365static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700366 if (!is_oem_id(uid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800367 return nullptr;
368 }
369
370 if (vendor_passwd.FindById(uid, state)) {
371 return &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700372 }
373
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700374 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700375 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
Tom Cherryfa5f61c2018-09-27 13:19:02 -0700376 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/vendor/bin/sh");
Mark Salyzynb38347a2016-04-05 09:09:46 -0700377
378 passwd* pw = &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700379 pw->pw_uid = uid;
380 pw->pw_gid = uid;
381 return pw;
382}
383
384static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700385 if (!is_oem_id(gid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800386 return nullptr;
387 }
388
389 if (vendor_group.FindById(gid, state)) {
390 return &state->group_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700391 }
392
393 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700394 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700395
396 group* gr = &state->group_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700397 gr->gr_gid = gid;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700398 return gr;
399}
400
401// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700402// 0 to AID_APP_START-1 -> "system", "radio", etc.
403// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
404// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
405// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700406// returns a passwd structure (sets errno to ENOENT on failure).
407static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800408 if (uid < AID_APP_START || !is_valid_app_id(uid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700409 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700410 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700411 }
412
413 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
414
Jeff Sharkey934bc862016-12-13 14:03:19 -0700415 const uid_t appid = uid % AID_USER_OFFSET;
416 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700417 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
418 } else {
419 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
420 }
421
422 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
423
424 passwd* pw = &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700425 pw->pw_uid = uid;
426 pw->pw_gid = uid;
427 return pw;
428}
429
430// Translate a gid into the corresponding app_<gid>
431// group structure (sets errno to ENOENT on failure).
432static group* app_id_to_group(gid_t gid, group_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800433 if (gid < AID_APP_START || !is_valid_app_id(gid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700434 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700435 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700436 }
437
438 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
439
440 group* gr = &state->group_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700441 gr->gr_gid = gid;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700442 return gr;
443}
444
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700445passwd* getpwuid_internal(uid_t uid, passwd_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700446 if (auto* android_id_info = find_android_id_info(uid); android_id_info != nullptr) {
447 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700448 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700449
Mark Salyzynb38347a2016-04-05 09:09:46 -0700450 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700451 passwd* pw = oem_id_to_passwd(uid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700452 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700453 return pw;
454 }
455 return app_id_to_passwd(uid, state);
456}
457
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700458passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800459 passwd_state_t* state = get_passwd_tls_buffer();
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700460 return getpwuid_internal(uid, state);
461}
Mark Salyzynb38347a2016-04-05 09:09:46 -0700462
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700463passwd* getpwnam_internal(const char* login, passwd_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700464 if (auto* android_id_info = find_android_id_info(login); android_id_info != nullptr) {
465 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700466 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800467
468 if (vendor_passwd.FindByName(login, state)) {
469 if (is_oem_id(state->passwd_.pw_uid)) {
470 return &state->passwd_;
471 }
472 }
473
Mark Salyzynb38347a2016-04-05 09:09:46 -0700474 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700475 passwd* pw = oem_id_to_passwd(oem_id_from_name(login), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700476 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700477 return pw;
478 }
479 return app_id_to_passwd(app_id_from_name(login, false), state);
480}
481
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700482passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
483 passwd_state_t* state = get_passwd_tls_buffer();
484 return getpwnam_internal(login, state);
485}
486
487static int getpasswd_r(bool by_name, const char* name, uid_t uid, struct passwd* pwd, char* buf,
488 size_t buflen, struct passwd** result) {
489 ErrnoRestorer errno_restorer;
490 *result = nullptr;
491 char* p =
492 reinterpret_cast<char*>(__BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
493 if (p + sizeof(passwd_state_t) > buf + buflen) {
494 return ERANGE;
495 }
496 passwd_state_t* state = reinterpret_cast<passwd_state_t*>(p);
497 init_passwd_state(state);
498 passwd* retval = (by_name ? getpwnam_internal(name, state) : getpwuid_internal(uid, state));
499 if (retval != nullptr) {
500 *pwd = *retval;
501 *result = pwd;
502 return 0;
503 }
504 return errno;
505}
506
507int getpwnam_r(const char* name, passwd* pwd, char* buf, size_t byte_count, passwd** result) {
508 return getpasswd_r(true, name, -1, pwd, buf, byte_count, result);
509}
510
511int getpwuid_r(uid_t uid, passwd* pwd, char* buf, size_t byte_count, passwd** result) {
512 return getpasswd_r(false, nullptr, uid, pwd, buf, byte_count, result);
513}
514
Mark Salyzynb38347a2016-04-05 09:09:46 -0700515// All users are in just one group, the one passed in.
516int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
517 if (*ngroups < 1) {
518 *ngroups = 1;
519 return -1;
520 }
521 groups[0] = group;
522 return (*ngroups = 1);
523}
524
525char* getlogin() { // NOLINT: implementing bad function.
526 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
Elliott Hughes06bd5862017-07-28 16:27:49 -0700527 return pw ? pw->pw_name : nullptr;
528}
529
530int getlogin_r(char* buf, size_t size) {
531 char* login = getlogin();
532 if (login == nullptr) return errno;
533 size_t login_length = strlen(login) + 1;
534 if (login_length > size) return ERANGE;
535 memcpy(buf, login, login_length);
536 return 0;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700537}
538
Mark Salyzyn722ab052016-04-06 10:35:48 -0700539void setpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800540 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700541 if (state) {
542 state->getpwent_idx = 0;
543 }
544}
545
546void endpwent() {
547 setpwent();
548}
549
550passwd* getpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800551 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700552 if (state->getpwent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700553 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700554 }
555
556 size_t start = 0;
557 ssize_t end = android_id_count;
558 if (state->getpwent_idx < end) {
559 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
560 }
561
562 start = end;
563 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
564
565 if (state->getpwent_idx < end) {
566 return oem_id_to_passwd(
567 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
568 }
569
570 start = end;
571 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
572
573 if (state->getpwent_idx < end) {
574 return oem_id_to_passwd(
575 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
576 }
577
Tom Cherry4362f892017-11-14 08:50:43 -0800578 state->getpwent_idx = get_next_app_id(state->getpwent_idx);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700579
Tom Cherry4362f892017-11-14 08:50:43 -0800580 if (state->getpwent_idx != -1) {
581 return app_id_to_passwd(state->getpwent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700582 }
583
584 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700585 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700586}
587
Mark Salyzynb38347a2016-04-05 09:09:46 -0700588static group* getgrgid_internal(gid_t gid, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700589 if (auto* android_id_info = find_android_id_info(gid); android_id_info != nullptr) {
590 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700591 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700592
Mark Salyzynb38347a2016-04-05 09:09:46 -0700593 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700594 group* grp = oem_id_to_group(gid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700595 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700596 return grp;
597 }
598 return app_id_to_group(gid, state);
599}
600
601group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700602 group_state_t* state = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -0700603 return getgrgid_internal(gid, state);
604}
605
606static group* getgrnam_internal(const char* name, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700607 if (auto* android_id_info = find_android_id_info(name); android_id_info != nullptr) {
608 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700609 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800610
611 if (vendor_group.FindByName(name, state)) {
612 if (is_oem_id(state->group_.gr_gid)) {
613 return &state->group_;
614 }
615 }
616
Mark Salyzynb38347a2016-04-05 09:09:46 -0700617 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700618 group* grp = oem_id_to_group(oem_id_from_name(name), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700619 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700620 return grp;
621 }
622 return app_id_to_group(app_id_from_name(name, true), state);
623}
624
625group* getgrnam(const char* name) { // NOLINT: implementing bad function.
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700626 group_state_t* state = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -0700627 return getgrnam_internal(name, state);
628}
629
630static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
631 size_t buflen, struct group** result) {
632 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700633 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700634 char* p = reinterpret_cast<char*>(
Dan Alberta613d0d2017-10-05 16:39:33 -0700635 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
Mark Salyzynb38347a2016-04-05 09:09:46 -0700636 if (p + sizeof(group_state_t) > buf + buflen) {
637 return ERANGE;
638 }
639 group_state_t* state = reinterpret_cast<group_state_t*>(p);
640 init_group_state(state);
641 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700642 if (retval != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700643 *grp = *retval;
644 *result = grp;
645 return 0;
646 }
647 return errno;
648}
649
650int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700651 return getgroup_r(false, nullptr, gid, grp, buf, buflen, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700652}
653
654int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
655 struct group **result) {
656 return getgroup_r(true, name, 0, grp, buf, buflen, result);
657}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700658
659void setgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800660 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700661 if (state) {
662 state->getgrent_idx = 0;
663 }
664}
665
666void endgrent() {
667 setgrent();
668}
669
670group* getgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800671 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700672 if (state->getgrent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700673 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700674 }
675
676 size_t start = 0;
677 ssize_t end = android_id_count;
678 if (state->getgrent_idx < end) {
Mark Salyzyn722ab052016-04-06 10:35:48 -0700679 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
680 }
681
682 start = end;
683 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
684
685 if (state->getgrent_idx < end) {
Mark Salyzyn722ab052016-04-06 10:35:48 -0700686 return oem_id_to_group(
687 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
688 }
689
690 start = end;
691 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
692
693 if (state->getgrent_idx < end) {
Mark Salyzyn722ab052016-04-06 10:35:48 -0700694 return oem_id_to_group(
695 state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state);
696 }
697
698 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700699 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
Mark Salyzyn722ab052016-04-06 10:35:48 -0700700
Tom Cherry4362f892017-11-14 08:50:43 -0800701 state->getgrent_idx = get_next_app_id(state->getgrent_idx);
702
703 if (state->getgrent_idx != -1) {
704 return app_id_to_group(state->getgrent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700705 }
706
707 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700708 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700709}