blob: d2313e311e0b22e2daa9efc6ac36f07e84f17fe0 [file] [log] [blame]
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
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
17#define LOG_TAG "keystore"
18
19#include "permissions.h"
20
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070021#include <cutils/sockets.h>
Logan Chiencdc813f2018-04-23 13:52:28 +080022#include <log/log.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070023#include <private/android_filesystem_config.h>
24
25#include <selinux/android.h>
26
27#include "keystore_utils.h"
28
29/* perm_labels associcated with keystore_key SELinux class verbs. */
30const char* perm_labels[] = {
Shawn Willdene2a7b522017-04-11 09:27:40 -060031 "get_state",
32 "get",
33 "insert",
34 "delete",
35 "exist",
36 "list",
37 "reset",
38 "password",
39 "lock",
40 "unlock",
41 "is_empty",
42 "sign",
43 "verify",
44 "grant",
45 "duplicate",
46 "clear_uid",
47 "add_auth",
48 "user_changed",
49 "gen_unique_id",
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070050};
51
52struct user_euid {
53 uid_t uid;
54 uid_t euid;
55};
56
Branden Archer84e72312019-01-04 10:33:16 -080057user_euid user_euids[] = {{AID_VPN, AID_SYSTEM},
58 {AID_WIFI, AID_SYSTEM},
59 {AID_ROOT, AID_SYSTEM},
60 {AID_WIFI, AID_KEYSTORE},
61 {AID_KEYSTORE, AID_WIFI},
62
63#ifdef GRANT_ROOT_ALL_PERMISSIONS
64 // Allow VTS tests to act on behalf of the wifi user
65 {AID_WIFI, AID_ROOT}
66#endif
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070067};
68
69struct user_perm {
70 uid_t uid;
71 perm_t perms;
72};
73
74static user_perm user_perms[] = {
75 {AID_SYSTEM, static_cast<perm_t>((uint32_t)(~0))},
76 {AID_VPN, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY)},
77 {AID_WIFI, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY)},
Branden Archer84e72312019-01-04 10:33:16 -080078
79#ifdef GRANT_ROOT_ALL_PERMISSIONS
80 // Allow VTS tests running as root to perform all operations
81 {AID_ROOT, static_cast<perm_t>((uint32_t)(~0))},
82#else
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070083 {AID_ROOT, static_cast<perm_t>(P_GET)},
Branden Archer84e72312019-01-04 10:33:16 -080084#endif
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070085};
86
Shawn Willdene2a7b522017-04-11 09:27:40 -060087static const perm_t DEFAULT_PERMS = static_cast<perm_t>(
88 P_GET_STATE | P_GET | P_INSERT | P_DELETE | P_EXIST | P_LIST | P_SIGN | P_VERIFY |
89 P_GEN_UNIQUE_ID /* Only privileged apps can do this, but enforcement is done by SELinux */);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070090
91struct audit_data {
92 pid_t pid;
93 uid_t uid;
94};
95
96const char* get_perm_label(perm_t perm) {
97 unsigned int index = ffs(perm);
98 if (index > 0 && index <= (sizeof(perm_labels) / sizeof(perm_labels[0]))) {
99 return perm_labels[index - 1];
100 } else {
101 ALOGE("Keystore: Failed to retrieve permission label.\n");
102 abort();
103 }
104}
105
106static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len) {
107 struct audit_data* ad = reinterpret_cast<struct audit_data*>(data);
108 if (!ad) {
109 ALOGE("No keystore audit data");
110 return 0;
111 }
112
113 snprintf(buf, len, "pid=%d uid=%d", ad->pid, ad->uid);
114 return 0;
115}
116
117static char* tctx;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700118
119int configure_selinux() {
Nick Kralevichd13eef02016-12-09 17:11:22 -0800120 union selinux_callback cb;
121 cb.func_audit = audit_callback;
122 selinux_set_callback(SELINUX_CB_AUDIT, cb);
123 cb.func_log = selinux_log_callback;
124 selinux_set_callback(SELINUX_CB_LOG, cb);
125 if (getcon(&tctx) != 0) {
126 ALOGE("SELinux: Could not acquire target context. Aborting keystore.\n");
127 return -1;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700128 }
129
130 return 0;
131}
132
133static bool keystore_selinux_check_access(uid_t uid, perm_t perm, pid_t spid) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700134 audit_data ad;
Yi Kongd2916752018-07-26 17:44:27 -0700135 char* sctx = nullptr;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700136 const char* selinux_class = "keystore_key";
137 const char* str_perm = get_perm_label(perm);
138
139 if (!str_perm) {
140 return false;
141 }
142
143 if (getpidcon(spid, &sctx) != 0) {
144 ALOGE("SELinux: Failed to get source pid context.\n");
145 return false;
146 }
147
148 ad.pid = spid;
149 ad.uid = uid;
150
151 bool allowed = selinux_check_access(sctx, tctx, selinux_class, str_perm,
152 reinterpret_cast<void*>(&ad)) == 0;
153 freecon(sctx);
154 return allowed;
155}
156
157/**
158 * Returns the UID that the callingUid should act as. This is here for
159 * legacy support of the WiFi and VPN systems and should be removed
160 * when WiFi can operate in its own namespace.
161 */
162uid_t get_keystore_euid(uid_t uid) {
163 for (size_t i = 0; i < sizeof(user_euids) / sizeof(user_euids[0]); i++) {
164 struct user_euid user = user_euids[i];
165 if (user.uid == uid) {
166 return user.euid;
167 }
168 }
169
170 return uid;
171}
172
173bool has_permission(uid_t uid, perm_t perm, pid_t spid) {
174 // All system users are equivalent for multi-user support.
175 if (get_app_id(uid) == AID_SYSTEM) {
176 uid = AID_SYSTEM;
177 }
178
179 for (size_t i = 0; i < sizeof(user_perms) / sizeof(user_perms[0]); i++) {
180 struct user_perm user = user_perms[i];
181 if (user.uid == uid) {
182 return (user.perms & perm) && keystore_selinux_check_access(uid, perm, spid);
183 }
184 }
185
186 return (DEFAULT_PERMS & perm) && keystore_selinux_check_access(uid, perm, spid);
187}
188
189/**
190 * Returns true if the callingUid is allowed to interact in the targetUid's
191 * namespace.
192 */
193bool is_granted_to(uid_t callingUid, uid_t targetUid) {
194 if (callingUid == targetUid) {
195 return true;
196 }
197 for (size_t i = 0; i < sizeof(user_euids) / sizeof(user_euids[0]); i++) {
198 struct user_euid user = user_euids[i];
199 if (user.euid == callingUid && user.uid == targetUid) {
200 return true;
201 }
202 }
203
204 return false;
205}