Home | History | Annotate | Download | only in bionic
      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>
     34 #include <pthread.h>
     35 #include <pwd.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <unistd.h>
     39 
     40 #include "private/android_filesystem_config.h"
     41 #include "private/ErrnoRestorer.h"
     42 #include "private/libc_logging.h"
     43 
     44 // Thread-specific state for the non-reentrant functions.
     45 static pthread_once_t stubs_once = PTHREAD_ONCE_INIT;
     46 static pthread_key_t stubs_key;
     47 struct stubs_state_t {
     48   passwd passwd_;
     49   group group_;
     50   char* group_members_[2];
     51   char app_name_buffer_[32];
     52   char group_name_buffer_[32];
     53   char dir_buffer_[32];
     54   char sh_buffer_[32];
     55 };
     56 
     57 static int do_getpw_r(int by_name, const char* name, uid_t uid,
     58                       passwd* dst, char* buf, size_t byte_count,
     59                       passwd** result) {
     60   // getpwnam_r and getpwuid_r don't modify errno, but library calls we
     61   // make might.
     62   ErrnoRestorer errno_restorer;
     63   *result = NULL;
     64 
     65   // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
     66   // storage, so we can call them as long as we copy everything out
     67   // before returning.
     68   const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
     69 
     70   // POSIX allows failure to find a match to be considered a non-error.
     71   // Reporting success (0) but with *result NULL is glibc's behavior.
     72   if (src == NULL) {
     73     return (errno == ENOENT) ? 0 : errno;
     74   }
     75 
     76   // Work out where our strings will go in 'buf', and whether we've got
     77   // enough space.
     78   size_t required_byte_count = 0;
     79   dst->pw_name = buf;
     80   required_byte_count += strlen(src->pw_name) + 1;
     81   dst->pw_dir = buf + required_byte_count;
     82   required_byte_count += strlen(src->pw_dir) + 1;
     83   dst->pw_shell = buf + required_byte_count;
     84   required_byte_count += strlen(src->pw_shell) + 1;
     85   if (byte_count < required_byte_count) {
     86     return ERANGE;
     87   }
     88 
     89   // Copy the strings.
     90   snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
     91 
     92   // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic.
     93   dst->pw_passwd = NULL;
     94 #ifdef __LP64__
     95   dst->pw_gecos = NULL;
     96 #endif
     97 
     98   // Copy the integral fields.
     99   dst->pw_gid = src->pw_gid;
    100   dst->pw_uid = src->pw_uid;
    101 
    102   *result = dst;
    103   return 0;
    104 }
    105 
    106 int getpwnam_r(const char* name, passwd* pwd,
    107                char* buf, size_t byte_count, passwd** result) {
    108   return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
    109 }
    110 
    111 int getpwuid_r(uid_t uid, passwd* pwd,
    112                char* buf, size_t byte_count, passwd** result) {
    113   return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
    114 }
    115 
    116 static stubs_state_t* stubs_state_alloc() {
    117   stubs_state_t*  s = static_cast<stubs_state_t*>(calloc(1, sizeof(*s)));
    118   if (s != NULL) {
    119     s->group_.gr_mem = s->group_members_;
    120   }
    121   return s;
    122 }
    123 
    124 static void stubs_state_free(void* ptr) {
    125   stubs_state_t* state = static_cast<stubs_state_t*>(ptr);
    126   free(state);
    127 }
    128 
    129 static void __stubs_key_init() {
    130   pthread_key_create(&stubs_key, stubs_state_free);
    131 }
    132 
    133 static stubs_state_t* __stubs_state() {
    134   pthread_once(&stubs_once, __stubs_key_init);
    135   stubs_state_t* s = static_cast<stubs_state_t*>(pthread_getspecific(stubs_key));
    136   if (s == NULL) {
    137     s = stubs_state_alloc();
    138     if (s == NULL) {
    139       errno = ENOMEM;  // Just in case.
    140     } else {
    141       if (pthread_setspecific(stubs_key, s) != 0) {
    142         stubs_state_free(s);
    143         errno = ENOMEM;
    144         s = NULL;
    145       }
    146     }
    147   }
    148   return s;
    149 }
    150 
    151 static passwd* android_iinfo_to_passwd(stubs_state_t* state,
    152                                        const android_id_info* iinfo) {
    153   snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
    154   snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
    155 
    156   passwd* pw = &state->passwd_;
    157   pw->pw_name  = (char*) iinfo->name;
    158   pw->pw_uid   = iinfo->aid;
    159   pw->pw_gid   = iinfo->aid;
    160   pw->pw_dir   = state->dir_buffer_;
    161   pw->pw_shell = state->sh_buffer_;
    162   return pw;
    163 }
    164 
    165 static group* android_iinfo_to_group(group* gr,
    166                                      const android_id_info* iinfo) {
    167   gr->gr_name   = (char*) iinfo->name;
    168   gr->gr_gid    = iinfo->aid;
    169   gr->gr_mem[0] = gr->gr_name;
    170   gr->gr_mem[1] = NULL;
    171   return gr;
    172 }
    173 
    174 static passwd* android_id_to_passwd(stubs_state_t* state, unsigned id) {
    175   for (size_t n = 0; n < android_id_count; ++n) {
    176     if (android_ids[n].aid == id) {
    177       return android_iinfo_to_passwd(state, android_ids + n);
    178     }
    179   }
    180   return NULL;
    181 }
    182 
    183 static passwd* android_name_to_passwd(stubs_state_t* state, const char* name) {
    184   for (size_t n = 0; n < android_id_count; ++n) {
    185     if (!strcmp(android_ids[n].name, name)) {
    186       return android_iinfo_to_passwd(state, android_ids + n);
    187     }
    188   }
    189   return NULL;
    190 }
    191 
    192 static group* android_id_to_group(group* gr, unsigned id) {
    193   for (size_t n = 0; n < android_id_count; ++n) {
    194     if (android_ids[n].aid == id) {
    195       return android_iinfo_to_group(gr, android_ids + n);
    196     }
    197   }
    198   return NULL;
    199 }
    200 
    201 static group* android_name_to_group(group* gr, const char* name) {
    202   for (size_t n = 0; n < android_id_count; ++n) {
    203     if (!strcmp(android_ids[n].name, name)) {
    204       return android_iinfo_to_group(gr, android_ids + n);
    205     }
    206   }
    207   return NULL;
    208 }
    209 
    210 // Translate a user/group name to the corresponding user/group id.
    211 // u0_a1234 -> 0 * AID_USER + AID_APP + 1234
    212 // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
    213 // u1_system -> 1 * AID_USER + android_ids['system']
    214 // returns 0 and sets errno to ENOENT in case of error
    215 static unsigned app_id_from_name(const char* name) {
    216   if (name[0] != 'u' || !isdigit(name[1])) {
    217     errno = ENOENT;
    218     return 0;
    219   }
    220 
    221   char* end;
    222   unsigned long userid = strtoul(name+1, &end, 10);
    223   if (end[0] != '_' || end[1] == 0) {
    224     errno = ENOENT;
    225     return 0;
    226   }
    227 
    228   unsigned long appid = 0;
    229   if (end[1] == 'a' && isdigit(end[2])) {
    230     // end will point to \0 if the strtoul below succeeds.
    231     appid = strtoul(end+2, &end, 10) + AID_APP;
    232   } else if (end[1] == 'i' && isdigit(end[2])) {
    233     // end will point to \0 if the strtoul below succeeds.
    234     appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
    235   } else {
    236     for (size_t n = 0; n < android_id_count; n++) {
    237       if (!strcmp(android_ids[n].name, end + 1)) {
    238         appid = android_ids[n].aid;
    239         // Move the end pointer to the null terminator.
    240         end += strlen(android_ids[n].name) + 1;
    241       }
    242     }
    243   }
    244 
    245   // Check that the entire string was consumed by one of the 3 cases above.
    246   if (end[0] != 0) {
    247     errno = ENOENT;
    248     return 0;
    249   }
    250 
    251   // Check that user id won't overflow.
    252   if (userid > 1000) {
    253     errno = ENOENT;
    254     return 0;
    255   }
    256 
    257   // Check that app id is within range.
    258   if (appid >= AID_USER) {
    259     errno = ENOENT;
    260     return 0;
    261   }
    262 
    263   return (unsigned)(appid + userid*AID_USER);
    264 }
    265 
    266 static void print_app_name_from_appid_userid(const uid_t appid,
    267     const uid_t userid, char* buffer, const int bufferlen) {
    268   if (appid >= AID_ISOLATED_START) {
    269     snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
    270   } else if (userid == 0 && appid >= AID_SHARED_GID_START) {
    271     snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
    272   } else if (appid < AID_APP) {
    273     for (size_t n = 0; n < android_id_count; n++) {
    274       if (android_ids[n].aid == appid) {
    275         snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
    276         return;
    277       }
    278     }
    279   } else {
    280     snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
    281   }
    282 }
    283 
    284 static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
    285   const uid_t appid = uid % AID_USER;
    286   const uid_t userid = uid / AID_USER;
    287   return print_app_name_from_appid_userid(appid, userid, buffer, bufferlen);
    288 }
    289 
    290 // Translate a uid into the corresponding name.
    291 // 0 to AID_APP-1                   -> "system", "radio", etc.
    292 // AID_APP to AID_ISOLATED_START-1  -> u0_a1234
    293 // AID_ISOLATED_START to AID_USER-1 -> u0_i1234
    294 // AID_USER+                        -> u1_radio, u1_a1234, u2_i1234, etc.
    295 // returns a passwd structure (sets errno to ENOENT on failure).
    296 static passwd* app_id_to_passwd(uid_t uid, stubs_state_t* state) {
    297   passwd* pw = &state->passwd_;
    298 
    299   if (uid < AID_APP) {
    300     errno = ENOENT;
    301     return NULL;
    302   }
    303 
    304   const uid_t appid = uid % AID_USER;
    305   const uid_t userid = uid / AID_USER;
    306 
    307   print_app_name_from_appid_userid(appid, userid, state->app_name_buffer_,
    308                                    sizeof(state->app_name_buffer_));
    309 
    310   if (appid < AID_APP) {
    311       snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
    312   } else {
    313       snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
    314   }
    315 
    316   snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
    317 
    318   pw->pw_name  = state->app_name_buffer_;
    319   pw->pw_dir   = state->dir_buffer_;
    320   pw->pw_shell = state->sh_buffer_;
    321   pw->pw_uid   = uid;
    322   pw->pw_gid   = uid;
    323 
    324   return pw;
    325 }
    326 
    327 // Translate a gid into the corresponding app_<gid>
    328 // group structure (sets errno to ENOENT on failure).
    329 static group* app_id_to_group(gid_t gid, stubs_state_t* state) {
    330   if (gid < AID_APP) {
    331     errno = ENOENT;
    332     return NULL;
    333   }
    334 
    335   print_app_name_from_uid(gid, state->group_name_buffer_,
    336                           sizeof(state->group_name_buffer_));
    337 
    338   group* gr = &state->group_;
    339   gr->gr_name   = state->group_name_buffer_;
    340   gr->gr_gid    = gid;
    341   gr->gr_mem[0] = gr->gr_name;
    342   gr->gr_mem[1] = NULL;
    343   return gr;
    344 }
    345 
    346 
    347 passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
    348   stubs_state_t* state = __stubs_state();
    349   if (state == NULL) {
    350     return NULL;
    351   }
    352 
    353   passwd* pw = android_id_to_passwd(state, uid);
    354   if (pw != NULL) {
    355     return pw;
    356   }
    357   return app_id_to_passwd(uid, state);
    358 }
    359 
    360 passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
    361   stubs_state_t* state = __stubs_state();
    362   if (state == NULL) {
    363     return NULL;
    364   }
    365 
    366   passwd* pw = android_name_to_passwd(state, login);
    367   if (pw != NULL) {
    368     return pw;
    369   }
    370   return app_id_to_passwd(app_id_from_name(login), state);
    371 }
    372 
    373 // All users are in just one group, the one passed in.
    374 int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
    375     if (*ngroups < 1) {
    376         *ngroups = 1;
    377         return -1;
    378     }
    379     groups[0] = group;
    380     return (*ngroups = 1);
    381 }
    382 
    383 char* getlogin() { // NOLINT: implementing bad function.
    384   passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
    385   return (pw != NULL) ? pw->pw_name : NULL;
    386 }
    387 
    388 group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
    389   stubs_state_t* state = __stubs_state();
    390   if (state == NULL) {
    391     return NULL;
    392   }
    393 
    394   group* gr = android_id_to_group(&state->group_, gid);
    395   if (gr != NULL) {
    396     return gr;
    397   }
    398 
    399   return app_id_to_group(gid, state);
    400 }
    401 
    402 group* getgrnam(const char* name) { // NOLINT: implementing bad function.
    403   stubs_state_t* state = __stubs_state();
    404   if (state == NULL) {
    405     return NULL;
    406   }
    407 
    408   if (android_name_to_group(&state->group_, name) != 0) {
    409     return &state->group_;
    410   }
    411 
    412   return app_id_to_group(app_id_from_name(name), state);
    413 }
    414 
    415 // We don't have an /etc/networks, so all inputs return NULL.
    416 netent* getnetbyname(const char* /*name*/) {
    417   return NULL;
    418 }
    419 
    420 // We don't have an /etc/networks, so all inputs return NULL.
    421 netent* getnetbyaddr(uint32_t /*net*/, int /*type*/) {
    422   return NULL;
    423 }
    424 
    425 // We don't have an /etc/protocols, so all inputs return NULL.
    426 protoent* getprotobyname(const char* /*name*/) {
    427   return NULL;
    428 }
    429 
    430 // We don't have an /etc/protocols, so all inputs return NULL.
    431 protoent* getprotobynumber(int /*proto*/) {
    432   return NULL;
    433 }
    434 
    435 static void unimplemented_stub(const char* function) {
    436   const char* fmt = "%s(3) is not implemented on Android\n";
    437   __libc_format_log(ANDROID_LOG_WARN, "libc", fmt, function);
    438   fprintf(stderr, fmt, function);
    439 }
    440 
    441 #define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
    442 
    443 void endpwent() {
    444   UNIMPLEMENTED;
    445 }
    446 
    447 char* getusershell() {
    448   UNIMPLEMENTED;
    449   return NULL;
    450 }
    451 
    452 void setusershell() {
    453   UNIMPLEMENTED;
    454 }
    455 
    456 void endusershell() {
    457   UNIMPLEMENTED;
    458 }
    459 
    460 // Portable code should use sysconf(_SC_PAGE_SIZE) directly instead.
    461 int getpagesize() {
    462   // We dont use sysconf(3) here because that drags in stdio, which makes static binaries fat.
    463   return PAGE_SIZE;
    464 }
    465