blob: 136a098c45c3cd90b00ffcab3c694de8161ea63a [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
166static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) {
167 for (size_t n = 0; n < android_id_count; ++n) {
168 if (android_ids[n].aid == id) {
169 return android_iinfo_to_passwd(state, android_ids + n);
170 }
171 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700172 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700173}
174
175static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) {
176 for (size_t n = 0; n < android_id_count; ++n) {
177 if (!strcmp(android_ids[n].name, name)) {
178 return android_iinfo_to_passwd(state, android_ids + n);
179 }
180 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700181 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700182}
183
184static group* android_id_to_group(group_state_t* state, unsigned id) {
185 for (size_t n = 0; n < android_id_count; ++n) {
186 if (android_ids[n].aid == id) {
187 return android_iinfo_to_group(state, android_ids + n);
188 }
189 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700190 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700191}
192
193static group* android_name_to_group(group_state_t* state, const char* name) {
194 for (size_t n = 0; n < android_id_count; ++n) {
195 if (!strcmp(android_ids[n].name, name)) {
196 return android_iinfo_to_group(state, android_ids + n);
197 }
198 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700200}
201
Tom Cherry4362f892017-11-14 08:50:43 -0800202// These are a list of the reserved app ranges, and should never contain anything below
203// AID_APP_START. They exist per user, so a given uid/gid modulo AID_USER_OFFSET will map
204// to these ranges.
205static constexpr struct {
206 uid_t start;
207 uid_t end;
208} user_ranges[] = {
209 { AID_APP_START, AID_APP_END },
210 { AID_CACHE_GID_START, AID_CACHE_GID_END },
211 { AID_EXT_GID_START, AID_EXT_GID_END },
212 { AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END },
213 { AID_SHARED_GID_START, AID_SHARED_GID_END },
214 { AID_ISOLATED_START, AID_ISOLATED_END },
215};
216
217static constexpr bool verify_user_ranges_ascending() {
218 auto array_size = arraysize(user_ranges);
219 if (array_size < 2) return false;
220
221 if (user_ranges[0].start > user_ranges[0].end) return false;
222
223 for (size_t i = 1; i < array_size; ++i) {
224 if (user_ranges[i].start > user_ranges[i].end) return false;
225 if (user_ranges[i - 1].end > user_ranges[i].start) return false;
226 }
227 return true;
228}
229
230static_assert(verify_user_ranges_ascending(), "user_ranges must have ascending ranges");
231
232static bool is_valid_app_id(id_t id) {
233 id_t appid = id % AID_USER_OFFSET;
234
235 // AID_OVERFLOWUID is never a valid app id, so we explicitly return false to ensure this.
236 // This is true across all users, as there is no reason to ever map this id into any user range.
237 if (appid == AID_OVERFLOWUID) {
238 return false;
239 }
240
241 // If we've resolved to something before app_start, we have already checked against
242 // android_ids, so no need to check again.
243 if (appid < user_ranges[0].start) {
244 return true;
245 }
246
247 // Otherwise check that the appid is in one of the reserved ranges.
248 for (size_t i = 0; i < arraysize(user_ranges); ++i) {
249 if (appid >= user_ranges[i].start && appid <= user_ranges[i].end) {
250 return true;
251 }
252 }
253
254 return false;
255}
256
257// This provides an iterater for app_ids within the first user's app id's.
258static uid_t get_next_app_id(uid_t current_id) {
259 // If current_id is below the first of the user_ranges, then we're uninitialized, and return the
260 // first valid id.
261 if (current_id < user_ranges[0].start) {
262 return user_ranges[0].start;
263 }
264
265 uid_t incremented_id = current_id + 1;
266
267 // Check to see if our incremented_id is between two ranges, and if so, return the beginning of
268 // the next valid range.
269 for (size_t i = 1; i < arraysize(user_ranges); ++i) {
270 if (incremented_id > user_ranges[i - 1].end && incremented_id < user_ranges[i].start) {
271 return user_ranges[i].start;
272 }
273 }
274
275 // Check to see if our incremented_id is above final range, and return -1 to indicate that we've
276 // completed if so.
277 if (incremented_id > user_ranges[arraysize(user_ranges) - 1].end) {
278 return -1;
279 }
280
281 // Otherwise the incremented_id is valid, so return it.
282 return incremented_id;
283}
284
Mark Salyzynb38347a2016-04-05 09:09:46 -0700285// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700286// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
287// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
288// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
289// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
290// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700291// returns 0 and sets errno to ENOENT in case of error.
292static id_t app_id_from_name(const char* name, bool is_group) {
293 char* end;
294 unsigned long userid;
295 bool is_shared_gid = false;
296
297 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
298 end = const_cast<char*>(name+3);
299 userid = 0;
300 is_shared_gid = true;
301 } else if (name[0] == 'u' && isdigit(name[1])) {
302 userid = strtoul(name+1, &end, 10);
303 } else {
304 errno = ENOENT;
305 return 0;
306 }
307
308 if (end[0] != '_' || end[1] == 0) {
309 errno = ENOENT;
310 return 0;
311 }
312
313 unsigned long appid = 0;
314 if (end[1] == 'a' && isdigit(end[2])) {
315 if (is_shared_gid) {
316 // end will point to \0 if the strtoul below succeeds.
317 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
318 if (appid > AID_SHARED_GID_END) {
319 errno = ENOENT;
320 return 0;
321 }
322 } else {
323 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700324 appid = strtoul(end+2, &end, 10);
325 if (is_group && !strcmp(end, "_cache")) {
326 end += 6;
327 appid += AID_CACHE_GID_START;
328 } else {
329 appid += AID_APP_START;
330 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700331 }
332 } else if (end[1] == 'i' && isdigit(end[2])) {
333 // end will point to \0 if the strtoul below succeeds.
334 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
335 } else {
336 for (size_t n = 0; n < android_id_count; n++) {
337 if (!strcmp(android_ids[n].name, end + 1)) {
338 appid = android_ids[n].aid;
339 // Move the end pointer to the null terminator.
340 end += strlen(android_ids[n].name) + 1;
341 break;
342 }
343 }
344 }
345
346 // Check that the entire string was consumed by one of the 3 cases above.
347 if (end[0] != 0) {
348 errno = ENOENT;
349 return 0;
350 }
351
352 // Check that user id won't overflow.
353 if (userid > 1000) {
354 errno = ENOENT;
355 return 0;
356 }
357
358 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700359 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700360 errno = ENOENT;
361 return 0;
362 }
363
Jeff Sharkey934bc862016-12-13 14:03:19 -0700364 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700365}
366
367static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700368 const uid_t appid = uid % AID_USER_OFFSET;
369 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700370 if (appid >= AID_ISOLATED_START) {
371 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700372 } else if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700373 for (size_t n = 0; n < android_id_count; n++) {
374 if (android_ids[n].aid == appid) {
375 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
376 return;
377 }
378 }
379 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700380 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700381 }
382}
383
384static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700385 const uid_t appid = gid % AID_USER_OFFSET;
386 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700387 if (appid >= AID_ISOLATED_START) {
388 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
389 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
390 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700391 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
392 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
393 } else if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700394 for (size_t n = 0; n < android_id_count; n++) {
395 if (android_ids[n].aid == appid) {
396 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
397 return;
398 }
399 }
400 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700401 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700402 }
403}
404
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700405// oem_XXXX -> uid
406// Supported ranges:
407// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
408// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
409// Check OEM id is within range.
410static bool is_oem_id(id_t id) {
411 return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) ||
412 ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END)));
413}
414
Mark Salyzynb38347a2016-04-05 09:09:46 -0700415// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700416static id_t oem_id_from_name(const char* name) {
417 unsigned int id;
418 if (sscanf(name, "oem_%u", &id) != 1) {
419 return 0;
420 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700421 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700422 return 0;
423 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700424 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700425}
426
427static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700428 if (!is_oem_id(uid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800429 return nullptr;
430 }
431
432 if (vendor_passwd.FindById(uid, state)) {
433 return &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700434 }
435
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700436 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700437 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
438 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
439
440 passwd* pw = &state->passwd_;
441 pw->pw_name = state->name_buffer_;
442 pw->pw_dir = state->dir_buffer_;
443 pw->pw_shell = state->sh_buffer_;
444 pw->pw_uid = uid;
445 pw->pw_gid = uid;
446 return pw;
447}
448
449static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700450 if (!is_oem_id(gid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800451 return nullptr;
452 }
453
454 if (vendor_group.FindById(gid, state)) {
455 return &state->group_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700456 }
457
458 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700459 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700460
461 group* gr = &state->group_;
462 gr->gr_name = state->group_name_buffer_;
463 gr->gr_gid = gid;
464 gr->gr_mem[0] = gr->gr_name;
465 return gr;
466}
467
468// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700469// 0 to AID_APP_START-1 -> "system", "radio", etc.
470// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
471// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
472// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700473// returns a passwd structure (sets errno to ENOENT on failure).
474static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800475 if (uid < AID_APP_START || !is_valid_app_id(uid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700476 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700477 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700478 }
479
480 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
481
Jeff Sharkey934bc862016-12-13 14:03:19 -0700482 const uid_t appid = uid % AID_USER_OFFSET;
483 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700484 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
485 } else {
486 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
487 }
488
489 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
490
491 passwd* pw = &state->passwd_;
492 pw->pw_name = state->name_buffer_;
493 pw->pw_dir = state->dir_buffer_;
494 pw->pw_shell = state->sh_buffer_;
495 pw->pw_uid = uid;
496 pw->pw_gid = uid;
497 return pw;
498}
499
500// Translate a gid into the corresponding app_<gid>
501// group structure (sets errno to ENOENT on failure).
502static group* app_id_to_group(gid_t gid, group_state_t* state) {
Tom Cherry4362f892017-11-14 08:50:43 -0800503 if (gid < AID_APP_START || !is_valid_app_id(gid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700504 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700505 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700506 }
507
508 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
509
510 group* gr = &state->group_;
511 gr->gr_name = state->group_name_buffer_;
512 gr->gr_gid = gid;
513 gr->gr_mem[0] = gr->gr_name;
514 return gr;
515}
516
517passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800518 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700519 if (state == nullptr) {
520 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700521 }
522
523 passwd* pw = android_id_to_passwd(state, uid);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700524 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700525 return pw;
526 }
527 // Handle OEM range.
528 pw = oem_id_to_passwd(uid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700529 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700530 return pw;
531 }
532 return app_id_to_passwd(uid, state);
533}
534
535passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800536 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700537 if (state == nullptr) {
538 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700539 }
540
541 passwd* pw = android_name_to_passwd(state, login);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700542 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700543 return pw;
544 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800545
546 if (vendor_passwd.FindByName(login, state)) {
547 if (is_oem_id(state->passwd_.pw_uid)) {
548 return &state->passwd_;
549 }
550 }
551
Mark Salyzynb38347a2016-04-05 09:09:46 -0700552 // Handle OEM range.
553 pw = oem_id_to_passwd(oem_id_from_name(login), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700554 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700555 return pw;
556 }
557 return app_id_to_passwd(app_id_from_name(login, false), state);
558}
559
560// All users are in just one group, the one passed in.
561int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
562 if (*ngroups < 1) {
563 *ngroups = 1;
564 return -1;
565 }
566 groups[0] = group;
567 return (*ngroups = 1);
568}
569
570char* getlogin() { // NOLINT: implementing bad function.
571 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
Elliott Hughes06bd5862017-07-28 16:27:49 -0700572 return pw ? pw->pw_name : nullptr;
573}
574
575int getlogin_r(char* buf, size_t size) {
576 char* login = getlogin();
577 if (login == nullptr) return errno;
578 size_t login_length = strlen(login) + 1;
579 if (login_length > size) return ERANGE;
580 memcpy(buf, login, login_length);
581 return 0;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700582}
583
Mark Salyzyn722ab052016-04-06 10:35:48 -0700584void setpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800585 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700586 if (state) {
587 state->getpwent_idx = 0;
588 }
589}
590
591void endpwent() {
592 setpwent();
593}
594
595passwd* getpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800596 passwd_state_t* state = get_passwd_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700597 if (state == nullptr) {
598 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700599 }
600 if (state->getpwent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700601 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700602 }
603
604 size_t start = 0;
605 ssize_t end = android_id_count;
606 if (state->getpwent_idx < end) {
607 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
608 }
609
610 start = end;
611 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
612
613 if (state->getpwent_idx < end) {
614 return oem_id_to_passwd(
615 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
616 }
617
618 start = end;
619 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
620
621 if (state->getpwent_idx < end) {
622 return oem_id_to_passwd(
623 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
624 }
625
Tom Cherry4362f892017-11-14 08:50:43 -0800626 state->getpwent_idx = get_next_app_id(state->getpwent_idx);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700627
Tom Cherry4362f892017-11-14 08:50:43 -0800628 if (state->getpwent_idx != -1) {
629 return app_id_to_passwd(state->getpwent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700630 }
631
632 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700633 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700634}
635
Mark Salyzynb38347a2016-04-05 09:09:46 -0700636static group* getgrgid_internal(gid_t gid, group_state_t* state) {
637 group* grp = android_id_to_group(state, gid);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700638 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700639 return grp;
640 }
641 // Handle OEM range.
642 grp = oem_id_to_group(gid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700643 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700644 return grp;
645 }
646 return app_id_to_group(gid, state);
647}
648
649group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
650 group_state_t* state = __group_state();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700651 if (state == nullptr) {
652 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700653 }
654 return getgrgid_internal(gid, state);
655}
656
657static group* getgrnam_internal(const char* name, group_state_t* state) {
658 group* grp = android_name_to_group(state, name);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700659 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700660 return grp;
661 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800662
663 if (vendor_group.FindByName(name, state)) {
664 if (is_oem_id(state->group_.gr_gid)) {
665 return &state->group_;
666 }
667 }
668
Mark Salyzynb38347a2016-04-05 09:09:46 -0700669 // Handle OEM range.
670 grp = oem_id_to_group(oem_id_from_name(name), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700671 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700672 return grp;
673 }
674 return app_id_to_group(app_id_from_name(name, true), state);
675}
676
677group* getgrnam(const char* name) { // NOLINT: implementing bad function.
678 group_state_t* state = __group_state();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700679 if (state == nullptr) {
680 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700681 }
682 return getgrnam_internal(name, state);
683}
684
685static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
686 size_t buflen, struct group** result) {
687 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700688 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700689 char* p = reinterpret_cast<char*>(
Dan Alberta613d0d2017-10-05 16:39:33 -0700690 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
Mark Salyzynb38347a2016-04-05 09:09:46 -0700691 if (p + sizeof(group_state_t) > buf + buflen) {
692 return ERANGE;
693 }
694 group_state_t* state = reinterpret_cast<group_state_t*>(p);
695 init_group_state(state);
696 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700697 if (retval != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700698 *grp = *retval;
699 *result = grp;
700 return 0;
701 }
702 return errno;
703}
704
705int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700706 return getgroup_r(false, nullptr, gid, grp, buf, buflen, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700707}
708
709int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
710 struct group **result) {
711 return getgroup_r(true, name, 0, grp, buf, buflen, result);
712}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700713
714void setgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800715 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700716 if (state) {
717 state->getgrent_idx = 0;
718 }
719}
720
721void endgrent() {
722 setgrent();
723}
724
725group* getgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800726 group_state_t* state = get_group_tls_buffer();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700727 if (state == nullptr) {
728 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700729 }
730 if (state->getgrent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700731 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700732 }
733
734 size_t start = 0;
735 ssize_t end = android_id_count;
736 if (state->getgrent_idx < end) {
737 init_group_state(state);
738 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
739 }
740
741 start = end;
742 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
743
744 if (state->getgrent_idx < end) {
745 init_group_state(state);
746 return oem_id_to_group(
747 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
748 }
749
750 start = end;
751 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
752
753 if (state->getgrent_idx < end) {
754 init_group_state(state);
755 return oem_id_to_group(
756 state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state);
757 }
758
759 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700760 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
Mark Salyzyn722ab052016-04-06 10:35:48 -0700761
Tom Cherry4362f892017-11-14 08:50:43 -0800762 state->getgrent_idx = get_next_app_id(state->getgrent_idx);
763
764 if (state->getgrent_idx != -1) {
765 return app_id_to_group(state->getgrent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700766 }
767
768 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700769 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700770}