blob: e11f292f9ff001be5ad68d8ac0d1453325e0abea [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 <fcntl.h>
30#include <stdlib.h>
31#include <sys/cdefs.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080032#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/xattr.h>
35#include <unistd.h>
36
37#include <new>
38
39#include <async_safe/log.h>
40
41#include "prop_area.h"
Tom Cherryfd44b9f2017-11-08 14:01:00 -080042
43constexpr size_t PA_SIZE = 128 * 1024;
44constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
45constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
46
Tom Cherryf76bbf52017-11-08 14:01:00 -080047size_t prop_area::pa_size_ = 0;
48size_t prop_area::pa_data_size_ = 0;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080049
50prop_area* prop_area::map_prop_area_rw(const char* filename, const char* context,
51 bool* fsetxattr_failed) {
52 /* dev is a tmpfs that we can use to carve a shared workspace
53 * out of, so let's do that...
54 */
55 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
56
57 if (fd < 0) {
58 if (errno == EACCES) {
59 /* for consistency with the case where the process has already
60 * mapped the page in and segfaults when trying to write to it
61 */
62 abort();
63 }
64 return nullptr;
65 }
66
67 if (context) {
68 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
69 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
70 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
71 /*
72 * fsetxattr() will fail during system properties tests due to selinux policy.
73 * We do not want to create a custom policy for the tester, so we will continue in
74 * this function but set a flag that an error has occurred.
75 * Init, which is the only daemon that should ever call this function will abort
76 * when this error occurs.
77 * Otherwise, the tester will ignore it and continue, albeit without any selinux
78 * property separation.
79 */
80 if (fsetxattr_failed) {
81 *fsetxattr_failed = true;
82 }
83 }
84 }
85
86 if (ftruncate(fd, PA_SIZE) < 0) {
87 close(fd);
88 return nullptr;
89 }
90
Tom Cherryf76bbf52017-11-08 14:01:00 -080091 pa_size_ = PA_SIZE;
92 pa_data_size_ = pa_size_ - sizeof(prop_area);
Tom Cherryfd44b9f2017-11-08 14:01:00 -080093
Tom Cherryf76bbf52017-11-08 14:01:00 -080094 void* const memory_area = mmap(nullptr, pa_size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Tom Cherryfd44b9f2017-11-08 14:01:00 -080095 if (memory_area == MAP_FAILED) {
96 close(fd);
97 return nullptr;
98 }
99
100 prop_area* pa = new (memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
101
102 close(fd);
103 return pa;
104}
105
106prop_area* prop_area::map_fd_ro(const int fd) {
107 struct stat fd_stat;
108 if (fstat(fd, &fd_stat) < 0) {
109 return nullptr;
110 }
111
112 if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
113 ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
114 (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
115 return nullptr;
116 }
117
Tom Cherryf76bbf52017-11-08 14:01:00 -0800118 pa_size_ = fd_stat.st_size;
119 pa_data_size_ = pa_size_ - sizeof(prop_area);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800120
Tom Cherryf76bbf52017-11-08 14:01:00 -0800121 void* const map_result = mmap(nullptr, pa_size_, PROT_READ, MAP_SHARED, fd, 0);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800122 if (map_result == MAP_FAILED) {
123 return nullptr;
124 }
125
126 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
127 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800128 munmap(pa, pa_size_);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800129 return nullptr;
130 }
131
132 return pa;
133}
134
135prop_area* prop_area::map_prop_area(const char* filename) {
136 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
137 if (fd == -1) return nullptr;
138
139 prop_area* map_result = map_fd_ro(fd);
140 close(fd);
141
142 return map_result;
143}
144
145void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
146 const size_t aligned = __BIONIC_ALIGN(size, sizeof(uint_least32_t));
Tom Cherryf76bbf52017-11-08 14:01:00 -0800147 if (bytes_used_ + aligned > pa_data_size_) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800148 return nullptr;
149 }
150
151 *off = bytes_used_;
152 bytes_used_ += aligned;
153 return data_ + *off;
154}
155
156prop_bt* prop_area::new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off) {
157 uint_least32_t new_offset;
158 void* const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
159 if (p != nullptr) {
160 prop_bt* bt = new (p) prop_bt(name, namelen);
161 *off = new_offset;
162 return bt;
163 }
164
165 return nullptr;
166}
167
168prop_info* prop_area::new_prop_info(const char* name, uint32_t namelen, const char* value,
169 uint32_t valuelen, uint_least32_t* const off) {
170 uint_least32_t new_offset;
171 void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
172 if (p == nullptr) return nullptr;
173
174 prop_info* info;
175 if (valuelen >= PROP_VALUE_MAX) {
176 uint32_t long_value_offset = 0;
177 char* long_location = reinterpret_cast<char*>(allocate_obj(valuelen + 1, &long_value_offset));
178 if (!long_location) return nullptr;
179
180 memcpy(long_location, value, valuelen);
181 long_location[valuelen] = '\0';
182
183 // Both new_offset and long_value_offset are offsets based off of data_, however prop_info
184 // does not know what data_ is, so we change this offset to be an offset from the prop_info
185 // pointer that contains it.
186 long_value_offset -= new_offset;
187
188 info = new (p) prop_info(name, namelen, long_value_offset);
189 } else {
190 info = new (p) prop_info(name, namelen, value, valuelen);
191 }
192 *off = new_offset;
193 return info;
194}
195
196void* prop_area::to_prop_obj(uint_least32_t off) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800197 if (off > pa_data_size_) return nullptr;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800198
199 return (data_ + off);
200}
201
202inline prop_bt* prop_area::to_prop_bt(atomic_uint_least32_t* off_p) {
203 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
204 return reinterpret_cast<prop_bt*>(to_prop_obj(off));
205}
206
207inline prop_info* prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
208 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
209 return reinterpret_cast<prop_info*>(to_prop_obj(off));
210}
211
212inline prop_bt* prop_area::root_node() {
213 return reinterpret_cast<prop_bt*>(to_prop_obj(0));
214}
215
216static int cmp_prop_name(const char* one, uint32_t one_len, const char* two, uint32_t two_len) {
217 if (one_len < two_len)
218 return -1;
219 else if (one_len > two_len)
220 return 1;
221 else
222 return strncmp(one, two, one_len);
223}
224
225prop_bt* prop_area::find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen,
226 bool alloc_if_needed) {
227 prop_bt* current = bt;
228 while (true) {
229 if (!current) {
230 return nullptr;
231 }
232
233 const int ret = cmp_prop_name(name, namelen, current->name, current->namelen);
234 if (ret == 0) {
235 return current;
236 }
237
238 if (ret < 0) {
239 uint_least32_t left_offset = atomic_load_explicit(&current->left, memory_order_relaxed);
240 if (left_offset != 0) {
241 current = to_prop_bt(&current->left);
242 } else {
243 if (!alloc_if_needed) {
244 return nullptr;
245 }
246
247 uint_least32_t new_offset;
248 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
249 if (new_bt) {
250 atomic_store_explicit(&current->left, new_offset, memory_order_release);
251 }
252 return new_bt;
253 }
254 } else {
255 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
256 if (right_offset != 0) {
257 current = to_prop_bt(&current->right);
258 } else {
259 if (!alloc_if_needed) {
260 return nullptr;
261 }
262
263 uint_least32_t new_offset;
264 prop_bt* new_bt = new_prop_bt(name, namelen, &new_offset);
265 if (new_bt) {
266 atomic_store_explicit(&current->right, new_offset, memory_order_release);
267 }
268 return new_bt;
269 }
270 }
271 }
272}
273
274const prop_info* prop_area::find_property(prop_bt* const trie, const char* name, uint32_t namelen,
275 const char* value, uint32_t valuelen,
276 bool alloc_if_needed) {
277 if (!trie) return nullptr;
278
279 const char* remaining_name = name;
280 prop_bt* current = trie;
281 while (true) {
282 const char* sep = strchr(remaining_name, '.');
283 const bool want_subtree = (sep != nullptr);
284 const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
285
286 if (!substr_size) {
287 return nullptr;
288 }
289
290 prop_bt* root = nullptr;
291 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
292 if (children_offset != 0) {
293 root = to_prop_bt(&current->children);
294 } else if (alloc_if_needed) {
295 uint_least32_t new_offset;
296 root = new_prop_bt(remaining_name, substr_size, &new_offset);
297 if (root) {
298 atomic_store_explicit(&current->children, new_offset, memory_order_release);
299 }
300 }
301
302 if (!root) {
303 return nullptr;
304 }
305
306 current = find_prop_bt(root, remaining_name, substr_size, alloc_if_needed);
307 if (!current) {
308 return nullptr;
309 }
310
311 if (!want_subtree) break;
312
313 remaining_name = sep + 1;
314 }
315
316 uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
317 if (prop_offset != 0) {
318 return to_prop_info(&current->prop);
319 } else if (alloc_if_needed) {
320 uint_least32_t new_offset;
321 prop_info* new_info = new_prop_info(name, namelen, value, valuelen, &new_offset);
322 if (new_info) {
323 atomic_store_explicit(&current->prop, new_offset, memory_order_release);
324 }
325
326 return new_info;
327 } else {
328 return nullptr;
329 }
330}
331
332bool prop_area::foreach_property(prop_bt* const trie,
333 void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
334 if (!trie) return false;
335
336 uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
337 if (left_offset != 0) {
338 const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
339 if (err < 0) return false;
340 }
341 uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
342 if (prop_offset != 0) {
343 prop_info* info = to_prop_info(&trie->prop);
344 if (!info) return false;
345 propfn(info, cookie);
346 }
347 uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
348 if (children_offset != 0) {
349 const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
350 if (err < 0) return false;
351 }
352 uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
353 if (right_offset != 0) {
354 const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
355 if (err < 0) return false;
356 }
357
358 return true;
359}
360
361const prop_info* prop_area::find(const char* name) {
362 return find_property(root_node(), name, strlen(name), nullptr, 0, false);
363}
364
365bool prop_area::add(const char* name, unsigned int namelen, const char* value,
366 unsigned int valuelen) {
367 return find_property(root_node(), name, namelen, value, valuelen, true);
368}
369
370bool prop_area::foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
371 return foreach_property(root_node(), propfn, cookie);
372}