1 /*
2 * Copyright (C) 2017 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 "system_properties/contexts_serialized.h"
30
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <sys/mman.h>
34 #include <sys/prctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 #include <new>
39
40 #include <async_safe/log.h>
41 #include <private/android_filesystem_config.h>
42
43 #include "system_properties/system_properties.h"
44
InitializeContextNodes()45 bool ContextsSerialized::InitializeContextNodes() {
46 auto num_context_nodes = property_info_area_file_->num_contexts();
47 auto context_nodes_mmap_size = sizeof(ContextNode) * num_context_nodes;
48 // We want to avoid malloc in system properties, so we take an anonymous map instead (b/31659220).
49 void* const map_result = mmap(nullptr, context_nodes_mmap_size, PROT_READ | PROT_WRITE,
50 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
51 if (map_result == MAP_FAILED) {
52 return false;
53 }
54
55 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_result, context_nodes_mmap_size,
56 "System property context nodes");
57
58 context_nodes_ = reinterpret_cast<ContextNode*>(map_result);
59 num_context_nodes_ = num_context_nodes;
60 context_nodes_mmap_size_ = context_nodes_mmap_size;
61
62 for (size_t i = 0; i < num_context_nodes; ++i) {
63 new (&context_nodes_[i]) ContextNode(property_info_area_file_->context(i), dirname_);
64 }
65
66 return true;
67 }
68
MapSerialPropertyArea(bool access_rw,bool * fsetxattr_failed)69 bool ContextsSerialized::MapSerialPropertyArea(bool access_rw, bool* fsetxattr_failed) {
70 if (access_rw) {
71 serial_prop_area_ = prop_area::map_prop_area_rw(
72 serial_filename_.c_str(), "u:object_r:properties_serial:s0", fsetxattr_failed);
73 } else {
74 serial_prop_area_ = prop_area::map_prop_area(serial_filename_.c_str());
75 }
76 return serial_prop_area_;
77 }
78
79 // Note: load_default_path is only used for testing, as it will cause properties to be loaded from
80 // one file (specified by PropertyInfoAreaFile.LoadDefaultPath), but be written to "filename".
InitializeProperties(bool load_default_path)81 bool ContextsSerialized::InitializeProperties(bool load_default_path) {
82 if (load_default_path && !property_info_area_file_.LoadDefaultPath()) {
83 return false;
84 } else if (!load_default_path && !property_info_area_file_.LoadPath(tree_filename_.c_str())) {
85 return false;
86 }
87
88 if (!InitializeContextNodes()) {
89 FreeAndUnmap();
90 return false;
91 }
92
93 return true;
94 }
95
96 // Note: load_default_path is only used for testing, as it will cause properties to be loaded from
97 // one file (specified by PropertyInfoAreaFile.LoadDefaultPath), but be written to "filename".
Initialize(bool writable,const char * dirname,bool * fsetxattr_failed,bool load_default_path)98 bool ContextsSerialized::Initialize(bool writable, const char* dirname, bool* fsetxattr_failed,
99 bool load_default_path) {
100 dirname_ = dirname;
101 tree_filename_ = PropertiesFilename(dirname, "property_info");
102 serial_filename_ = PropertiesFilename(dirname, "properties_serial");
103
104 if (!InitializeProperties(load_default_path)) {
105 return false;
106 }
107
108 if (writable) {
109 mkdir(dirname_, S_IRWXU | S_IXGRP | S_IXOTH);
110 bool open_failed = false;
111 if (fsetxattr_failed) {
112 *fsetxattr_failed = false;
113 }
114
115 for (size_t i = 0; i < num_context_nodes_; ++i) {
116 if (!context_nodes_[i].Open(true, fsetxattr_failed)) {
117 open_failed = true;
118 }
119 }
120 if (open_failed || !MapSerialPropertyArea(true, fsetxattr_failed)) {
121 FreeAndUnmap();
122 return false;
123 }
124 } else {
125 if (!MapSerialPropertyArea(false, nullptr)) {
126 FreeAndUnmap();
127 return false;
128 }
129 }
130 return true;
131 }
132
GetPropAreaForName(const char * name)133 prop_area* ContextsSerialized::GetPropAreaForName(const char* name) {
134 uint32_t index;
135 property_info_area_file_->GetPropertyInfoIndexes(name, &index, nullptr);
136 if (index == ~0u || index >= num_context_nodes_) {
137 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Could not find context for property \"%s\"",
138 name);
139 return nullptr;
140 }
141 auto* context_node = &context_nodes_[index];
142 if (!context_node->pa()) {
143 // We explicitly do not check no_access_ in this case because unlike the
144 // case of foreach(), we want to generate an selinux audit for each
145 // non-permitted property access in this function.
146 context_node->Open(false, nullptr);
147 }
148 return context_node->pa();
149 }
150
ForEach(void (* propfn)(const prop_info * pi,void * cookie),void * cookie)151 void ContextsSerialized::ForEach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
152 for (size_t i = 0; i < num_context_nodes_; ++i) {
153 if (context_nodes_[i].CheckAccessAndOpen()) {
154 context_nodes_[i].pa()->foreach (propfn, cookie);
155 }
156 }
157 }
158
ResetAccess()159 void ContextsSerialized::ResetAccess() {
160 for (size_t i = 0; i < num_context_nodes_; ++i) {
161 context_nodes_[i].ResetAccess();
162 }
163 }
164
FreeAndUnmap()165 void ContextsSerialized::FreeAndUnmap() {
166 property_info_area_file_.Reset();
167 if (context_nodes_ != nullptr) {
168 for (size_t i = 0; i < num_context_nodes_; ++i) {
169 context_nodes_[i].Unmap();
170 }
171 munmap(context_nodes_, context_nodes_mmap_size_);
172 context_nodes_ = nullptr;
173 }
174 prop_area::unmap_prop_area(&serial_prop_area_);
175 serial_prop_area_ = nullptr;
176 }
177