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