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