blob: b8b0bd0c2d1946dbb143c92f63b8cdcb636ee95d [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
Mark Salyzyne6477492014-02-21 13:54:07 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Mark Salyzyn12bac902014-02-26 09:50:16 -08003 *
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 Cherry134096f2020-05-15 10:13:38 -070017#include "LogPermissions.h"
18
Mark Salyzyne6477492014-02-21 13:54:07 -080019#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
Mark Salyzyn1463b3d2015-04-01 09:10:04 -070022#include <string.h>
Tom Cherry5fe06cb2020-05-01 15:34:47 -070023#include <sys/socket.h>
24
25#include <vector>
Mark Salyzyne6477492014-02-21 13:54:07 -080026
27#include <private/android_filesystem_config.h>
28
Rubin Xu24f69a12022-05-11 21:41:26 +010029static bool checkGroup(char* buf, gid_t gidToCheck) {
Mark Salyzyn65059532017-03-10 14:31:54 -080030 char* ptr;
Mark Salyzyne6477492014-02-21 13:54:07 -080031 static const char ws[] = " \n";
Mark Salyzyne6477492014-02-21 13:54:07 -080032
Yi Kong36feb152018-07-13 17:39:22 -070033 for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(nullptr, ws, &ptr)) {
Mark Salyzyne6477492014-02-21 13:54:07 -080034 errno = 0;
Yi Kong36feb152018-07-13 17:39:22 -070035 gid_t Gid = strtol(buf, nullptr, 10);
Mark Salyzyne6477492014-02-21 13:54:07 -080036 if (errno != 0) {
37 return false;
38 }
Rubin Xu24f69a12022-05-11 21:41:26 +010039 if (Gid == gidToCheck) {
Mark Salyzyne0884912015-09-01 08:40:39 -070040 return true;
Mark Salyzyne6477492014-02-21 13:54:07 -080041 }
42 }
Mark Salyzyne0884912015-09-01 08:40:39 -070043 return false;
Mark Salyzyne6477492014-02-21 13:54:07 -080044}
45
Tom Cherry5fe06cb2020-05-01 15:34:47 -070046static bool UserIsPrivileged(int id) {
47 return id == AID_ROOT || id == AID_SYSTEM || id == AID_LOG;
48}
Mark Salyzyne6477492014-02-21 13:54:07 -080049
Tom Cherry5fe06cb2020-05-01 15:34:47 -070050// gets a list of supplementary group IDs associated with
51// the socket peer. This is implemented by opening
52// /proc/PID/status and look for the "Group:" line.
53//
54// This function introduces races especially since status
55// can change 'shape' while reading, the net result is err
56// on lack of permission.
Rubin Xu24f69a12022-05-11 21:41:26 +010057static bool checkSupplementaryGroup(uid_t uid, gid_t gid, pid_t pid, gid_t gidToCheck) {
Mark Salyzyne0884912015-09-01 08:40:39 -070058 char filename[256];
Mark Salyzync9690862015-12-04 10:59:45 -080059 snprintf(filename, sizeof(filename), "/proc/%u/status", pid);
Mark Salyzyne6477492014-02-21 13:54:07 -080060
Mark Salyzyne0884912015-09-01 08:40:39 -070061 bool ret;
Rubin Xu24f69a12022-05-11 21:41:26 +010062 bool foundGroup = false;
Mark Salyzyne6477492014-02-21 13:54:07 -080063 bool foundGid = false;
64 bool foundUid = false;
65
Mark Salyzyne0884912015-09-01 08:40:39 -070066 //
67 // Reading /proc/<pid>/status is rife with race conditions. All of /proc
Tom Cherry5fe06cb2020-05-01 15:34:47 -070068 // suffers from this and its use should be minimized.
Mark Salyzyne0884912015-09-01 08:40:39 -070069 //
70 // Notably the content from one 4KB page to the next 4KB page can be from a
71 // change in shape even if we are gracious enough to attempt to read
72 // atomically. getline can not even guarantee a page read is not split up
73 // and in effect can read from different vintages of the content.
74 //
75 // We are finding out in the field that a 'logcat -c' via adb occasionally
76 // is returned with permission denied when we did only one pass and thus
77 // breaking scripts. For security we still err on denying access if in
78 // doubt, but we expect the falses should be reduced significantly as
79 // three times is a charm.
80 //
Rubin Xu24f69a12022-05-11 21:41:26 +010081 for (int retry = 3; !(ret = foundGid && foundUid && foundGroup) && retry; --retry) {
Tom Cherry17b77582020-06-17 11:40:55 -070082 FILE* file = fopen(filename, "re");
Mark Salyzyne0884912015-09-01 08:40:39 -070083 if (!file) {
84 continue;
Mark Salyzyne6477492014-02-21 13:54:07 -080085 }
Mark Salyzyne6477492014-02-21 13:54:07 -080086
Yi Kong36feb152018-07-13 17:39:22 -070087 char* line = nullptr;
Mark Salyzyne0884912015-09-01 08:40:39 -070088 size_t len = 0;
89 while (getline(&line, &len, file) > 0) {
90 static const char groups_string[] = "Groups:\t";
91 static const char uid_string[] = "Uid:\t";
92 static const char gid_string[] = "Gid:\t";
Mark Salyzyne6477492014-02-21 13:54:07 -080093
Mark Salyzyne0884912015-09-01 08:40:39 -070094 if (strncmp(groups_string, line, sizeof(groups_string) - 1) == 0) {
Rubin Xu24f69a12022-05-11 21:41:26 +010095 if (checkGroup(line + sizeof(groups_string) - 1, gidToCheck)) {
96 foundGroup = true;
Mark Salyzyne0884912015-09-01 08:40:39 -070097 }
98 } else if (strncmp(uid_string, line, sizeof(uid_string) - 1) == 0) {
Mark Salyzyn65059532017-03-10 14:31:54 -080099 uid_t u[4] = { (uid_t)-1, (uid_t)-1, (uid_t)-1, (uid_t)-1 };
Mark Salyzyne0884912015-09-01 08:40:39 -0700100
Mark Salyzyn65059532017-03-10 14:31:54 -0800101 sscanf(line + sizeof(uid_string) - 1, "%u\t%u\t%u\t%u", &u[0],
102 &u[1], &u[2], &u[3]);
Mark Salyzyne0884912015-09-01 08:40:39 -0700103
104 // Protect against PID reuse by checking that UID is the same
Mark Salyzyn65059532017-03-10 14:31:54 -0800105 if ((uid == u[0]) && (uid == u[1]) && (uid == u[2]) &&
106 (uid == u[3])) {
Mark Salyzyne0884912015-09-01 08:40:39 -0700107 foundUid = true;
108 }
109 } else if (strncmp(gid_string, line, sizeof(gid_string) - 1) == 0) {
Mark Salyzyn65059532017-03-10 14:31:54 -0800110 gid_t g[4] = { (gid_t)-1, (gid_t)-1, (gid_t)-1, (gid_t)-1 };
Mark Salyzyne0884912015-09-01 08:40:39 -0700111
Mark Salyzyn65059532017-03-10 14:31:54 -0800112 sscanf(line + sizeof(gid_string) - 1, "%u\t%u\t%u\t%u", &g[0],
113 &g[1], &g[2], &g[3]);
Mark Salyzyne0884912015-09-01 08:40:39 -0700114
115 // Protect against PID reuse by checking that GID is the same
Mark Salyzyn65059532017-03-10 14:31:54 -0800116 if ((gid == g[0]) && (gid == g[1]) && (gid == g[2]) &&
117 (gid == g[3])) {
Mark Salyzyne0884912015-09-01 08:40:39 -0700118 foundGid = true;
119 }
120 }
121 }
122 free(line);
123 fclose(file);
Mark Salyzyne6477492014-02-21 13:54:07 -0800124 }
125
126 return ret;
127}
Mark Salyzync9690862015-12-04 10:59:45 -0800128
Rubin Xu24f69a12022-05-11 21:41:26 +0100129bool clientCanWriteSecurityLog(uid_t uid, gid_t gid, pid_t pid) {
130 if (UserIsPrivileged(uid) || UserIsPrivileged(gid)) {
131 return true;
132 }
133 return checkSupplementaryGroup(uid, gid, pid, AID_SECURITY_LOG_WRITER) ||
134 checkSupplementaryGroup(uid, gid, pid, AID_LOG);
135}
136
137bool clientHasLogCredentials(uid_t uid, gid_t gid, pid_t pid) {
138 if (UserIsPrivileged(uid) || UserIsPrivileged(gid)) {
139 return true;
140 }
141 // FYI We will typically be here for 'adb logcat'
142 return checkSupplementaryGroup(uid, gid, pid, AID_LOG);
143}
144
Mark Salyzyn65059532017-03-10 14:31:54 -0800145bool clientHasLogCredentials(SocketClient* cli) {
Tom Cherry5fe06cb2020-05-01 15:34:47 -0700146 if (UserIsPrivileged(cli->getUid()) || UserIsPrivileged(cli->getGid())) {
147 return true;
148 }
149
150 // Kernel version 4.13 added SO_PEERGROUPS to return the supplemental groups of a peer socket,
151 // so try that first then fallback to the above racy checking of /proc/<pid>/status if the
152 // kernel is too old. Per
153 // https://source.android.com/devices/architecture/kernel/android-common, the fallback can be
154 // removed no earlier than 2024.
155 auto supplemental_groups = std::vector<gid_t>(16, -1);
156 socklen_t groups_size = supplemental_groups.size() * sizeof(gid_t);
157
158 int result = getsockopt(cli->getSocket(), SOL_SOCKET, SO_PEERGROUPS, supplemental_groups.data(),
159 &groups_size);
160
161 if (result != 0) {
162 if (errno != ERANGE) {
163 return clientHasLogCredentials(cli->getUid(), cli->getGid(), cli->getPid());
164 }
165
166 supplemental_groups.resize(groups_size / sizeof(gid_t), -1);
167 result = getsockopt(cli->getSocket(), SOL_SOCKET, SO_PEERGROUPS, supplemental_groups.data(),
168 &groups_size);
169
170 // There is still some error after resizing supplemental_groups, fallback.
171 if (result != 0) {
172 return clientHasLogCredentials(cli->getUid(), cli->getGid(), cli->getPid());
173 }
174 }
175
176 supplemental_groups.resize(groups_size / sizeof(gid_t), -1);
177 for (const auto& gid : supplemental_groups) {
178 if (UserIsPrivileged(gid)) {
179 return true;
180 }
181 }
182
183 return false;
Mark Salyzync9690862015-12-04 10:59:45 -0800184}
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -0800185
186bool clientIsExemptedFromUserConsent(SocketClient* cli) {
Wenhao Wang9b132732022-02-08 08:13:15 -0800187 return cli->getUid() < AID_APP_START;
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -0800188}