| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -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 <netdb.h> | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 34 | #include <pthread.h> | 
|  | 35 | #include <pwd.h> | 
|  | 36 | #include <stdio.h> | 
|  | 37 | #include <stdlib.h> | 
| Mark Salyzyn | 56b2768 | 2015-03-31 16:55:42 -0700 | [diff] [blame] | 38 | #include <string.h> | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 39 | #include <unistd.h> | 
|  | 40 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 41 | #include "private/android_filesystem_config.h" | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 42 | #include "private/bionic_macros.h" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 43 | #include "private/ErrnoRestorer.h" | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 44 | #include "private/libc_logging.h" | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 45 | #include "private/ThreadLocalBuffer.h" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 46 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 47 | // POSIX seems to envisage an implementation where the <pwd.h> functions are | 
|  | 48 | // implemented by brute-force searching with getpwent(3), and the <grp.h> | 
|  | 49 | // functions are implemented similarly with getgrent(3). This means that it's | 
|  | 50 | // okay for all the <grp.h> functions to share state, and all the <passwd.h> | 
|  | 51 | // functions to share state, but <grp.h> functions can't clobber <passwd.h> | 
|  | 52 | // functions' state and vice versa. | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 53 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 54 | struct group_state_t { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 55 | group group_; | 
|  | 56 | char* group_members_[2]; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 57 | char group_name_buffer_[32]; | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 58 | }; | 
|  | 59 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 60 | struct passwd_state_t { | 
|  | 61 | passwd passwd_; | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 62 | char name_buffer_[32]; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 63 | char dir_buffer_[32]; | 
|  | 64 | char sh_buffer_[32]; | 
|  | 65 | }; | 
|  | 66 |  | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 67 | static ThreadLocalBuffer<group_state_t> g_group_tls_buffer; | 
|  | 68 | static ThreadLocalBuffer<passwd_state_t> g_passwd_tls_buffer; | 
|  | 69 |  | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 70 | static void init_group_state(group_state_t* state) { | 
|  | 71 | memset(state, 0, sizeof(group_state_t)); | 
|  | 72 | state->group_.gr_mem = state->group_members_; | 
|  | 73 | } | 
|  | 74 |  | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 75 | static group_state_t* __group_state() { | 
|  | 76 | group_state_t* result = g_group_tls_buffer.get(); | 
|  | 77 | if (result != nullptr) { | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 78 | init_group_state(result); | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 79 | } | 
|  | 80 | return result; | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 83 | static int do_getpw_r(int by_name, const char* name, uid_t uid, | 
|  | 84 | passwd* dst, char* buf, size_t byte_count, | 
|  | 85 | passwd** result) { | 
|  | 86 | // getpwnam_r and getpwuid_r don't modify errno, but library calls we | 
|  | 87 | // make might. | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 88 | ErrnoRestorer errno_restorer; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 89 | *result = NULL; | 
|  | 90 |  | 
|  | 91 | // Our implementation of getpwnam(3) and getpwuid(3) use thread-local | 
|  | 92 | // storage, so we can call them as long as we copy everything out | 
|  | 93 | // before returning. | 
|  | 94 | const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above. | 
|  | 95 |  | 
|  | 96 | // POSIX allows failure to find a match to be considered a non-error. | 
|  | 97 | // Reporting success (0) but with *result NULL is glibc's behavior. | 
|  | 98 | if (src == NULL) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 99 | return (errno == ENOENT) ? 0 : errno; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
|  | 102 | // Work out where our strings will go in 'buf', and whether we've got | 
|  | 103 | // enough space. | 
|  | 104 | size_t required_byte_count = 0; | 
|  | 105 | dst->pw_name = buf; | 
|  | 106 | required_byte_count += strlen(src->pw_name) + 1; | 
|  | 107 | dst->pw_dir = buf + required_byte_count; | 
|  | 108 | required_byte_count += strlen(src->pw_dir) + 1; | 
|  | 109 | dst->pw_shell = buf + required_byte_count; | 
|  | 110 | required_byte_count += strlen(src->pw_shell) + 1; | 
|  | 111 | if (byte_count < required_byte_count) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 112 | return ERANGE; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | // Copy the strings. | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 116 | snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 117 |  | 
| Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 118 | // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic. | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 119 | dst->pw_passwd = NULL; | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 120 | #if defined(__LP64__) | 
| Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 121 | dst->pw_gecos = NULL; | 
|  | 122 | #endif | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 123 |  | 
|  | 124 | // Copy the integral fields. | 
|  | 125 | dst->pw_gid = src->pw_gid; | 
|  | 126 | dst->pw_uid = src->pw_uid; | 
|  | 127 |  | 
|  | 128 | *result = dst; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 129 | return 0; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | int getpwnam_r(const char* name, passwd* pwd, | 
|  | 133 | char* buf, size_t byte_count, passwd** result) { | 
|  | 134 | return do_getpw_r(1, name, -1, pwd, buf, byte_count, result); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | int getpwuid_r(uid_t uid, passwd* pwd, | 
|  | 138 | char* buf, size_t byte_count, passwd** result) { | 
|  | 139 | return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result); | 
|  | 140 | } | 
|  | 141 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 142 | static passwd* android_iinfo_to_passwd(passwd_state_t* state, | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 143 | const android_id_info* iinfo) { | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 144 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 145 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); | 
|  | 146 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); | 
|  | 147 |  | 
|  | 148 | passwd* pw = &state->passwd_; | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 149 | pw->pw_name  = state->name_buffer_; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 150 | pw->pw_uid   = iinfo->aid; | 
|  | 151 | pw->pw_gid   = iinfo->aid; | 
|  | 152 | pw->pw_dir   = state->dir_buffer_; | 
|  | 153 | pw->pw_shell = state->sh_buffer_; | 
|  | 154 | return pw; | 
|  | 155 | } | 
|  | 156 |  | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 157 | static group* android_iinfo_to_group(group_state_t* state, | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 158 | const android_id_info* iinfo) { | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 159 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name); | 
|  | 160 |  | 
|  | 161 | group* gr = &state->group_; | 
|  | 162 | gr->gr_name   = state->group_name_buffer_; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 163 | gr->gr_gid    = iinfo->aid; | 
|  | 164 | gr->gr_mem[0] = gr->gr_name; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 165 | return gr; | 
|  | 166 | } | 
|  | 167 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 168 | static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 169 | for (size_t n = 0; n < android_id_count; ++n) { | 
|  | 170 | if (android_ids[n].aid == id) { | 
|  | 171 | return android_iinfo_to_passwd(state, android_ids + n); | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 | return NULL; | 
|  | 175 | } | 
|  | 176 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 177 | static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 178 | for (size_t n = 0; n < android_id_count; ++n) { | 
|  | 179 | if (!strcmp(android_ids[n].name, name)) { | 
|  | 180 | return android_iinfo_to_passwd(state, android_ids + n); | 
|  | 181 | } | 
|  | 182 | } | 
|  | 183 | return NULL; | 
|  | 184 | } | 
|  | 185 |  | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 186 | static group* android_id_to_group(group_state_t* state, unsigned id) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 187 | for (size_t n = 0; n < android_id_count; ++n) { | 
|  | 188 | if (android_ids[n].aid == id) { | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 189 | return android_iinfo_to_group(state, android_ids + n); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 190 | } | 
|  | 191 | } | 
|  | 192 | return NULL; | 
|  | 193 | } | 
|  | 194 |  | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 195 | static group* android_name_to_group(group_state_t* state, const char* name) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 196 | for (size_t n = 0; n < android_id_count; ++n) { | 
|  | 197 | if (!strcmp(android_ids[n].name, name)) { | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 198 | return android_iinfo_to_group(state, android_ids + n); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 199 | } | 
|  | 200 | } | 
|  | 201 | return NULL; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | // Translate a user/group name to the corresponding user/group id. | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 205 | // all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only) | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 206 | // u0_a1234 -> 0 * AID_USER + AID_APP + 1234 | 
|  | 207 | // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000 | 
|  | 208 | // u1_system -> 1 * AID_USER + android_ids['system'] | 
| Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 209 | // returns 0 and sets errno to ENOENT in case of error. | 
| Elliott Hughes | c56af08 | 2015-01-22 11:02:59 -0800 | [diff] [blame] | 210 | static id_t app_id_from_name(const char* name, bool is_group) { | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 211 | char* end; | 
|  | 212 | unsigned long userid; | 
|  | 213 | bool is_shared_gid = false; | 
|  | 214 |  | 
|  | 215 | if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') { | 
|  | 216 | end = const_cast<char*>(name+3); | 
|  | 217 | userid = 0; | 
|  | 218 | is_shared_gid = true; | 
|  | 219 | } else if (name[0] == 'u' && isdigit(name[1])) { | 
|  | 220 | userid = strtoul(name+1, &end, 10); | 
|  | 221 | } else { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 222 | errno = ENOENT; | 
|  | 223 | return 0; | 
|  | 224 | } | 
|  | 225 |  | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 226 | if (end[0] != '_' || end[1] == 0) { | 
|  | 227 | errno = ENOENT; | 
|  | 228 | return 0; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | unsigned long appid = 0; | 
|  | 232 | if (end[1] == 'a' && isdigit(end[2])) { | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 233 | if (is_shared_gid) { | 
|  | 234 | // end will point to \0 if the strtoul below succeeds. | 
|  | 235 | appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START; | 
|  | 236 | if (appid > AID_SHARED_GID_END) { | 
|  | 237 | errno = ENOENT; | 
|  | 238 | return 0; | 
|  | 239 | } | 
|  | 240 | } else { | 
|  | 241 | // end will point to \0 if the strtoul below succeeds. | 
|  | 242 | appid = strtoul(end+2, &end, 10) + AID_APP; | 
|  | 243 | } | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 244 | } else if (end[1] == 'i' && isdigit(end[2])) { | 
|  | 245 | // end will point to \0 if the strtoul below succeeds. | 
|  | 246 | appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; | 
|  | 247 | } else { | 
|  | 248 | for (size_t n = 0; n < android_id_count; n++) { | 
|  | 249 | if (!strcmp(android_ids[n].name, end + 1)) { | 
|  | 250 | appid = android_ids[n].aid; | 
|  | 251 | // Move the end pointer to the null terminator. | 
|  | 252 | end += strlen(android_ids[n].name) + 1; | 
| Yabin Cui | 72a6fdc | 2015-04-18 14:07:41 -0700 | [diff] [blame] | 253 | break; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 254 | } | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | // Check that the entire string was consumed by one of the 3 cases above. | 
|  | 259 | if (end[0] != 0) { | 
|  | 260 | errno = ENOENT; | 
|  | 261 | return 0; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | // Check that user id won't overflow. | 
|  | 265 | if (userid > 1000) { | 
|  | 266 | errno = ENOENT; | 
|  | 267 | return 0; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | // Check that app id is within range. | 
|  | 271 | if (appid >= AID_USER) { | 
|  | 272 | errno = ENOENT; | 
|  | 273 | return 0; | 
|  | 274 | } | 
|  | 275 |  | 
| Elliott Hughes | c56af08 | 2015-01-22 11:02:59 -0800 | [diff] [blame] | 276 | return (appid + userid*AID_USER); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 279 | static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) { | 
|  | 280 | const uid_t appid = uid % AID_USER; | 
|  | 281 | const uid_t userid = uid / AID_USER; | 
| Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 282 | if (appid >= AID_ISOLATED_START) { | 
|  | 283 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); | 
| Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 284 | } else if (appid < AID_APP) { | 
|  | 285 | for (size_t n = 0; n < android_id_count; n++) { | 
|  | 286 | if (android_ids[n].aid == appid) { | 
|  | 287 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); | 
|  | 288 | return; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 289 | } | 
|  | 290 | } | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 291 | } else { | 
| Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 292 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 293 | } | 
|  | 294 | } | 
|  | 295 |  | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 296 | static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) { | 
|  | 297 | const uid_t appid = gid % AID_USER; | 
|  | 298 | const uid_t userid = gid / AID_USER; | 
|  | 299 | if (appid >= AID_ISOLATED_START) { | 
|  | 300 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); | 
|  | 301 | } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) { | 
|  | 302 | snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); | 
|  | 303 | } else if (appid < AID_APP) { | 
|  | 304 | for (size_t n = 0; n < android_id_count; n++) { | 
|  | 305 | if (android_ids[n].aid == appid) { | 
|  | 306 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); | 
|  | 307 | return; | 
|  | 308 | } | 
|  | 309 | } | 
|  | 310 | } else { | 
|  | 311 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); | 
|  | 312 | } | 
| Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 313 | } | 
|  | 314 |  | 
| Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 315 | // Translate an OEM name to the corresponding user/group id. | 
|  | 316 | // oem_XXX -> AID_OEM_RESERVED_2_START + XXX, iff XXX is within range. | 
|  | 317 | static id_t oem_id_from_name(const char* name) { | 
|  | 318 | unsigned int id; | 
|  | 319 | if (sscanf(name, "oem_%u", &id) != 1) { | 
|  | 320 | return 0; | 
|  | 321 | } | 
|  | 322 | // Check OEM id is within range. | 
|  | 323 | if (id > (AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START)) { | 
|  | 324 | return 0; | 
|  | 325 | } | 
|  | 326 | return AID_OEM_RESERVED_2_START + static_cast<id_t>(id); | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { | 
|  | 330 | if (uid < AID_OEM_RESERVED_2_START || uid > AID_OEM_RESERVED_2_END) { | 
|  | 331 | return NULL; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", | 
|  | 335 | uid - AID_OEM_RESERVED_2_START); | 
|  | 336 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); | 
|  | 337 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); | 
|  | 338 |  | 
|  | 339 | passwd* pw = &state->passwd_; | 
|  | 340 | pw->pw_name  = state->name_buffer_; | 
|  | 341 | pw->pw_dir   = state->dir_buffer_; | 
|  | 342 | pw->pw_shell = state->sh_buffer_; | 
|  | 343 | pw->pw_uid   = uid; | 
|  | 344 | pw->pw_gid   = uid; | 
|  | 345 | return pw; | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | static group* oem_id_to_group(gid_t gid, group_state_t* state) { | 
|  | 349 | if (gid < AID_OEM_RESERVED_2_START || gid > AID_OEM_RESERVED_2_END) { | 
|  | 350 | return NULL; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), | 
|  | 354 | "oem_%u", gid - AID_OEM_RESERVED_2_START); | 
|  | 355 |  | 
|  | 356 | group* gr = &state->group_; | 
|  | 357 | gr->gr_name   = state->group_name_buffer_; | 
|  | 358 | gr->gr_gid    = gid; | 
|  | 359 | gr->gr_mem[0] = gr->gr_name; | 
|  | 360 | return gr; | 
|  | 361 | } | 
|  | 362 |  | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 363 | // Translate a uid into the corresponding name. | 
|  | 364 | // 0 to AID_APP-1                   -> "system", "radio", etc. | 
|  | 365 | // AID_APP to AID_ISOLATED_START-1  -> u0_a1234 | 
|  | 366 | // AID_ISOLATED_START to AID_USER-1 -> u0_i1234 | 
|  | 367 | // AID_USER+                        -> u1_radio, u1_a1234, u2_i1234, etc. | 
|  | 368 | // returns a passwd structure (sets errno to ENOENT on failure). | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 369 | static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 370 | if (uid < AID_APP) { | 
|  | 371 | errno = ENOENT; | 
|  | 372 | return NULL; | 
|  | 373 | } | 
|  | 374 |  | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 375 | print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_)); | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 376 |  | 
| Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 377 | const uid_t appid = uid % AID_USER; | 
| Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 378 | if (appid < AID_APP) { | 
|  | 379 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); | 
|  | 380 | } else { | 
|  | 381 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data"); | 
|  | 382 | } | 
|  | 383 |  | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 384 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); | 
|  | 385 |  | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 386 | passwd* pw = &state->passwd_; | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 387 | pw->pw_name  = state->name_buffer_; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 388 | pw->pw_dir   = state->dir_buffer_; | 
|  | 389 | pw->pw_shell = state->sh_buffer_; | 
|  | 390 | pw->pw_uid   = uid; | 
|  | 391 | pw->pw_gid   = uid; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 392 | return pw; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | // Translate a gid into the corresponding app_<gid> | 
|  | 396 | // group structure (sets errno to ENOENT on failure). | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 397 | static group* app_id_to_group(gid_t gid, group_state_t* state) { | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 398 | if (gid < AID_APP) { | 
|  | 399 | errno = ENOENT; | 
|  | 400 | return NULL; | 
|  | 401 | } | 
|  | 402 |  | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 403 | print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_)); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 404 |  | 
|  | 405 | group* gr = &state->group_; | 
|  | 406 | gr->gr_name   = state->group_name_buffer_; | 
|  | 407 | gr->gr_gid    = gid; | 
|  | 408 | gr->gr_mem[0] = gr->gr_name; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 409 | return gr; | 
|  | 410 | } | 
|  | 411 |  | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 412 | passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function. | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 413 | passwd_state_t* state = g_passwd_tls_buffer.get(); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 414 | if (state == NULL) { | 
|  | 415 | return NULL; | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | passwd* pw = android_id_to_passwd(state, uid); | 
|  | 419 | if (pw != NULL) { | 
|  | 420 | return pw; | 
|  | 421 | } | 
| Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 422 | // Handle OEM range. | 
|  | 423 | pw = oem_id_to_passwd(uid, state); | 
|  | 424 | if (pw != NULL) { | 
|  | 425 | return pw; | 
|  | 426 | } | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 427 | return app_id_to_passwd(uid, state); | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 431 | passwd_state_t* state = g_passwd_tls_buffer.get(); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 432 | if (state == NULL) { | 
|  | 433 | return NULL; | 
|  | 434 | } | 
|  | 435 |  | 
|  | 436 | passwd* pw = android_name_to_passwd(state, login); | 
|  | 437 | if (pw != NULL) { | 
|  | 438 | return pw; | 
|  | 439 | } | 
| Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 440 | // Handle OEM range. | 
|  | 441 | pw = oem_id_to_passwd(oem_id_from_name(login), state); | 
|  | 442 | if (pw != NULL) { | 
|  | 443 | return pw; | 
|  | 444 | } | 
| Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 445 | return app_id_to_passwd(app_id_from_name(login, false), state); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Elliott Hughes | 6fa26e2 | 2012-10-22 16:04:56 -0700 | [diff] [blame] | 448 | // All users are in just one group, the one passed in. | 
|  | 449 | int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) { | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 450 | if (*ngroups < 1) { | 
|  | 451 | *ngroups = 1; | 
|  | 452 | return -1; | 
|  | 453 | } | 
|  | 454 | groups[0] = group; | 
|  | 455 | return (*ngroups = 1); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
|  | 458 | char* getlogin() { // NOLINT: implementing bad function. | 
|  | 459 | passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function. | 
|  | 460 | return (pw != NULL) ? pw->pw_name : NULL; | 
|  | 461 | } | 
|  | 462 |  | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 463 | static group* getgrgid_internal(gid_t gid, group_state_t* state) { | 
|  | 464 | group* grp = android_id_to_group(state, gid); | 
|  | 465 | if (grp != NULL) { | 
|  | 466 | return grp; | 
|  | 467 | } | 
| Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 468 | // Handle OEM range. | 
|  | 469 | grp = oem_id_to_group(gid, state); | 
|  | 470 | if (grp != NULL) { | 
|  | 471 | return grp; | 
|  | 472 | } | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 473 | return app_id_to_group(gid, state); | 
|  | 474 | } | 
|  | 475 |  | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 476 | group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 477 | group_state_t* state = __group_state(); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 478 | if (state == NULL) { | 
|  | 479 | return NULL; | 
|  | 480 | } | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 481 | return getgrgid_internal(gid, state); | 
|  | 482 | } | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 483 |  | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 484 | static group* getgrnam_internal(const char* name, group_state_t* state) { | 
|  | 485 | group* grp = android_name_to_group(state, name); | 
|  | 486 | if (grp != NULL) { | 
|  | 487 | return grp; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 488 | } | 
| Jorge Lucangeli Obes | a39e301 | 2015-09-22 11:46:43 -0700 | [diff] [blame] | 489 | // Handle OEM range. | 
|  | 490 | grp = oem_id_to_group(oem_id_from_name(name), state); | 
|  | 491 | if (grp != NULL) { | 
|  | 492 | return grp; | 
|  | 493 | } | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 494 | return app_id_to_group(app_id_from_name(name, true), state); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 495 | } | 
|  | 496 |  | 
|  | 497 | group* getgrnam(const char* name) { // NOLINT: implementing bad function. | 
| Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 498 | group_state_t* state = __group_state(); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 499 | if (state == NULL) { | 
|  | 500 | return NULL; | 
|  | 501 | } | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 502 | return getgrnam_internal(name, state); | 
|  | 503 | } | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 504 |  | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 505 | static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf, | 
|  | 506 | size_t buflen, struct group** result) { | 
|  | 507 | ErrnoRestorer errno_restorer; | 
|  | 508 | *result = NULL; | 
|  | 509 | char* p = reinterpret_cast<char*>( | 
|  | 510 | BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t))); | 
|  | 511 | if (p + sizeof(group_state_t) > buf + buflen) { | 
|  | 512 | return ERANGE; | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 513 | } | 
| Yabin Cui | c4786d3 | 2015-07-20 19:46:26 -0700 | [diff] [blame] | 514 | group_state_t* state = reinterpret_cast<group_state_t*>(p); | 
|  | 515 | init_group_state(state); | 
|  | 516 | group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state)); | 
|  | 517 | if (retval != NULL) { | 
|  | 518 | *grp = *retval; | 
|  | 519 | *result = grp; | 
|  | 520 | return 0; | 
|  | 521 | } | 
|  | 522 | return errno; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) { | 
|  | 526 | return getgroup_r(false, NULL, gid, grp, buf, buflen, result); | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen, | 
|  | 530 | struct group **result) { | 
|  | 531 | return getgroup_r(true, name, 0, grp, buf, buflen, result); | 
| Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 532 | } | 
|  | 533 |  | 
| Elliott Hughes | 6fa26e2 | 2012-10-22 16:04:56 -0700 | [diff] [blame] | 534 | // We don't have an /etc/networks, so all inputs return NULL. | 
|  | 535 | netent* getnetbyname(const char* /*name*/) { | 
|  | 536 | return NULL; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | // We don't have an /etc/networks, so all inputs return NULL. | 
|  | 540 | netent* getnetbyaddr(uint32_t /*net*/, int /*type*/) { | 
|  | 541 | return NULL; | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | // We don't have an /etc/protocols, so all inputs return NULL. | 
|  | 545 | protoent* getprotobyname(const char* /*name*/) { | 
|  | 546 | return NULL; | 
|  | 547 | } | 
|  | 548 |  | 
|  | 549 | // We don't have an /etc/protocols, so all inputs return NULL. | 
|  | 550 | protoent* getprotobynumber(int /*proto*/) { | 
|  | 551 | return NULL; | 
|  | 552 | } | 
|  | 553 |  | 
| Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 554 | // Portable code should use sysconf(_SC_PAGE_SIZE) directly instead. | 
| Bernhard Rosenkraenzer | 9ae59c0 | 2013-09-18 23:29:08 +0200 | [diff] [blame] | 555 | int getpagesize() { | 
| Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 556 | // We dont use sysconf(3) here because that drags in stdio, which makes static binaries fat. | 
|  | 557 | return PAGE_SIZE; | 
| Bernhard Rosenkraenzer | 9ae59c0 | 2013-09-18 23:29:08 +0200 | [diff] [blame] | 558 | } |