| 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 |  | 
 | 46 | // POSIX seems to envisage an implementation where the <pwd.h> functions are | 
 | 47 | // implemented by brute-force searching with getpwent(3), and the <grp.h> | 
 | 48 | // functions are implemented similarly with getgrent(3). This means that it's | 
 | 49 | // okay for all the <grp.h> functions to share state, and all the <passwd.h> | 
 | 50 | // functions to share state, but <grp.h> functions can't clobber <passwd.h> | 
 | 51 | // functions' state and vice versa. | 
 | 52 |  | 
 | 53 | struct group_state_t { | 
 | 54 |   group group_; | 
 | 55 |   char* group_members_[2]; | 
 | 56 |   char group_name_buffer_[32]; | 
| Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 57 |   // Must be last so init_group_state can run a simple memset for the above | 
 | 58 |   ssize_t getgrent_idx; | 
| Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 59 | }; | 
 | 60 |  | 
 | 61 | struct passwd_state_t { | 
 | 62 |   passwd passwd_; | 
 | 63 |   char name_buffer_[32]; | 
 | 64 |   char dir_buffer_[32]; | 
 | 65 |   char sh_buffer_[32]; | 
| Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 66 |   ssize_t getpwent_idx; | 
| Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 67 | }; | 
 | 68 |  | 
 | 69 | static ThreadLocalBuffer<group_state_t> g_group_tls_buffer; | 
 | 70 | static ThreadLocalBuffer<passwd_state_t> g_passwd_tls_buffer; | 
 | 71 |  | 
 | 72 | static void init_group_state(group_state_t* state) { | 
| Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 73 |   memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx)); | 
| Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 74 |   state->group_.gr_mem = state->group_members_; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static group_state_t* __group_state() { | 
 | 78 |   group_state_t* result = g_group_tls_buffer.get(); | 
 | 79 |   if (result != nullptr) { | 
 | 80 |     init_group_state(result); | 
 | 81 |   } | 
 | 82 |   return result; | 
 | 83 | } | 
 | 84 |  | 
 | 85 | static int do_getpw_r(int by_name, const char* name, uid_t uid, | 
 | 86 |                       passwd* dst, char* buf, size_t byte_count, | 
 | 87 |                       passwd** result) { | 
 | 88 |   // getpwnam_r and getpwuid_r don't modify errno, but library calls we | 
 | 89 |   // make might. | 
 | 90 |   ErrnoRestorer errno_restorer; | 
 | 91 |   *result = NULL; | 
 | 92 |  | 
 | 93 |   // Our implementation of getpwnam(3) and getpwuid(3) use thread-local | 
 | 94 |   // storage, so we can call them as long as we copy everything out | 
 | 95 |   // before returning. | 
 | 96 |   const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above. | 
 | 97 |  | 
 | 98 |   // POSIX allows failure to find a match to be considered a non-error. | 
 | 99 |   // Reporting success (0) but with *result NULL is glibc's behavior. | 
 | 100 |   if (src == NULL) { | 
 | 101 |     return (errno == ENOENT) ? 0 : errno; | 
 | 102 |   } | 
 | 103 |  | 
 | 104 |   // Work out where our strings will go in 'buf', and whether we've got | 
 | 105 |   // enough space. | 
 | 106 |   size_t required_byte_count = 0; | 
 | 107 |   dst->pw_name = buf; | 
 | 108 |   required_byte_count += strlen(src->pw_name) + 1; | 
 | 109 |   dst->pw_dir = buf + required_byte_count; | 
 | 110 |   required_byte_count += strlen(src->pw_dir) + 1; | 
 | 111 |   dst->pw_shell = buf + required_byte_count; | 
 | 112 |   required_byte_count += strlen(src->pw_shell) + 1; | 
 | 113 |   if (byte_count < required_byte_count) { | 
 | 114 |     return ERANGE; | 
 | 115 |   } | 
 | 116 |  | 
 | 117 |   // Copy the strings. | 
 | 118 |   snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell); | 
 | 119 |  | 
 | 120 |   // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic. | 
 | 121 |   // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL. | 
 | 122 |   dst->pw_passwd = NULL; | 
 | 123 | #if defined(__LP64__) | 
 | 124 |   dst->pw_gecos = NULL; | 
 | 125 | #endif | 
 | 126 |  | 
 | 127 |   // Copy the integral fields. | 
 | 128 |   dst->pw_gid = src->pw_gid; | 
 | 129 |   dst->pw_uid = src->pw_uid; | 
 | 130 |  | 
 | 131 |   *result = dst; | 
 | 132 |   return 0; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | int getpwnam_r(const char* name, passwd* pwd, | 
 | 136 |                char* buf, size_t byte_count, passwd** result) { | 
 | 137 |   return do_getpw_r(1, name, -1, pwd, buf, byte_count, result); | 
 | 138 | } | 
 | 139 |  | 
 | 140 | int getpwuid_r(uid_t uid, passwd* pwd, | 
 | 141 |                char* buf, size_t byte_count, passwd** result) { | 
 | 142 |   return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result); | 
 | 143 | } | 
 | 144 |  | 
 | 145 | static passwd* android_iinfo_to_passwd(passwd_state_t* state, | 
 | 146 |                                        const android_id_info* iinfo) { | 
 | 147 |   snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name); | 
 | 148 |   snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); | 
 | 149 |   snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); | 
 | 150 |  | 
 | 151 |   passwd* pw = &state->passwd_; | 
 | 152 |   pw->pw_name  = state->name_buffer_; | 
 | 153 |   pw->pw_uid   = iinfo->aid; | 
 | 154 |   pw->pw_gid   = iinfo->aid; | 
 | 155 |   pw->pw_dir   = state->dir_buffer_; | 
 | 156 |   pw->pw_shell = state->sh_buffer_; | 
 | 157 |   return pw; | 
 | 158 | } | 
 | 159 |  | 
 | 160 | static group* android_iinfo_to_group(group_state_t* state, | 
 | 161 |                                      const android_id_info* iinfo) { | 
 | 162 |   snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name); | 
 | 163 |  | 
 | 164 |   group* gr = &state->group_; | 
 | 165 |   gr->gr_name   = state->group_name_buffer_; | 
 | 166 |   gr->gr_gid    = iinfo->aid; | 
 | 167 |   gr->gr_mem[0] = gr->gr_name; | 
 | 168 |   return gr; | 
 | 169 | } | 
 | 170 |  | 
 | 171 | static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) { | 
 | 172 |   for (size_t n = 0; n < android_id_count; ++n) { | 
 | 173 |     if (android_ids[n].aid == id) { | 
 | 174 |       return android_iinfo_to_passwd(state, android_ids + n); | 
 | 175 |     } | 
 | 176 |   } | 
 | 177 |   return NULL; | 
 | 178 | } | 
 | 179 |  | 
 | 180 | static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { | 
 | 181 |   for (size_t n = 0; n < android_id_count; ++n) { | 
 | 182 |     if (!strcmp(android_ids[n].name, name)) { | 
 | 183 |       return android_iinfo_to_passwd(state, android_ids + n); | 
 | 184 |     } | 
 | 185 |   } | 
 | 186 |   return NULL; | 
 | 187 | } | 
 | 188 |  | 
 | 189 | static group* android_id_to_group(group_state_t* state, unsigned id) { | 
 | 190 |   for (size_t n = 0; n < android_id_count; ++n) { | 
 | 191 |     if (android_ids[n].aid == id) { | 
 | 192 |       return android_iinfo_to_group(state, android_ids + n); | 
 | 193 |     } | 
 | 194 |   } | 
 | 195 |   return NULL; | 
 | 196 | } | 
 | 197 |  | 
 | 198 | static group* android_name_to_group(group_state_t* state, const char* name) { | 
 | 199 |   for (size_t n = 0; n < android_id_count; ++n) { | 
 | 200 |     if (!strcmp(android_ids[n].name, name)) { | 
 | 201 |       return android_iinfo_to_group(state, android_ids + n); | 
 | 202 |     } | 
 | 203 |   } | 
 | 204 |   return NULL; | 
 | 205 | } | 
 | 206 |  | 
 | 207 | // Translate a user/group name to the corresponding user/group id. | 
 | 208 | // all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only) | 
 | 209 | // u0_a1234 -> 0 * AID_USER + AID_APP + 1234 | 
 | 210 | // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000 | 
 | 211 | // u1_system -> 1 * AID_USER + android_ids['system'] | 
 | 212 | // returns 0 and sets errno to ENOENT in case of error. | 
 | 213 | static id_t app_id_from_name(const char* name, bool is_group) { | 
 | 214 |   char* end; | 
 | 215 |   unsigned long userid; | 
 | 216 |   bool is_shared_gid = false; | 
 | 217 |  | 
 | 218 |   if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') { | 
 | 219 |     end = const_cast<char*>(name+3); | 
 | 220 |     userid = 0; | 
 | 221 |     is_shared_gid = true; | 
 | 222 |   } else if (name[0] == 'u' && isdigit(name[1])) { | 
 | 223 |     userid = strtoul(name+1, &end, 10); | 
 | 224 |   } else { | 
 | 225 |     errno = ENOENT; | 
 | 226 |     return 0; | 
 | 227 |   } | 
 | 228 |  | 
 | 229 |   if (end[0] != '_' || end[1] == 0) { | 
 | 230 |     errno = ENOENT; | 
 | 231 |     return 0; | 
 | 232 |   } | 
 | 233 |  | 
 | 234 |   unsigned long appid = 0; | 
 | 235 |   if (end[1] == 'a' && isdigit(end[2])) { | 
 | 236 |     if (is_shared_gid) { | 
 | 237 |       // end will point to \0 if the strtoul below succeeds. | 
 | 238 |       appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START; | 
 | 239 |       if (appid > AID_SHARED_GID_END) { | 
 | 240 |         errno = ENOENT; | 
 | 241 |         return 0; | 
 | 242 |       } | 
 | 243 |     } else { | 
 | 244 |       // end will point to \0 if the strtoul below succeeds. | 
 | 245 |       appid = strtoul(end+2, &end, 10) + AID_APP; | 
 | 246 |     } | 
 | 247 |   } else if (end[1] == 'i' && isdigit(end[2])) { | 
 | 248 |     // end will point to \0 if the strtoul below succeeds. | 
 | 249 |     appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; | 
 | 250 |   } else { | 
 | 251 |     for (size_t n = 0; n < android_id_count; n++) { | 
 | 252 |       if (!strcmp(android_ids[n].name, end + 1)) { | 
 | 253 |         appid = android_ids[n].aid; | 
 | 254 |         // Move the end pointer to the null terminator. | 
 | 255 |         end += strlen(android_ids[n].name) + 1; | 
 | 256 |         break; | 
 | 257 |       } | 
 | 258 |     } | 
 | 259 |   } | 
 | 260 |  | 
 | 261 |   // Check that the entire string was consumed by one of the 3 cases above. | 
 | 262 |   if (end[0] != 0) { | 
 | 263 |     errno = ENOENT; | 
 | 264 |     return 0; | 
 | 265 |   } | 
 | 266 |  | 
 | 267 |   // Check that user id won't overflow. | 
 | 268 |   if (userid > 1000) { | 
 | 269 |     errno = ENOENT; | 
 | 270 |     return 0; | 
 | 271 |   } | 
 | 272 |  | 
 | 273 |   // Check that app id is within range. | 
 | 274 |   if (appid >= AID_USER) { | 
 | 275 |     errno = ENOENT; | 
 | 276 |     return 0; | 
 | 277 |   } | 
 | 278 |  | 
 | 279 |   return (appid + userid*AID_USER); | 
 | 280 | } | 
 | 281 |  | 
 | 282 | static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) { | 
 | 283 |   const uid_t appid = uid % AID_USER; | 
 | 284 |   const uid_t userid = uid / AID_USER; | 
 | 285 |   if (appid >= AID_ISOLATED_START) { | 
 | 286 |     snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); | 
 | 287 |   } else if (appid < AID_APP) { | 
 | 288 |     for (size_t n = 0; n < android_id_count; n++) { | 
 | 289 |       if (android_ids[n].aid == appid) { | 
 | 290 |         snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); | 
 | 291 |         return; | 
 | 292 |       } | 
 | 293 |     } | 
 | 294 |   } else { | 
 | 295 |     snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); | 
 | 296 |   } | 
 | 297 | } | 
 | 298 |  | 
 | 299 | static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) { | 
 | 300 |   const uid_t appid = gid % AID_USER; | 
 | 301 |   const uid_t userid = gid / AID_USER; | 
 | 302 |   if (appid >= AID_ISOLATED_START) { | 
 | 303 |     snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); | 
 | 304 |   } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) { | 
 | 305 |     snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); | 
 | 306 |   } else if (appid < AID_APP) { | 
 | 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 { | 
 | 314 |     snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); | 
 | 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. | 
 | 374 | // 0 to AID_APP-1                   -> "system", "radio", etc. | 
 | 375 | // AID_APP to AID_ISOLATED_START-1  -> u0_a1234 | 
 | 376 | // AID_ISOLATED_START to AID_USER-1 -> u0_i1234 | 
 | 377 | // AID_USER+                        -> u1_radio, u1_a1234, u2_i1234, etc. | 
 | 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) { | 
 | 380 |   if (uid < AID_APP) { | 
 | 381 |     errno = ENOENT; | 
 | 382 |     return NULL; | 
 | 383 |   } | 
 | 384 |  | 
 | 385 |   print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_)); | 
 | 386 |  | 
 | 387 |   const uid_t appid = uid % AID_USER; | 
 | 388 |   if (appid < AID_APP) { | 
 | 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) { | 
 | 408 |   if (gid < AID_APP) { | 
 | 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. | 
 | 423 |   passwd_state_t* state = g_passwd_tls_buffer.get(); | 
 | 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. | 
 | 441 |   passwd_state_t* state = g_passwd_tls_buffer.get(); | 
 | 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. | 
 | 470 |   return (pw != NULL) ? pw->pw_name : NULL; | 
 | 471 | } | 
 | 472 |  | 
| Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 473 | void setpwent() { | 
 | 474 |   passwd_state_t* state = g_passwd_tls_buffer.get(); | 
 | 475 |   if (state) { | 
 | 476 |     state->getpwent_idx = 0; | 
 | 477 |   } | 
 | 478 | } | 
 | 479 |  | 
 | 480 | void endpwent() { | 
 | 481 |   setpwent(); | 
 | 482 | } | 
 | 483 |  | 
 | 484 | passwd* getpwent() { | 
 | 485 |   passwd_state_t* state = g_passwd_tls_buffer.get(); | 
 | 486 |   if (state == NULL) { | 
 | 487 |     return NULL; | 
 | 488 |   } | 
 | 489 |   if (state->getpwent_idx < 0) { | 
 | 490 |     return NULL; | 
 | 491 |   } | 
 | 492 |  | 
 | 493 |   size_t start = 0; | 
 | 494 |   ssize_t end = android_id_count; | 
 | 495 |   if (state->getpwent_idx < end) { | 
 | 496 |     return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++); | 
 | 497 |   } | 
 | 498 |  | 
 | 499 |   start = end; | 
 | 500 |   end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; | 
 | 501 |  | 
 | 502 |   if (state->getpwent_idx < end) { | 
 | 503 |     return oem_id_to_passwd( | 
 | 504 |         state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state); | 
 | 505 |   } | 
 | 506 |  | 
 | 507 |   start = end; | 
 | 508 |   end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; | 
 | 509 |  | 
 | 510 |   if (state->getpwent_idx < end) { | 
 | 511 |     return oem_id_to_passwd( | 
 | 512 |         state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state); | 
 | 513 |   } | 
 | 514 |  | 
 | 515 |   start = end; | 
 | 516 |   end += AID_USER - AID_APP; // Do not expose higher users | 
 | 517 |  | 
 | 518 |   if (state->getpwent_idx < end) { | 
 | 519 |     return app_id_to_passwd(state->getpwent_idx++ - start + AID_APP, state); | 
 | 520 |   } | 
 | 521 |  | 
 | 522 |   // We are not reporting u1_a* and higher or we will be here forever | 
 | 523 |   state->getpwent_idx = -1; | 
 | 524 |   return NULL; | 
 | 525 | } | 
 | 526 |  | 
| Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 527 | static group* getgrgid_internal(gid_t gid, group_state_t* state) { | 
 | 528 |   group* grp = android_id_to_group(state, gid); | 
 | 529 |   if (grp != NULL) { | 
 | 530 |     return grp; | 
 | 531 |   } | 
 | 532 |   // Handle OEM range. | 
 | 533 |   grp = oem_id_to_group(gid, state); | 
 | 534 |   if (grp != NULL) { | 
 | 535 |     return grp; | 
 | 536 |   } | 
 | 537 |   return app_id_to_group(gid, state); | 
 | 538 | } | 
 | 539 |  | 
 | 540 | group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. | 
 | 541 |   group_state_t* state = __group_state(); | 
 | 542 |   if (state == NULL) { | 
 | 543 |     return NULL; | 
 | 544 |   } | 
 | 545 |   return getgrgid_internal(gid, state); | 
 | 546 | } | 
 | 547 |  | 
 | 548 | static group* getgrnam_internal(const char* name, group_state_t* state) { | 
 | 549 |   group* grp = android_name_to_group(state, name); | 
 | 550 |   if (grp != NULL) { | 
 | 551 |     return grp; | 
 | 552 |   } | 
 | 553 |   // Handle OEM range. | 
 | 554 |   grp = oem_id_to_group(oem_id_from_name(name), state); | 
 | 555 |   if (grp != NULL) { | 
 | 556 |     return grp; | 
 | 557 |   } | 
 | 558 |   return app_id_to_group(app_id_from_name(name, true), state); | 
 | 559 | } | 
 | 560 |  | 
 | 561 | group* getgrnam(const char* name) { // NOLINT: implementing bad function. | 
 | 562 |   group_state_t* state = __group_state(); | 
 | 563 |   if (state == NULL) { | 
 | 564 |     return NULL; | 
 | 565 |   } | 
 | 566 |   return getgrnam_internal(name, state); | 
 | 567 | } | 
 | 568 |  | 
 | 569 | static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf, | 
 | 570 |                       size_t buflen, struct group** result) { | 
 | 571 |   ErrnoRestorer errno_restorer; | 
 | 572 |   *result = NULL; | 
 | 573 |   char* p = reinterpret_cast<char*>( | 
 | 574 |       BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t))); | 
 | 575 |   if (p + sizeof(group_state_t) > buf + buflen) { | 
 | 576 |     return ERANGE; | 
 | 577 |   } | 
 | 578 |   group_state_t* state = reinterpret_cast<group_state_t*>(p); | 
 | 579 |   init_group_state(state); | 
 | 580 |   group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state)); | 
 | 581 |   if (retval != NULL) { | 
 | 582 |     *grp = *retval; | 
 | 583 |     *result = grp; | 
 | 584 |     return 0; | 
 | 585 |   } | 
 | 586 |   return errno; | 
 | 587 | } | 
 | 588 |  | 
 | 589 | int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) { | 
 | 590 |   return getgroup_r(false, NULL, gid, grp, buf, buflen, result); | 
 | 591 | } | 
 | 592 |  | 
 | 593 | int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen, | 
 | 594 |                struct group **result) { | 
 | 595 |   return getgroup_r(true, name, 0, grp, buf, buflen, result); | 
 | 596 | } | 
| Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 597 |  | 
 | 598 | void setgrent() { | 
 | 599 |   group_state_t* state = g_group_tls_buffer.get(); | 
 | 600 |   if (state) { | 
 | 601 |     state->getgrent_idx = 0; | 
 | 602 |   } | 
 | 603 | } | 
 | 604 |  | 
 | 605 | void endgrent() { | 
 | 606 |   setgrent(); | 
 | 607 | } | 
 | 608 |  | 
 | 609 | group* getgrent() { | 
 | 610 |   group_state_t* state = g_group_tls_buffer.get(); | 
 | 611 |   if (state == NULL) { | 
 | 612 |     return NULL; | 
 | 613 |   } | 
 | 614 |   if (state->getgrent_idx < 0) { | 
 | 615 |     return NULL; | 
 | 616 |   } | 
 | 617 |  | 
 | 618 |   size_t start = 0; | 
 | 619 |   ssize_t end = android_id_count; | 
 | 620 |   if (state->getgrent_idx < end) { | 
 | 621 |     init_group_state(state); | 
 | 622 |     return android_iinfo_to_group(state, android_ids + state->getgrent_idx++); | 
 | 623 |   } | 
 | 624 |  | 
 | 625 |   start = end; | 
 | 626 |   end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; | 
 | 627 |  | 
 | 628 |   if (state->getgrent_idx < end) { | 
 | 629 |     init_group_state(state); | 
 | 630 |     return oem_id_to_group( | 
 | 631 |         state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state); | 
 | 632 |   } | 
 | 633 |  | 
 | 634 |   start = end; | 
 | 635 |   end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_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_2_START, state); | 
 | 641 |   } | 
 | 642 |  | 
 | 643 |   start = end; | 
 | 644 |   end += AID_USER - AID_APP; // Do not expose higher groups | 
 | 645 |  | 
 | 646 |   if (state->getgrent_idx < end) { | 
 | 647 |     init_group_state(state); | 
 | 648 |     return app_id_to_group(state->getgrent_idx++ - start + AID_APP, state); | 
 | 649 |   } | 
 | 650 |  | 
 | 651 |   // We are not reporting u1_a* and higher or we will be here forever | 
 | 652 |   state->getgrent_idx = -1; | 
 | 653 |   return NULL; | 
 | 654 | } |