blob: c6e09a287e17629412a7de60cbe003da3ff9f937 [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"
61static group_state_t* get_group_tls_buffer() {
62 return &__get_bionic_tls().group;
63}
Mark Salyzynb38347a2016-04-05 09:09:46 -070064
Josh Gao5e2285d2017-02-22 12:19:05 -080065static passwd_state_t* get_passwd_tls_buffer() {
66 return &__get_bionic_tls().passwd;
67}
Mark Salyzynb38347a2016-04-05 09:09:46 -070068
69static void init_group_state(group_state_t* state) {
Mark Salyzyn722ab052016-04-06 10:35:48 -070070 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
Mark Salyzynb38347a2016-04-05 09:09:46 -070071 state->group_.gr_mem = state->group_members_;
72}
73
74static group_state_t* __group_state() {
Josh Gao5e2285d2017-02-22 12:19:05 -080075 group_state_t* result = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -070076 if (result != nullptr) {
77 init_group_state(result);
78 }
79 return result;
80}
81
82static int do_getpw_r(int by_name, const char* name, uid_t uid,
83 passwd* dst, char* buf, size_t byte_count,
84 passwd** result) {
85 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
86 // make might.
87 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -070089
90 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
91 // storage, so we can call them as long as we copy everything out
92 // before returning.
93 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
94
95 // POSIX allows failure to find a match to be considered a non-error.
96 // Reporting success (0) but with *result NULL is glibc's behavior.
Yi Kong32bc0fc2018-08-02 17:31:13 -070097 if (src == nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -070098 return (errno == ENOENT) ? 0 : errno;
99 }
100
101 // Work out where our strings will go in 'buf', and whether we've got
102 // enough space.
103 size_t required_byte_count = 0;
104 dst->pw_name = buf;
105 required_byte_count += strlen(src->pw_name) + 1;
106 dst->pw_dir = buf + required_byte_count;
107 required_byte_count += strlen(src->pw_dir) + 1;
108 dst->pw_shell = buf + required_byte_count;
109 required_byte_count += strlen(src->pw_shell) + 1;
110 if (byte_count < required_byte_count) {
111 return ERANGE;
112 }
113
114 // Copy the strings.
115 snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
116
117 // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic.
118 // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700119 dst->pw_passwd = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700120#if defined(__LP64__)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 dst->pw_gecos = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700122#endif
123
124 // Copy the integral fields.
125 dst->pw_gid = src->pw_gid;
126 dst->pw_uid = src->pw_uid;
127
128 *result = dst;
129 return 0;
130}
131
132int getpwnam_r(const char* name, passwd* pwd,
133 char* buf, size_t byte_count, passwd** result) {
134 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
135}
136
137int getpwuid_r(uid_t uid, passwd* pwd,
138 char* buf, size_t byte_count, passwd** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700139 return do_getpw_r(0, nullptr, uid, pwd, buf, byte_count, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700140}
141
142static passwd* android_iinfo_to_passwd(passwd_state_t* state,
143 const android_id_info* iinfo) {
144 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
145 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
146 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
147
148 passwd* pw = &state->passwd_;
149 pw->pw_name = state->name_buffer_;
150 pw->pw_uid = iinfo->aid;
151 pw->pw_gid = iinfo->aid;
152 pw->pw_dir = state->dir_buffer_;
153 pw->pw_shell = state->sh_buffer_;
154 return pw;
155}
156
157static group* android_iinfo_to_group(group_state_t* state,
158 const android_id_info* iinfo) {
159 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
160
161 group* gr = &state->group_;
162 gr->gr_name = state->group_name_buffer_;
163 gr->gr_gid = iinfo->aid;
164 gr->gr_mem[0] = gr->gr_name;
165 return gr;
166}
167
Tom Cherry5fb07632019-04-24 13:35:39 -0700168static const android_id_info* find_android_id_info(unsigned id) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700169 for (size_t n = 0; n < android_id_count; ++n) {
170 if (android_ids[n].aid == id) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700171 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700172 }
173 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700174 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700175}
176
Tom Cherry5fb07632019-04-24 13:35:39 -0700177static const android_id_info* find_android_id_info(const char* name) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700178 for (size_t n = 0; n < android_id_count; ++n) {
179 if (!strcmp(android_ids[n].name, name)) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700180 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700181 }
182 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700183 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700184}
185
Tom Cherry4362f892017-11-14 08:50:43 -0800186// These are a list of the reserved app ranges, and should never contain anything below
187// AID_APP_START. They exist per user, so a given uid/gid modulo AID_USER_OFFSET will map
188// to these ranges.
189static constexpr struct {
190 uid_t start;
191 uid_t end;
192} user_ranges[] = {
193 { AID_APP_START, AID_APP_END },
194 { AID_CACHE_GID_START, AID_CACHE_GID_END },
195 { AID_EXT_GID_START, AID_EXT_GID_END },
196 { AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END },
197 { AID_SHARED_GID_START, AID_SHARED_GID_END },
198 { AID_ISOLATED_START, AID_ISOLATED_END },
199};
200
201static constexpr bool verify_user_ranges_ascending() {
202 auto array_size = arraysize(user_ranges);
203 if (array_size < 2) return false;
204
205 if (user_ranges[0].start > user_ranges[0].end) return false;
206
207 for (size_t i = 1; i < array_size; ++i) {
208 if (user_ranges[i].start > user_ranges[i].end) return false;
209 if (user_ranges[i - 1].end > user_ranges[i].start) return false;
210 }
211 return true;
212}
213
214static_assert(verify_user_ranges_ascending(), "user_ranges must have ascending ranges");
215
216static bool is_valid_app_id(id_t id) {
217 id_t appid = id % AID_USER_OFFSET;
218
219 // AID_OVERFLOWUID is never a valid app id, so we explicitly return false to ensure this.
220 // This is true across all users, as there is no reason to ever map this id into any user range.
221 if (appid == AID_OVERFLOWUID) {
222 return false;
223 }
224
225 // If we've resolved to something before app_start, we have already checked against
226 // android_ids, so no need to check again.
227 if (appid < user_ranges[0].start) {
228 return true;
229 }
230
231 // Otherwise check that the appid is in one of the reserved ranges.
232 for (size_t i = 0; i < arraysize(user_ranges); ++i) {
233 if (appid >= user_ranges[i].start && appid <= user_ranges[i].end) {
234 return true;
235 }
236 }
237
238 return false;
239}
240
241// This provides an iterater for app_ids within the first user's app id's.
242static uid_t get_next_app_id(uid_t current_id) {
243 // If current_id is below the first of the user_ranges, then we're uninitialized, and return the
244 // first valid id.
245 if (current_id < user_ranges[0].start) {
246 return user_ranges[0].start;
247 }
248
249 uid_t incremented_id = current_id + 1;
250
251 // Check to see if our incremented_id is between two ranges, and if so, return the beginning of
252 // the next valid range.
253 for (size_t i = 1; i < arraysize(user_ranges); ++i) {
254 if (incremented_id > user_ranges[i - 1].end && incremented_id < user_ranges[i].start) {
255 return user_ranges[i].start;
256 }
257 }
258
259 // Check to see if our incremented_id is above final range, and return -1 to indicate that we've
260 // completed if so.
261 if (incremented_id > user_ranges[arraysize(user_ranges) - 1].end) {
262 return -1;
263 }
264
265 // Otherwise the incremented_id is valid, so return it.
266 return incremented_id;
267}
268
Mark Salyzynb38347a2016-04-05 09:09:46 -0700269// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700270// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
271// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
272// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
273// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
274// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700275// returns 0 and sets errno to ENOENT in case of error.
276static id_t app_id_from_name(const char* name, bool is_group) {
277 char* end;
278 unsigned long userid;
279 bool is_shared_gid = false;
280
281 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
282 end = const_cast<char*>(name+3);
283 userid = 0;
284 is_shared_gid = true;
285 } else if (name[0] == 'u' && isdigit(name[1])) {
286 userid = strtoul(name+1, &end, 10);
287 } else {
288 errno = ENOENT;
289 return 0;
290 }
291
292 if (end[0] != '_' || end[1] == 0) {
293 errno = ENOENT;
294 return 0;
295 }
296
297 unsigned long appid = 0;
298 if (end[1] == 'a' && isdigit(end[2])) {
299 if (is_shared_gid) {
300 // end will point to \0 if the strtoul below succeeds.
301 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
302 if (appid > AID_SHARED_GID_END) {
303 errno = ENOENT;
304 return 0;
305 }
306 } else {
307 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700308 appid = strtoul(end+2, &end, 10);
309 if (is_group && !strcmp(end, "_cache")) {
310 end += 6;
311 appid += AID_CACHE_GID_START;
312 } else {
313 appid += AID_APP_START;
314 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700315 }
316 } else if (end[1] == 'i' && isdigit(end[2])) {
317 // end will point to \0 if the strtoul below succeeds.
318 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
Tom Cherry5fb07632019-04-24 13:35:39 -0700319 } else if (auto* android_id_info = find_android_id_info(end + 1); android_id_info != nullptr) {
320 appid = android_id_info->aid;
321 end += strlen(android_id_info->name) + 1;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700322 }
323
324 // Check that the entire string was consumed by one of the 3 cases above.
325 if (end[0] != 0) {
326 errno = ENOENT;
327 return 0;
328 }
329
330 // Check that user id won't overflow.
331 if (userid > 1000) {
332 errno = ENOENT;
333 return 0;
334 }
335
336 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700337 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700338 errno = ENOENT;
339 return 0;
340 }
341
Jeff Sharkey934bc862016-12-13 14:03:19 -0700342 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700343}
344
345static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700346 const uid_t appid = uid % AID_USER_OFFSET;
347 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700348 if (appid >= AID_ISOLATED_START) {
349 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700350 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700351 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
352 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700353 }
354 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700355 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700356 }
357}
358
359static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700360 const uid_t appid = gid % AID_USER_OFFSET;
361 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700362 if (appid >= AID_ISOLATED_START) {
363 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
364 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
365 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700366 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
367 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
368 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700369 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
370 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700371 }
372 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700373 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700374 }
375}
376
Tom Cherryb368a0b2019-04-24 11:12:59 -0700377static bool device_launched_before_api_29() {
378 // Check if ro.product.first_api_level is set to a value > 0 and < 29, if so, this device was
379 // launched before API 29 (Q). Any other value is considered to be either in development or
380 // launched after.
381 // Cache the value as __system_property_get() is expensive and this may be called often.
382 static bool result = [] {
383 char value[PROP_VALUE_MAX] = { 0 };
384 if (__system_property_get("ro.product.first_api_level", value) == 0) {
385 return false;
386 }
387 int value_int = atoi(value);
388 return value_int != 0 && value_int < 29;
389 }();
390 return result;
391}
392
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700393// oem_XXXX -> uid
394// Supported ranges:
395// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
396// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
397// Check OEM id is within range.
398static bool is_oem_id(id_t id) {
Tom Cherryb368a0b2019-04-24 11:12:59 -0700399 // Upgrading devices launched before API level 29 may not comply with the below check.
400 // Due to the difficulty in changing uids after launch, it is waived for these devices.
401 // The legacy range:
402 // AID_OEM_RESERVED_START to AID_EVERYBODY (2900-9996), excluding builtin AIDs.
403 if (device_launched_before_api_29() && id >= AID_OEM_RESERVED_START && id < AID_EVERYBODY &&
404 find_android_id_info(id) == nullptr) {
405 return true;
406 }
407
408 return (id >= AID_OEM_RESERVED_START && id <= AID_OEM_RESERVED_END) ||
409 (id >= AID_OEM_RESERVED_2_START && id <= AID_OEM_RESERVED_2_END);
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700410}
411
Mark Salyzynb38347a2016-04-05 09:09:46 -0700412// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700413static id_t oem_id_from_name(const char* name) {
414 unsigned int id;
415 if (sscanf(name, "oem_%u", &id) != 1) {
416 return 0;
417 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700418 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700419 return 0;
420 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700421 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700422}
423
424static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700425 if (!is_oem_id(uid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800426 return nullptr;
427 }
428
429 if (vendor_passwd.FindById(uid, state)) {
430 return &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700431 }
432
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700433 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700434 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
Tom Cherryfa5f61c2018-09-27 13:19:02 -0700435 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/vendor/bin/sh");
Mark Salyzynb38347a2016-04-05 09:09:46 -0700436
437 passwd* pw = &state->passwd_;
438 pw->pw_name = state->name_buffer_;
439 pw->pw_dir = state->dir_buffer_;
440 pw->pw_shell = state->sh_buffer_;
441 pw->pw_uid = uid;
442 pw->pw_gid = uid;
443 return pw;
444}
445
446static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700447 if (!is_oem_id(gid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800448 return nullptr;
449 }
450
451 if (vendor_group.FindById(gid, state)) {
452 return &state->group_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700453 }
454
455 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700456 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700457
458 group* gr = &state->group_;
459 gr->gr_name = state->group_name_buffer_;
460 gr->gr_gid = gid;
461 gr->gr_mem[0] = gr->gr_name;
462 return gr;
463}
464
465// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700466// 0 to AID_APP_START-1 -> "system", "radio", etc.
467// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
468// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
469// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700470// returns a passwd structure (sets errno to ENOENT on failure).
471static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800472 if (uid < AID_APP_START || !is_valid_app_id(uid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700473 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700474 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700475 }
476
477 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
478
Jeff Sharkey934bc862016-12-13 14:03:19 -0700479 const uid_t appid = uid % AID_USER_OFFSET;
480 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700481 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
482 } else {
483 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
484 }
485
486 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
487
488 passwd* pw = &state->passwd_;
489 pw->pw_name = state->name_buffer_;
490 pw->pw_dir = state->dir_buffer_;
491 pw->pw_shell = state->sh_buffer_;
492 pw->pw_uid = uid;
493 pw->pw_gid = uid;
494 return pw;
495}
496
497// Translate a gid into the corresponding app_<gid>
498// group structure (sets errno to ENOENT on failure).
499static group* app_id_to_group(gid_t gid, group_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800500 if (gid < AID_APP_START || !is_valid_app_id(gid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700501 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700502 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700503 }
504
505 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
506
507 group* gr = &state->group_;
508 gr->gr_name = state->group_name_buffer_;
509 gr->gr_gid = gid;
510 gr->gr_mem[0] = gr->gr_name;
511 return gr;
512}
513
514passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800515 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700516 if (state == nullptr) {
517 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700518 }
519
Tom Cherry5fb07632019-04-24 13:35:39 -0700520 if (auto* android_id_info = find_android_id_info(uid); android_id_info != nullptr) {
521 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700522 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700523
Mark Salyzynb38347a2016-04-05 09:09:46 -0700524 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700525 passwd* pw = oem_id_to_passwd(uid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700526 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700527 return pw;
528 }
529 return app_id_to_passwd(uid, state);
530}
531
532passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800533 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700534 if (state == nullptr) {
535 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700536 }
537
Tom Cherry5fb07632019-04-24 13:35:39 -0700538 if (auto* android_id_info = find_android_id_info(login); android_id_info != nullptr) {
539 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700540 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800541
542 if (vendor_passwd.FindByName(login, state)) {
543 if (is_oem_id(state->passwd_.pw_uid)) {
544 return &state->passwd_;
545 }
546 }
547
Mark Salyzynb38347a2016-04-05 09:09:46 -0700548 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700549 passwd* pw = oem_id_to_passwd(oem_id_from_name(login), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700550 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700551 return pw;
552 }
553 return app_id_to_passwd(app_id_from_name(login, false), state);
554}
555
556// All users are in just one group, the one passed in.
557int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
558 if (*ngroups < 1) {
559 *ngroups = 1;
560 return -1;
561 }
562 groups[0] = group;
563 return (*ngroups = 1);
564}
565
566char* getlogin() { // NOLINT: implementing bad function.
567 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
Elliott Hughes06bd5862017-07-28 16:27:49 -0700568 return pw ? pw->pw_name : nullptr;
569}
570
571int getlogin_r(char* buf, size_t size) {
572 char* login = getlogin();
573 if (login == nullptr) return errno;
574 size_t login_length = strlen(login) + 1;
575 if (login_length > size) return ERANGE;
576 memcpy(buf, login, login_length);
577 return 0;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700578}
579
Mark Salyzyn722ab052016-04-06 10:35:48 -0700580void setpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800581 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700582 if (state) {
583 state->getpwent_idx = 0;
584 }
585}
586
587void endpwent() {
588 setpwent();
589}
590
591passwd* getpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800592 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700593 if (state == nullptr) {
594 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700595 }
596 if (state->getpwent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700597 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700598 }
599
600 size_t start = 0;
601 ssize_t end = android_id_count;
602 if (state->getpwent_idx < end) {
603 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
604 }
605
606 start = end;
607 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
608
609 if (state->getpwent_idx < end) {
610 return oem_id_to_passwd(
611 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
612 }
613
614 start = end;
615 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
616
617 if (state->getpwent_idx < end) {
618 return oem_id_to_passwd(
619 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
620 }
621
Tom Cherry4362f892017-11-14 08:50:43 -0800622 state->getpwent_idx = get_next_app_id(state->getpwent_idx);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700623
Tom Cherry4362f892017-11-14 08:50:43 -0800624 if (state->getpwent_idx != -1) {
625 return app_id_to_passwd(state->getpwent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700626 }
627
628 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700629 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700630}
631
Mark Salyzynb38347a2016-04-05 09:09:46 -0700632static group* getgrgid_internal(gid_t gid, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700633 if (auto* android_id_info = find_android_id_info(gid); android_id_info != nullptr) {
634 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700635 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700636
Mark Salyzynb38347a2016-04-05 09:09:46 -0700637 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700638 group* grp = oem_id_to_group(gid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700639 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700640 return grp;
641 }
642 return app_id_to_group(gid, state);
643}
644
645group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
646 group_state_t* state = __group_state();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700647 if (state == nullptr) {
648 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700649 }
650 return getgrgid_internal(gid, state);
651}
652
653static group* getgrnam_internal(const char* name, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700654 if (auto* android_id_info = find_android_id_info(name); android_id_info != nullptr) {
655 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700656 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800657
658 if (vendor_group.FindByName(name, state)) {
659 if (is_oem_id(state->group_.gr_gid)) {
660 return &state->group_;
661 }
662 }
663
Mark Salyzynb38347a2016-04-05 09:09:46 -0700664 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700665 group* grp = oem_id_to_group(oem_id_from_name(name), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700666 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700667 return grp;
668 }
669 return app_id_to_group(app_id_from_name(name, true), state);
670}
671
672group* getgrnam(const char* name) { // NOLINT: implementing bad function.
673 group_state_t* state = __group_state();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700674 if (state == nullptr) {
675 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700676 }
677 return getgrnam_internal(name, state);
678}
679
680static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
681 size_t buflen, struct group** result) {
682 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700683 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700684 char* p = reinterpret_cast<char*>(
Dan Alberta613d0d2017-10-05 16:39:33 -0700685 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
Mark Salyzynb38347a2016-04-05 09:09:46 -0700686 if (p + sizeof(group_state_t) > buf + buflen) {
687 return ERANGE;
688 }
689 group_state_t* state = reinterpret_cast<group_state_t*>(p);
690 init_group_state(state);
691 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700692 if (retval != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700693 *grp = *retval;
694 *result = grp;
695 return 0;
696 }
697 return errno;
698}
699
700int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700701 return getgroup_r(false, nullptr, gid, grp, buf, buflen, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700702}
703
704int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
705 struct group **result) {
706 return getgroup_r(true, name, 0, grp, buf, buflen, result);
707}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700708
709void setgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800710 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700711 if (state) {
712 state->getgrent_idx = 0;
713 }
714}
715
716void endgrent() {
717 setgrent();
718}
719
720group* getgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800721 group_state_t* state = get_group_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700722 if (state == nullptr) {
723 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700724 }
725 if (state->getgrent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700726 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700727 }
728
729 size_t start = 0;
730 ssize_t end = android_id_count;
731 if (state->getgrent_idx < end) {
732 init_group_state(state);
733 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
734 }
735
736 start = end;
737 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
738
739 if (state->getgrent_idx < end) {
740 init_group_state(state);
741 return oem_id_to_group(
742 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
743 }
744
745 start = end;
746 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
747
748 if (state->getgrent_idx < end) {
749 init_group_state(state);
750 return oem_id_to_group(
751 state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state);
752 }
753
754 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700755 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
Mark Salyzyn722ab052016-04-06 10:35:48 -0700756
Tom Cherry4362f892017-11-14 08:50:43 -0800757 state->getgrent_idx = get_next_app_id(state->getgrent_idx);
758
759 if (state->getgrent_idx != -1) {
760 return app_id_to_group(state->getgrent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700761 }
762
763 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700764 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700765}