blob: d37fadd38b9b3bd2f77c4f32a3112bb0a9d16228 [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 Cherry6b116d12019-04-25 10:34:07 -070029#include "private/grp_pwd.h"
30
Tom Cherryb368a0b2019-04-24 11:12:59 -070031#include <android/api-level.h>
Mark Salyzynb38347a2016-04-05 09:09:46 -070032#include <ctype.h>
33#include <errno.h>
34#include <grp.h>
35#include <mntent.h>
36#include <pthread.h>
37#include <pwd.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
Tom Cherryb368a0b2019-04-24 11:12:59 -070041#include <sys/system_properties.h>
Tom Cherry6b116d12019-04-25 10:34:07 -070042#include <sys/types.h>
Mark Salyzynb38347a2016-04-05 09:09:46 -070043#include <unistd.h>
44
Tom Cherry6b116d12019-04-25 10:34:07 -070045#include "private/ErrnoRestorer.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070046#include "private/android_filesystem_config.h"
47#include "private/bionic_macros.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070048
Elliott Hughes3f6eee92016-12-13 23:47:25 +000049// Generated android_ids array
50#include "generated_android_ids.h"
Tom Cherry6034ef82018-02-02 16:10:07 -080051#include "grp_pwd_file.h"
52
Tom Cherryc2b9fec2018-05-10 13:33:06 -070053static PasswdFile vendor_passwd("/vendor/etc/passwd", "vendor_");
54static GroupFile vendor_group("/vendor/etc/group", "vendor_");
Elliott Hughes3f6eee92016-12-13 23:47:25 +000055
Mark Salyzynb38347a2016-04-05 09:09:46 -070056// POSIX seems to envisage an implementation where the <pwd.h> functions are
57// implemented by brute-force searching with getpwent(3), and the <grp.h>
58// functions are implemented similarly with getgrent(3). This means that it's
59// okay for all the <grp.h> functions to share state, and all the <passwd.h>
60// functions to share state, but <grp.h> functions can't clobber <passwd.h>
61// functions' state and vice versa.
Josh Gao5e2285d2017-02-22 12:19:05 -080062#include "bionic/pthread_internal.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070063
64static void init_group_state(group_state_t* state) {
Mark Salyzyn722ab052016-04-06 10:35:48 -070065 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
Tom Cherryc57c5bd2019-05-14 17:02:28 -070066 state->group_.gr_name = state->group_name_buffer_;
Mark Salyzynb38347a2016-04-05 09:09:46 -070067 state->group_.gr_mem = state->group_members_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -070068 state->group_.gr_mem[0] = state->group_.gr_name;
Mark Salyzynb38347a2016-04-05 09:09:46 -070069}
70
Tom Cherryc57c5bd2019-05-14 17:02:28 -070071static group_state_t* get_group_tls_buffer() {
72 auto result = &__get_bionic_tls().group;
73 init_group_state(result);
Mark Salyzynb38347a2016-04-05 09:09:46 -070074 return result;
75}
76
Tom Cherryc57c5bd2019-05-14 17:02:28 -070077static void init_passwd_state(passwd_state_t* state) {
78 memset(state, 0, sizeof(passwd_state_t) - sizeof(state->getpwent_idx));
79 state->passwd_.pw_name = state->name_buffer_;
80 state->passwd_.pw_dir = state->dir_buffer_;
81 state->passwd_.pw_shell = state->sh_buffer_;
Mark Salyzynb38347a2016-04-05 09:09:46 -070082}
83
Tom Cherryc57c5bd2019-05-14 17:02:28 -070084static passwd_state_t* get_passwd_tls_buffer() {
85 auto result = &__get_bionic_tls().passwd;
86 init_passwd_state(result);
87 return result;
Mark Salyzynb38347a2016-04-05 09:09:46 -070088}
89
90static passwd* android_iinfo_to_passwd(passwd_state_t* state,
91 const android_id_info* iinfo) {
92 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
93 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
94 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
95
96 passwd* pw = &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -070097 pw->pw_uid = iinfo->aid;
98 pw->pw_gid = iinfo->aid;
Mark Salyzynb38347a2016-04-05 09:09:46 -070099 return pw;
100}
101
102static group* android_iinfo_to_group(group_state_t* state,
103 const android_id_info* iinfo) {
104 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
105
106 group* gr = &state->group_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700107 gr->gr_gid = iinfo->aid;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700108 return gr;
109}
110
Tom Cherry5fb07632019-04-24 13:35:39 -0700111static const android_id_info* find_android_id_info(unsigned id) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700112 for (size_t n = 0; n < android_id_count; ++n) {
113 if (android_ids[n].aid == id) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700114 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700115 }
116 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700117 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700118}
119
Tom Cherry5fb07632019-04-24 13:35:39 -0700120static const android_id_info* find_android_id_info(const char* name) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700121 for (size_t n = 0; n < android_id_count; ++n) {
122 if (!strcmp(android_ids[n].name, name)) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700123 return &android_ids[n];
Mark Salyzynb38347a2016-04-05 09:09:46 -0700124 }
125 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700126 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700127}
128
Tom Cherry4362f892017-11-14 08:50:43 -0800129// These are a list of the reserved app ranges, and should never contain anything below
130// AID_APP_START. They exist per user, so a given uid/gid modulo AID_USER_OFFSET will map
131// to these ranges.
Tom Cherry6b116d12019-04-25 10:34:07 -0700132struct IdRange {
133 id_t start;
134 id_t end;
135};
136
137static constexpr IdRange user_ranges[] = {
138 { AID_APP_START, AID_APP_END },
139 { AID_ISOLATED_START, AID_ISOLATED_END },
140};
141
142static constexpr IdRange group_ranges[] = {
Tom Cherry4362f892017-11-14 08:50:43 -0800143 { AID_APP_START, AID_APP_END },
144 { AID_CACHE_GID_START, AID_CACHE_GID_END },
145 { AID_EXT_GID_START, AID_EXT_GID_END },
146 { AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END },
147 { AID_SHARED_GID_START, AID_SHARED_GID_END },
148 { AID_ISOLATED_START, AID_ISOLATED_END },
149};
150
Tom Cherry6b116d12019-04-25 10:34:07 -0700151template <class T, size_t N>
152static constexpr bool verify_user_ranges_ascending(T (&ranges)[N]) {
153 auto array_size = N;
Tom Cherry4362f892017-11-14 08:50:43 -0800154 if (array_size < 2) return false;
155
Tom Cherry6b116d12019-04-25 10:34:07 -0700156 if (ranges[0].start > ranges[0].end) return false;
Tom Cherry4362f892017-11-14 08:50:43 -0800157
158 for (size_t i = 1; i < array_size; ++i) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700159 if (ranges[i].start > ranges[i].end) return false;
160 if (ranges[i - 1].end > ranges[i].start) return false;
Tom Cherry4362f892017-11-14 08:50:43 -0800161 }
162 return true;
163}
164
Tom Cherry6b116d12019-04-25 10:34:07 -0700165static_assert(verify_user_ranges_ascending(user_ranges), "user_ranges must have ascending ranges");
166static_assert(verify_user_ranges_ascending(group_ranges), "user_ranges must have ascending ranges");
Tom Cherry4362f892017-11-14 08:50:43 -0800167
Tom Cherry6b116d12019-04-25 10:34:07 -0700168// This list comes from PackageManagerService.java, where platform AIDs are added to list of valid
169// AIDs for packages via addSharedUserLPw().
170static constexpr const id_t secondary_user_platform_ids[] = {
171 AID_SYSTEM, AID_RADIO, AID_LOG, AID_NFC, AID_BLUETOOTH,
172 AID_SHELL, AID_SECURE_ELEMENT, AID_NETWORK_STACK,
173};
174
175static bool platform_id_secondary_user_allowed(id_t id) {
176 for (const auto& allowed_id : secondary_user_platform_ids) {
177 if (allowed_id == id) {
178 return true;
179 }
180 }
181 return false;
182}
183
184static bool is_valid_app_id(id_t id, bool is_group) {
Tom Cherry4362f892017-11-14 08:50:43 -0800185 id_t appid = id % AID_USER_OFFSET;
186
187 // AID_OVERFLOWUID is never a valid app id, so we explicitly return false to ensure this.
188 // This is true across all users, as there is no reason to ever map this id into any user range.
189 if (appid == AID_OVERFLOWUID) {
190 return false;
191 }
192
Tom Cherry6b116d12019-04-25 10:34:07 -0700193 auto ranges_size = is_group ? arraysize(group_ranges) : arraysize(user_ranges);
194 auto ranges = is_group ? group_ranges : user_ranges;
195
196 // If we're checking an appid that resolves below the user range, then it's a platform AID for a
197 // seconary user. We only allow a reduced set of these, so we must check that it is allowed.
198 if (appid < ranges[0].start && platform_id_secondary_user_allowed(appid)) {
Tom Cherry4362f892017-11-14 08:50:43 -0800199 return true;
200 }
201
Tom Cherry6b116d12019-04-25 10:34:07 -0700202 // The shared GID range is only valid for the first user.
203 if (appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END && appid != id) {
204 return false;
205 }
206
Tom Cherry4362f892017-11-14 08:50:43 -0800207 // Otherwise check that the appid is in one of the reserved ranges.
Tom Cherry6b116d12019-04-25 10:34:07 -0700208 for (size_t i = 0; i < ranges_size; ++i) {
209 if (appid >= ranges[i].start && appid <= ranges[i].end) {
Tom Cherry4362f892017-11-14 08:50:43 -0800210 return true;
211 }
212 }
213
214 return false;
215}
216
217// This provides an iterater for app_ids within the first user's app id's.
Tom Cherry6b116d12019-04-25 10:34:07 -0700218static id_t get_next_app_id(id_t current_id, bool is_group) {
219 auto ranges_size = is_group ? arraysize(group_ranges) : arraysize(user_ranges);
220 auto ranges = is_group ? group_ranges : user_ranges;
221
222 // If current_id is below the first of the ranges, then we're uninitialized, and return the first
223 // valid id.
224 if (current_id < ranges[0].start) {
225 return ranges[0].start;
Tom Cherry4362f892017-11-14 08:50:43 -0800226 }
227
Tom Cherry6b116d12019-04-25 10:34:07 -0700228 id_t incremented_id = current_id + 1;
Tom Cherry4362f892017-11-14 08:50:43 -0800229
230 // Check to see if our incremented_id is between two ranges, and if so, return the beginning of
231 // the next valid range.
Tom Cherry6b116d12019-04-25 10:34:07 -0700232 for (size_t i = 1; i < ranges_size; ++i) {
233 if (incremented_id > ranges[i - 1].end && incremented_id < ranges[i].start) {
234 return ranges[i].start;
Tom Cherry4362f892017-11-14 08:50:43 -0800235 }
236 }
237
238 // Check to see if our incremented_id is above final range, and return -1 to indicate that we've
239 // completed if so.
Tom Cherry6b116d12019-04-25 10:34:07 -0700240 if (incremented_id > ranges[ranges_size - 1].end) {
Tom Cherry4362f892017-11-14 08:50:43 -0800241 return -1;
242 }
243
244 // Otherwise the incremented_id is valid, so return it.
245 return incremented_id;
246}
247
Mark Salyzynb38347a2016-04-05 09:09:46 -0700248// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700249// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
Tom Cherry6b116d12019-04-25 10:34:07 -0700250// u0_a1234_ext_cache -> 0 * AID_USER_OFFSET + AID_EXT_CACHE_GID_START + 1234 (group name only)
251// u0_a1234_ext -> 0 * AID_USER_OFFSET + AID_EXT_GID_START + 1234 (group name only)
Jeff Sharkey934bc862016-12-13 14:03:19 -0700252// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
253// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
254// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
255// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700256// returns 0 and sets errno to ENOENT in case of error.
257static id_t app_id_from_name(const char* name, bool is_group) {
258 char* end;
259 unsigned long userid;
260 bool is_shared_gid = false;
261
262 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
263 end = const_cast<char*>(name+3);
264 userid = 0;
265 is_shared_gid = true;
266 } else if (name[0] == 'u' && isdigit(name[1])) {
267 userid = strtoul(name+1, &end, 10);
268 } else {
269 errno = ENOENT;
270 return 0;
271 }
272
273 if (end[0] != '_' || end[1] == 0) {
274 errno = ENOENT;
275 return 0;
276 }
277
278 unsigned long appid = 0;
279 if (end[1] == 'a' && isdigit(end[2])) {
280 if (is_shared_gid) {
281 // end will point to \0 if the strtoul below succeeds.
282 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
283 if (appid > AID_SHARED_GID_END) {
284 errno = ENOENT;
285 return 0;
286 }
287 } else {
288 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700289 appid = strtoul(end+2, &end, 10);
Tom Cherry6b116d12019-04-25 10:34:07 -0700290 if (is_group) {
291 if (!strcmp(end, "_ext_cache")) {
292 end += 10;
293 appid += AID_EXT_CACHE_GID_START;
294 } else if (!strcmp(end, "_ext")) {
295 end += 4;
296 appid += AID_EXT_GID_START;
297 } else if (!strcmp(end, "_cache")) {
298 end += 6;
299 appid += AID_CACHE_GID_START;
300 } else {
301 appid += AID_APP_START;
302 }
Jeff Sharkey934bc862016-12-13 14:03:19 -0700303 } else {
304 appid += AID_APP_START;
305 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700306 }
307 } else if (end[1] == 'i' && isdigit(end[2])) {
308 // end will point to \0 if the strtoul below succeeds.
309 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
Tom Cherry5fb07632019-04-24 13:35:39 -0700310 } else if (auto* android_id_info = find_android_id_info(end + 1); android_id_info != nullptr) {
311 appid = android_id_info->aid;
312 end += strlen(android_id_info->name) + 1;
Tom Cherry6b116d12019-04-25 10:34:07 -0700313 if (!platform_id_secondary_user_allowed(appid)) {
314 errno = ENOENT;
315 return 0;
316 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700317 }
318
319 // Check that the entire string was consumed by one of the 3 cases above.
320 if (end[0] != 0) {
321 errno = ENOENT;
322 return 0;
323 }
324
325 // Check that user id won't overflow.
326 if (userid > 1000) {
327 errno = ENOENT;
328 return 0;
329 }
330
331 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700332 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700333 errno = ENOENT;
334 return 0;
335 }
336
Jeff Sharkey934bc862016-12-13 14:03:19 -0700337 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700338}
339
340static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700341 const uid_t appid = uid % AID_USER_OFFSET;
342 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700343 if (appid >= AID_ISOLATED_START) {
344 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700345 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700346 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
347 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700348 }
349 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700350 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700351 }
352}
353
354static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700355 const uid_t appid = gid % AID_USER_OFFSET;
356 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700357 if (appid >= AID_ISOLATED_START) {
358 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
359 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
360 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Tom Cherry6b116d12019-04-25 10:34:07 -0700361 } else if (appid >= AID_EXT_CACHE_GID_START && appid <= AID_EXT_CACHE_GID_END) {
362 snprintf(buffer, bufferlen, "u%u_a%u_ext_cache", userid, appid - AID_EXT_CACHE_GID_START);
363 } else if (appid >= AID_EXT_GID_START && appid <= AID_EXT_GID_END) {
364 snprintf(buffer, bufferlen, "u%u_a%u_ext", userid, appid - AID_EXT_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700365 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
366 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
367 } else if (appid < AID_APP_START) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700368 if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
369 snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700370 }
371 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700372 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700373 }
374}
375
Tom Cherryb368a0b2019-04-24 11:12:59 -0700376static bool device_launched_before_api_29() {
377 // Check if ro.product.first_api_level is set to a value > 0 and < 29, if so, this device was
378 // launched before API 29 (Q). Any other value is considered to be either in development or
379 // launched after.
380 // Cache the value as __system_property_get() is expensive and this may be called often.
381 static bool result = [] {
382 char value[PROP_VALUE_MAX] = { 0 };
383 if (__system_property_get("ro.product.first_api_level", value) == 0) {
384 return false;
385 }
386 int value_int = atoi(value);
387 return value_int != 0 && value_int < 29;
388 }();
389 return result;
390}
391
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700392// oem_XXXX -> uid
393// Supported ranges:
394// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
395// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
396// Check OEM id is within range.
397static bool is_oem_id(id_t id) {
Tom Cherryb368a0b2019-04-24 11:12:59 -0700398 // Upgrading devices launched before API level 29 may not comply with the below check.
399 // Due to the difficulty in changing uids after launch, it is waived for these devices.
400 // The legacy range:
401 // AID_OEM_RESERVED_START to AID_EVERYBODY (2900-9996), excluding builtin AIDs.
402 if (device_launched_before_api_29() && id >= AID_OEM_RESERVED_START && id < AID_EVERYBODY &&
403 find_android_id_info(id) == nullptr) {
404 return true;
405 }
406
407 return (id >= AID_OEM_RESERVED_START && id <= AID_OEM_RESERVED_END) ||
408 (id >= AID_OEM_RESERVED_2_START && id <= AID_OEM_RESERVED_2_END);
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700409}
410
Mark Salyzynb38347a2016-04-05 09:09:46 -0700411// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700412static id_t oem_id_from_name(const char* name) {
413 unsigned int id;
414 if (sscanf(name, "oem_%u", &id) != 1) {
415 return 0;
416 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700417 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700418 return 0;
419 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700420 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700421}
422
423static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700424 if (!is_oem_id(uid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800425 return nullptr;
426 }
427
428 if (vendor_passwd.FindById(uid, state)) {
429 return &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700430 }
431
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700432 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700433 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
Tom Cherryfa5f61c2018-09-27 13:19:02 -0700434 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/vendor/bin/sh");
Mark Salyzynb38347a2016-04-05 09:09:46 -0700435
436 passwd* pw = &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700437 pw->pw_uid = uid;
438 pw->pw_gid = uid;
439 return pw;
440}
441
442static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700443 if (!is_oem_id(gid)) {
Tom Cherry6034ef82018-02-02 16:10:07 -0800444 return nullptr;
445 }
446
447 if (vendor_group.FindById(gid, state)) {
448 return &state->group_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700449 }
450
451 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700452 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700453
454 group* gr = &state->group_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700455 gr->gr_gid = gid;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700456 return gr;
457}
458
459// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700460// 0 to AID_APP_START-1 -> "system", "radio", etc.
461// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
462// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
463// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700464// returns a passwd structure (sets errno to ENOENT on failure).
465static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700466 if (uid < AID_APP_START || !is_valid_app_id(uid, false)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700467 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700468 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700469 }
470
471 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
472
Jeff Sharkey934bc862016-12-13 14:03:19 -0700473 const uid_t appid = uid % AID_USER_OFFSET;
474 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700475 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
476 } else {
477 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
478 }
479
480 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
481
482 passwd* pw = &state->passwd_;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700483 pw->pw_uid = uid;
484 pw->pw_gid = uid;
485 return pw;
486}
487
488// Translate a gid into the corresponding app_<gid>
489// group structure (sets errno to ENOENT on failure).
490static group* app_id_to_group(gid_t gid, group_state_t* state) {
Tom Cherry6b116d12019-04-25 10:34:07 -0700491 if (gid < AID_APP_START || !is_valid_app_id(gid, true)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700492 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700493 return nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700494 }
495
496 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
497
498 group* gr = &state->group_;
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700499 gr->gr_gid = gid;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700500 return gr;
501}
502
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700503passwd* getpwuid_internal(uid_t uid, passwd_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700504 if (auto* android_id_info = find_android_id_info(uid); android_id_info != nullptr) {
505 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700506 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700507
Mark Salyzynb38347a2016-04-05 09:09:46 -0700508 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700509 passwd* pw = oem_id_to_passwd(uid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700510 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700511 return pw;
512 }
513 return app_id_to_passwd(uid, state);
514}
515
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700516passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800517 passwd_state_t* state = get_passwd_tls_buffer();
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700518 return getpwuid_internal(uid, state);
519}
Mark Salyzynb38347a2016-04-05 09:09:46 -0700520
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700521passwd* getpwnam_internal(const char* login, passwd_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700522 if (auto* android_id_info = find_android_id_info(login); android_id_info != nullptr) {
523 return android_iinfo_to_passwd(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700524 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800525
526 if (vendor_passwd.FindByName(login, state)) {
527 if (is_oem_id(state->passwd_.pw_uid)) {
528 return &state->passwd_;
529 }
530 }
531
Mark Salyzynb38347a2016-04-05 09:09:46 -0700532 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700533 passwd* pw = oem_id_to_passwd(oem_id_from_name(login), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700534 if (pw != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700535 return pw;
536 }
537 return app_id_to_passwd(app_id_from_name(login, false), state);
538}
539
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700540passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
541 passwd_state_t* state = get_passwd_tls_buffer();
542 return getpwnam_internal(login, state);
543}
544
545static int getpasswd_r(bool by_name, const char* name, uid_t uid, struct passwd* pwd, char* buf,
546 size_t buflen, struct passwd** result) {
547 ErrnoRestorer errno_restorer;
548 *result = nullptr;
549 char* p =
550 reinterpret_cast<char*>(__BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
551 if (p + sizeof(passwd_state_t) > buf + buflen) {
552 return ERANGE;
553 }
554 passwd_state_t* state = reinterpret_cast<passwd_state_t*>(p);
555 init_passwd_state(state);
556 passwd* retval = (by_name ? getpwnam_internal(name, state) : getpwuid_internal(uid, state));
557 if (retval != nullptr) {
558 *pwd = *retval;
559 *result = pwd;
560 return 0;
561 }
562 return errno;
563}
564
565int getpwnam_r(const char* name, passwd* pwd, char* buf, size_t byte_count, passwd** result) {
566 return getpasswd_r(true, name, -1, pwd, buf, byte_count, result);
567}
568
569int getpwuid_r(uid_t uid, passwd* pwd, char* buf, size_t byte_count, passwd** result) {
570 return getpasswd_r(false, nullptr, uid, pwd, buf, byte_count, result);
571}
572
Mark Salyzynb38347a2016-04-05 09:09:46 -0700573// All users are in just one group, the one passed in.
574int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
575 if (*ngroups < 1) {
576 *ngroups = 1;
577 return -1;
578 }
579 groups[0] = group;
580 return (*ngroups = 1);
581}
582
583char* getlogin() { // NOLINT: implementing bad function.
584 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
Elliott Hughes06bd5862017-07-28 16:27:49 -0700585 return pw ? pw->pw_name : nullptr;
586}
587
588int getlogin_r(char* buf, size_t size) {
589 char* login = getlogin();
590 if (login == nullptr) return errno;
591 size_t login_length = strlen(login) + 1;
592 if (login_length > size) return ERANGE;
593 memcpy(buf, login, login_length);
594 return 0;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700595}
596
Mark Salyzyn722ab052016-04-06 10:35:48 -0700597void setpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800598 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700599 if (state) {
600 state->getpwent_idx = 0;
601 }
602}
603
604void endpwent() {
605 setpwent();
606}
607
608passwd* getpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800609 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700610 if (state->getpwent_idx < 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700611 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700612 }
613
614 size_t start = 0;
615 ssize_t end = android_id_count;
616 if (state->getpwent_idx < end) {
617 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
618 }
619
620 start = end;
621 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
622
623 if (state->getpwent_idx < end) {
624 return oem_id_to_passwd(
625 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
626 }
627
628 start = end;
629 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
630
631 if (state->getpwent_idx < end) {
632 return oem_id_to_passwd(
633 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
634 }
635
Tom Cherry6b116d12019-04-25 10:34:07 -0700636 state->getpwent_idx = get_next_app_id(state->getpwent_idx, false);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700637
Tom Cherry4362f892017-11-14 08:50:43 -0800638 if (state->getpwent_idx != -1) {
639 return app_id_to_passwd(state->getpwent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700640 }
641
642 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700643 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700644}
645
Mark Salyzynb38347a2016-04-05 09:09:46 -0700646static group* getgrgid_internal(gid_t gid, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700647 if (auto* android_id_info = find_android_id_info(gid); android_id_info != nullptr) {
648 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700649 }
Tom Cherry5fb07632019-04-24 13:35:39 -0700650
Mark Salyzynb38347a2016-04-05 09:09:46 -0700651 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700652 group* grp = oem_id_to_group(gid, state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700653 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700654 return grp;
655 }
656 return app_id_to_group(gid, state);
657}
658
659group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700660 group_state_t* state = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -0700661 return getgrgid_internal(gid, state);
662}
663
664static group* getgrnam_internal(const char* name, group_state_t* state) {
Tom Cherry5fb07632019-04-24 13:35:39 -0700665 if (auto* android_id_info = find_android_id_info(name); android_id_info != nullptr) {
666 return android_iinfo_to_group(state, android_id_info);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700667 }
Tom Cherry6034ef82018-02-02 16:10:07 -0800668
669 if (vendor_group.FindByName(name, state)) {
670 if (is_oem_id(state->group_.gr_gid)) {
671 return &state->group_;
672 }
673 }
674
Mark Salyzynb38347a2016-04-05 09:09:46 -0700675 // Handle OEM range.
Tom Cherry5fb07632019-04-24 13:35:39 -0700676 group* grp = oem_id_to_group(oem_id_from_name(name), state);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700677 if (grp != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700678 return grp;
679 }
680 return app_id_to_group(app_id_from_name(name, true), state);
681}
682
683group* getgrnam(const char* name) { // NOLINT: implementing bad function.
Tom Cherryc57c5bd2019-05-14 17:02:28 -0700684 group_state_t* state = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -0700685 return getgrnam_internal(name, state);
686}
687
688static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
689 size_t buflen, struct group** result) {
690 ErrnoRestorer errno_restorer;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700691 *result = nullptr;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700692 char* p = reinterpret_cast<char*>(
Dan Alberta613d0d2017-10-05 16:39:33 -0700693 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
Mark Salyzynb38347a2016-04-05 09:09:46 -0700694 if (p + sizeof(group_state_t) > buf + buflen) {
695 return ERANGE;
696 }
697 group_state_t* state = reinterpret_cast<group_state_t*>(p);
698 init_group_state(state);
699 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700700 if (retval != nullptr) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700701 *grp = *retval;
702 *result = grp;
703 return 0;
704 }
705 return errno;
706}
707
708int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700709 return getgroup_r(false, nullptr, gid, grp, buf, buflen, result);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700710}
711
712int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
713 struct group **result) {
714 return getgroup_r(true, name, 0, grp, buf, buflen, result);
715}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700716
717void setgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800718 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700719 if (state) {
720 state->getgrent_idx = 0;
721 }
722}
723
724void endgrent() {
725 setgrent();
726}
727
728group* getgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800729 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700730 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) {
Mark Salyzyn722ab052016-04-06 10:35:48 -0700737 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
738 }
739
740 start = end;
741 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
742
743 if (state->getgrent_idx < end) {
Mark Salyzyn722ab052016-04-06 10:35:48 -0700744 return oem_id_to_group(
745 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
746 }
747
748 start = end;
749 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
750
751 if (state->getgrent_idx < end) {
Mark Salyzyn722ab052016-04-06 10:35:48 -0700752 return oem_id_to_group(
753 state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state);
754 }
755
756 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700757 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
Mark Salyzyn722ab052016-04-06 10:35:48 -0700758
Tom Cherry6b116d12019-04-25 10:34:07 -0700759 state->getgrent_idx = get_next_app_id(state->getgrent_idx, true);
Tom Cherry4362f892017-11-14 08:50:43 -0800760
761 if (state->getgrent_idx != -1) {
762 return app_id_to_group(state->getgrent_idx, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700763 }
764
765 // We are not reporting u1_a* and higher or we will be here forever
Yi Kong32bc0fc2018-08-02 17:31:13 -0700766 return nullptr;
Mark Salyzyn722ab052016-04-06 10:35:48 -0700767}