OSDN Git Service

wip
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / su.c
1 /*
2 ** Copyright 2010, Adam Shanks (@ChainsDD)
3 ** Copyright 2008, Zinx Verituse (@zinxv)
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 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <sys/wait.h>
22 #include <sys/select.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <stdint.h>
31 #include <pwd.h>
32 #include <sys/stat.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <pwd.h>
36
37 #include "su.h"
38 #include "utils.h"
39
40 int get_shell_uid() {
41   struct passwd* ppwd = getpwnam("shell");
42   if (NULL == ppwd) {
43     return -1;
44   }
45   
46   return ppwd->pw_uid;
47 }
48
49 void exec_log(char *priority, char* logline) {
50   int pid;
51   if ((pid = fork()) == 0) {
52     execl("/system/bin/log", "/system/bin/log", "-p", priority, "-t", LOG_TAG, logline);
53     exit(0);
54   }
55 }
56
57 void exec_loge(const char* fmt, ...) {
58   va_list args;
59
60   char logline[PATH_MAX];
61   va_start(args, fmt);
62   vsnprintf(logline, PATH_MAX, fmt, args);
63   va_end(args);
64   exec_log("e", logline);
65 }
66 void exec_logw(const char* fmt, ...) {
67   va_list args;
68
69   char logline[PATH_MAX];
70   va_start(args, fmt);
71   vsnprintf(logline, PATH_MAX, fmt, args);
72   va_end(args);
73   exec_log("w", logline);
74 }
75 void exec_logd(const char* fmt, ...) {
76   va_list args;
77
78   char logline[PATH_MAX];
79   va_start(args, fmt);
80   vsnprintf(logline, PATH_MAX, fmt, args);
81   va_end(args);
82   exec_log("d", logline);
83 }
84
85 static int from_init(struct su_initiator *from)
86 {
87     char path[PATH_MAX], exe[PATH_MAX];
88     char args[4096], *argv0, *argv_rest;
89     int fd;
90     ssize_t len;
91     int i;
92     int err;
93
94     from->uid = getuid();
95     from->pid = getppid();
96
97     /* Get the command line */
98     snprintf(path, sizeof(path), "/proc/%u/cmdline", from->pid);
99     fd = open(path, O_RDONLY);
100     if (fd < 0) {
101         PLOGE("Opening command line");
102         return -1;
103     }
104     len = read(fd, args, sizeof(args));
105     err = errno;
106     close(fd);
107     if (len < 0 || len == sizeof(args)) {
108         PLOGEV("Reading command line", err);
109         return -1;
110     }
111
112     argv0 = args;
113     argv_rest = NULL;
114     for (i = 0; i < len; i++) {
115         if (args[i] == '\0') {
116             if (!argv_rest) {
117                 argv_rest = &args[i+1];
118             } else {
119                 args[i] = ' ';
120             }
121         }
122     }
123     args[len] = '\0';
124
125     if (argv_rest) {
126         strncpy(from->args, argv_rest, sizeof(from->args));
127         from->args[sizeof(from->args)-1] = '\0';
128     } else {
129         from->args[0] = '\0';
130     }
131
132     /* If this isn't app_process, use the real path instead of argv[0] */
133     snprintf(path, sizeof(path), "/proc/%u/exe", from->pid);
134     len = readlink(path, exe, sizeof(exe));
135     if (len < 0) {
136         PLOGE("Getting exe path");
137         return -1;
138     }
139     exe[len] = '\0';
140     if (strcmp(exe, "/system/bin/app_process")) {
141         argv0 = exe;
142     }
143
144     strncpy(from->bin, argv0, sizeof(from->bin));
145     from->bin[sizeof(from->bin)-1] = '\0';
146
147     return 0;
148 }
149
150 static void read_options(struct su_context *ctx)
151 {
152     char mode[12];
153     FILE *fp;
154     if ((fp = fopen(REQUESTOR_OPTIONS, "r"))) {
155         fgets(mode, sizeof(mode), fp);
156         if (strcmp(mode, "user\n") == 0) {
157             ctx->user.owner_mode = 0;
158         } else if (strcmp(mode, "owner\n") == 0) {
159             ctx->user.owner_mode = 1;
160         }
161     }
162 }
163
164 static void user_init(struct su_context *ctx)
165 {
166     if (ctx->from.uid > 99999) {
167         ctx->user.userid = ctx->from.uid / 100000;
168         if (!ctx->user.owner_mode) {
169             snprintf(ctx->user.data_path, PATH_MAX, "/data/user/%d/%s",
170                     ctx->user.userid, REQUESTOR);
171             snprintf(ctx->user.store_path, PATH_MAX, "/data/user/%d/%s/files/stored",
172                     ctx->user.userid, REQUESTOR);
173             snprintf(ctx->user.store_default, PATH_MAX, "/data/user/%d/%s/files/stored/default",
174                     ctx->user.userid, REQUESTOR);
175         }
176     }
177 }
178
179 static void populate_environment(const struct su_context *ctx)
180 {
181     struct passwd *pw;
182
183     if (ctx->to.keepenv)
184         return;
185
186     pw = getpwuid(ctx->to.uid);
187     if (pw) {
188         setenv("HOME", pw->pw_dir, 1);
189         setenv("SHELL", ctx->to.shell, 1);
190         if (ctx->to.login || ctx->to.uid) {
191             setenv("USER", pw->pw_name, 1);
192             setenv("LOGNAME", pw->pw_name, 1);
193         }
194     }
195 }
196
197 void set_identity(unsigned int uid)
198 {
199     /*
200      * Set effective uid back to root, otherwise setres[ug]id will fail
201      * if uid isn't root.
202      */
203     if (seteuid(0)) {
204         PLOGE("seteuid (root)");
205         exit(EXIT_FAILURE);
206     }
207     if (setresgid(uid, uid, uid)) {
208         PLOGE("setresgid (%u)", uid);
209         exit(EXIT_FAILURE);
210     }
211     if (setresuid(uid, uid, uid)) {
212         PLOGE("setresuid (%u)", uid);
213         exit(EXIT_FAILURE);
214     }
215 }
216
217 static void socket_cleanup(struct su_context *ctx)
218 {
219     if (ctx && ctx->sock_path[0]) {
220         if (unlink(ctx->sock_path))
221             PLOGE("unlink (%s)", ctx->sock_path);
222         ctx->sock_path[0] = 0;
223     }
224 }
225
226 /*
227  * For use in signal handlers/atexit-function
228  * NOTE: su_ctx points to main's local variable.
229  *       It's OK due to the program uses exit(3), not return from main()
230  */
231 static struct su_context *su_ctx = NULL;
232
233 static void cleanup(void)
234 {
235     socket_cleanup(su_ctx);
236 }
237
238 static void cleanup_signal(int sig)
239 {
240     socket_cleanup(su_ctx);
241     exit(128 + sig);
242 }
243
244 void sigchld_handler(int sig)
245 {
246     child_cleanup(su_ctx);
247     (void)sig;
248 }
249
250 static int socket_create_temp(char *path, size_t len)
251 {
252     int fd;
253     struct sockaddr_un sun;
254
255     fd = socket(AF_LOCAL, SOCK_STREAM, 0);
256     if (fd < 0) {
257         PLOGE("socket");
258         return -1;
259     }
260     if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
261         PLOGE("fcntl FD_CLOEXEC");
262         goto err;
263     }
264
265     memset(&sun, 0, sizeof(sun));
266     sun.sun_family = AF_LOCAL;
267     snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
268     memset(sun.sun_path, 0, sizeof(sun.sun_path));
269     snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
270
271     /*
272      * Delete the socket to protect from situations when
273      * something bad occured previously and the kernel reused pid from that process.
274      * Small probability, isn't it.
275      */
276     unlink(sun.sun_path);
277
278     if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
279         PLOGE("bind");
280         goto err;
281     }
282
283     if (listen(fd, 1) < 0) {
284         PLOGE("listen");
285         goto err;
286     }
287
288     return fd;
289 err:
290     close(fd);
291     return -1;
292 }
293
294 static int socket_accept(int serv_fd)
295 {
296     struct timeval tv;
297     fd_set fds;
298     int fd, rc;
299
300     /* Wait 20 seconds for a connection, then give up. */
301     tv.tv_sec = 20;
302     tv.tv_usec = 0;
303     FD_ZERO(&fds);
304     FD_SET(serv_fd, &fds);
305     do {
306         rc = select(serv_fd + 1, &fds, NULL, NULL, &tv);
307     } while (rc < 0 && errno == EINTR);
308     if (rc < 1) {
309         PLOGE("select");
310         return -1;
311     }
312
313     fd = accept(serv_fd, NULL, NULL);
314     if (fd < 0) {
315         PLOGE("accept");
316         return -1;
317     }
318
319     return fd;
320 }
321
322 static int socket_send_request(int fd, const struct su_context *ctx)
323 {
324     size_t len;
325     size_t bin_size, cmd_size;
326     char *cmd;
327
328 #define write_token(fd, data)                \
329 do {                            \
330     uint32_t __data = htonl(data);            \
331     size_t __count = sizeof(__data);        \
332     size_t __len = write((fd), &__data, __count);    \
333     if (__len != __count) {                \
334         PLOGE("write(" #data ")");        \
335         return -1;                \
336     }                        \
337 } while (0)
338
339     write_token(fd, PROTO_VERSION);
340     write_token(fd, PATH_MAX);
341     write_token(fd, ARG_MAX);
342     write_token(fd, ctx->from.uid);
343     write_token(fd, ctx->to.uid);
344     bin_size = strlen(ctx->from.bin) + 1;
345     write_token(fd, bin_size);
346     len = write(fd, ctx->from.bin, bin_size);
347     if (len != bin_size) {
348         PLOGE("write(bin)");
349         return -1;
350     }
351     cmd = get_command(&ctx->to);
352     cmd_size = strlen(cmd) + 1;
353     write_token(fd, cmd_size);
354     len = write(fd, cmd, cmd_size);
355     if (len != cmd_size) {
356         PLOGE("write(cmd)");
357         return -1;
358     }
359     return 0;
360 }
361
362 static int socket_receive_result(int fd, char *result, ssize_t result_len)
363 {
364     ssize_t len;
365     
366     len = read(fd, result, result_len-1);
367     if (len < 0) {
368         PLOGE("read(result)");
369         return -1;
370     }
371     result[len] = '\0';
372
373     return 0;
374 }
375
376 static void usage(int status)
377 {
378     FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
379
380     fprintf(stream,
381     "Usage: su [options] [--] [-] [LOGIN] [--] [args...]\n\n"
382     "Options:\n"
383     "  -c, --command COMMAND         pass COMMAND to the invoked shell\n"
384     "  -h, --help                    display this help message and exit\n"
385     "  -, -l, --login                pretend the shell to be a login shell\n"
386     "  -m, -p,\n"
387     "  --preserve-environment        do not change environment variables\n"
388     "  -s, --shell SHELL             use SHELL instead of the default " DEFAULT_SHELL "\n"
389     "  -v, --version                 display version number and exit\n"
390     "  -V                            display version code and exit,\n"
391     "                                this is used almost exclusively by Superuser.apk\n");
392     exit(status);
393 }
394
395 static __attribute__ ((noreturn)) void deny(struct su_context *ctx)
396 {
397     char *cmd = get_command(&ctx->to);
398
399     // No send to UI denied requests for shell and root users (they are in the log)
400     if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
401         send_intent(ctx, DENY, ACTION_RESULT);
402     }
403     LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
404     fprintf(stderr, "%s\n", strerror(EACCES));
405     exit(EXIT_FAILURE);
406 }
407
408 static __attribute__ ((noreturn)) void allow(struct su_context *ctx)
409 {
410     char *arg0;
411     int argc, err;
412
413     umask(ctx->umask);
414     // No send to UI accepted requests for shell and root users (they are in the log)
415     if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
416         send_intent(ctx, ALLOW, ACTION_RESULT);
417     }
418
419     arg0 = strrchr (ctx->to.shell, '/');
420     arg0 = (arg0) ? arg0 + 1 : ctx->to.shell;
421     if (ctx->to.login) {
422         int s = strlen(arg0) + 2;
423         char *p = malloc(s);
424
425         if (!p)
426             exit(EXIT_FAILURE);
427
428         *p = '-';
429         strcpy(p + 1, arg0);
430         arg0 = p;
431     }
432
433     populate_environment(ctx);
434     set_identity(ctx->to.uid);
435
436 #define PARG(arg)                                    \
437     (ctx->to.optind + (arg) < ctx->to.argc) ? " " : "",                    \
438     (ctx->to.optind + (arg) < ctx->to.argc) ? ctx->to.argv[ctx->to.optind + (arg)] : ""
439
440     LOGD("%u %s executing %u %s using shell %s : %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
441             ctx->from.uid, ctx->from.bin,
442             ctx->to.uid, get_command(&ctx->to), ctx->to.shell,
443             arg0, PARG(0), PARG(1), PARG(2), PARG(3), PARG(4), PARG(5),
444             (ctx->to.optind + 6 < ctx->to.argc) ? " ..." : "");
445
446     argc = ctx->to.optind;
447     if (ctx->to.command) {
448         ctx->to.argv[--argc] = ctx->to.command;
449         ctx->to.argv[--argc] = "-c";
450     }
451     ctx->to.argv[--argc] = arg0;
452     execv(ctx->to.shell, ctx->to.argv + argc);
453     err = errno;
454     PLOGE("exec");
455     fprintf(stderr, "Cannot execute %s: %s\n", ctx->to.shell, strerror(err));
456     exit(EXIT_FAILURE);
457 }
458
459 /*
460  * CyanogenMod-specific behavior
461  *
462  * we can't simply use the property service, since we aren't launched from init
463  * and can't trust the location of the property workspace.
464  * Find the properties ourselves.
465  */
466 int access_disabled(const struct su_initiator *from)
467 {
468     char *data;
469     char build_type[PROPERTY_VALUE_MAX];
470     char debuggable[PROPERTY_VALUE_MAX], enabled[PROPERTY_VALUE_MAX];
471     size_t len;
472
473     data = read_file("/system/build.prop");
474     if (check_property(data, "ro.cm.version")) {
475         get_property(data, build_type, "ro.build.type", "");
476         free(data);
477
478         data = read_file("/default.prop");
479         get_property(data, debuggable, "ro.debuggable", "0");
480         free(data);
481         /* only allow su on debuggable builds */
482         if (strcmp("1", debuggable) != 0) {
483             LOGE("Root access is disabled on non-debug builds");
484             return 1;
485         }
486
487         data = read_file("/data/property/persist.sys.root_access");
488         if (data != NULL) {
489             len = strlen(data);
490             if (len >= PROPERTY_VALUE_MAX)
491                 memcpy(enabled, "1", 2);
492             else
493                 memcpy(enabled, data, len + 1);
494             free(data);
495         } else
496             memcpy(enabled, "1", 2);
497
498         /* enforce persist.sys.root_access on non-eng builds for apps */
499         if (strcmp("eng", build_type) != 0 &&
500                 from->uid != AID_SHELL && from->uid != AID_ROOT &&
501                 (atoi(enabled) & CM_ROOT_ACCESS_APPS_ONLY) != CM_ROOT_ACCESS_APPS_ONLY ) {
502             LOGE("Apps root access is disabled by system setting - "
503                  "enable it under settings -> developer options");
504             return 1;
505         }
506
507         /* disallow su in a shell if appropriate */
508         if (from->uid == AID_SHELL &&
509                 (atoi(enabled) & CM_ROOT_ACCESS_ADB_ONLY) != CM_ROOT_ACCESS_ADB_ONLY ) {
510             LOGE("Shell root access is disabled by a system setting - "
511                  "enable it under settings -> developer options");
512             return 1;
513         }
514         
515     }
516     return 0;
517 }
518
519 int main(int argc, char *argv[])
520 {
521     // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
522     /* The same list than GLibc at this point */
523     static const char* const unsec_vars[] = {
524         "GCONV_PATH",
525         "GETCONF_DIR",
526         "HOSTALIASES",
527         "LD_AUDIT",
528         "LD_DEBUG",
529         "LD_DEBUG_OUTPUT",
530         "LD_DYNAMIC_WEAK",
531         "LD_LIBRARY_PATH",
532         "LD_ORIGIN_PATH",
533         "LD_PRELOAD",
534         "LD_PROFILE",
535         "LD_SHOW_AUXV",
536         "LD_USE_LOAD_BIAS",
537         "LOCALDOMAIN",
538         "LOCPATH",
539         "MALLOC_TRACE",
540         "MALLOC_CHECK_",
541         "NIS_PATH",
542         "NLSPATH",
543         "RESOLV_HOST_CONF",
544         "RES_OPTIONS",
545         "TMPDIR",
546         "TZDIR",
547         "LD_AOUT_LIBRARY_PATH",
548         "LD_AOUT_PRELOAD",
549         // not listed in linker, used due to system() call
550         "IFS",
551     };
552     const char* const* cp   = unsec_vars;
553     const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
554     while (cp < endp) {
555         unsetenv(*cp);
556         cp++;
557     }
558
559     /*
560      * set LD_LIBRARY_PATH if the linker has wiped out it due to we're suid.
561      * This occurs on Android 4.0+
562      */
563     setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 0);
564
565     LOGD("su invoked.");
566
567     struct su_context ctx = {
568         .from = {
569             .pid = -1,
570             .uid = 0,
571             .bin = "",
572             .args = "",
573         },
574         .to = {
575             .uid = AID_ROOT,
576             .login = 0,
577             .keepenv = 0,
578             .shell = DEFAULT_SHELL,
579             .command = NULL,
580             .argv = argv,
581             .argc = argc,
582             .optind = 0,
583         },
584         .user = {
585             .userid = 0,
586             .owner_mode = -1,
587             .data_path = REQUESTOR_DATA_PATH,
588             .store_path = REQUESTOR_STORED_PATH,
589             .store_default = REQUESTOR_STORED_DEFAULT,
590         },
591     };
592     struct stat st;
593     int c, socket_serv_fd, fd;
594     char buf[64], *result;
595     allow_t dballow;
596     struct option long_opts[] = {
597         { "command",            required_argument,    NULL, 'c' },
598         { "help",            no_argument,        NULL, 'h' },
599         { "login",            no_argument,        NULL, 'l' },
600         { "preserve-environment",    no_argument,        NULL, 'p' },
601         { "shell",            required_argument,    NULL, 's' },
602         { "version",            no_argument,        NULL, 'v' },
603         { NULL, 0, NULL, 0 },
604     };
605
606     while ((c = getopt_long(argc, argv, "+c:hlmps:Vv", long_opts, NULL)) != -1) {
607         switch(c) {
608         case 'c':
609             ctx.to.command = optarg;
610             break;
611         case 'h':
612             usage(EXIT_SUCCESS);
613             break;
614         case 'l':
615             ctx.to.login = 1;
616             break;
617         case 'm':
618         case 'p':
619             ctx.to.keepenv = 1;
620             break;
621         case 's':
622             ctx.to.shell = optarg;
623             break;
624         case 'V':
625             printf("%d\n", VERSION_CODE);
626             exit(EXIT_SUCCESS);
627         case 'v':
628             printf("%s\n", VERSION);
629             exit(EXIT_SUCCESS);
630         default:
631             /* Bionic getopt_long doesn't terminate its error output by newline */
632             fprintf(stderr, "\n");
633             usage(2);
634         }
635     }
636     if (optind < argc && !strcmp(argv[optind], "-")) {
637         ctx.to.login = 1;
638         optind++;
639     }
640     /* username or uid */
641     if (optind < argc && strcmp(argv[optind], "--")) {
642         struct passwd *pw;
643         pw = getpwnam(argv[optind]);
644         if (!pw) {
645             char *endptr;
646
647             /* It seems we shouldn't do this at all */
648             errno = 0;
649             ctx.to.uid = strtoul(argv[optind], &endptr, 10);
650             if (errno || *endptr) {
651                 LOGE("Unknown id: %s\n", argv[optind]);
652                 fprintf(stderr, "Unknown id: %s\n", argv[optind]);
653                 exit(EXIT_FAILURE);
654             }
655         } else {
656             ctx.to.uid = pw->pw_uid;
657         }
658         optind++;
659     }
660     if (optind < argc && !strcmp(argv[optind], "--")) {
661         optind++;
662     }
663     ctx.to.optind = optind;
664
665     su_ctx = &ctx;
666     if (from_init(&ctx.from) < 0) {
667         deny(&ctx);
668     }
669         
670     read_options(&ctx);
671     user_init(&ctx);
672     
673     if (ctx.user.owner_mode == -1 && ctx.user.userid != 0)
674         deny(&ctx);
675
676     if (access_disabled(&ctx.from))
677         deny(&ctx);
678
679     ctx.umask = umask(027);
680
681     if (ctx.from.uid == AID_ROOT || ctx.from.uid == AID_SHELL)
682         allow(&ctx);
683
684     if (stat(ctx.user.data_path, &st) < 0) {
685         PLOGE("stat");
686         deny(&ctx);
687     }
688
689     if (st.st_gid != st.st_uid)
690     {
691         LOGE("Bad uid/gid %d/%d for Superuser Requestor application",
692                 (int)st.st_uid, (int)st.st_gid);
693         deny(&ctx);
694     }
695
696     mkdir(REQUESTOR_CACHE_PATH, 0770);
697     if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
698         PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
699         deny(&ctx);
700     }
701
702     if (setgroups(0, NULL)) {
703         PLOGE("setgroups");
704         deny(&ctx);
705     }
706     if (setegid(st.st_gid)) {
707         PLOGE("setegid (%lu)", st.st_gid);
708         deny(&ctx);
709     }
710     if (seteuid(st.st_uid)) {
711         PLOGE("seteuid (%lu)", st.st_uid);
712         deny(&ctx);
713     }
714
715     dballow = database_check(&ctx);
716     switch (dballow) {
717         case INTERACTIVE: break;
718         case ALLOW: allow(&ctx);    /* never returns */
719         case DENY:
720         default: deny(&ctx);        /* never returns too */
721     }
722     
723     socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));
724     LOGD(ctx.sock_path);
725     if (socket_serv_fd < 0) {
726         deny(&ctx);
727     }
728
729     signal(SIGHUP, cleanup_signal);
730     signal(SIGPIPE, cleanup_signal);
731     signal(SIGTERM, cleanup_signal);
732     signal(SIGQUIT, cleanup_signal);
733     signal(SIGINT, cleanup_signal);
734     signal(SIGABRT, cleanup_signal);
735
736     if (send_intent(&ctx, INTERACTIVE, ACTION_REQUEST) < 0) {
737         deny(&ctx);
738     }
739
740     atexit(cleanup);
741
742     fd = socket_accept(socket_serv_fd);
743     if (fd < 0) {
744         deny(&ctx);
745     }
746     if (socket_send_request(fd, &ctx)) {
747         deny(&ctx);
748     }
749     if (socket_receive_result(fd, buf, sizeof(buf))) {
750         deny(&ctx);
751     }
752
753     close(fd);
754     close(socket_serv_fd);
755     socket_cleanup(&ctx);
756
757     result = buf;
758
759 #define SOCKET_RESPONSE    "socket:"
760     if (strncmp(result, SOCKET_RESPONSE, sizeof(SOCKET_RESPONSE) - 1))
761         LOGW("SECURITY RISK: Requestor still receives credentials in intent");
762     else
763         result += sizeof(SOCKET_RESPONSE) - 1;
764
765     if (!strcmp(result, "DENY")) {
766         deny(&ctx);
767     } else if (!strcmp(result, "ALLOW")) {
768         allow(&ctx);
769     } else {
770         LOGE("unknown response from Superuser Requestor: %s", result);
771         deny(&ctx);
772     }
773 }