blob: 4668eed28c7f6d2aa4998e92968489aa332f7b4b [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
Tom Cherrye275d6d2017-12-11 23:31:33 -080029#include "system_properties/prop_area.h"
30
Dan Albert3429c092017-12-12 15:42:45 -080031#include <errno.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080032#include <fcntl.h>
33#include <stdlib.h>
34#include <sys/cdefs.h>
Tom Cherryfd44b9f2017-11-08 14:01:00 -080035#include <sys/stat.h>
36#include <sys/types.h>
37#include <sys/xattr.h>
38#include <unistd.h>
39
40#include <new>
41
42#include <async_safe/log.h>
43
Dennis Shen3d1ce8c2024-01-04 14:18:38 +000044constexpr size_t PA_SIZE = 128 * 1024;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080045constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
46constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;
47
Tom Cherryf76bbf52017-11-08 14:01:00 -080048size_t prop_area::pa_size_ = 0;
49size_t prop_area::pa_data_size_ = 0;
Tom Cherryfd44b9f2017-11-08 14:01:00 -080050
51prop_area* prop_area::map_prop_area_rw(const char* filename, const char* context,
52 bool* fsetxattr_failed) {
53 /* dev is a tmpfs that we can use to carve a shared workspace
54 * out of, so let's do that...
55 */
56 const int fd = open(filename, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_EXCL, 0444);
57
58 if (fd < 0) {
59 if (errno == EACCES) {
60 /* for consistency with the case where the process has already
61 * mapped the page in and segfaults when trying to write to it
62 */
63 abort();
64 }
65 return nullptr;
66 }
67
68 if (context) {
69 if (fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0) != 0) {
70 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
71 "fsetxattr failed to set context (%s) for \"%s\"", context, filename);
72 /*
73 * fsetxattr() will fail during system properties tests due to selinux policy.
74 * We do not want to create a custom policy for the tester, so we will continue in
75 * this function but set a flag that an error has occurred.
76 * Init, which is the only daemon that should ever call this function will abort
77 * when this error occurs.
78 * Otherwise, the tester will ignore it and continue, albeit without any selinux
79 * property separation.
80 */
81 if (fsetxattr_failed) {
82 *fsetxattr_failed = true;
83 }
84 }
85 }
86
87 if (ftruncate(fd, PA_SIZE) < 0) {
88 close(fd);
89 return nullptr;
90 }
91
Tom Cherryf76bbf52017-11-08 14:01:00 -080092 pa_size_ = PA_SIZE;
93 pa_data_size_ = pa_size_ - sizeof(prop_area);
Tom Cherryfd44b9f2017-11-08 14:01:00 -080094
Tom Cherryf76bbf52017-11-08 14:01:00 -080095 void* const memory_area = mmap(nullptr, pa_size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Tom Cherryfd44b9f2017-11-08 14:01:00 -080096 if (memory_area == MAP_FAILED) {
97 close(fd);
98 return nullptr;
99 }
100
101 prop_area* pa = new (memory_area) prop_area(PROP_AREA_MAGIC, PROP_AREA_VERSION);
102
103 close(fd);
104 return pa;
105}
106
107prop_area* prop_area::map_fd_ro(const int fd) {
108 struct stat fd_stat;
109 if (fstat(fd, &fd_stat) < 0) {
110 return nullptr;
111 }
112
113 if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
114 ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
115 (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
116 return nullptr;
117 }
118
Tom Cherryf76bbf52017-11-08 14:01:00 -0800119 pa_size_ = fd_stat.st_size;
120 pa_data_size_ = pa_size_ - sizeof(prop_area);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800121
Tom Cherryf76bbf52017-11-08 14:01:00 -0800122 void* const map_result = mmap(nullptr, pa_size_, PROT_READ, MAP_SHARED, fd, 0);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800123 if (map_result == MAP_FAILED) {
124 return nullptr;
125 }
126
127 prop_area* pa = reinterpret_cast<prop_area*>(map_result);
128 if ((pa->magic() != PROP_AREA_MAGIC) || (pa->version() != PROP_AREA_VERSION)) {
Tom Cherryf76bbf52017-11-08 14:01:00 -0800129 munmap(pa, pa_size_);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800130 return nullptr;
131 }
132
133 return pa;
134}
135
136prop_area* prop_area::map_prop_area(const char* filename) {
137 int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
138 if (fd == -1) return nullptr;
139
140 prop_area* map_result = map_fd_ro(fd);
141 close(fd);
142
143 return map_result;
144}
145
146void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
147 const size_t aligned = __BIONIC_ALIGN(size, sizeof(uint_least32_t));
Tom Cherryf76bbf52017-11-08 14:01:00 -0800148 if (bytes_used_ + aligned > pa_data_size_) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800149 return nullptr;
150 }
151
152 *off = bytes_used_;
153 bytes_used_ += aligned;
154 return data_ + *off;
155}
156
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000157prop_trie_node* prop_area::new_prop_trie_node(const char* name, uint32_t namelen,
158 uint_least32_t* const off) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800159 uint_least32_t new_offset;
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000160 void* const p = allocate_obj(sizeof(prop_trie_node) + namelen + 1, &new_offset);
161 if (p == nullptr) return nullptr;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800162
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000163 prop_trie_node* node = new (p) prop_trie_node(name, namelen);
164 *off = new_offset;
165 return node;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800166}
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
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000202inline prop_trie_node* prop_area::to_prop_trie_node(atomic_uint_least32_t* off_p) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800203 uint_least32_t off = atomic_load_explicit(off_p, memory_order_consume);
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000204 return reinterpret_cast<prop_trie_node*>(to_prop_obj(off));
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800205}
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
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000212inline prop_trie_node* prop_area::root_node() {
213 return reinterpret_cast<prop_trie_node*>(to_prop_obj(0));
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800214}
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
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000225prop_trie_node* prop_area::find_prop_trie_node(prop_trie_node* const trie, const char* name,
226 uint32_t namelen, bool alloc_if_needed) {
227 prop_trie_node* current = trie;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800228 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) {
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000241 current = to_prop_trie_node(&current->left);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800242 } else {
243 if (!alloc_if_needed) {
244 return nullptr;
245 }
246
247 uint_least32_t new_offset;
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000248 prop_trie_node* new_node = new_prop_trie_node(name, namelen, &new_offset);
249 if (new_node) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800250 atomic_store_explicit(&current->left, new_offset, memory_order_release);
251 }
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000252 return new_node;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800253 }
254 } else {
255 uint_least32_t right_offset = atomic_load_explicit(&current->right, memory_order_relaxed);
256 if (right_offset != 0) {
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000257 current = to_prop_trie_node(&current->right);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800258 } else {
259 if (!alloc_if_needed) {
260 return nullptr;
261 }
262
263 uint_least32_t new_offset;
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000264 prop_trie_node* new_node = new_prop_trie_node(name, namelen, &new_offset);
265 if (new_node) {
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800266 atomic_store_explicit(&current->right, new_offset, memory_order_release);
267 }
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000268 return new_node;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800269 }
270 }
271 }
272}
273
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000274const prop_info* prop_area::find_property(prop_trie_node* const trie, const char* name,
275 uint32_t namelen, const char* value, uint32_t valuelen,
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800276 bool alloc_if_needed) {
277 if (!trie) return nullptr;
278
279 const char* remaining_name = name;
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000280 prop_trie_node* current = trie;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800281 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
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000290 prop_trie_node* root = nullptr;
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800291 uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
292 if (children_offset != 0) {
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000293 root = to_prop_trie_node(&current->children);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800294 } else if (alloc_if_needed) {
295 uint_least32_t new_offset;
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000296 root = new_prop_trie_node(remaining_name, substr_size, &new_offset);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800297 if (root) {
298 atomic_store_explicit(&current->children, new_offset, memory_order_release);
299 }
300 }
301
302 if (!root) {
303 return nullptr;
304 }
305
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000306 current = find_prop_trie_node(root, remaining_name, substr_size, alloc_if_needed);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800307 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
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000332bool prop_area::foreach_property(prop_trie_node* const trie,
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800333 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) {
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000338 const int err = foreach_property(to_prop_trie_node(&trie->left), propfn, cookie);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800339 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) {
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000349 const int err = foreach_property(to_prop_trie_node(&trie->children), propfn, cookie);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800350 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) {
Elliott Hughesc37aa7a2023-08-08 14:04:02 +0000354 const int err = foreach_property(to_prop_trie_node(&trie->right), propfn, cookie);
Tom Cherryfd44b9f2017-11-08 14:01:00 -0800355 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}