blob: b297c59dfb128f12e534aafa16df70ec0b08b4e2 [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
21#include <cutils/log.h>
22#include <cutils/sockets.h>
23#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
57user_euid user_euids[] = {
58 {AID_VPN, AID_SYSTEM}, {AID_WIFI, AID_SYSTEM}, {AID_ROOT, AID_SYSTEM},
Roshan Pius6d38eda2017-03-30 14:50:23 -070059 {AID_WIFI, AID_KEYSTORE}, {AID_KEYSTORE, AID_WIFI}
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070060};
61
62struct user_perm {
63 uid_t uid;
64 perm_t perms;
65};
66
67static user_perm user_perms[] = {
68 {AID_SYSTEM, static_cast<perm_t>((uint32_t)(~0))},
69 {AID_VPN, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY)},
70 {AID_WIFI, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY)},
Branden Archer82640d42019-02-21 13:16:11 -080071 {AID_BLUETOOTH, static_cast<perm_t>(P_GET | P_INSERT | P_DELETE | P_EXIST | P_SIGN | P_VERIFY)},
Branden Archercfe00de2019-02-22 08:13:33 -080072
73#ifdef GRANT_ROOT_ALL_PERMISSIONS
74 // Allow VTS tests running as root to perform all operations
75 {AID_ROOT, static_cast<perm_t>((uint32_t)(~0))},
76#else
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070077 {AID_ROOT, static_cast<perm_t>(P_GET)},
Branden Archercfe00de2019-02-22 08:13:33 -080078#endif
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070079};
80
Shawn Willdene2a7b522017-04-11 09:27:40 -060081static const perm_t DEFAULT_PERMS = static_cast<perm_t>(
82 P_GET_STATE | P_GET | P_INSERT | P_DELETE | P_EXIST | P_LIST | P_SIGN | P_VERIFY |
83 P_GEN_UNIQUE_ID /* Only privileged apps can do this, but enforcement is done by SELinux */);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070084
85struct audit_data {
86 pid_t pid;
87 uid_t uid;
88};
89
90const char* get_perm_label(perm_t perm) {
91 unsigned int index = ffs(perm);
92 if (index > 0 && index <= (sizeof(perm_labels) / sizeof(perm_labels[0]))) {
93 return perm_labels[index - 1];
94 } else {
95 ALOGE("Keystore: Failed to retrieve permission label.\n");
96 abort();
97 }
98}
99
100static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len) {
101 struct audit_data* ad = reinterpret_cast<struct audit_data*>(data);
102 if (!ad) {
103 ALOGE("No keystore audit data");
104 return 0;
105 }
106
107 snprintf(buf, len, "pid=%d uid=%d", ad->pid, ad->uid);
108 return 0;
109}
110
111static char* tctx;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700112
113int configure_selinux() {
Nick Kralevichd13eef02016-12-09 17:11:22 -0800114 union selinux_callback cb;
115 cb.func_audit = audit_callback;
116 selinux_set_callback(SELINUX_CB_AUDIT, cb);
117 cb.func_log = selinux_log_callback;
118 selinux_set_callback(SELINUX_CB_LOG, cb);
119 if (getcon(&tctx) != 0) {
120 ALOGE("SELinux: Could not acquire target context. Aborting keystore.\n");
121 return -1;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700122 }
123
124 return 0;
125}
126
127static bool keystore_selinux_check_access(uid_t uid, perm_t perm, pid_t spid) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700128 audit_data ad;
129 char* sctx = NULL;
130 const char* selinux_class = "keystore_key";
131 const char* str_perm = get_perm_label(perm);
132
133 if (!str_perm) {
134 return false;
135 }
136
137 if (getpidcon(spid, &sctx) != 0) {
138 ALOGE("SELinux: Failed to get source pid context.\n");
139 return false;
140 }
141
142 ad.pid = spid;
143 ad.uid = uid;
144
145 bool allowed = selinux_check_access(sctx, tctx, selinux_class, str_perm,
146 reinterpret_cast<void*>(&ad)) == 0;
147 freecon(sctx);
148 return allowed;
149}
150
151/**
152 * Returns the UID that the callingUid should act as. This is here for
153 * legacy support of the WiFi and VPN systems and should be removed
154 * when WiFi can operate in its own namespace.
155 */
156uid_t get_keystore_euid(uid_t uid) {
157 for (size_t i = 0; i < sizeof(user_euids) / sizeof(user_euids[0]); i++) {
158 struct user_euid user = user_euids[i];
159 if (user.uid == uid) {
160 return user.euid;
161 }
162 }
163
164 return uid;
165}
166
167bool has_permission(uid_t uid, perm_t perm, pid_t spid) {
168 // All system users are equivalent for multi-user support.
169 if (get_app_id(uid) == AID_SYSTEM) {
170 uid = AID_SYSTEM;
171 }
172
173 for (size_t i = 0; i < sizeof(user_perms) / sizeof(user_perms[0]); i++) {
174 struct user_perm user = user_perms[i];
175 if (user.uid == uid) {
176 return (user.perms & perm) && keystore_selinux_check_access(uid, perm, spid);
177 }
178 }
179
180 return (DEFAULT_PERMS & perm) && keystore_selinux_check_access(uid, perm, spid);
181}
182
183/**
184 * Returns true if the callingUid is allowed to interact in the targetUid's
185 * namespace.
186 */
187bool is_granted_to(uid_t callingUid, uid_t targetUid) {
188 if (callingUid == targetUid) {
189 return true;
190 }
191 for (size_t i = 0; i < sizeof(user_euids) / sizeof(user_euids[0]); i++) {
192 struct user_euid user = user_euids[i];
193 if (user.euid == callingUid && user.uid == targetUid) {
194 return true;
195 }
196 }
197
198 return false;
199}