blob: 77f20691c587eb1e3505e1ec7fae92de411c7168 [file] [log] [blame]
Tom Cherryfd44b9f2017-11-08 14:01:00 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "contexts_split.h"
30
31#include <ctype.h>
32#include <stdlib.h>
33#include <string.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080034#include <sys/stat.h>
35
36#include <async_safe/log.h>
37
38#include "context_node.h"
Tom Cherryf76bbf52017-11-08 14:01:00 -080039#include "property_filename.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080040
41class ContextListNode : public ContextNode {
42 public:
43 ContextListNode(ContextListNode* next, const char* context)
44 : ContextNode(strdup(context)), next(next) {
45 }
46
47 ~ContextListNode() {
48 free(const_cast<char*>(context()));
49 }
50
51 ContextListNode* next;
52};
53
54struct PrefixNode {
55 PrefixNode(struct PrefixNode* next, const char* prefix, ContextListNode* context)
56 : prefix(strdup(prefix)), prefix_len(strlen(prefix)), context(context), next(next) {
57 }
58 ~PrefixNode() {
59 free(prefix);
60 }
61 char* prefix;
62 const size_t prefix_len;
63 ContextListNode* context;
64 PrefixNode* next;
65};
66
67template <typename List, typename... Args>
68static inline void ListAdd(List** list, Args... args) {
69 *list = new List(*list, args...);
70}
71
72static void ListAddAfterLen(PrefixNode** list, const char* prefix, ContextListNode* context) {
73 size_t prefix_len = strlen(prefix);
74
75 auto next_list = list;
76
77 while (*next_list) {
78 if ((*next_list)->prefix_len < prefix_len || (*next_list)->prefix[0] == '*') {
79 ListAdd(next_list, prefix, context);
80 return;
81 }
82 next_list = &(*next_list)->next;
83 }
84 ListAdd(next_list, prefix, context);
85}
86
87template <typename List, typename Func>
88static void ListForEach(List* list, Func func) {
89 while (list) {
90 func(list);
91 list = list->next;
92 }
93}
94
95template <typename List, typename Func>
96static List* ListFind(List* list, Func func) {
97 while (list) {
98 if (func(list)) {
99 return list;
100 }
101 list = list->next;
102 }
103 return nullptr;
104}
105
106template <typename List>
107static void ListFree(List** list) {
108 while (*list) {
109 auto old_list = *list;
110 *list = old_list->next;
111 delete old_list;
112 }
113}
114
115// The below two functions are duplicated from label_support.c in libselinux.
116// TODO: Find a location suitable for these functions such that both libc and
117// libselinux can share a common source file.
118
119// The read_spec_entries and read_spec_entry functions may be used to
120// replace sscanf to read entries from spec files. The file and
121// property services now use these.
122
123// Read an entry from a spec file (e.g. file_contexts)
124static inline int read_spec_entry(char** entry, char** ptr, int* len) {
125 *entry = nullptr;
126 char* tmp_buf = nullptr;
127
128 while (isspace(**ptr) && **ptr != '\0') (*ptr)++;
129
130 tmp_buf = *ptr;
131 *len = 0;
132
133 while (!isspace(**ptr) && **ptr != '\0') {
134 (*ptr)++;
135 (*len)++;
136 }
137
138 if (*len) {
139 *entry = strndup(tmp_buf, *len);
140 if (!*entry) return -1;
141 }
142
143 return 0;
144}
145
146// line_buf - Buffer containing the spec entries .
147// num_args - The number of spec parameter entries to process.
148// ... - A 'char **spec_entry' for each parameter.
149// returns - The number of items processed.
150//
151// This function calls read_spec_entry() to do the actual string processing.
152static int read_spec_entries(char* line_buf, int num_args, ...) {
153 char **spec_entry, *buf_p;
154 int len, rc, items, entry_len = 0;
155 va_list ap;
156
157 len = strlen(line_buf);
158 if (line_buf[len - 1] == '\n')
159 line_buf[len - 1] = '\0';
160 else
161 // Handle case if line not \n terminated by bumping
162 // the len for the check below (as the line is NUL
163 // terminated by getline(3))
164 len++;
165
166 buf_p = line_buf;
167 while (isspace(*buf_p)) buf_p++;
168
169 // Skip comment lines and empty lines.
170 if (*buf_p == '#' || *buf_p == '\0') return 0;
171
172 // Process the spec file entries
173 va_start(ap, num_args);
174
175 items = 0;
176 while (items < num_args) {
177 spec_entry = va_arg(ap, char**);
178
179 if (len - 1 == buf_p - line_buf) {
180 va_end(ap);
181 return items;
182 }
183
184 rc = read_spec_entry(spec_entry, &buf_p, &entry_len);
185 if (rc < 0) {
186 va_end(ap);
187 return rc;
188 }
189 if (entry_len) items++;
190 }
191 va_end(ap);
192 return items;
193}
194
Tom Cherryf76bbf52017-11-08 14:01:00 -0800195bool ContextsSplit::MapSerialPropertyArea(bool access_rw, bool* fsetxattr_failed) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800196 char filename[PROP_FILENAME_MAX];
197 int len = async_safe_format_buffer(filename, sizeof(filename), "%s/properties_serial",
198 property_filename);
199 if (len < 0 || len > PROP_FILENAME_MAX) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800200 serial_prop_area_ = nullptr;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800201 return false;
202 }
203
204 if (access_rw) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800205 serial_prop_area_ =
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800206 prop_area::map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed);
207 } else {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800208 serial_prop_area_ = prop_area::map_prop_area(filename);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800209 }
Tom Cherryf76bbf52017-11-08 14:01:00 -0800210 return serial_prop_area_;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800211}
212
213bool ContextsSplit::InitializePropertiesFromFile(const char* filename) {
214 FILE* file = fopen(filename, "re");
215 if (!file) {
216 return false;
217 }
218
219 char* buffer = nullptr;
220 size_t line_len;
221 char* prop_prefix = nullptr;
222 char* context = nullptr;
223
224 while (getline(&buffer, &line_len, file) > 0) {
225 int items = read_spec_entries(buffer, 2, &prop_prefix, &context);
226 if (items <= 0) {
227 continue;
228 }
229 if (items == 1) {
230 free(prop_prefix);
231 continue;
232 }
233
234 // init uses ctl.* properties as an IPC mechanism and does not write them
235 // to a property file, therefore we do not need to create property files
236 // to store them.
237 if (!strncmp(prop_prefix, "ctl.", 4)) {
238 free(prop_prefix);
239 free(context);
240 continue;
241 }
242
243 auto old_context = ListFind(
244 contexts_, [context](ContextListNode* l) { return !strcmp(l->context(), context); });
245 if (old_context) {
246 ListAddAfterLen(&prefixes_, prop_prefix, old_context);
247 } else {
248 ListAdd(&contexts_, context);
249 ListAddAfterLen(&prefixes_, prop_prefix, contexts_);
250 }
251 free(prop_prefix);
252 free(context);
253 }
254
255 free(buffer);
256 fclose(file);
257
258 return true;
259}
260
261bool ContextsSplit::InitializeProperties() {
262 // If we do find /property_contexts, then this is being
263 // run as part of the OTA updater on older release that had
264 // /property_contexts - b/34370523
265 if (InitializePropertiesFromFile("/property_contexts")) {
266 return true;
267 }
268
269 // Use property_contexts from /system & /vendor, fall back to those from /
270 if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
271 if (!InitializePropertiesFromFile("/system/etc/selinux/plat_property_contexts")) {
272 return false;
273 }
274 // Don't check for failure here, so we always have a sane list of properties.
275 // E.g. In case of recovery, the vendor partition will not have mounted and we
276 // still need the system / platform properties to function.
277 InitializePropertiesFromFile("/vendor/etc/selinux/nonplat_property_contexts");
278 } else {
279 if (!InitializePropertiesFromFile("/plat_property_contexts")) {
280 return false;
281 }
282 InitializePropertiesFromFile("/nonplat_property_contexts");
283 }
284
285 return true;
286}
287
288bool ContextsSplit::Initialize(bool writable) {
289 if (!InitializeProperties()) {
290 return false;
291 }
292
293 if (writable) {
294 mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH);
295 bool open_failed = false;
296 bool fsetxattr_failed = false;
297 ListForEach(contexts_, [&fsetxattr_failed, &open_failed](ContextListNode* l) {
298 if (!l->Open(true, &fsetxattr_failed)) {
299 open_failed = true;
300 }
301 });
Tom Cherryf76bbf52017-11-08 14:01:00 -0800302 if (open_failed || !MapSerialPropertyArea(true, &fsetxattr_failed)) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800303 FreeAndUnmap();
304 return false;
305 }
306
307 return !fsetxattr_failed;
308 } else {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800309 if (!MapSerialPropertyArea(false, nullptr)) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800310 FreeAndUnmap();
311 return false;
312 }
313 }
314 return true;
315}
316
317prop_area* ContextsSplit::GetPropAreaForName(const char* name) {
318 auto entry = ListFind(prefixes_, [name](PrefixNode* l) {
319 return l->prefix[0] == '*' || !strncmp(l->prefix, name, l->prefix_len);
320 });
321 if (!entry) {
322 return nullptr;
323 }
324
325 auto cnode = entry->context;
326 if (!cnode->pa()) {
327 // We explicitly do not check no_access_ in this case because unlike the
328 // case of foreach(), we want to generate an selinux audit for each
329 // non-permitted property access in this function.
330 cnode->Open(false, nullptr);
331 }
332 return cnode->pa();
333}
334
335void ContextsSplit::ForEach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
336 ListForEach(contexts_, [propfn, cookie](ContextListNode* l) {
337 if (l->CheckAccessAndOpen()) {
338 l->pa()->foreach (propfn, cookie);
339 }
340 });
341}
342
343void ContextsSplit::ResetAccess() {
344 ListForEach(contexts_, [](ContextListNode* l) { l->ResetAccess(); });
345}
346
347void ContextsSplit::FreeAndUnmap() {
348 ListFree(&prefixes_);
349 ListFree(&contexts_);
Tom Cherryf76bbf52017-11-08 14:01:00 -0800350 prop_area::unmap_prop_area(&serial_prop_area_);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800351}