2 * Copyright (C) 2012-2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <private/android_filesystem_config.h>
23 #include "LogCommand.h"
25 LogCommand::LogCommand(const char *cmd)
26 : FrameworkCommand(cmd) {
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.
33 // This function introduces races especially since status
34 // can change 'shape' while reading, the net result is err
35 // on lack of permission.
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
42 static bool groupIsLog(char *buf) {
44 static const char ws[] = " \n";
47 for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(NULL, ws, &ptr)) {
49 gid_t Gid = strtol(buf, NULL, 10);
60 bool clientHasLogCredentials(SocketClient * cli) {
61 uid_t uid = cli->getUid();
62 if (uid == AID_ROOT) {
66 gid_t gid = cli->getGid();
67 if ((gid == AID_ROOT) || (gid == AID_SYSTEM) || (gid == AID_LOG)) {
71 // FYI We will typically be here for 'adb logcat'
75 snprintf(filename, sizeof(filename), "/proc/%d/status", cli->getPid());
77 FILE *file = fopen(filename, "r");
82 bool foundGid = false;
83 bool foundUid = false;
86 while (fgets(line, sizeof(line), file)) {
87 static const char groups_string[] = "Groups:\t";
88 static const char uid_string[] = "Uid:\t";
89 static const char gid_string[] = "Gid:\t";
91 if (strncmp(groups_string, line, strlen(groups_string)) == 0) {
92 ret = groupIsLog(line + strlen(groups_string));
96 } else if (strncmp(uid_string, line, strlen(uid_string)) == 0) {
97 uid_t u[4] = { (uid_t) -1, (uid_t) -1, (uid_t) -1, (uid_t) -1};
99 sscanf(line + strlen(uid_string), "%u\t%u\t%u\t%u",
100 &u[0], &u[1], &u[2], &u[3]);
102 // Protect against PID reuse by checking that the UID is the same
103 if ((uid != u[0]) || (uid != u[1]) || (uid != u[2]) || (uid != u[3])) {
108 } else if (strncmp(gid_string, line, strlen(gid_string)) == 0) {
109 gid_t g[4] = { (gid_t) -1, (gid_t) -1, (gid_t) -1, (gid_t) -1};
111 sscanf(line + strlen(gid_string), "%u\t%u\t%u\t%u",
112 &g[0], &g[1], &g[2], &g[3]);
114 // Protect against PID reuse by checking that the GID is the same
115 if ((gid != g[0]) || (gid != g[1]) || (gid != g[2]) || (gid != g[3])) {
125 if (!foundGid || !foundUid) {