blob: ca6ea220cded4d2f795864c39735ddb4759c92c5 [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
29#include <ctype.h>
30#include <errno.h>
31#include <grp.h>
32#include <mntent.h>
33#include <pthread.h>
34#include <pwd.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39
40#include "private/android_filesystem_config.h"
41#include "private/bionic_macros.h"
Josh Gao5e2285d2017-02-22 12:19:05 -080042#include "private/grp_pwd.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070043#include "private/ErrnoRestorer.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070044
Elliott Hughes3f6eee92016-12-13 23:47:25 +000045// Generated android_ids array
46#include "generated_android_ids.h"
Tom Cherry6034ef82018-02-02 16:10:07 -080047#include "grp_pwd_file.h"
48
Tom Cherryc2b9fec2018-05-10 13:33:06 -070049static PasswdFile vendor_passwd("/vendor/etc/passwd", "vendor_");
50static GroupFile vendor_group("/vendor/etc/group", "vendor_");
Elliott Hughes3f6eee92016-12-13 23:47:25 +000051
Mark Salyzynb38347a2016-04-05 09:09:46 -070052// POSIX seems to envisage an implementation where the <pwd.h> functions are
53// implemented by brute-force searching with getpwent(3), and the <grp.h>
54// functions are implemented similarly with getgrent(3). This means that it's
55// okay for all the <grp.h> functions to share state, and all the <passwd.h>
56// functions to share state, but <grp.h> functions can't clobber <passwd.h>
57// functions' state and vice versa.
Josh Gao5e2285d2017-02-22 12:19:05 -080058#include "bionic/pthread_internal.h"
59static group_state_t* get_group_tls_buffer() {
60 return &__get_bionic_tls().group;
61}
Mark Salyzynb38347a2016-04-05 09:09:46 -070062
Josh Gao5e2285d2017-02-22 12:19:05 -080063static passwd_state_t* get_passwd_tls_buffer() {
64 return &__get_bionic_tls().passwd;
65}
Mark Salyzynb38347a2016-04-05 09:09:46 -070066
67static void init_group_state(group_state_t* state) {
Mark Salyzyn722ab052016-04-06 10:35:48 -070068 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
Mark Salyzynb38347a2016-04-05 09:09:46 -070069 state->group_.gr_mem = state->group_members_;
70}
71
72static group_state_t* __group_state() {
Josh Gao5e2285d2017-02-22 12:19:05 -080073 group_state_t* result = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -070074 if (result != nullptr) {
75 init_group_state(result);
76 }
77 return result;
78}
79
80static int do_getpw_r(int by_name, const char* name, uid_t uid,
81 passwd* dst, char* buf, size_t byte_count,
82 passwd** result) {
83 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
84 // make might.
85 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -070086 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -070087
88 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
89 // storage, so we can call them as long as we copy everything out
90 // before returning.
91 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
92
93 // POSIX allows failure to find a match to be considered a non-error.
94 // Reporting success (0) but with *result NULL is glibc's behavior.
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 if (src == nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -070096 return (errno == ENOENT) ? 0 : errno;
97 }
98
99 // Work out where our strings will go in 'buf', and whether we've got
100 // enough space.
101 size_t required_byte_count = 0;
102 dst->pw_name = buf;
103 required_byte_count += strlen(src->pw_name) + 1;
104 dst->pw_dir = buf + required_byte_count;
105 required_byte_count += strlen(src->pw_dir) + 1;
106 dst->pw_shell = buf + required_byte_count;
107 required_byte_count += strlen(src->pw_shell) + 1;
108 if (byte_count < required_byte_count) {
109 return ERANGE;
110 }
111
112 // Copy the strings.
113 snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
114
115 // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic.
116 // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700117 dst->pw_passwd = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700118#if defined(__LP64__)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700119 dst->pw_gecos = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700120#endif
121
122 // Copy the integral fields.
123 dst->pw_gid = src->pw_gid;
124 dst->pw_uid = src->pw_uid;
125
126 *result = dst;
127 return 0;
128}
129
130int getpwnam_r(const char* name, passwd* pwd,
131 char* buf, size_t byte_count, passwd** result) {
132 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
133}
134
135int getpwuid_r(uid_t uid, passwd* pwd,
136 char* buf, size_t byte_count, passwd** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700137 return do_getpw_r(0, nullptr, uid, pwd, buf, byte_count, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700138}
139
140static passwd* android_iinfo_to_passwd(passwd_state_t* state,
141 const android_id_info* iinfo) {
142 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
143 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
144 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
145
146 passwd* pw = &state->passwd_;
147 pw->pw_name = state->name_buffer_;
148 pw->pw_uid = iinfo->aid;
149 pw->pw_gid = iinfo->aid;
150 pw->pw_dir = state->dir_buffer_;
151 pw->pw_shell = state->sh_buffer_;
152 return pw;
153}
154
155static group* android_iinfo_to_group(group_state_t* state,
156 const android_id_info* iinfo) {
157 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
158
159 group* gr = &state->group_;
160 gr->gr_name = state->group_name_buffer_;
161 gr->gr_gid = iinfo->aid;
162 gr->gr_mem[0] = gr->gr_name;
163 return gr;
164}
165
Tom Cherry5fb07632019-04-24 13:35:39 -0700166static const android_id_info* find_android_id_info(unsigned id) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700167 for (size_t n = 0; n < android_id_count; ++n) {
168 if (android_ids[n].aid == id) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700169 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700170 }
171 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700172 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700173}
174
Tom Cherry5fb07632019-04-24 13:35:39 -0700175static const android_id_info* find_android_id_info(const char* name) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700176 for (size_t n = 0; n < android_id_count; ++n) {
177 if (!strcmp(android_ids[n].name, name)) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700178 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700179 }
180 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700181 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700182}
183
Tom Cherry4362f892017-11-14 08:50:43 -0800184// These are a list of the reserved app ranges, and should never contain anything below
185// AID_APP_START. They exist per user, so a given uid/gid modulo AID_USER_OFFSET will map
186// to these ranges.
187static constexpr struct {
188 uid_t start;
189 uid_t end;
190} user_ranges[] = {
191 { AID_APP_START, AID_APP_END },
192 { AID_CACHE_GID_START, AID_CACHE_GID_END },
193 { AID_EXT_GID_START, AID_EXT_GID_END },
194 { AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END },
195 { AID_SHARED_GID_START, AID_SHARED_GID_END },
196 { AID_ISOLATED_START, AID_ISOLATED_END },
197};
198
199static constexpr bool verify_user_ranges_ascending() {
200 auto array_size = arraysize(user_ranges);
201 if (array_size < 2) return false;
202
203 if (user_ranges[0].start > user_ranges[0].end) return false;
204
205 for (size_t i = 1; i < array_size; ++i) {
206 if (user_ranges[i].start > user_ranges[i].end) return false;
207 if (user_ranges[i - 1].end > user_ranges[i].start) return false;
208 }
209 return true;
210}
211
212static_assert(verify_user_ranges_ascending(), "user_ranges must have ascending ranges");
213
214static bool is_valid_app_id(id_t id) {
215 id_t appid = id % AID_USER_OFFSET;
216
217 // AID_OVERFLOWUID is never a valid app id, so we explicitly return false to ensure this.
218 // This is true across all users, as there is no reason to ever map this id into any user range.
219 if (appid == AID_OVERFLOWUID) {
220 return false;
221 }
222
223 // If we've resolved to something before app_start, we have already checked against
224 // android_ids, so no need to check again.
225 if (appid < user_ranges[0].start) {
226 return true;
227 }
228
229 // Otherwise check that the appid is in one of the reserved ranges.
230 for (size_t i = 0; i < arraysize(user_ranges); ++i) {
231 if (appid >= user_ranges[i].start && appid <= user_ranges[i].end) {
232 return true;
233 }
234 }
235
236 return false;
237}
238
239// This provides an iterater for app_ids within the first user's app id's.
240static uid_t get_next_app_id(uid_t current_id) {
241 // If current_id is below the first of the user_ranges, then we're uninitialized, and return the
242 // first valid id.
243 if (current_id < user_ranges[0].start) {
244 return user_ranges[0].start;
245 }
246
247 uid_t incremented_id = current_id + 1;
248
249 // Check to see if our incremented_id is between two ranges, and if so, return the beginning of
250 // the next valid range.
251 for (size_t i = 1; i < arraysize(user_ranges); ++i) {
252 if (incremented_id > user_ranges[i - 1].end && incremented_id < user_ranges[i].start) {
253 return user_ranges[i].start;
254 }
255 }
256
257 // Check to see if our incremented_id is above final range, and return -1 to indicate that we've
258 // completed if so.
259 if (incremented_id > user_ranges[arraysize(user_ranges) - 1].end) {
260 return -1;
261 }
262
263 // Otherwise the incremented_id is valid, so return it.
264 return incremented_id;
265}
266
Mark Salyzynb38347a2016-04-05 09:09:46 -0700267// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700268// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
269// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
270// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
271// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
272// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700273// returns 0 and sets errno to ENOENT in case of error.
274static id_t app_id_from_name(const char* name, bool is_group) {
275 char* end;
276 unsigned long userid;
277 bool is_shared_gid = false;
278
279 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
280 end = const_cast<char*>(name+3);
281 userid = 0;
282 is_shared_gid = true;
283 } else if (name[0] == 'u' && isdigit(name[1])) {
284 userid = strtoul(name+1, &end, 10);
285 } else {
286 errno = ENOENT;
287 return 0;
288 }
289
290 if (end[0] != '_' || end[1] == 0) {
291 errno = ENOENT;
292 return 0;
293 }
294
295 unsigned long appid = 0;
296 if (end[1] == 'a' && isdigit(end[2])) {
297 if (is_shared_gid) {
298 // end will point to \0 if the strtoul below succeeds.
299 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
300 if (appid > AID_SHARED_GID_END) {
301 errno = ENOENT;
302 return 0;
303 }
304 } else {
305 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700306 appid = strtoul(end+2, &end, 10);
307 if (is_group && !strcmp(end, "_cache")) {
308 end += 6;
309 appid += AID_CACHE_GID_START;
310 } else {
311 appid += AID_APP_START;
312 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700313 }
314 } else if (end[1] == 'i' && isdigit(end[2])) {
315 // end will point to \0 if the strtoul below succeeds.
316 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
Tom Cherry5fb07632019-04-24 13:35:39 -0700317 } else if (auto* android_id_info = find_android_id_info(end + 1); android_id_info != nullptr) {
318 appid = android_id_info->aid;
319 end += strlen(android_id_info->name) + 1;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700320 }
321
322 // Check that the entire string was consumed by one of the 3 cases above.
323 if (end[0] != 0) {
324 errno = ENOENT;
325 return 0;
326 }
327
328 // Check that user id won't overflow.
329 if (userid > 1000) {
330 errno = ENOENT;
331 return 0;
332 }
333
334 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700335 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700336 errno = ENOENT;
337 return 0;
338 }
339
Jeff Sharkey934bc862016-12-13 14:03:19 -0700340 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700341}
342
343static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700344 const uid_t appid = uid % AID_USER_OFFSET;
345 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700346 if (appid >= AID_ISOLATED_START) {
347 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700348 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700349 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
350 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700351 }
352 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700353 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700354 }
355}
356
357static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700358 const uid_t appid = gid % AID_USER_OFFSET;
359 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700360 if (appid >= AID_ISOLATED_START) {
361 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
362 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
363 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700364 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
365 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
366 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700367 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
368 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700369 }
370 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700371 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700372 }
373}
374
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700375// oem_XXXX -> uid
376// Supported ranges:
377// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
378// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
379// Check OEM id is within range.
380static bool is_oem_id(id_t id) {
381 return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) ||
382 ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END)));
383}
384
Mark Salyzynb38347a2016-04-05 09:09:46 -0700385// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700386static id_t oem_id_from_name(const char* name) {
387 unsigned int id;
388 if (sscanf(name, "oem_%u", &id) != 1) {
389 return 0;
390 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700391 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700392 return 0;
393 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700394 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700395}
396
397static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700398 if (!is_oem_id(uid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800399 return nullptr;
400 }
401
402 if (vendor_passwd.FindById(uid, state)) {
403 return &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700404 }
405
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700406 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700407 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
Tom Cherryfa5f61c2018-09-27 13:19:02 -0700408 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/vendor/bin/sh");
Mark Salyzynb38347a2016-04-05 09:09:46 -0700409
410 passwd* pw = &state->passwd_;
411 pw->pw_name = state->name_buffer_;
412 pw->pw_dir = state->dir_buffer_;
413 pw->pw_shell = state->sh_buffer_;
414 pw->pw_uid = uid;
415 pw->pw_gid = uid;
416 return pw;
417}
418
419static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700420 if (!is_oem_id(gid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800421 return nullptr;
422 }
423
424 if (vendor_group.FindById(gid, state)) {
425 return &state->group_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700426 }
427
428 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700429 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700430
431 group* gr = &state->group_;
432 gr->gr_name = state->group_name_buffer_;
433 gr->gr_gid = gid;
434 gr->gr_mem[0] = gr->gr_name;
435 return gr;
436}
437
438// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700439// 0 to AID_APP_START-1 -> "system", "radio", etc.
440// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
441// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
442// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700443// returns a passwd structure (sets errno to ENOENT on failure).
444static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800445 if (uid < AID_APP_START || !is_valid_app_id(uid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700446 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700447 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700448 }
449
450 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
451
Jeff Sharkey934bc862016-12-13 14:03:19 -0700452 const uid_t appid = uid % AID_USER_OFFSET;
453 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700454 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
455 } else {
456 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
457 }
458
459 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
460
461 passwd* pw = &state->passwd_;
462 pw->pw_name = state->name_buffer_;
463 pw->pw_dir = state->dir_buffer_;
464 pw->pw_shell = state->sh_buffer_;
465 pw->pw_uid = uid;
466 pw->pw_gid = uid;
467 return pw;
468}
469
470// Translate a gid into the corresponding app_<gid>
471// group structure (sets errno to ENOENT on failure).
472static group* app_id_to_group(gid_t gid, group_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800473 if (gid < AID_APP_START || !is_valid_app_id(gid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700474 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700475 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700476 }
477
478 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
479
480 group* gr = &state->group_;
481 gr->gr_name = state->group_name_buffer_;
482 gr->gr_gid = gid;
483 gr->gr_mem[0] = gr->gr_name;
484 return gr;
485}
486
487passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800488 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700489 if (state == nullptr) {
490 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700491 }
492
Tom Cherry5fb07632019-04-24 13:35:39 -0700493 if (auto* android_id_info = find_android_id_info(uid); android_id_info != nullptr) {
494 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700495 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700496
Mark Salyzynb38347a2016-04-05 09:09:46 -0700497 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700498 passwd* pw = oem_id_to_passwd(uid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700499 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700500 return pw;
501 }
502 return app_id_to_passwd(uid, state);
503}
504
505passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800506 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700507 if (state == nullptr) {
508 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700509 }
510
Tom Cherry5fb07632019-04-24 13:35:39 -0700511 if (auto* android_id_info = find_android_id_info(login); android_id_info != nullptr) {
512 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700513 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800514
515 if (vendor_passwd.FindByName(login, state)) {
516 if (is_oem_id(state->passwd_.pw_uid)) {
517 return &state->passwd_;
518 }
519 }
520
Mark Salyzynb38347a2016-04-05 09:09:46 -0700521 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700522 passwd* pw = oem_id_to_passwd(oem_id_from_name(login), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700523 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700524 return pw;
525 }
526 return app_id_to_passwd(app_id_from_name(login, false), state);
527}
528
529// All users are in just one group, the one passed in.
530int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
531 if (*ngroups < 1) {
532 *ngroups = 1;
533 return -1;
534 }
535 groups[0] = group;
536 return (*ngroups = 1);
537}
538
539char* getlogin() { // NOLINT: implementing bad function.
540 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
Elliott Hughes06bd5862017-07-28 16:27:49 -0700541 return pw ? pw->pw_name : nullptr;
542}
543
544int getlogin_r(char* buf, size_t size) {
545 char* login = getlogin();
546 if (login == nullptr) return errno;
547 size_t login_length = strlen(login) + 1;
548 if (login_length > size) return ERANGE;
549 memcpy(buf, login, login_length);
550 return 0;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700551}
552
Mark Salyzyn722ab052016-04-06 10:35:48 -0700553void setpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800554 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700555 if (state) {
556 state->getpwent_idx = 0;
557 }
558}
559
560void endpwent() {
561 setpwent();
562}
563
564passwd* getpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800565 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700566 if (state == nullptr) {
567 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700568 }
569 if (state->getpwent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700570 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700571 }
572
573 size_t start = 0;
574 ssize_t end = android_id_count;
575 if (state->getpwent_idx < end) {
576 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
577 }
578
579 start = end;
580 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
581
582 if (state->getpwent_idx < end) {
583 return oem_id_to_passwd(
584 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
585 }
586
587 start = end;
588 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
589
590 if (state->getpwent_idx < end) {
591 return oem_id_to_passwd(
592 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
593 }
594
Tom Cherry4362f892017-11-14 08:50:43 -0800595 state->getpwent_idx = get_next_app_id(state->getpwent_idx);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700596
Tom Cherry4362f892017-11-14 08:50:43 -0800597 if (state->getpwent_idx != -1) {
598 return app_id_to_passwd(state->getpwent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700599 }
600
601 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700602 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700603}
604
Mark Salyzynb38347a2016-04-05 09:09:46 -0700605static group* getgrgid_internal(gid_t gid, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700606 if (auto* android_id_info = find_android_id_info(gid); android_id_info != nullptr) {
607 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700608 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700609
Mark Salyzynb38347a2016-04-05 09:09:46 -0700610 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700611 group* grp = oem_id_to_group(gid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700612 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700613 return grp;
614 }
615 return app_id_to_group(gid, state);
616}
617
618group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
619 group_state_t* state = __group_state();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700620 if (state == nullptr) {
621 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700622 }
623 return getgrgid_internal(gid, state);
624}
625
626static group* getgrnam_internal(const char* name, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700627 if (auto* android_id_info = find_android_id_info(name); android_id_info != nullptr) {
628 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700629 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800630
631 if (vendor_group.FindByName(name, state)) {
632 if (is_oem_id(state->group_.gr_gid)) {
633 return &state->group_;
634 }
635 }
636
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(oem_id_from_name(name), 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(app_id_from_name(name, true), state);
643}
644
645group* getgrnam(const char* name) { // 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 getgrnam_internal(name, state);
651}
652
653static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
654 size_t buflen, struct group** result) {
655 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700656 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700657 char* p = reinterpret_cast<char*>(
Dan Alberta613d0d2017-10-05 16:39:33 -0700658 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
Mark Salyzynb38347a2016-04-05 09:09:46 -0700659 if (p + sizeof(group_state_t) > buf + buflen) {
660 return ERANGE;
661 }
662 group_state_t* state = reinterpret_cast<group_state_t*>(p);
663 init_group_state(state);
664 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700665 if (retval != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700666 *grp = *retval;
667 *result = grp;
668 return 0;
669 }
670 return errno;
671}
672
673int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700674 return getgroup_r(false, nullptr, gid, grp, buf, buflen, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700675}
676
677int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
678 struct group **result) {
679 return getgroup_r(true, name, 0, grp, buf, buflen, result);
680}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700681
682void setgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800683 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700684 if (state) {
685 state->getgrent_idx = 0;
686 }
687}
688
689void endgrent() {
690 setgrent();
691}
692
693group* getgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800694 group_state_t* state = get_group_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700695 if (state == nullptr) {
696 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700697 }
698 if (state->getgrent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700699 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700700 }
701
702 size_t start = 0;
703 ssize_t end = android_id_count;
704 if (state->getgrent_idx < end) {
705 init_group_state(state);
706 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
707 }
708
709 start = end;
710 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
711
712 if (state->getgrent_idx < end) {
713 init_group_state(state);
714 return oem_id_to_group(
715 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
716 }
717
718 start = end;
719 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
720
721 if (state->getgrent_idx < end) {
722 init_group_state(state);
723 return oem_id_to_group(
724 state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state);
725 }
726
727 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700728 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
Mark Salyzyn722ab052016-04-06 10:35:48 -0700729
Tom Cherry4362f892017-11-14 08:50:43 -0800730 state->getgrent_idx = get_next_app_id(state->getgrent_idx);
731
732 if (state->getgrent_idx != -1) {
733 return app_id_to_group(state->getgrent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700734 }
735
736 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700737 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700738}