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