2 ** Copyright 2010, Adam Shanks (@ChainsDD)
3 ** Copyright 2008, Zinx Verituse (@zinxv)
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
23 #include <sys/select.h>
35 #include <sys/types.h>
41 extern int daemon_from_uid;
42 extern int daemon_from_pid;
44 unsigned get_shell_uid() {
45 struct passwd* ppwd = getpwnam("shell");
53 unsigned get_system_uid() {
54 struct passwd* ppwd = getpwnam("system");
62 unsigned get_radio_uid() {
63 struct passwd* ppwd = getpwnam("radio");
71 int fork_zero_fucks() {
75 waitpid(pid, &status, 0);
85 void exec_log(int priority, const char* fmt, ...) {
86 static int log_fd = -1;
92 log_fd = open("/dev/log/main", O_WRONLY);
99 vsnprintf(msg, PATH_MAX, fmt, args);
102 vec[0].iov_base = (unsigned char *) &priority;
104 vec[1].iov_base = (void *) LOG_TAG;
105 vec[1].iov_len = strlen(LOG_TAG) + 1;
106 vec[2].iov_base = (void *) msg;
107 vec[2].iov_len = strlen(msg) + 1;
109 writev(log_fd, vec, 3);
112 static int from_init(struct su_initiator *from) {
113 char path[PATH_MAX], exe[PATH_MAX];
114 char args[4096], *argv0, *argv_rest;
120 from->uid = getuid();
121 from->pid = getppid();
124 from->uid = daemon_from_uid;
125 from->pid = daemon_from_pid;
128 /* Get the command line */
129 snprintf(path, sizeof(path), "/proc/%u/cmdline", from->pid);
130 fd = open(path, O_RDONLY);
132 PLOGE("Opening command line");
135 len = read(fd, args, sizeof(args));
138 if (len < 0 || len == sizeof(args)) {
139 PLOGEV("Reading command line", err);
145 for (i = 0; i < len; i++) {
146 if (args[i] == '\0') {
148 argv_rest = &args[i+1];
157 strncpy(from->args, argv_rest, sizeof(from->args));
158 from->args[sizeof(from->args)-1] = '\0';
160 from->args[0] = '\0';
163 /* If this isn't app_process, use the real path instead of argv[0] */
164 snprintf(path, sizeof(path), "/proc/%u/exe", from->pid);
165 len = readlink(path, exe, sizeof(exe));
167 PLOGE("Getting exe path");
171 if (strcmp(exe, "/system/bin/app_process")) {
175 strncpy(from->bin, argv0, sizeof(from->bin));
176 from->bin[sizeof(from->bin)-1] = '\0';
179 pw = getpwuid(from->uid);
180 if (pw && pw->pw_name) {
181 strncpy(from->name, pw->pw_name, sizeof(from->name));
187 static int get_multiuser_mode() {
189 char sdk_ver[PROPERTY_VALUE_MAX];
191 data = read_file("/system/build.prop");
192 get_property(data, sdk_ver, "ro.build.version.sdk", "0");
195 int sdk = atoi(sdk_ver);
197 return MULTIUSER_MODE_NONE;
199 int ret = MULTIUSER_MODE_OWNER_ONLY;
202 if ((fp = fopen(REQUESTOR_MULTIUSER_MODE, "r"))) {
203 fgets(mode, sizeof(mode), fp);
204 int last = strlen(mode) - 1;
205 if (mode[last] == '\n')
207 if (strcmp(mode, MULTIUSER_VALUE_USER) == 0) {
208 ret = MULTIUSER_MODE_USER;
209 } else if (strcmp(mode, MULTIUSER_VALUE_OWNER_MANAGED) == 0) {
210 ret = MULTIUSER_MODE_OWNER_MANAGED;
213 ret = MULTIUSER_MODE_OWNER_ONLY;
220 static void read_options(struct su_context *ctx) {
221 ctx->user.multiuser_mode = get_multiuser_mode();
224 static void user_init(struct su_context *ctx) {
225 if (ctx->from.uid > 99999) {
226 ctx->user.android_user_id = ctx->from.uid / 100000;
227 if (ctx->user.multiuser_mode == MULTIUSER_MODE_USER) {
228 snprintf(ctx->user.database_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR_DATABASE_PATH);
229 snprintf(ctx->user.base_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR);
234 static void populate_environment(const struct su_context *ctx) {
240 pw = getpwuid(ctx->to.uid);
242 setenv("HOME", pw->pw_dir, 1);
244 setenv("SHELL", ctx->to.shell, 1);
246 setenv("SHELL", DEFAULT_SHELL, 1);
247 if (ctx->to.login || ctx->to.uid) {
248 setenv("USER", pw->pw_name, 1);
249 setenv("LOGNAME", pw->pw_name, 1);
254 void set_identity(unsigned int uid) {
256 * Set effective uid back to root, otherwise setres[ug]id will fail
260 PLOGE("seteuid (root)");
263 if (setresgid(uid, uid, uid)) {
264 PLOGE("setresgid (%u)", uid);
267 if (setresuid(uid, uid, uid)) {
268 PLOGE("setresuid (%u)", uid);
273 static void socket_cleanup(struct su_context *ctx) {
274 if (ctx && ctx->sock_path[0]) {
275 if (unlink(ctx->sock_path))
276 PLOGE("unlink (%s)", ctx->sock_path);
277 ctx->sock_path[0] = 0;
282 * For use in signal handlers/atexit-function
283 * NOTE: su_ctx points to main's local variable.
284 * It's OK due to the program uses exit(3), not return from main()
286 static struct su_context *su_ctx = NULL;
288 static void cleanup(void) {
289 socket_cleanup(su_ctx);
292 static void cleanup_signal(int sig) {
293 socket_cleanup(su_ctx);
297 static int socket_create_temp(char *path, size_t len) {
299 struct sockaddr_un sun;
301 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
306 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
307 PLOGE("fcntl FD_CLOEXEC");
311 memset(&sun, 0, sizeof(sun));
312 sun.sun_family = AF_LOCAL;
313 snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
314 memset(sun.sun_path, 0, sizeof(sun.sun_path));
315 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
318 * Delete the socket to protect from situations when
319 * something bad occured previously and the kernel reused pid from that process.
320 * Small probability, isn't it.
322 unlink(sun.sun_path);
324 if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
329 if (listen(fd, 1) < 0) {
340 static int socket_accept(int serv_fd) {
345 /* Wait 20 seconds for a connection, then give up. */
349 FD_SET(serv_fd, &fds);
351 rc = select(serv_fd + 1, &fds, NULL, NULL, &tv);
352 } while (rc < 0 && errno == EINTR);
358 fd = accept(serv_fd, NULL, NULL);
367 static int socket_send_request(int fd, const struct su_context *ctx) {
368 #define write_data(fd, data, data_len) \
370 size_t __len = htonl(data_len); \
371 __len = write((fd), &__len, sizeof(__len)); \
372 if (__len != sizeof(__len)) { \
373 PLOGE("write(" #data ")"); \
376 __len = write((fd), data, data_len); \
377 if (__len != data_len) { \
378 PLOGE("write(" #data ")"); \
383 #define write_string_data(fd, name, data) \
385 write_data(fd, name, strlen(name)); \
386 write_data(fd, data, strlen(data)); \
389 // stringify everything.
390 #define write_token(fd, name, data) \
393 snprintf(buf, sizeof(buf), "%d", data); \
394 write_string_data(fd, name, buf); \
397 write_token(fd, "version", PROTO_VERSION);
398 write_token(fd, "binary.version", VERSION_CODE);
399 write_token(fd, "pid", ctx->from.pid);
400 write_string_data(fd, "from.name", ctx->from.name);
401 write_string_data(fd, "to.name", ctx->to.name);
402 write_token(fd, "from.uid", ctx->from.uid);
403 write_token(fd, "to.uid", ctx->to.uid);
404 write_string_data(fd, "from.bin", ctx->from.bin);
405 // TODO: Fix issue where not using -c does not result a in a command
406 write_string_data(fd, "command", get_command(&ctx->to));
407 write_token(fd, "eof", PROTO_VERSION);
411 static int socket_receive_result(int fd, char *result, ssize_t result_len) {
414 LOGV("waiting for user");
415 len = read(fd, result, result_len-1);
417 PLOGE("read(result)");
425 static void usage(int status) {
426 FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
429 "Usage: su [options] [--] [-] [LOGIN] [--] [args...]\n\n"
431 " --daemon start the su daemon agent\n"
432 " -c, --command COMMAND pass COMMAND to the invoked shell\n"
433 " -h, --help display this help message and exit\n"
434 " -, -l, --login pretend the shell to be a login shell\n"
436 " --preserve-environment do not change environment variables\n"
437 " -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n"
438 " -u display the multiuser mode and exit\n"
439 " -v, --version display version number and exit\n"
440 " -V display version code and exit,\n"
441 " this is used almost exclusively by Superuser.apk\n");
445 static __attribute__ ((noreturn)) void deny(struct su_context *ctx) {
446 char *cmd = get_command(&ctx->to);
450 // no need to log if called by root
451 if (ctx->from.uid == AID_ROOT)
454 // dumpstate (which logs to logcat/shell) will spam the crap out of the system with su calls
455 if (strcmp("/system/bin/dumpstate", ctx->from.bin) == 0)
459 send_result(ctx, DENY);
461 LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
462 fprintf(stderr, "%s\n", strerror(EACCES));
466 static __attribute__ ((noreturn)) void allow(struct su_context *ctx) {
473 // no need to log if called by root
474 if (ctx->from.uid == AID_ROOT)
477 // dumpstate (which logs to logcat/shell) will spam the crap out of the system with su calls
478 if (strcmp("/system/bin/dumpstate", ctx->from.bin) == 0)
482 send_result(ctx, ALLOW);
485 argc = ctx->to.optind;
486 if (ctx->to.command) {
487 binary = ctx->to.shell;
488 ctx->to.argv[--argc] = ctx->to.command;
489 ctx->to.argv[--argc] = "-c";
491 else if (ctx->to.shell) {
492 binary = ctx->to.shell;
495 if (ctx->to.argv[argc]) {
496 binary = ctx->to.argv[argc++];
499 binary = DEFAULT_SHELL;
503 arg0 = strrchr (binary, '/');
504 arg0 = (arg0) ? arg0 + 1 : binary;
506 int s = strlen(arg0) + 2;
517 populate_environment(ctx);
518 set_identity(ctx->to.uid);
521 (argc + (arg) < ctx->to.argc) ? " " : "", \
522 (argc + (arg) < ctx->to.argc) ? ctx->to.argv[argc + (arg)] : ""
524 LOGD("%u %s executing %u %s using binary %s : %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
525 ctx->from.uid, ctx->from.bin,
526 ctx->to.uid, get_command(&ctx->to), binary,
527 arg0, PARG(0), PARG(1), PARG(2), PARG(3), PARG(4), PARG(5),
528 (ctx->to.optind + 6 < ctx->to.argc) ? " ..." : "");
530 ctx->to.argv[--argc] = arg0;
531 execvp(binary, ctx->to.argv + argc);
534 fprintf(stderr, "Cannot execute %s: %s\n", binary, strerror(err));
539 * CyanogenMod-specific behavior
541 * we can't simply use the property service, since we aren't launched from init
542 * and can't trust the location of the property workspace.
543 * Find the properties ourselves.
545 int access_disabled(const struct su_initiator *from) {
546 #ifndef SUPERUSER_EMBEDDED
550 char build_type[PROPERTY_VALUE_MAX];
551 char debuggable[PROPERTY_VALUE_MAX], enabled[PROPERTY_VALUE_MAX];
554 data = read_file("/system/build.prop");
555 if (check_property(data, "ro.cm.version")) {
556 get_property(data, build_type, "ro.build.type", "");
559 data = read_file("/default.prop");
560 get_property(data, debuggable, "ro.debuggable", "0");
562 /* only allow su on debuggable builds */
563 if (strcmp("1", debuggable) != 0) {
564 LOGE("Root access is disabled on non-debug builds");
568 data = read_file("/data/property/persist.sys.root_access");
571 if (len >= PROPERTY_VALUE_MAX)
572 memcpy(enabled, "1", 2);
574 memcpy(enabled, data, len + 1);
577 memcpy(enabled, "1", 2);
579 /* enforce persist.sys.root_access on non-eng builds for apps */
580 if (strcmp("eng", build_type) != 0 &&
581 from->uid != AID_SHELL && from->uid != AID_ROOT &&
582 (atoi(enabled) & CM_ROOT_ACCESS_APPS_ONLY) != CM_ROOT_ACCESS_APPS_ONLY ) {
583 LOGE("Apps root access is disabled by system setting - "
584 "enable it under settings -> developer options");
588 /* disallow su in a shell if appropriate */
589 if (from->uid == AID_SHELL &&
590 (atoi(enabled) & CM_ROOT_ACCESS_ADB_ONLY) != CM_ROOT_ACCESS_ADB_ONLY ) {
591 LOGE("Shell root access is disabled by a system setting - "
592 "enable it under settings -> developer options");
601 static int get_api_version() {
602 char sdk_ver[PROPERTY_VALUE_MAX];
603 char *data = read_file("/system/build.prop");
604 get_property(data, sdk_ver, "ro.build.version.sdk", "0");
605 int ver = atoi(sdk_ver);
610 static void fork_for_samsung(void)
612 // Samsung CONFIG_SEC_RESTRICT_SETUID wants the parent process to have
613 // EUID 0, or else our setresuid() calls will be denied. So make sure
614 // all such syscalls are executed by a child process.
627 exit(WEXITSTATUS(rv));
632 int main(int argc, char *argv[]) {
633 return su_main(argc, argv, 1);
636 int su_main(int argc, char *argv[], int need_client) {
637 // start up in daemon mode if prompted
638 if (argc == 2 && strcmp(argv[1], "--daemon") == 0) {
642 int ppid = getppid();
645 // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
646 /* The same list than GLibc at this point */
647 static const char* const unsec_vars[] = {
671 "LD_AOUT_LIBRARY_PATH",
673 // not listed in linker, used due to system() call
676 const char* const* cp = unsec_vars;
677 const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
684 * set LD_LIBRARY_PATH if the linker has wiped out it due to we're suid.
685 * This occurs on Android 4.0+
687 setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 0);
691 struct su_context ctx = {
711 .android_user_id = 0,
712 .multiuser_mode = MULTIUSER_MODE_OWNER_ONLY,
713 .database_path = REQUESTOR_DATA_PATH REQUESTOR_DATABASE_PATH,
714 .base_path = REQUESTOR_DATA_PATH REQUESTOR
718 int c, socket_serv_fd, fd;
719 char buf[64], *result;
721 struct option long_opts[] = {
722 { "command", required_argument, NULL, 'c' },
723 { "help", no_argument, NULL, 'h' },
724 { "login", no_argument, NULL, 'l' },
725 { "preserve-environment", no_argument, NULL, 'p' },
726 { "shell", required_argument, NULL, 's' },
727 { "version", no_argument, NULL, 'v' },
728 { NULL, 0, NULL, 0 },
731 while ((c = getopt_long(argc, argv, "+c:hlmps:Vvu", long_opts, NULL)) != -1) {
734 ctx.to.shell = DEFAULT_SHELL;
735 ctx.to.command = optarg;
748 ctx.to.shell = optarg;
751 printf("%d\n", VERSION_CODE);
754 printf("%s\n", VERSION);
757 switch (get_multiuser_mode()) {
758 case MULTIUSER_MODE_USER:
759 printf("%s\n", MULTIUSER_VALUE_USER);
761 case MULTIUSER_MODE_OWNER_MANAGED:
762 printf("%s\n", MULTIUSER_VALUE_OWNER_MANAGED);
764 case MULTIUSER_MODE_OWNER_ONLY:
765 printf("%s\n", MULTIUSER_VALUE_OWNER_ONLY);
767 case MULTIUSER_MODE_NONE:
768 printf("%s\n", MULTIUSER_VALUE_NONE);
773 /* Bionic getopt_long doesn't terminate its error output by newline */
774 fprintf(stderr, "\n");
780 // attempt to use the daemon client if not root,
781 // or this is api 18 and adb shell (/data is not readable even as root)
782 // or just always use it on API 19+ (ART)
783 if ((geteuid() != AID_ROOT && getuid() != AID_ROOT) ||
784 (get_api_version() >= 18 && getuid() == AID_SHELL) ||
785 get_api_version() >= 19) {
786 // attempt to connect to daemon...
787 LOGD("starting daemon client %d %d", getuid(), geteuid());
788 return connect_daemon(argc, argv, ppid);
792 if (optind < argc && !strcmp(argv[optind], "-")) {
796 /* username or uid */
797 if (optind < argc && strcmp(argv[optind], "--")) {
799 pw = getpwnam(argv[optind]);
803 /* It seems we shouldn't do this at all */
805 ctx.to.uid = strtoul(argv[optind], &endptr, 10);
806 if (errno || *endptr) {
807 LOGE("Unknown id: %s\n", argv[optind]);
808 fprintf(stderr, "Unknown id: %s\n", argv[optind]);
812 ctx.to.uid = pw->pw_uid;
814 strncpy(ctx.to.name, pw->pw_name, sizeof(ctx.to.name));
818 if (optind < argc && !strcmp(argv[optind], "--")) {
821 ctx.to.optind = optind;
824 if (from_init(&ctx.from) < 0) {
831 // the latter two are necessary for stock ROMs like note 2 which do dumb things with su, or crash otherwise
832 if (ctx.from.uid == AID_ROOT) {
833 LOGD("Allowing root/system/radio.");
837 // verify superuser is installed
838 if (stat(ctx.user.base_path, &st) < 0) {
839 // send to market (disabled, because people are and think this is hijacking their su)
840 // if (0 == strcmp(JAVA_PACKAGE_NAME, REQUESTOR))
841 // silent_run("am start -d http://www.clockworkmod.com/superuser/install.html -a android.intent.action.VIEW");
842 PLOGE("stat %s", ctx.user.base_path);
846 // odd perms on superuser data dir
847 if (st.st_gid != st.st_uid) {
848 LOGE("Bad uid/gid %d/%d for Superuser Requestor application",
849 (int)st.st_uid, (int)st.st_gid);
853 // always allow if this is the superuser uid
854 // superuser needs to be able to reenable itself when disabled...
855 if (ctx.from.uid == st.st_uid) {
859 // check if superuser is disabled completely
860 if (access_disabled(&ctx.from)) {
861 LOGD("access_disabled");
865 // autogrant shell at this point
866 if (ctx.from.uid == AID_SHELL) {
867 LOGD("Allowing shell.");
871 // deny if this is a non owner request and owner mode only
872 if (ctx.user.multiuser_mode == MULTIUSER_MODE_OWNER_ONLY && ctx.user.android_user_id != 0) {
876 ctx.umask = umask(027);
878 int ret = mkdir(REQUESTOR_CACHE_PATH, 0770);
879 if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
880 PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
884 if (setgroups(0, NULL)) {
888 if (setegid(st.st_gid)) {
889 PLOGE("setegid (%lu)", st.st_gid);
892 if (seteuid(st.st_uid)) {
893 PLOGE("seteuid (%lu)", st.st_uid);
897 dballow = database_check(&ctx);
903 allow(&ctx); /* never returns */
907 deny(&ctx); /* never returns too */
910 socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));
912 if (socket_serv_fd < 0) {
916 signal(SIGHUP, cleanup_signal);
917 signal(SIGPIPE, cleanup_signal);
918 signal(SIGTERM, cleanup_signal);
919 signal(SIGQUIT, cleanup_signal);
920 signal(SIGINT, cleanup_signal);
921 signal(SIGABRT, cleanup_signal);
923 if (send_request(&ctx) < 0) {
929 fd = socket_accept(socket_serv_fd);
933 if (socket_send_request(fd, &ctx)) {
936 if (socket_receive_result(fd, buf, sizeof(buf))) {
941 close(socket_serv_fd);
942 socket_cleanup(&ctx);
946 #define SOCKET_RESPONSE "socket:"
947 if (strncmp(result, SOCKET_RESPONSE, sizeof(SOCKET_RESPONSE) - 1))
948 LOGW("SECURITY RISK: Requestor still receives credentials in intent");
950 result += sizeof(SOCKET_RESPONSE) - 1;
952 if (!strcmp(result, "DENY")) {
954 } else if (!strcmp(result, "ALLOW")) {
957 LOGE("unknown response from Superuser Requestor: %s", result);