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