The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 1 | /* |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008 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 | */ |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 16 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <error.h> |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 19 | #include <getopt.h> |
| 20 | #include <paths.h> |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 26 | |
The Android Open Source Project | 26aaac4 | 2009-03-18 17:39:49 -0700 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
| 28 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 29 | void pwtoid(const char* tok, uid_t* uid, gid_t* gid) { |
| 30 | struct passwd* pw = getpwnam(tok); |
JP Abgrall | d198c42 | 2013-01-10 17:04:42 -0800 | [diff] [blame] | 31 | if (pw) { |
| 32 | if (uid) *uid = pw->pw_uid; |
| 33 | if (gid) *gid = pw->pw_gid; |
| 34 | } else { |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 35 | char* end; |
| 36 | errno = 0; |
| 37 | uid_t tmpid = strtoul(tok, &end, 10); |
| 38 | if (errno != 0 || end == tok) error(1, errno, "invalid uid/gid '%s'", tok); |
JP Abgrall | d198c42 | 2013-01-10 17:04:42 -0800 | [diff] [blame] | 39 | if (uid) *uid = tmpid; |
| 40 | if (gid) *gid = tmpid; |
| 41 | } |
| 42 | } |
| 43 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 44 | void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) { |
JP Abgrall | d198c42 | 2013-01-10 17:04:42 -0800 | [diff] [blame] | 45 | char *clobberablegids; |
| 46 | char *nexttok; |
| 47 | char *tok; |
| 48 | int gids_found; |
| 49 | |
| 50 | if (!uidgids || !*uidgids) { |
| 51 | *gid = *uid = 0; |
| 52 | *gids_count = 0; |
| 53 | return; |
| 54 | } |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 55 | |
JP Abgrall | d198c42 | 2013-01-10 17:04:42 -0800 | [diff] [blame] | 56 | clobberablegids = strdup(uidgids); |
| 57 | strcpy(clobberablegids, uidgids); |
| 58 | nexttok = clobberablegids; |
| 59 | tok = strsep(&nexttok, ","); |
| 60 | pwtoid(tok, uid, gid); |
| 61 | tok = strsep(&nexttok, ","); |
| 62 | if (!tok) { |
| 63 | /* gid is already set above */ |
| 64 | *gids_count = 0; |
| 65 | free(clobberablegids); |
| 66 | return; |
| 67 | } |
| 68 | pwtoid(tok, NULL, gid); |
| 69 | gids_found = 0; |
| 70 | while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) { |
| 71 | pwtoid(tok, NULL, gids); |
| 72 | gids_found++; |
| 73 | gids++; |
| 74 | } |
| 75 | if (nexttok && gids_found == *gids_count) { |
| 76 | fprintf(stderr, "too many group ids\n"); |
| 77 | } |
| 78 | *gids_count = gids_found; |
| 79 | free(clobberablegids); |
| 80 | } |
| 81 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 82 | int main(int argc, char** argv) { |
| 83 | uid_t current_uid = getuid(); |
| 84 | if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed"); |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 85 | |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 86 | // Handle -h and --help. |
Elliott Hughes | 499ba7d | 2015-03-26 17:09:41 -0700 | [diff] [blame] | 87 | ++argv; |
| 88 | if (*argv && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0)) { |
| 89 | fprintf(stderr, |
Elliott Hughes | cf83f01 | 2019-02-20 22:38:23 -0800 | [diff] [blame] | 90 | "usage: su [WHO [COMMAND...]]\n" |
Elliott Hughes | 499ba7d | 2015-03-26 17:09:41 -0700 | [diff] [blame] | 91 | "\n" |
Elliott Hughes | cf83f01 | 2019-02-20 22:38:23 -0800 | [diff] [blame] | 92 | "Switch to WHO (default 'root') and run the given COMMAND (default sh).\n" |
Elliott Hughes | 499ba7d | 2015-03-26 17:09:41 -0700 | [diff] [blame] | 93 | "\n" |
Elliott Hughes | cf83f01 | 2019-02-20 22:38:23 -0800 | [diff] [blame] | 94 | "WHO is a comma-separated list of user, group, and supplementary groups\n" |
| 95 | "in that order.\n" |
Elliott Hughes | 499ba7d | 2015-03-26 17:09:41 -0700 | [diff] [blame] | 96 | "\n"); |
| 97 | return 0; |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 98 | } |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 99 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 100 | // The default user is root. |
| 101 | uid_t uid = 0; |
| 102 | gid_t gid = 0; |
Nick Kralevich | 17928c8 | 2012-04-02 13:29:35 -0700 | [diff] [blame] | 103 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 104 | // If there are any arguments, the first argument is the uid/gid/supplementary groups. |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 105 | if (*argv) { |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 106 | gid_t gids[10]; |
JP Abgrall | d198c42 | 2013-01-10 17:04:42 -0800 | [diff] [blame] | 107 | int gids_count = sizeof(gids)/sizeof(gids[0]); |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 108 | extract_uidgids(*argv, &uid, &gid, gids, &gids_count); |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 109 | if (gids_count) { |
| 110 | if (setgroups(gids_count, gids)) { |
| 111 | error(1, errno, "setgroups failed"); |
JP Abgrall | d198c42 | 2013-01-10 17:04:42 -0800 | [diff] [blame] | 112 | } |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 113 | } |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 114 | ++argv; |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 117 | if (setgid(gid)) error(1, errno, "setgid failed"); |
| 118 | if (setuid(uid)) error(1, errno, "setuid failed"); |
| 119 | |
Elliott Hughes | 48049dd | 2015-03-17 21:19:25 -0700 | [diff] [blame] | 120 | // Reset parts of the environment. |
| 121 | setenv("PATH", _PATH_DEFPATH, 1); |
| 122 | unsetenv("IFS"); |
| 123 | struct passwd* pw = getpwuid(uid); |
Mark Salyzyn | 75dbde9 | 2016-03-08 13:36:00 -0800 | [diff] [blame] | 124 | if (pw) { |
| 125 | setenv("LOGNAME", pw->pw_name, 1); |
| 126 | setenv("USER", pw->pw_name, 1); |
| 127 | } else { |
| 128 | unsetenv("LOGNAME"); |
| 129 | unsetenv("USER"); |
| 130 | } |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 131 | |
| 132 | // Set up the arguments for exec. |
Elliott Hughes | 436af25 | 2015-03-17 12:35:45 -0700 | [diff] [blame] | 133 | char* exec_args[argc + 1]; // Having too much space is fine. |
Elliott Hughes | 436af25 | 2015-03-17 12:35:45 -0700 | [diff] [blame] | 134 | size_t i = 0; |
| 135 | for (; *argv != NULL; ++i) { |
| 136 | exec_args[i] = *argv++; |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 137 | } |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 138 | // Default to the standard shell. |
Elliott Hughes | dee9705 | 2016-10-07 15:19:02 -0700 | [diff] [blame] | 139 | if (i == 0) exec_args[i++] = const_cast<char*>("/system/bin/sh"); |
Elliott Hughes | 436af25 | 2015-03-17 12:35:45 -0700 | [diff] [blame] | 140 | exec_args[i] = NULL; |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 141 | |
Elliott Hughes | 68cf303 | 2015-03-14 10:15:06 -0700 | [diff] [blame] | 142 | execvp(exec_args[0], exec_args); |
| 143 | error(1, errno, "failed to exec %s", exec_args[0]); |
The Android Open Source Project | e16cb84 | 2009-03-03 19:32:58 -0800 | [diff] [blame] | 144 | } |