Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 42 | #include "private/grp_pwd.h" |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 43 | #include "private/ErrnoRestorer.h" |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 3f6eee9 | 2016-12-13 23:47:25 +0000 | [diff] [blame] | 45 | // Generated android_ids array |
| 46 | #include "generated_android_ids.h" |
| 47 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 48 | // POSIX seems to envisage an implementation where the <pwd.h> functions are |
| 49 | // implemented by brute-force searching with getpwent(3), and the <grp.h> |
| 50 | // functions are implemented similarly with getgrent(3). This means that it's |
| 51 | // okay for all the <grp.h> functions to share state, and all the <passwd.h> |
| 52 | // functions to share state, but <grp.h> functions can't clobber <passwd.h> |
| 53 | // functions' state and vice versa. |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 54 | #include "bionic/pthread_internal.h" |
| 55 | static group_state_t* get_group_tls_buffer() { |
| 56 | return &__get_bionic_tls().group; |
| 57 | } |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 58 | |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 59 | static passwd_state_t* get_passwd_tls_buffer() { |
| 60 | return &__get_bionic_tls().passwd; |
| 61 | } |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 62 | |
| 63 | static void init_group_state(group_state_t* state) { |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 64 | memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx)); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 65 | state->group_.gr_mem = state->group_members_; |
| 66 | } |
| 67 | |
| 68 | static group_state_t* __group_state() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 69 | group_state_t* result = get_group_tls_buffer(); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 70 | if (result != nullptr) { |
| 71 | init_group_state(result); |
| 72 | } |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | static int do_getpw_r(int by_name, const char* name, uid_t uid, |
| 77 | passwd* dst, char* buf, size_t byte_count, |
| 78 | passwd** result) { |
| 79 | // getpwnam_r and getpwuid_r don't modify errno, but library calls we |
| 80 | // make might. |
| 81 | ErrnoRestorer errno_restorer; |
| 82 | *result = NULL; |
| 83 | |
| 84 | // Our implementation of getpwnam(3) and getpwuid(3) use thread-local |
| 85 | // storage, so we can call them as long as we copy everything out |
| 86 | // before returning. |
| 87 | const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above. |
| 88 | |
| 89 | // POSIX allows failure to find a match to be considered a non-error. |
| 90 | // Reporting success (0) but with *result NULL is glibc's behavior. |
| 91 | if (src == NULL) { |
| 92 | return (errno == ENOENT) ? 0 : errno; |
| 93 | } |
| 94 | |
| 95 | // Work out where our strings will go in 'buf', and whether we've got |
| 96 | // enough space. |
| 97 | size_t required_byte_count = 0; |
| 98 | dst->pw_name = buf; |
| 99 | required_byte_count += strlen(src->pw_name) + 1; |
| 100 | dst->pw_dir = buf + required_byte_count; |
| 101 | required_byte_count += strlen(src->pw_dir) + 1; |
| 102 | dst->pw_shell = buf + required_byte_count; |
| 103 | required_byte_count += strlen(src->pw_shell) + 1; |
| 104 | if (byte_count < required_byte_count) { |
| 105 | return ERANGE; |
| 106 | } |
| 107 | |
| 108 | // Copy the strings. |
| 109 | snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell); |
| 110 | |
| 111 | // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic. |
| 112 | // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL. |
| 113 | dst->pw_passwd = NULL; |
| 114 | #if defined(__LP64__) |
| 115 | dst->pw_gecos = NULL; |
| 116 | #endif |
| 117 | |
| 118 | // Copy the integral fields. |
| 119 | dst->pw_gid = src->pw_gid; |
| 120 | dst->pw_uid = src->pw_uid; |
| 121 | |
| 122 | *result = dst; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int getpwnam_r(const char* name, passwd* pwd, |
| 127 | char* buf, size_t byte_count, passwd** result) { |
| 128 | return do_getpw_r(1, name, -1, pwd, buf, byte_count, result); |
| 129 | } |
| 130 | |
| 131 | int getpwuid_r(uid_t uid, passwd* pwd, |
| 132 | char* buf, size_t byte_count, passwd** result) { |
| 133 | return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result); |
| 134 | } |
| 135 | |
| 136 | static passwd* android_iinfo_to_passwd(passwd_state_t* state, |
| 137 | const android_id_info* iinfo) { |
| 138 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name); |
| 139 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 140 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 141 | |
| 142 | passwd* pw = &state->passwd_; |
| 143 | pw->pw_name = state->name_buffer_; |
| 144 | pw->pw_uid = iinfo->aid; |
| 145 | pw->pw_gid = iinfo->aid; |
| 146 | pw->pw_dir = state->dir_buffer_; |
| 147 | pw->pw_shell = state->sh_buffer_; |
| 148 | return pw; |
| 149 | } |
| 150 | |
| 151 | static group* android_iinfo_to_group(group_state_t* state, |
| 152 | const android_id_info* iinfo) { |
| 153 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name); |
| 154 | |
| 155 | group* gr = &state->group_; |
| 156 | gr->gr_name = state->group_name_buffer_; |
| 157 | gr->gr_gid = iinfo->aid; |
| 158 | gr->gr_mem[0] = gr->gr_name; |
| 159 | return gr; |
| 160 | } |
| 161 | |
| 162 | static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) { |
| 163 | for (size_t n = 0; n < android_id_count; ++n) { |
| 164 | if (android_ids[n].aid == id) { |
| 165 | return android_iinfo_to_passwd(state, android_ids + n); |
| 166 | } |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { |
| 172 | for (size_t n = 0; n < android_id_count; ++n) { |
| 173 | if (!strcmp(android_ids[n].name, name)) { |
| 174 | return android_iinfo_to_passwd(state, android_ids + n); |
| 175 | } |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | static group* android_id_to_group(group_state_t* state, unsigned id) { |
| 181 | for (size_t n = 0; n < android_id_count; ++n) { |
| 182 | if (android_ids[n].aid == id) { |
| 183 | return android_iinfo_to_group(state, android_ids + n); |
| 184 | } |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | static group* android_name_to_group(group_state_t* state, const char* name) { |
| 190 | for (size_t n = 0; n < android_id_count; ++n) { |
| 191 | if (!strcmp(android_ids[n].name, name)) { |
| 192 | return android_iinfo_to_group(state, android_ids + n); |
| 193 | } |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame^] | 198 | // These are a list of the reserved app ranges, and should never contain anything below |
| 199 | // AID_APP_START. They exist per user, so a given uid/gid modulo AID_USER_OFFSET will map |
| 200 | // to these ranges. |
| 201 | static constexpr struct { |
| 202 | uid_t start; |
| 203 | uid_t end; |
| 204 | } user_ranges[] = { |
| 205 | { AID_APP_START, AID_APP_END }, |
| 206 | { AID_CACHE_GID_START, AID_CACHE_GID_END }, |
| 207 | { AID_EXT_GID_START, AID_EXT_GID_END }, |
| 208 | { AID_EXT_CACHE_GID_START, AID_EXT_CACHE_GID_END }, |
| 209 | { AID_SHARED_GID_START, AID_SHARED_GID_END }, |
| 210 | { AID_ISOLATED_START, AID_ISOLATED_END }, |
| 211 | }; |
| 212 | |
| 213 | static constexpr bool verify_user_ranges_ascending() { |
| 214 | auto array_size = arraysize(user_ranges); |
| 215 | if (array_size < 2) return false; |
| 216 | |
| 217 | if (user_ranges[0].start > user_ranges[0].end) return false; |
| 218 | |
| 219 | for (size_t i = 1; i < array_size; ++i) { |
| 220 | if (user_ranges[i].start > user_ranges[i].end) return false; |
| 221 | if (user_ranges[i - 1].end > user_ranges[i].start) return false; |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | static_assert(verify_user_ranges_ascending(), "user_ranges must have ascending ranges"); |
| 227 | |
| 228 | static bool is_valid_app_id(id_t id) { |
| 229 | id_t appid = id % AID_USER_OFFSET; |
| 230 | |
| 231 | // AID_OVERFLOWUID is never a valid app id, so we explicitly return false to ensure this. |
| 232 | // This is true across all users, as there is no reason to ever map this id into any user range. |
| 233 | if (appid == AID_OVERFLOWUID) { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | // If we've resolved to something before app_start, we have already checked against |
| 238 | // android_ids, so no need to check again. |
| 239 | if (appid < user_ranges[0].start) { |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | // Otherwise check that the appid is in one of the reserved ranges. |
| 244 | for (size_t i = 0; i < arraysize(user_ranges); ++i) { |
| 245 | if (appid >= user_ranges[i].start && appid <= user_ranges[i].end) { |
| 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | // This provides an iterater for app_ids within the first user's app id's. |
| 254 | static uid_t get_next_app_id(uid_t current_id) { |
| 255 | // If current_id is below the first of the user_ranges, then we're uninitialized, and return the |
| 256 | // first valid id. |
| 257 | if (current_id < user_ranges[0].start) { |
| 258 | return user_ranges[0].start; |
| 259 | } |
| 260 | |
| 261 | uid_t incremented_id = current_id + 1; |
| 262 | |
| 263 | // Check to see if our incremented_id is between two ranges, and if so, return the beginning of |
| 264 | // the next valid range. |
| 265 | for (size_t i = 1; i < arraysize(user_ranges); ++i) { |
| 266 | if (incremented_id > user_ranges[i - 1].end && incremented_id < user_ranges[i].start) { |
| 267 | return user_ranges[i].start; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Check to see if our incremented_id is above final range, and return -1 to indicate that we've |
| 272 | // completed if so. |
| 273 | if (incremented_id > user_ranges[arraysize(user_ranges) - 1].end) { |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | // Otherwise the incremented_id is valid, so return it. |
| 278 | return incremented_id; |
| 279 | } |
| 280 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 281 | // Translate a user/group name to the corresponding user/group id. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 282 | // all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only) |
| 283 | // u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only) |
| 284 | // u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234 |
| 285 | // u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000 |
| 286 | // u1_system -> 1 * AID_USER_OFFSET + android_ids['system'] |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 287 | // returns 0 and sets errno to ENOENT in case of error. |
| 288 | static id_t app_id_from_name(const char* name, bool is_group) { |
| 289 | char* end; |
| 290 | unsigned long userid; |
| 291 | bool is_shared_gid = false; |
| 292 | |
| 293 | if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') { |
| 294 | end = const_cast<char*>(name+3); |
| 295 | userid = 0; |
| 296 | is_shared_gid = true; |
| 297 | } else if (name[0] == 'u' && isdigit(name[1])) { |
| 298 | userid = strtoul(name+1, &end, 10); |
| 299 | } else { |
| 300 | errno = ENOENT; |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | if (end[0] != '_' || end[1] == 0) { |
| 305 | errno = ENOENT; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | unsigned long appid = 0; |
| 310 | if (end[1] == 'a' && isdigit(end[2])) { |
| 311 | if (is_shared_gid) { |
| 312 | // end will point to \0 if the strtoul below succeeds. |
| 313 | appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START; |
| 314 | if (appid > AID_SHARED_GID_END) { |
| 315 | errno = ENOENT; |
| 316 | return 0; |
| 317 | } |
| 318 | } else { |
| 319 | // end will point to \0 if the strtoul below succeeds. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 320 | appid = strtoul(end+2, &end, 10); |
| 321 | if (is_group && !strcmp(end, "_cache")) { |
| 322 | end += 6; |
| 323 | appid += AID_CACHE_GID_START; |
| 324 | } else { |
| 325 | appid += AID_APP_START; |
| 326 | } |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 327 | } |
| 328 | } else if (end[1] == 'i' && isdigit(end[2])) { |
| 329 | // end will point to \0 if the strtoul below succeeds. |
| 330 | appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; |
| 331 | } else { |
| 332 | for (size_t n = 0; n < android_id_count; n++) { |
| 333 | if (!strcmp(android_ids[n].name, end + 1)) { |
| 334 | appid = android_ids[n].aid; |
| 335 | // Move the end pointer to the null terminator. |
| 336 | end += strlen(android_ids[n].name) + 1; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Check that the entire string was consumed by one of the 3 cases above. |
| 343 | if (end[0] != 0) { |
| 344 | errno = ENOENT; |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | // Check that user id won't overflow. |
| 349 | if (userid > 1000) { |
| 350 | errno = ENOENT; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | // Check that app id is within range. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 355 | if (appid >= AID_USER_OFFSET) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 356 | errno = ENOENT; |
| 357 | return 0; |
| 358 | } |
| 359 | |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 360 | return (appid + userid*AID_USER_OFFSET); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 364 | const uid_t appid = uid % AID_USER_OFFSET; |
| 365 | const uid_t userid = uid / AID_USER_OFFSET; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 366 | if (appid >= AID_ISOLATED_START) { |
| 367 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 368 | } else if (appid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 369 | for (size_t n = 0; n < android_id_count; n++) { |
| 370 | if (android_ids[n].aid == appid) { |
| 371 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 372 | return; |
| 373 | } |
| 374 | } |
| 375 | } else { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 376 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 381 | const uid_t appid = gid % AID_USER_OFFSET; |
| 382 | const uid_t userid = gid / AID_USER_OFFSET; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 383 | if (appid >= AID_ISOLATED_START) { |
| 384 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
| 385 | } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) { |
| 386 | snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 387 | } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) { |
| 388 | snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START); |
| 389 | } else if (appid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 390 | for (size_t n = 0; n < android_id_count; n++) { |
| 391 | if (android_ids[n].aid == appid) { |
| 392 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | } else { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 397 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 401 | // oem_XXXX -> uid |
| 402 | // Supported ranges: |
| 403 | // AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999) |
| 404 | // AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999) |
| 405 | // Check OEM id is within range. |
| 406 | static bool is_oem_id(id_t id) { |
| 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))); |
| 409 | } |
| 410 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 411 | // Translate an OEM name to the corresponding user/group id. |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 412 | static 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 Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 417 | if (!is_oem_id(id)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 420 | return static_cast<id_t>(id); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 424 | if (!is_oem_id(uid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
| 427 | |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 428 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 429 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 430 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 431 | |
| 432 | passwd* pw = &state->passwd_; |
| 433 | pw->pw_name = state->name_buffer_; |
| 434 | pw->pw_dir = state->dir_buffer_; |
| 435 | pw->pw_shell = state->sh_buffer_; |
| 436 | pw->pw_uid = uid; |
| 437 | pw->pw_gid = uid; |
| 438 | return pw; |
| 439 | } |
| 440 | |
| 441 | static group* oem_id_to_group(gid_t gid, group_state_t* state) { |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 442 | if (!is_oem_id(gid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 447 | "oem_%u", gid); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 448 | |
| 449 | group* gr = &state->group_; |
| 450 | gr->gr_name = state->group_name_buffer_; |
| 451 | gr->gr_gid = gid; |
| 452 | gr->gr_mem[0] = gr->gr_name; |
| 453 | return gr; |
| 454 | } |
| 455 | |
| 456 | // Translate a uid into the corresponding name. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 457 | // 0 to AID_APP_START-1 -> "system", "radio", etc. |
| 458 | // AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234 |
| 459 | // AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234 |
| 460 | // AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc. |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 461 | // returns a passwd structure (sets errno to ENOENT on failure). |
| 462 | static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) { |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame^] | 463 | if (uid < AID_APP_START || !is_valid_app_id(uid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 464 | errno = ENOENT; |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_)); |
| 469 | |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 470 | const uid_t appid = uid % AID_USER_OFFSET; |
| 471 | if (appid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 472 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 473 | } else { |
| 474 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data"); |
| 475 | } |
| 476 | |
| 477 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 478 | |
| 479 | passwd* pw = &state->passwd_; |
| 480 | pw->pw_name = state->name_buffer_; |
| 481 | pw->pw_dir = state->dir_buffer_; |
| 482 | pw->pw_shell = state->sh_buffer_; |
| 483 | 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). |
| 490 | static group* app_id_to_group(gid_t gid, group_state_t* state) { |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame^] | 491 | if (gid < AID_APP_START || !is_valid_app_id(gid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 492 | errno = ENOENT; |
| 493 | return NULL; |
| 494 | } |
| 495 | |
| 496 | print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_)); |
| 497 | |
| 498 | group* gr = &state->group_; |
| 499 | gr->gr_name = state->group_name_buffer_; |
| 500 | gr->gr_gid = gid; |
| 501 | gr->gr_mem[0] = gr->gr_name; |
| 502 | return gr; |
| 503 | } |
| 504 | |
| 505 | passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function. |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 506 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 507 | if (state == NULL) { |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | passwd* pw = android_id_to_passwd(state, uid); |
| 512 | if (pw != NULL) { |
| 513 | return pw; |
| 514 | } |
| 515 | // Handle OEM range. |
| 516 | pw = oem_id_to_passwd(uid, state); |
| 517 | if (pw != NULL) { |
| 518 | return pw; |
| 519 | } |
| 520 | return app_id_to_passwd(uid, state); |
| 521 | } |
| 522 | |
| 523 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 524 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 525 | if (state == NULL) { |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | passwd* pw = android_name_to_passwd(state, login); |
| 530 | if (pw != NULL) { |
| 531 | return pw; |
| 532 | } |
| 533 | // Handle OEM range. |
| 534 | pw = oem_id_to_passwd(oem_id_from_name(login), state); |
| 535 | if (pw != NULL) { |
| 536 | return pw; |
| 537 | } |
| 538 | return app_id_to_passwd(app_id_from_name(login, false), state); |
| 539 | } |
| 540 | |
| 541 | // All users are in just one group, the one passed in. |
| 542 | int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) { |
| 543 | if (*ngroups < 1) { |
| 544 | *ngroups = 1; |
| 545 | return -1; |
| 546 | } |
| 547 | groups[0] = group; |
| 548 | return (*ngroups = 1); |
| 549 | } |
| 550 | |
| 551 | char* getlogin() { // NOLINT: implementing bad function. |
| 552 | passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function. |
Elliott Hughes | 06bd586 | 2017-07-28 16:27:49 -0700 | [diff] [blame] | 553 | return pw ? pw->pw_name : nullptr; |
| 554 | } |
| 555 | |
| 556 | int getlogin_r(char* buf, size_t size) { |
| 557 | char* login = getlogin(); |
| 558 | if (login == nullptr) return errno; |
| 559 | size_t login_length = strlen(login) + 1; |
| 560 | if (login_length > size) return ERANGE; |
| 561 | memcpy(buf, login, login_length); |
| 562 | return 0; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 565 | void setpwent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 566 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 567 | if (state) { |
| 568 | state->getpwent_idx = 0; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | void endpwent() { |
| 573 | setpwent(); |
| 574 | } |
| 575 | |
| 576 | passwd* getpwent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 577 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 578 | if (state == NULL) { |
| 579 | return NULL; |
| 580 | } |
| 581 | if (state->getpwent_idx < 0) { |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | size_t start = 0; |
| 586 | ssize_t end = android_id_count; |
| 587 | if (state->getpwent_idx < end) { |
| 588 | return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++); |
| 589 | } |
| 590 | |
| 591 | start = end; |
| 592 | end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; |
| 593 | |
| 594 | if (state->getpwent_idx < end) { |
| 595 | return oem_id_to_passwd( |
| 596 | state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state); |
| 597 | } |
| 598 | |
| 599 | start = end; |
| 600 | end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; |
| 601 | |
| 602 | if (state->getpwent_idx < end) { |
| 603 | return oem_id_to_passwd( |
| 604 | state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state); |
| 605 | } |
| 606 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame^] | 607 | state->getpwent_idx = get_next_app_id(state->getpwent_idx); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 608 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame^] | 609 | if (state->getpwent_idx != -1) { |
| 610 | return app_id_to_passwd(state->getpwent_idx, state); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // We are not reporting u1_a* and higher or we will be here forever |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 614 | return NULL; |
| 615 | } |
| 616 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 617 | static group* getgrgid_internal(gid_t gid, group_state_t* state) { |
| 618 | group* grp = android_id_to_group(state, gid); |
| 619 | if (grp != NULL) { |
| 620 | return grp; |
| 621 | } |
| 622 | // Handle OEM range. |
| 623 | grp = oem_id_to_group(gid, state); |
| 624 | if (grp != NULL) { |
| 625 | return grp; |
| 626 | } |
| 627 | return app_id_to_group(gid, state); |
| 628 | } |
| 629 | |
| 630 | group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. |
| 631 | group_state_t* state = __group_state(); |
| 632 | if (state == NULL) { |
| 633 | return NULL; |
| 634 | } |
| 635 | return getgrgid_internal(gid, state); |
| 636 | } |
| 637 | |
| 638 | static group* getgrnam_internal(const char* name, group_state_t* state) { |
| 639 | group* grp = android_name_to_group(state, name); |
| 640 | if (grp != NULL) { |
| 641 | return grp; |
| 642 | } |
| 643 | // Handle OEM range. |
| 644 | grp = oem_id_to_group(oem_id_from_name(name), state); |
| 645 | if (grp != NULL) { |
| 646 | return grp; |
| 647 | } |
| 648 | return app_id_to_group(app_id_from_name(name, true), state); |
| 649 | } |
| 650 | |
| 651 | group* getgrnam(const char* name) { // NOLINT: implementing bad function. |
| 652 | group_state_t* state = __group_state(); |
| 653 | if (state == NULL) { |
| 654 | return NULL; |
| 655 | } |
| 656 | return getgrnam_internal(name, state); |
| 657 | } |
| 658 | |
| 659 | static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf, |
| 660 | size_t buflen, struct group** result) { |
| 661 | ErrnoRestorer errno_restorer; |
| 662 | *result = NULL; |
| 663 | char* p = reinterpret_cast<char*>( |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 664 | __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t))); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 665 | if (p + sizeof(group_state_t) > buf + buflen) { |
| 666 | return ERANGE; |
| 667 | } |
| 668 | group_state_t* state = reinterpret_cast<group_state_t*>(p); |
| 669 | init_group_state(state); |
| 670 | group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state)); |
| 671 | if (retval != NULL) { |
| 672 | *grp = *retval; |
| 673 | *result = grp; |
| 674 | return 0; |
| 675 | } |
| 676 | return errno; |
| 677 | } |
| 678 | |
| 679 | int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) { |
| 680 | return getgroup_r(false, NULL, gid, grp, buf, buflen, result); |
| 681 | } |
| 682 | |
| 683 | int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen, |
| 684 | struct group **result) { |
| 685 | return getgroup_r(true, name, 0, grp, buf, buflen, result); |
| 686 | } |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 687 | |
| 688 | void setgrent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 689 | group_state_t* state = get_group_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 690 | if (state) { |
| 691 | state->getgrent_idx = 0; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | void endgrent() { |
| 696 | setgrent(); |
| 697 | } |
| 698 | |
| 699 | group* getgrent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 700 | group_state_t* state = get_group_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 701 | if (state == NULL) { |
| 702 | return NULL; |
| 703 | } |
| 704 | if (state->getgrent_idx < 0) { |
| 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | size_t start = 0; |
| 709 | ssize_t end = android_id_count; |
| 710 | if (state->getgrent_idx < end) { |
| 711 | init_group_state(state); |
| 712 | return android_iinfo_to_group(state, android_ids + state->getgrent_idx++); |
| 713 | } |
| 714 | |
| 715 | start = end; |
| 716 | end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; |
| 717 | |
| 718 | if (state->getgrent_idx < end) { |
| 719 | init_group_state(state); |
| 720 | return oem_id_to_group( |
| 721 | state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state); |
| 722 | } |
| 723 | |
| 724 | start = end; |
| 725 | end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; |
| 726 | |
| 727 | if (state->getgrent_idx < end) { |
| 728 | init_group_state(state); |
| 729 | return oem_id_to_group( |
| 730 | state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state); |
| 731 | } |
| 732 | |
| 733 | start = end; |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 734 | end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 735 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame^] | 736 | state->getgrent_idx = get_next_app_id(state->getgrent_idx); |
| 737 | |
| 738 | if (state->getgrent_idx != -1) { |
| 739 | return app_id_to_group(state->getgrent_idx, state); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // We are not reporting u1_a* and higher or we will be here forever |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 743 | return NULL; |
| 744 | } |