blob: f05aaa001b9211771be1fb36718cf255becda31f [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
Tom Cherrye275d6d2017-12-11 23:31:33 -080029#include "system_properties/contexts_serialized.h"
Tom Cherry79b724c2017-11-17 17:14:05 -080030
31#include <fcntl.h>
Tom Cherrye275d6d2017-12-11 23:31:33 -080032#include <limits.h>
Tom Cherry79b724c2017-11-17 17:14:05 -080033#include <sys/mman.h>
Elliott Hughes99d54652018-08-22 10:36:23 -070034#include <sys/prctl.h>
Tom Cherry79b724c2017-11-17 17:14:05 -080035#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <new>
39
40#include <async_safe/log.h>
41
Tom Cherrye275d6d2017-12-11 23:31:33 -080042#include "system_properties/system_properties.h"
Tom Cherry79b724c2017-11-17 17:14:05 -080043
44bool ContextsSerialized::InitializeContextNodes() {
45 auto num_context_nodes = property_info_area_file_->num_contexts();
46 auto context_nodes_mmap_size = sizeof(ContextNode) * num_context_nodes;
47 // We want to avoid malloc in system properties, so we take an anonymous map instead (b/31659220).
48 void* const map_result = mmap(nullptr, context_nodes_mmap_size, PROT_READ | PROT_WRITE,
49 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
50 if (map_result == MAP_FAILED) {
51 return false;
52 }
53
54 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_result, context_nodes_mmap_size,
55 "System property context nodes");
56
57 context_nodes_ = reinterpret_cast<ContextNode*>(map_result);
58 num_context_nodes_ = num_context_nodes;
59 context_nodes_mmap_size_ = context_nodes_mmap_size;
60
61 for (size_t i = 0; i < num_context_nodes; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -080062 new (&context_nodes_[i]) ContextNode(property_info_area_file_->context(i), filename_);
Tom Cherry79b724c2017-11-17 17:14:05 -080063 }
64
65 return true;
66}
67
68bool ContextsSerialized::MapSerialPropertyArea(bool access_rw, bool* fsetxattr_failed) {
Elliott Hughesd19f7b12023-08-30 21:19:55 +000069 PropertiesFilename filename(filename_, "properties_serial");
Tom Cherry79b724c2017-11-17 17:14:05 -080070 if (access_rw) {
Elliott Hughesd19f7b12023-08-30 21:19:55 +000071 serial_prop_area_ = prop_area::map_prop_area_rw(
72 filename.c_str(), "u:object_r:properties_serial:s0", fsetxattr_failed);
Tom Cherry79b724c2017-11-17 17:14:05 -080073 } else {
Elliott Hughesd19f7b12023-08-30 21:19:55 +000074 serial_prop_area_ = prop_area::map_prop_area(filename.c_str());
Tom Cherry79b724c2017-11-17 17:14:05 -080075 }
76 return serial_prop_area_;
77}
78
79bool ContextsSerialized::InitializeProperties() {
80 if (!property_info_area_file_.LoadDefaultPath()) {
81 return false;
82 }
83
84 if (!InitializeContextNodes()) {
85 FreeAndUnmap();
86 return false;
87 }
88
89 return true;
90}
91
Tom Cherrye275d6d2017-12-11 23:31:33 -080092bool ContextsSerialized::Initialize(bool writable, const char* filename, bool* fsetxattr_failed) {
93 filename_ = filename;
Tom Cherry79b724c2017-11-17 17:14:05 -080094 if (!InitializeProperties()) {
95 return false;
96 }
97
98 if (writable) {
Tom Cherrye275d6d2017-12-11 23:31:33 -080099 mkdir(filename_, S_IRWXU | S_IXGRP | S_IXOTH);
Tom Cherry79b724c2017-11-17 17:14:05 -0800100 bool open_failed = false;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800101 if (fsetxattr_failed) {
102 *fsetxattr_failed = false;
103 }
Tom Cherry79b724c2017-11-17 17:14:05 -0800104
105 for (size_t i = 0; i < num_context_nodes_; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800106 if (!context_nodes_[i].Open(true, fsetxattr_failed)) {
Tom Cherry79b724c2017-11-17 17:14:05 -0800107 open_failed = true;
108 }
109 }
Tom Cherrye275d6d2017-12-11 23:31:33 -0800110 if (open_failed || !MapSerialPropertyArea(true, fsetxattr_failed)) {
Tom Cherry79b724c2017-11-17 17:14:05 -0800111 FreeAndUnmap();
112 return false;
113 }
Tom Cherry79b724c2017-11-17 17:14:05 -0800114 } else {
115 if (!MapSerialPropertyArea(false, nullptr)) {
116 FreeAndUnmap();
117 return false;
118 }
119 }
120 return true;
121}
122
123prop_area* ContextsSerialized::GetPropAreaForName(const char* name) {
124 uint32_t index;
125 property_info_area_file_->GetPropertyInfoIndexes(name, &index, nullptr);
126 if (index == ~0u || index >= num_context_nodes_) {
127 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Could not find context for property \"%s\"",
128 name);
129 return nullptr;
130 }
131 auto* context_node = &context_nodes_[index];
132 if (!context_node->pa()) {
133 // We explicitly do not check no_access_ in this case because unlike the
134 // case of foreach(), we want to generate an selinux audit for each
135 // non-permitted property access in this function.
136 context_node->Open(false, nullptr);
137 }
138 return context_node->pa();
139}
140
141void ContextsSerialized::ForEach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
142 for (size_t i = 0; i < num_context_nodes_; ++i) {
143 if (context_nodes_[i].CheckAccessAndOpen()) {
144 context_nodes_[i].pa()->foreach (propfn, cookie);
145 }
146 }
147}
148
149void ContextsSerialized::ResetAccess() {
150 for (size_t i = 0; i < num_context_nodes_; ++i) {
151 context_nodes_[i].ResetAccess();
152 }
153}
154
155void ContextsSerialized::FreeAndUnmap() {
156 property_info_area_file_.Reset();
157 if (context_nodes_ != nullptr) {
158 for (size_t i = 0; i < num_context_nodes_; ++i) {
159 context_nodes_[i].Unmap();
160 }
161 munmap(context_nodes_, context_nodes_mmap_size_);
162 context_nodes_ = nullptr;
163 }
164 prop_area::unmap_prop_area(&serial_prop_area_);
165 serial_prop_area_ = nullptr;
166}