| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <errno.h> | 
|  | 18 | #include <error.h> | 
|  | 19 | #include <paths.h> | 
|  | 20 | #include <pwd.h> | 
|  | 21 | #include <stdlib.h> | 
|  | 22 | #include <string.h> | 
|  | 23 | #include <sys/capability.h> | 
|  | 24 | #include <sys/stat.h> | 
|  | 25 | #include <sys/types.h> | 
|  | 26 | #include <unistd.h> | 
|  | 27 |  | 
| Jorge Lucangeli Obes | a377ff0 | 2016-07-15 11:24:20 -0400 | [diff] [blame] | 28 | #include <libminijail.h> | 
|  | 29 | #include <scoped_minijail.h> | 
|  | 30 |  | 
| Luis Hector Chavez | ef62f3f | 2018-06-27 10:40:10 -0700 | [diff] [blame] | 31 | #include <android-base/properties.h> | 
| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 32 | #include <packagelistparser/packagelistparser.h> | 
|  | 33 | #include <private/android_filesystem_config.h> | 
|  | 34 | #include <selinux/android.h> | 
|  | 35 |  | 
|  | 36 | // The purpose of this program is to run a command as a specific | 
|  | 37 | // application user-id. Typical usage is: | 
|  | 38 | // | 
|  | 39 | //   run-as <package-name> <command> <args> | 
|  | 40 | // | 
|  | 41 | //  The 'run-as' binary is installed with CAP_SETUID and CAP_SETGID file | 
|  | 42 | //  capabilities, but will check the following: | 
|  | 43 | // | 
| Luis Hector Chavez | ef62f3f | 2018-06-27 10:40:10 -0700 | [diff] [blame] | 44 | //  - that the ro.boot.disable_runas property is not set | 
| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 45 | //  - that it is invoked from the 'shell' or 'root' user (abort otherwise) | 
|  | 46 | //  - that '<package-name>' is the name of an installed and debuggable package | 
|  | 47 | //  - that the package's data directory is well-formed | 
|  | 48 | // | 
|  | 49 | //  If so, it will drop to the application's user id / group id, cd to the | 
|  | 50 | //  package's data directory, then run the command there. | 
|  | 51 | // | 
|  | 52 | //  This can be useful for a number of different things on production devices: | 
|  | 53 | // | 
|  | 54 | //  - Allow application developers to look at their own application data | 
|  | 55 | //    during development. | 
|  | 56 | // | 
|  | 57 | //  - Run the 'gdbserver' binary executable to allow native debugging | 
|  | 58 | // | 
|  | 59 |  | 
|  | 60 | static bool packagelist_parse_callback(pkg_info* this_package, void* userdata) { | 
|  | 61 | pkg_info* p = reinterpret_cast<pkg_info*>(userdata); | 
|  | 62 | if (strcmp(p->name, this_package->name) == 0) { | 
|  | 63 | *p = *this_package; | 
|  | 64 | return false; // Stop searching. | 
|  | 65 | } | 
|  | 66 | packagelist_free(this_package); | 
|  | 67 | return true; // Keep searching. | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | static bool check_directory(const char* path, uid_t uid) { | 
|  | 71 | struct stat st; | 
|  | 72 | if (TEMP_FAILURE_RETRY(lstat(path, &st)) == -1) return false; | 
|  | 73 |  | 
|  | 74 | // /data/user/0 is a known safe symlink. | 
|  | 75 | if (strcmp("/data/user/0", path) == 0) return true; | 
|  | 76 |  | 
|  | 77 | // Must be a real directory, not a symlink. | 
|  | 78 | if (!S_ISDIR(st.st_mode)) return false; | 
|  | 79 |  | 
|  | 80 | // Must be owned by specific uid/gid. | 
|  | 81 | if (st.st_uid != uid || st.st_gid != uid) return false; | 
|  | 82 |  | 
|  | 83 | // Must not be readable or writable by others. | 
|  | 84 | if ((st.st_mode & (S_IROTH|S_IWOTH)) != 0) return false; | 
|  | 85 |  | 
|  | 86 | return true; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | // This function is used to check the data directory path for safety. | 
|  | 90 | // We check that every sub-directory is owned by the 'system' user | 
|  | 91 | // and exists and is not a symlink. We also check that the full directory | 
|  | 92 | // path is properly owned by the user ID. | 
|  | 93 | static bool check_data_path(const char* data_path, uid_t uid) { | 
|  | 94 | // The path should be absolute. | 
|  | 95 | if (data_path[0] != '/') return false; | 
|  | 96 |  | 
|  | 97 | // Look for all sub-paths, we do that by finding | 
|  | 98 | // directory separators in the input path and | 
|  | 99 | // checking each sub-path independently. | 
|  | 100 | for (int nn = 1; data_path[nn] != '\0'; nn++) { | 
|  | 101 | char subpath[PATH_MAX]; | 
|  | 102 |  | 
|  | 103 | /* skip non-separator characters */ | 
|  | 104 | if (data_path[nn] != '/') continue; | 
|  | 105 |  | 
|  | 106 | /* handle trailing separator case */ | 
|  | 107 | if (data_path[nn+1] == '\0') break; | 
|  | 108 |  | 
|  | 109 | /* found a separator, check that data_path is not too long. */ | 
|  | 110 | if (nn >= (int)(sizeof subpath)) return false; | 
|  | 111 |  | 
|  | 112 | /* reject any '..' subpath */ | 
|  | 113 | if (nn >= 3               && | 
|  | 114 | data_path[nn-3] == '/' && | 
|  | 115 | data_path[nn-2] == '.' && | 
|  | 116 | data_path[nn-1] == '.') { | 
|  | 117 | return false; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | /* copy to 'subpath', then check ownership */ | 
|  | 121 | memcpy(subpath, data_path, nn); | 
|  | 122 | subpath[nn] = '\0'; | 
|  | 123 |  | 
|  | 124 | if (!check_directory(subpath, AID_SYSTEM)) return false; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | // All sub-paths were checked, now verify that the full data | 
|  | 128 | // directory is owned by the application uid. | 
|  | 129 | return check_directory(data_path, uid); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | int main(int argc, char* argv[]) { | 
|  | 133 | // Check arguments. | 
|  | 134 | if (argc < 2) { | 
|  | 135 | error(1, 0, "usage: run-as <package-name> [--user <uid>] <command> [<args>]\n"); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | // This program runs with CAP_SETUID and CAP_SETGID capabilities on Android | 
|  | 139 | // production devices. Check user id of caller --- must be 'shell' or 'root'. | 
|  | 140 | if (getuid() != AID_SHELL && getuid() != AID_ROOT) { | 
|  | 141 | error(1, 0, "only 'shell' or 'root' users can run this program"); | 
|  | 142 | } | 
|  | 143 |  | 
| Luis Hector Chavez | ef62f3f | 2018-06-27 10:40:10 -0700 | [diff] [blame] | 144 | // Some devices can disable running run-as, such as Chrome OS when running in | 
|  | 145 | // non-developer mode. | 
|  | 146 | if (android::base::GetBoolProperty("ro.boot.disable_runas", false)) { | 
|  | 147 | error(1, 0, "run-as is disabled from the kernel commandline"); | 
|  | 148 | } | 
|  | 149 |  | 
| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 150 | char* pkgname = argv[1]; | 
|  | 151 | int cmd_argv_offset = 2; | 
|  | 152 |  | 
|  | 153 | // Get user_id from command line if provided. | 
|  | 154 | int userId = 0; | 
|  | 155 | if ((argc >= 4) && !strcmp(argv[2], "--user")) { | 
|  | 156 | userId = atoi(argv[3]); | 
|  | 157 | if (userId < 0) error(1, 0, "negative user id: %d", userId); | 
|  | 158 | cmd_argv_offset += 2; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | // Retrieve package information from system, switching egid so we can read the file. | 
|  | 162 | gid_t old_egid = getegid(); | 
|  | 163 | if (setegid(AID_PACKAGE_INFO) == -1) error(1, errno, "setegid(AID_PACKAGE_INFO) failed"); | 
|  | 164 | pkg_info info; | 
|  | 165 | memset(&info, 0, sizeof(info)); | 
|  | 166 | info.name = pkgname; | 
|  | 167 | if (!packagelist_parse(packagelist_parse_callback, &info)) { | 
|  | 168 | error(1, errno, "packagelist_parse failed"); | 
|  | 169 | } | 
|  | 170 | if (info.uid == 0) { | 
|  | 171 | error(1, 0, "unknown package: %s", pkgname); | 
|  | 172 | } | 
|  | 173 | if (setegid(old_egid) == -1) error(1, errno, "couldn't restore egid"); | 
|  | 174 |  | 
|  | 175 | // Verify that user id is not too big. | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 176 | if ((UID_MAX - info.uid) / AID_USER_OFFSET < (uid_t)userId) { | 
| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 177 | error(1, 0, "user id too big: %d", userId); | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | // Calculate user app ID. | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 181 | uid_t userAppId = (AID_USER_OFFSET * userId) + info.uid; | 
| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 182 |  | 
|  | 183 | // Reject system packages. | 
|  | 184 | if (userAppId < AID_APP) { | 
|  | 185 | error(1, 0, "package not an application: %s", pkgname); | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | // Reject any non-debuggable package. | 
|  | 189 | if (!info.debuggable) { | 
|  | 190 | error(1, 0, "package not debuggable: %s", pkgname); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | // Check that the data directory path is valid. | 
|  | 194 | if (!check_data_path(info.data_dir, userAppId)) { | 
|  | 195 | error(1, 0, "package has corrupt installation: %s", pkgname); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | // Ensure that we change all real/effective/saved IDs at the | 
|  | 199 | // same time to avoid nasty surprises. | 
|  | 200 | uid_t uid = userAppId; | 
|  | 201 | uid_t gid = userAppId; | 
| Jorge Lucangeli Obes | a377ff0 | 2016-07-15 11:24:20 -0400 | [diff] [blame] | 202 | ScopedMinijail j(minijail_new()); | 
|  | 203 | minijail_change_uid(j.get(), uid); | 
|  | 204 | minijail_change_gid(j.get(), gid); | 
| Jorge Lucangeli Obes | 8c27e19 | 2017-09-29 14:56:07 -0400 | [diff] [blame] | 205 | minijail_keep_supplementary_gids(j.get()); | 
| Jorge Lucangeli Obes | a377ff0 | 2016-07-15 11:24:20 -0400 | [diff] [blame] | 206 | minijail_enter(j.get()); | 
| Elliott Hughes | 0c8bf57 | 2016-07-07 16:22:19 -0700 | [diff] [blame] | 207 |  | 
|  | 208 | if (selinux_android_setcontext(uid, 0, info.seinfo, pkgname) < 0) { | 
|  | 209 | error(1, errno, "couldn't set SELinux security context"); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | // cd into the data directory, and set $HOME correspondingly. | 
|  | 213 | if (TEMP_FAILURE_RETRY(chdir(info.data_dir)) == -1) { | 
|  | 214 | error(1, errno, "couldn't chdir to package's data directory"); | 
|  | 215 | } | 
|  | 216 | setenv("HOME", info.data_dir, 1); | 
|  | 217 |  | 
|  | 218 | // Reset parts of the environment, like su would. | 
|  | 219 | setenv("PATH", _PATH_DEFPATH, 1); | 
|  | 220 | unsetenv("IFS"); | 
|  | 221 |  | 
|  | 222 | // Set the user-specific parts for this user. | 
|  | 223 | passwd* pw = getpwuid(uid); | 
|  | 224 | setenv("LOGNAME", pw->pw_name, 1); | 
|  | 225 | setenv("SHELL", pw->pw_shell, 1); | 
|  | 226 | setenv("USER", pw->pw_name, 1); | 
|  | 227 |  | 
|  | 228 | // User specified command for exec. | 
|  | 229 | if ((argc >= cmd_argv_offset + 1) && | 
|  | 230 | (execvp(argv[cmd_argv_offset], argv+cmd_argv_offset) == -1)) { | 
|  | 231 | error(1, errno, "exec failed for %s", argv[cmd_argv_offset]); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | // Default exec shell. | 
|  | 235 | execlp(_PATH_BSHELL, "sh", NULL); | 
|  | 236 | error(1, errno, "exec failed"); | 
|  | 237 | } |