| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 1 | /* | 
 | 2 | ** | 
 | 3 | ** Copyright 2010, The Android Open Source Project | 
 | 4 | ** | 
 | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 6 | ** you may not use this file except in compliance with the License. | 
 | 7 | ** You may obtain a copy of the License at | 
 | 8 | ** | 
 | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 10 | ** | 
 | 11 | ** Unless required by applicable law or agreed to in writing, software | 
 | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
 | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 14 | ** See the License for the specific language governing permissions and | 
 | 15 | ** limitations under the License. | 
 | 16 | */ | 
 | 17 |  | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 18 | #define PROGNAME "run-as" | 
 | 19 | #define LOG_TAG  PROGNAME | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 20 |  | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 21 | #include <dirent.h> | 
 | 22 | #include <errno.h> | 
| Elliott Hughes | a372f6f | 2015-11-03 14:31:46 -0800 | [diff] [blame] | 23 | #include <paths.h> | 
 | 24 | #include <pwd.h> | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 25 | #include <stdarg.h> | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 26 | #include <stdio.h> | 
 | 27 | #include <stdlib.h> | 
 | 28 | #include <string.h> | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 29 | #include <sys/capability.h> | 
 | 30 | #include <sys/cdefs.h> | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 32 | #include <sys/types.h> | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 33 | #include <time.h> | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 34 | #include <unistd.h> | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 35 |  | 
 | 36 | #include <private/android_filesystem_config.h> | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 37 | #include <selinux/android.h> | 
 | 38 |  | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 39 | #include "package.h" | 
 | 40 |  | 
 | 41 | /* | 
 | 42 |  *  WARNING WARNING WARNING WARNING | 
 | 43 |  * | 
| Alex Klyubin | b0739c6 | 2013-08-05 13:03:58 -0700 | [diff] [blame] | 44 |  *  This program runs with CAP_SETUID and CAP_SETGID capabilities on Android | 
 | 45 |  *  production devices. Be very conservative when modifying it to avoid any | 
 | 46 |  *  serious security issue. Keep in mind the following: | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 47 |  * | 
 | 48 |  *  - This program should only run for the 'root' or 'shell' users | 
 | 49 |  * | 
| Nick Kralevich | b2d8f89 | 2012-01-23 11:00:36 -0800 | [diff] [blame] | 50 |  *  - Avoid anything that is more complex than simple system calls | 
 | 51 |  *    until the uid/gid has been dropped to that of a normal user | 
 | 52 |  *    or you are sure to exit. | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 53 |  * | 
 | 54 |  *    This avoids depending on environment variables, system properties | 
 | 55 |  *    and other external factors that may affect the C library in | 
 | 56 |  *    unpredictable ways. | 
 | 57 |  * | 
 | 58 |  *  - Do not trust user input and/or the filesystem whenever possible. | 
 | 59 |  * | 
 | 60 |  *  Read README.TXT for more details. | 
 | 61 |  * | 
 | 62 |  * | 
 | 63 |  * | 
 | 64 |  * The purpose of this program is to run a command as a specific | 
 | 65 |  * application user-id. Typical usage is: | 
 | 66 |  * | 
 | 67 |  *   run-as <package-name> <command> <args> | 
 | 68 |  * | 
| Alex Klyubin | b0739c6 | 2013-08-05 13:03:58 -0700 | [diff] [blame] | 69 |  *  The 'run-as' binary is installed with CAP_SETUID and CAP_SETGID file | 
 | 70 |  *  capabilities, but will check the following: | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 71 |  * | 
 | 72 |  *  - that it is invoked from the 'shell' or 'root' user (abort otherwise) | 
 | 73 |  *  - that '<package-name>' is the name of an installed and debuggable package | 
 | 74 |  *  - that the package's data directory is well-formed (see package.c) | 
 | 75 |  * | 
| Alex Klyubin | b0739c6 | 2013-08-05 13:03:58 -0700 | [diff] [blame] | 76 |  *  If so, it will drop to the application's user id / group id, cd to the | 
 | 77 |  *  package's data directory, then run the command there. | 
 | 78 |  * | 
 | 79 |  *  NOTE: In the future it might not be possible to cd to the package's data | 
 | 80 |  *  directory under that package's user id / group id, in which case this | 
 | 81 |  *  utility will need to be changed accordingly. | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 82 |  * | 
 | 83 |  *  This can be useful for a number of different things on production devices: | 
 | 84 |  * | 
 | 85 |  *  - Allow application developers to look at their own applicative data | 
 | 86 |  *    during development. | 
 | 87 |  * | 
 | 88 |  *  - Run the 'gdbserver' binary executable to allow native debugging | 
 | 89 |  */ | 
 | 90 |  | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 91 | __noreturn static void | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 92 | panic(const char* format, ...) | 
 | 93 | { | 
 | 94 |     va_list args; | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 95 |     int e = errno; | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 96 |  | 
 | 97 |     fprintf(stderr, "%s: ", PROGNAME); | 
 | 98 |     va_start(args, format); | 
 | 99 |     vfprintf(stderr, format, args); | 
 | 100 |     va_end(args); | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 101 |     exit(e ? -e : 1); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 102 | } | 
 | 103 |  | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 104 | static void | 
 | 105 | usage(void) | 
 | 106 | { | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 107 |     panic("Usage:\n    " PROGNAME " <package-name> [--user <uid>] <command> [<args>]\n"); | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 108 | } | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 109 |  | 
 | 110 | int main(int argc, char **argv) | 
 | 111 | { | 
 | 112 |     const char* pkgname; | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 113 |     uid_t myuid, uid, gid, userAppId = 0; | 
 | 114 |     int commandArgvOfs = 2, userId = 0; | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 115 |     PackageInfo info; | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 116 |     struct __user_cap_header_struct capheader; | 
 | 117 |     struct __user_cap_data_struct capdata[2]; | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 118 |  | 
 | 119 |     /* check arguments */ | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 120 |     if (argc < 2) { | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 121 |         usage(); | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 122 |     } | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 123 |  | 
 | 124 |     /* check userid of caller - must be 'shell' or 'root' */ | 
 | 125 |     myuid = getuid(); | 
 | 126 |     if (myuid != AID_SHELL && myuid != AID_ROOT) { | 
 | 127 |         panic("only 'shell' or 'root' users can run this program\n"); | 
 | 128 |     } | 
 | 129 |  | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 130 |     memset(&capheader, 0, sizeof(capheader)); | 
 | 131 |     memset(&capdata, 0, sizeof(capdata)); | 
 | 132 |     capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
 | 133 |     capdata[CAP_TO_INDEX(CAP_SETUID)].effective |= CAP_TO_MASK(CAP_SETUID); | 
 | 134 |     capdata[CAP_TO_INDEX(CAP_SETGID)].effective |= CAP_TO_MASK(CAP_SETGID); | 
 | 135 |     capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID); | 
 | 136 |     capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID); | 
 | 137 |  | 
 | 138 |     if (capset(&capheader, &capdata[0]) < 0) { | 
 | 139 |         panic("Could not set capabilities: %s\n", strerror(errno)); | 
 | 140 |     } | 
 | 141 |  | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 142 |     pkgname = argv[1]; | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 143 |  | 
 | 144 |     /* get user_id from command line if provided */ | 
 | 145 |     if ((argc >= 4) && !strcmp(argv[2], "--user")) { | 
 | 146 |         userId = atoi(argv[3]); | 
 | 147 |         if (userId < 0) | 
 | 148 |             panic("Negative user id %d is provided\n", userId); | 
 | 149 |         commandArgvOfs += 2; | 
 | 150 |     } | 
 | 151 |  | 
 | 152 |     /* retrieve package information from system (does setegid) */ | 
 | 153 |     if (get_package_info(pkgname, userId, &info) < 0) { | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 154 |         panic("Package '%s' is unknown\n", pkgname); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 155 |     } | 
 | 156 |  | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 157 |     /* verify that user id is not too big. */ | 
 | 158 |     if ((UID_MAX - info.uid) / AID_USER < (uid_t)userId) { | 
 | 159 |         panic("User id %d is too big\n", userId); | 
 | 160 |     } | 
 | 161 |  | 
 | 162 |     /* calculate user app ID. */ | 
 | 163 |     userAppId = (AID_USER * userId) + info.uid; | 
 | 164 |  | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 165 |     /* reject system packages */ | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 166 |     if (userAppId < AID_APP) { | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 167 |         panic("Package '%s' is not an application\n", pkgname); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 168 |     } | 
 | 169 |  | 
 | 170 |     /* reject any non-debuggable package */ | 
 | 171 |     if (!info.isDebuggable) { | 
 | 172 |         panic("Package '%s' is not debuggable\n", pkgname); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 173 |     } | 
 | 174 |  | 
 | 175 |     /* check that the data directory path is valid */ | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 176 |     if (check_data_path(info.dataDir, userAppId) < 0) { | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 177 |         panic("Package '%s' has corrupt installation\n", pkgname); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 178 |     } | 
 | 179 |  | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 180 |     /* Ensure that we change all real/effective/saved IDs at the | 
 | 181 |      * same time to avoid nasty surprises. | 
 | 182 |      */ | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 183 |     uid = gid = userAppId; | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 184 |     if(setresgid(gid,gid,gid) || setresuid(uid,uid,uid)) { | 
 | 185 |         panic("Permission denied\n"); | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 186 |     } | 
 | 187 |  | 
 | 188 |     /* Required if caller has uid and gid all non-zero */ | 
 | 189 |     memset(&capdata, 0, sizeof(capdata)); | 
 | 190 |     if (capset(&capheader, &capdata[0]) < 0) { | 
 | 191 |         panic("Could not clear all capabilities: %s\n", strerror(errno)); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 192 |     } | 
 | 193 |  | 
| Robert Craig | fced3de | 2013-03-26 08:09:09 -0400 | [diff] [blame] | 194 |     if (selinux_android_setcontext(uid, 0, info.seinfo, pkgname) < 0) { | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 195 |         panic("Could not set SELinux security context: %s\n", strerror(errno)); | 
| Stephen Smalley | 4ead8be | 2012-11-13 12:56:48 -0500 | [diff] [blame] | 196 |     } | 
 | 197 |  | 
| Elliott Hughes | a372f6f | 2015-11-03 14:31:46 -0800 | [diff] [blame] | 198 |     // cd into the data directory, and set $HOME correspondingly. | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 199 |     if (TEMP_FAILURE_RETRY(chdir(info.dataDir)) < 0) { | 
 | 200 |         panic("Could not cd to package's data directory: %s\n", strerror(errno)); | 
| Alex Klyubin | b0739c6 | 2013-08-05 13:03:58 -0700 | [diff] [blame] | 201 |     } | 
| Elliott Hughes | a372f6f | 2015-11-03 14:31:46 -0800 | [diff] [blame] | 202 |     setenv("HOME", info.dataDir, 1); | 
 | 203 |  | 
 | 204 |     // Reset parts of the environment, like su would. | 
 | 205 |     setenv("PATH", _PATH_DEFPATH, 1); | 
 | 206 |     unsetenv("IFS"); | 
 | 207 |  | 
 | 208 |     // Set the user-specific parts for this user. | 
 | 209 |     struct passwd* pw = getpwuid(uid); | 
 | 210 |     setenv("LOGNAME", pw->pw_name, 1); | 
 | 211 |     setenv("SHELL", pw->pw_shell, 1); | 
 | 212 |     setenv("USER", pw->pw_name, 1); | 
| Alex Klyubin | b0739c6 | 2013-08-05 13:03:58 -0700 | [diff] [blame] | 213 |  | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 214 |     /* User specified command for exec. */ | 
| Oleksiy Vyalov | a08d313 | 2015-06-03 16:56:29 -0700 | [diff] [blame] | 215 |     if ((argc >= commandArgvOfs + 1) && | 
 | 216 |         (execvp(argv[commandArgvOfs], argv+commandArgvOfs) < 0)) { | 
 | 217 |         panic("exec failed for %s: %s\n", argv[commandArgvOfs], strerror(errno)); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 218 |     } | 
 | 219 |  | 
 | 220 |     /* Default exec shell. */ | 
 | 221 |     execlp("/system/bin/sh", "sh", NULL); | 
 | 222 |  | 
| Mark Salyzyn | db5334a | 2015-03-25 16:11:53 -0700 | [diff] [blame] | 223 |     panic("exec failed: %s\n", strerror(errno)); | 
| David 'Digit' Turner | 1f4d952 | 2010-03-02 18:05:23 -0800 | [diff] [blame] | 224 | } |