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 | |
| 198 | // Translate a user/group name to the corresponding user/group id. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 199 | // all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only) |
| 200 | // u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only) |
| 201 | // u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234 |
| 202 | // u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000 |
| 203 | // u1_system -> 1 * AID_USER_OFFSET + android_ids['system'] |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 204 | // returns 0 and sets errno to ENOENT in case of error. |
| 205 | static id_t app_id_from_name(const char* name, bool is_group) { |
| 206 | char* end; |
| 207 | unsigned long userid; |
| 208 | bool is_shared_gid = false; |
| 209 | |
| 210 | if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') { |
| 211 | end = const_cast<char*>(name+3); |
| 212 | userid = 0; |
| 213 | is_shared_gid = true; |
| 214 | } else if (name[0] == 'u' && isdigit(name[1])) { |
| 215 | userid = strtoul(name+1, &end, 10); |
| 216 | } else { |
| 217 | errno = ENOENT; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | if (end[0] != '_' || end[1] == 0) { |
| 222 | errno = ENOENT; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | unsigned long appid = 0; |
| 227 | if (end[1] == 'a' && isdigit(end[2])) { |
| 228 | if (is_shared_gid) { |
| 229 | // end will point to \0 if the strtoul below succeeds. |
| 230 | appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START; |
| 231 | if (appid > AID_SHARED_GID_END) { |
| 232 | errno = ENOENT; |
| 233 | return 0; |
| 234 | } |
| 235 | } else { |
| 236 | // end will point to \0 if the strtoul below succeeds. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 237 | appid = strtoul(end+2, &end, 10); |
| 238 | if (is_group && !strcmp(end, "_cache")) { |
| 239 | end += 6; |
| 240 | appid += AID_CACHE_GID_START; |
| 241 | } else { |
| 242 | appid += AID_APP_START; |
| 243 | } |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 244 | } |
| 245 | } else if (end[1] == 'i' && isdigit(end[2])) { |
| 246 | // end will point to \0 if the strtoul below succeeds. |
| 247 | appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; |
| 248 | } else { |
| 249 | for (size_t n = 0; n < android_id_count; n++) { |
| 250 | if (!strcmp(android_ids[n].name, end + 1)) { |
| 251 | appid = android_ids[n].aid; |
| 252 | // Move the end pointer to the null terminator. |
| 253 | end += strlen(android_ids[n].name) + 1; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Check that the entire string was consumed by one of the 3 cases above. |
| 260 | if (end[0] != 0) { |
| 261 | errno = ENOENT; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | // Check that user id won't overflow. |
| 266 | if (userid > 1000) { |
| 267 | errno = ENOENT; |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | // Check that app id is within range. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 272 | if (appid >= AID_USER_OFFSET) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 273 | errno = ENOENT; |
| 274 | return 0; |
| 275 | } |
| 276 | |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 277 | return (appid + userid*AID_USER_OFFSET); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | 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] | 281 | const uid_t appid = uid % AID_USER_OFFSET; |
| 282 | const uid_t userid = uid / AID_USER_OFFSET; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 283 | if (appid >= AID_ISOLATED_START) { |
| 284 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 285 | } else if (appid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 286 | for (size_t n = 0; n < android_id_count; n++) { |
| 287 | if (android_ids[n].aid == appid) { |
| 288 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 289 | return; |
| 290 | } |
| 291 | } |
| 292 | } else { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 293 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | 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] | 298 | const uid_t appid = gid % AID_USER_OFFSET; |
| 299 | const uid_t userid = gid / AID_USER_OFFSET; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 300 | if (appid >= AID_ISOLATED_START) { |
| 301 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
| 302 | } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) { |
| 303 | snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 304 | } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) { |
| 305 | snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START); |
| 306 | } else if (appid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 307 | for (size_t n = 0; n < android_id_count; n++) { |
| 308 | if (android_ids[n].aid == appid) { |
| 309 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 310 | return; |
| 311 | } |
| 312 | } |
| 313 | } else { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 314 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 318 | // oem_XXXX -> uid |
| 319 | // Supported ranges: |
| 320 | // AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999) |
| 321 | // AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999) |
| 322 | // Check OEM id is within range. |
| 323 | static bool is_oem_id(id_t id) { |
| 324 | return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) || |
| 325 | ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END))); |
| 326 | } |
| 327 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 328 | // Translate an OEM name to the corresponding user/group id. |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 329 | static id_t oem_id_from_name(const char* name) { |
| 330 | unsigned int id; |
| 331 | if (sscanf(name, "oem_%u", &id) != 1) { |
| 332 | return 0; |
| 333 | } |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 334 | if (!is_oem_id(id)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | } |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 337 | return static_cast<id_t>(id); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | 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] | 341 | if (!is_oem_id(uid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 342 | return NULL; |
| 343 | } |
| 344 | |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 345 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 346 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 347 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 348 | |
| 349 | passwd* pw = &state->passwd_; |
| 350 | pw->pw_name = state->name_buffer_; |
| 351 | pw->pw_dir = state->dir_buffer_; |
| 352 | pw->pw_shell = state->sh_buffer_; |
| 353 | pw->pw_uid = uid; |
| 354 | pw->pw_gid = uid; |
| 355 | return pw; |
| 356 | } |
| 357 | |
| 358 | 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] | 359 | if (!is_oem_id(gid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 364 | "oem_%u", gid); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 365 | |
| 366 | group* gr = &state->group_; |
| 367 | gr->gr_name = state->group_name_buffer_; |
| 368 | gr->gr_gid = gid; |
| 369 | gr->gr_mem[0] = gr->gr_name; |
| 370 | return gr; |
| 371 | } |
| 372 | |
| 373 | // Translate a uid into the corresponding name. |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 374 | // 0 to AID_APP_START-1 -> "system", "radio", etc. |
| 375 | // AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234 |
| 376 | // AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234 |
| 377 | // AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc. |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 378 | // returns a passwd structure (sets errno to ENOENT on failure). |
| 379 | 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] | 380 | if (uid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 381 | errno = ENOENT; |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_)); |
| 386 | |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 387 | const uid_t appid = uid % AID_USER_OFFSET; |
| 388 | if (appid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 389 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 390 | } else { |
| 391 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data"); |
| 392 | } |
| 393 | |
| 394 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 395 | |
| 396 | passwd* pw = &state->passwd_; |
| 397 | pw->pw_name = state->name_buffer_; |
| 398 | pw->pw_dir = state->dir_buffer_; |
| 399 | pw->pw_shell = state->sh_buffer_; |
| 400 | pw->pw_uid = uid; |
| 401 | pw->pw_gid = uid; |
| 402 | return pw; |
| 403 | } |
| 404 | |
| 405 | // Translate a gid into the corresponding app_<gid> |
| 406 | // group structure (sets errno to ENOENT on failure). |
| 407 | 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] | 408 | if (gid < AID_APP_START) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 409 | errno = ENOENT; |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_)); |
| 414 | |
| 415 | group* gr = &state->group_; |
| 416 | gr->gr_name = state->group_name_buffer_; |
| 417 | gr->gr_gid = gid; |
| 418 | gr->gr_mem[0] = gr->gr_name; |
| 419 | return gr; |
| 420 | } |
| 421 | |
| 422 | passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function. |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 423 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 424 | if (state == NULL) { |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | passwd* pw = android_id_to_passwd(state, uid); |
| 429 | if (pw != NULL) { |
| 430 | return pw; |
| 431 | } |
| 432 | // Handle OEM range. |
| 433 | pw = oem_id_to_passwd(uid, state); |
| 434 | if (pw != NULL) { |
| 435 | return pw; |
| 436 | } |
| 437 | return app_id_to_passwd(uid, state); |
| 438 | } |
| 439 | |
| 440 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 441 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 442 | if (state == NULL) { |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | passwd* pw = android_name_to_passwd(state, login); |
| 447 | if (pw != NULL) { |
| 448 | return pw; |
| 449 | } |
| 450 | // Handle OEM range. |
| 451 | pw = oem_id_to_passwd(oem_id_from_name(login), state); |
| 452 | if (pw != NULL) { |
| 453 | return pw; |
| 454 | } |
| 455 | return app_id_to_passwd(app_id_from_name(login, false), state); |
| 456 | } |
| 457 | |
| 458 | // All users are in just one group, the one passed in. |
| 459 | int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) { |
| 460 | if (*ngroups < 1) { |
| 461 | *ngroups = 1; |
| 462 | return -1; |
| 463 | } |
| 464 | groups[0] = group; |
| 465 | return (*ngroups = 1); |
| 466 | } |
| 467 | |
| 468 | char* getlogin() { // NOLINT: implementing bad function. |
| 469 | 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] | 470 | return pw ? pw->pw_name : nullptr; |
| 471 | } |
| 472 | |
| 473 | int getlogin_r(char* buf, size_t size) { |
| 474 | char* login = getlogin(); |
| 475 | if (login == nullptr) return errno; |
| 476 | size_t login_length = strlen(login) + 1; |
| 477 | if (login_length > size) return ERANGE; |
| 478 | memcpy(buf, login, login_length); |
| 479 | return 0; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 482 | void setpwent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 483 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 484 | if (state) { |
| 485 | state->getpwent_idx = 0; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void endpwent() { |
| 490 | setpwent(); |
| 491 | } |
| 492 | |
| 493 | passwd* getpwent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 494 | passwd_state_t* state = get_passwd_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 495 | if (state == NULL) { |
| 496 | return NULL; |
| 497 | } |
| 498 | if (state->getpwent_idx < 0) { |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | size_t start = 0; |
| 503 | ssize_t end = android_id_count; |
| 504 | if (state->getpwent_idx < end) { |
| 505 | return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++); |
| 506 | } |
| 507 | |
| 508 | start = end; |
| 509 | end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; |
| 510 | |
| 511 | if (state->getpwent_idx < end) { |
| 512 | return oem_id_to_passwd( |
| 513 | state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state); |
| 514 | } |
| 515 | |
| 516 | start = end; |
| 517 | end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; |
| 518 | |
| 519 | if (state->getpwent_idx < end) { |
| 520 | return oem_id_to_passwd( |
| 521 | state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state); |
| 522 | } |
| 523 | |
| 524 | start = end; |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 525 | end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher users |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 526 | |
| 527 | if (state->getpwent_idx < end) { |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 528 | 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] | 529 | } |
| 530 | |
| 531 | // We are not reporting u1_a* and higher or we will be here forever |
| 532 | state->getpwent_idx = -1; |
| 533 | return NULL; |
| 534 | } |
| 535 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 536 | static group* getgrgid_internal(gid_t gid, group_state_t* state) { |
| 537 | group* grp = android_id_to_group(state, gid); |
| 538 | if (grp != NULL) { |
| 539 | return grp; |
| 540 | } |
| 541 | // Handle OEM range. |
| 542 | grp = oem_id_to_group(gid, state); |
| 543 | if (grp != NULL) { |
| 544 | return grp; |
| 545 | } |
| 546 | return app_id_to_group(gid, state); |
| 547 | } |
| 548 | |
| 549 | group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. |
| 550 | group_state_t* state = __group_state(); |
| 551 | if (state == NULL) { |
| 552 | return NULL; |
| 553 | } |
| 554 | return getgrgid_internal(gid, state); |
| 555 | } |
| 556 | |
| 557 | static group* getgrnam_internal(const char* name, group_state_t* state) { |
| 558 | group* grp = android_name_to_group(state, name); |
| 559 | if (grp != NULL) { |
| 560 | return grp; |
| 561 | } |
| 562 | // Handle OEM range. |
| 563 | grp = oem_id_to_group(oem_id_from_name(name), state); |
| 564 | if (grp != NULL) { |
| 565 | return grp; |
| 566 | } |
| 567 | return app_id_to_group(app_id_from_name(name, true), state); |
| 568 | } |
| 569 | |
| 570 | group* getgrnam(const char* name) { // NOLINT: implementing bad function. |
| 571 | group_state_t* state = __group_state(); |
| 572 | if (state == NULL) { |
| 573 | return NULL; |
| 574 | } |
| 575 | return getgrnam_internal(name, state); |
| 576 | } |
| 577 | |
| 578 | static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf, |
| 579 | size_t buflen, struct group** result) { |
| 580 | ErrnoRestorer errno_restorer; |
| 581 | *result = NULL; |
| 582 | char* p = reinterpret_cast<char*>( |
| 583 | BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t))); |
| 584 | if (p + sizeof(group_state_t) > buf + buflen) { |
| 585 | return ERANGE; |
| 586 | } |
| 587 | group_state_t* state = reinterpret_cast<group_state_t*>(p); |
| 588 | init_group_state(state); |
| 589 | group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state)); |
| 590 | if (retval != NULL) { |
| 591 | *grp = *retval; |
| 592 | *result = grp; |
| 593 | return 0; |
| 594 | } |
| 595 | return errno; |
| 596 | } |
| 597 | |
| 598 | int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) { |
| 599 | return getgroup_r(false, NULL, gid, grp, buf, buflen, result); |
| 600 | } |
| 601 | |
| 602 | int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen, |
| 603 | struct group **result) { |
| 604 | return getgroup_r(true, name, 0, grp, buf, buflen, result); |
| 605 | } |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 606 | |
| 607 | void setgrent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 608 | group_state_t* state = get_group_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 609 | if (state) { |
| 610 | state->getgrent_idx = 0; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | void endgrent() { |
| 615 | setgrent(); |
| 616 | } |
| 617 | |
| 618 | group* getgrent() { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 619 | group_state_t* state = get_group_tls_buffer(); |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 620 | if (state == NULL) { |
| 621 | return NULL; |
| 622 | } |
| 623 | if (state->getgrent_idx < 0) { |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | size_t start = 0; |
| 628 | ssize_t end = android_id_count; |
| 629 | if (state->getgrent_idx < end) { |
| 630 | init_group_state(state); |
| 631 | return android_iinfo_to_group(state, android_ids + state->getgrent_idx++); |
| 632 | } |
| 633 | |
| 634 | start = end; |
| 635 | end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; |
| 636 | |
| 637 | if (state->getgrent_idx < end) { |
| 638 | init_group_state(state); |
| 639 | return oem_id_to_group( |
| 640 | state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state); |
| 641 | } |
| 642 | |
| 643 | start = end; |
| 644 | end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; |
| 645 | |
| 646 | if (state->getgrent_idx < end) { |
| 647 | init_group_state(state); |
| 648 | return oem_id_to_group( |
| 649 | state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state); |
| 650 | } |
| 651 | |
| 652 | start = end; |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 653 | end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 654 | |
| 655 | if (state->getgrent_idx < end) { |
| 656 | init_group_state(state); |
Jeff Sharkey | 934bc86 | 2016-12-13 14:03:19 -0700 | [diff] [blame] | 657 | 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] | 658 | } |
| 659 | |
| 660 | // We are not reporting u1_a* and higher or we will be here forever |
| 661 | state->getgrent_idx = -1; |
| 662 | return NULL; |
| 663 | } |