blob: 3c0ecc4a9dab53671aedb8dad4290ccaa033ae82 [file] [log] [blame]
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -08001/*
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
18#define PROGNAME "run-as"
19#define LOG_TAG PROGNAME
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <dirent.h>
27#include <errno.h>
28#include <unistd.h>
29#include <time.h>
30#include <stdarg.h>
31
Stephen Smalley4ead8be2012-11-13 12:56:48 -050032#include <selinux/android.h>
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080033#include <private/android_filesystem_config.h>
34#include "package.h"
35
36/*
37 * WARNING WARNING WARNING WARNING
38 *
39 * This program runs as set-uid root on Android production devices.
40 * Be very conservative when modifying it to avoid any serious
41 * security issue. Keep in mind the following:
42 *
43 * - This program should only run for the 'root' or 'shell' users
44 *
Nick Kralevichb2d8f892012-01-23 11:00:36 -080045 * - Avoid anything that is more complex than simple system calls
46 * until the uid/gid has been dropped to that of a normal user
47 * or you are sure to exit.
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080048 *
49 * This avoids depending on environment variables, system properties
50 * and other external factors that may affect the C library in
51 * unpredictable ways.
52 *
53 * - Do not trust user input and/or the filesystem whenever possible.
54 *
55 * Read README.TXT for more details.
56 *
57 *
58 *
59 * The purpose of this program is to run a command as a specific
60 * application user-id. Typical usage is:
61 *
62 * run-as <package-name> <command> <args>
63 *
64 * The 'run-as' binary is setuid, but will check the following:
65 *
66 * - that it is invoked from the 'shell' or 'root' user (abort otherwise)
67 * - that '<package-name>' is the name of an installed and debuggable package
68 * - that the package's data directory is well-formed (see package.c)
69 *
70 * If so, it will cd to the package's data directory, drop to the application's
71 * user id / group id then run the command there.
72 *
73 * This can be useful for a number of different things on production devices:
74 *
75 * - Allow application developers to look at their own applicative data
76 * during development.
77 *
78 * - Run the 'gdbserver' binary executable to allow native debugging
79 */
80
81static void
82usage(void)
83{
84 const char* str = "Usage: " PROGNAME " <package-name> <command> [<args>]\n\n";
85 write(1, str, strlen(str));
86 exit(1);
87}
88
89
90static void
91panic(const char* format, ...)
92{
93 va_list args;
94
95 fprintf(stderr, "%s: ", PROGNAME);
96 va_start(args, format);
97 vfprintf(stderr, format, args);
98 va_end(args);
99 exit(1);
100}
101
102
103int main(int argc, char **argv)
104{
105 const char* pkgname;
106 int myuid, uid, gid;
107 PackageInfo info;
108
109 /* check arguments */
110 if (argc < 2)
111 usage();
112
113 /* check userid of caller - must be 'shell' or 'root' */
114 myuid = getuid();
115 if (myuid != AID_SHELL && myuid != AID_ROOT) {
116 panic("only 'shell' or 'root' users can run this program\n");
117 }
118
119 /* retrieve package information from system */
120 pkgname = argv[1];
121 if (get_package_info(pkgname, &info) < 0) {
122 panic("Package '%s' is unknown\n", pkgname);
123 return 1;
124 }
125
126 /* reject system packages */
127 if (info.uid < AID_APP) {
128 panic("Package '%s' is not an application\n", pkgname);
129 return 1;
130 }
131
132 /* reject any non-debuggable package */
133 if (!info.isDebuggable) {
134 panic("Package '%s' is not debuggable\n", pkgname);
135 return 1;
136 }
137
138 /* check that the data directory path is valid */
139 if (check_data_path(info.dataDir, info.uid) < 0) {
140 panic("Package '%s' has corrupt installation\n", pkgname);
141 return 1;
142 }
143
144 /* then move to it */
145 {
146 int ret;
147 do {
148 ret = chdir(info.dataDir);
149 } while (ret < 0 && errno == EINTR);
150
151 if (ret < 0) {
152 panic("Could not cd to package's data directory: %s\n", strerror(errno));
153 return 1;
154 }
155 }
156
157 /* Ensure that we change all real/effective/saved IDs at the
158 * same time to avoid nasty surprises.
159 */
160 uid = gid = info.uid;
161 if(setresgid(gid,gid,gid) || setresuid(uid,uid,uid)) {
162 panic("Permission denied\n");
163 return 1;
164 }
165
Robert Craigfced3de2013-03-26 08:09:09 -0400166 if (selinux_android_setcontext(uid, 0, info.seinfo, pkgname) < 0) {
Stephen Smalley4ead8be2012-11-13 12:56:48 -0500167 panic("Could not set SELinux security context: %s\n", strerror(errno));
168 return 1;
169 }
170
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800171 /* User specified command for exec. */
172 if (argc >= 3 ) {
173 if (execvp(argv[2], argv+2) < 0) {
174 panic("exec failed for %s Error:%s\n", argv[2], strerror(errno));
175 return -errno;
176 }
177 }
178
179 /* Default exec shell. */
180 execlp("/system/bin/sh", "sh", NULL);
181
182 panic("exec failed\n");
183 return 1;
184}