blob: e4c138eab1dfead6504d85aaa788485facd2718b [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
Mark Salyzyn1114f182014-02-21 13:54:07 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Mark Salyzyn0175b072014-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
Mark Salyzyn1114f182014-02-21 13:54:07 -080017#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20
21#include <private/android_filesystem_config.h>
22
Mark Salyzyn0175b072014-02-26 09:50:16 -080023#include "LogCommand.h"
24
Mark Salyzync03e72c2014-02-18 11:23:53 -080025LogCommand::LogCommand(const char *cmd)
26 : FrameworkCommand(cmd) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080027}
Mark Salyzyn1114f182014-02-21 13:54:07 -080028
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
42static bool groupIsLog(char *buf) {
43 char *ptr;
44 static const char ws[] = " \n";
45 bool ret = false;
46
47 for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(NULL, ws, &ptr)) {
48 errno = 0;
49 gid_t Gid = strtol(buf, NULL, 10);
50 if (errno != 0) {
51 return false;
52 }
53 if (Gid == AID_LOG) {
54 ret = true;
55 }
56 }
57 return ret;
58}
59
60bool clientHasLogCredentials(SocketClient * cli) {
61 uid_t uid = cli->getUid();
62 if (uid == AID_ROOT) {
63 return true;
64 }
65
66 gid_t gid = cli->getGid();
Mark Salyzyn57a0af92014-05-09 17:44:18 -070067 if ((gid == AID_ROOT) || (gid == AID_SYSTEM) || (gid == AID_LOG)) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080068 return true;
69 }
70
71 // FYI We will typically be here for 'adb logcat'
72 bool ret = false;
73
74 char filename[1024];
75 snprintf(filename, sizeof(filename), "/proc/%d/status", cli->getPid());
76
77 FILE *file = fopen(filename, "r");
78 if (!file) {
79 return ret;
80 }
81
82 bool foundGid = false;
83 bool foundUid = false;
84
85 char line[1024];
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";
90
91 if (strncmp(groups_string, line, strlen(groups_string)) == 0) {
92 ret = groupIsLog(line + strlen(groups_string));
93 if (!ret) {
94 break;
95 }
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};
98
99 sscanf(line + strlen(uid_string), "%u\t%u\t%u\t%u",
100 &u[0], &u[1], &u[2], &u[3]);
101
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])) {
104 ret = false;
105 break;
106 }
107 foundUid = true;
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};
110
111 sscanf(line + strlen(gid_string), "%u\t%u\t%u\t%u",
112 &g[0], &g[1], &g[2], &g[3]);
113
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])) {
116 ret = false;
117 break;
118 }
119 foundGid = true;
120 }
121 }
122
123 fclose(file);
124
125 if (!foundGid || !foundUid) {
126 ret = false;
127 }
128
129 return ret;
130}