| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2012-2014 The Android Open Source Project | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 17 | #include <errno.h> | 
|  | 18 | #include <stdio.h> | 
|  | 19 | #include <stdlib.h> | 
| Mark Salyzyn | 801bcec | 2015-04-01 09:10:04 -0700 | [diff] [blame] | 20 | #include <string.h> | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 21 |  | 
|  | 22 | #include <private/android_filesystem_config.h> | 
|  | 23 |  | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 24 | #include "LogCommand.h" | 
|  | 25 |  | 
| Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 26 | LogCommand::LogCommand(const char *cmd) : FrameworkCommand(cmd) { | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | } | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | // gets a list of supplementary group IDs associated with | 
|  | 30 | // the socket peer.  This is implemented by opening | 
|  | 31 | // /proc/PID/status and look for the "Group:" line. | 
|  | 32 | // | 
|  | 33 | // This function introduces races especially since status | 
|  | 34 | // can change 'shape' while reading, the net result is err | 
|  | 35 | // on lack of permission. | 
|  | 36 | // | 
|  | 37 | // Race-free alternative is to introduce pairs of sockets | 
|  | 38 | // and threads for each command and reading, one each that | 
|  | 39 | // has open permissions, and one that has restricted | 
|  | 40 | // permissions. | 
|  | 41 |  | 
|  | 42 | static bool groupIsLog(char *buf) { | 
|  | 43 | char *ptr; | 
|  | 44 | static const char ws[] = " \n"; | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 45 |  | 
|  | 46 | for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(NULL, ws, &ptr)) { | 
|  | 47 | errno = 0; | 
|  | 48 | gid_t Gid = strtol(buf, NULL, 10); | 
|  | 49 | if (errno != 0) { | 
|  | 50 | return false; | 
|  | 51 | } | 
|  | 52 | if (Gid == AID_LOG) { | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 53 | return true; | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 54 | } | 
|  | 55 | } | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 56 | return false; | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
|  | 59 | bool clientHasLogCredentials(SocketClient * cli) { | 
|  | 60 | uid_t uid = cli->getUid(); | 
|  | 61 | if (uid == AID_ROOT) { | 
|  | 62 | return true; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | gid_t gid = cli->getGid(); | 
| Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 66 | if ((gid == AID_ROOT) || (gid == AID_SYSTEM) || (gid == AID_LOG)) { | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 67 | return true; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | // FYI We will typically be here for 'adb logcat' | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 71 | char filename[256]; | 
|  | 72 | snprintf(filename, sizeof(filename), "/proc/%u/status", cli->getPid()); | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 73 |  | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 74 | bool ret; | 
|  | 75 | bool foundLog = false; | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 76 | bool foundGid = false; | 
|  | 77 | bool foundUid = false; | 
|  | 78 |  | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 79 | // | 
|  | 80 | // Reading /proc/<pid>/status is rife with race conditions. All of /proc | 
|  | 81 | // suffers from this and its use should be minimized. However, we have no | 
|  | 82 | // choice. | 
|  | 83 | // | 
|  | 84 | // Notably the content from one 4KB page to the next 4KB page can be from a | 
|  | 85 | // change in shape even if we are gracious enough to attempt to read | 
|  | 86 | // atomically. getline can not even guarantee a page read is not split up | 
|  | 87 | // and in effect can read from different vintages of the content. | 
|  | 88 | // | 
|  | 89 | // We are finding out in the field that a 'logcat -c' via adb occasionally | 
|  | 90 | // is returned with permission denied when we did only one pass and thus | 
|  | 91 | // breaking scripts. For security we still err on denying access if in | 
|  | 92 | // doubt, but we expect the falses  should be reduced significantly as | 
|  | 93 | // three times is a charm. | 
|  | 94 | // | 
|  | 95 | for (int retry = 3; | 
|  | 96 | !(ret = foundGid && foundUid && foundLog) && retry; | 
|  | 97 | --retry) { | 
|  | 98 | FILE *file = fopen(filename, "r"); | 
|  | 99 | if (!file) { | 
|  | 100 | continue; | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 101 | } | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 102 |  | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 103 | char *line = NULL; | 
|  | 104 | size_t len = 0; | 
|  | 105 | while (getline(&line, &len, file) > 0) { | 
|  | 106 | static const char groups_string[] = "Groups:\t"; | 
|  | 107 | static const char uid_string[] = "Uid:\t"; | 
|  | 108 | static const char gid_string[] = "Gid:\t"; | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 109 |  | 
| Mark Salyzyn | 86eb38f | 2015-09-01 08:40:39 -0700 | [diff] [blame] | 110 | if (strncmp(groups_string, line, sizeof(groups_string) - 1) == 0) { | 
|  | 111 | if (groupIsLog(line + sizeof(groups_string) - 1)) { | 
|  | 112 | foundLog = true; | 
|  | 113 | } | 
|  | 114 | } else if (strncmp(uid_string, line, sizeof(uid_string) - 1) == 0) { | 
|  | 115 | uid_t u[4] = { (uid_t) -1, (uid_t) -1, (uid_t) -1, (uid_t) -1}; | 
|  | 116 |  | 
|  | 117 | sscanf(line + sizeof(uid_string) - 1, "%u\t%u\t%u\t%u", | 
|  | 118 | &u[0], &u[1], &u[2], &u[3]); | 
|  | 119 |  | 
|  | 120 | // Protect against PID reuse by checking that UID is the same | 
|  | 121 | if ((uid == u[0]) | 
|  | 122 | && (uid == u[1]) | 
|  | 123 | && (uid == u[2]) | 
|  | 124 | && (uid == u[3])) { | 
|  | 125 | foundUid = true; | 
|  | 126 | } | 
|  | 127 | } else if (strncmp(gid_string, line, sizeof(gid_string) - 1) == 0) { | 
|  | 128 | gid_t g[4] = { (gid_t) -1, (gid_t) -1, (gid_t) -1, (gid_t) -1}; | 
|  | 129 |  | 
|  | 130 | sscanf(line + sizeof(gid_string) - 1, "%u\t%u\t%u\t%u", | 
|  | 131 | &g[0], &g[1], &g[2], &g[3]); | 
|  | 132 |  | 
|  | 133 | // Protect against PID reuse by checking that GID is the same | 
|  | 134 | if ((gid == g[0]) | 
|  | 135 | && (gid == g[1]) | 
|  | 136 | && (gid == g[2]) | 
|  | 137 | && (gid == g[3])) { | 
|  | 138 | foundGid = true; | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 | free(line); | 
|  | 143 | fclose(file); | 
| Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | return ret; | 
|  | 147 | } |