| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 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 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 29 | #include "system_properties/contexts_serialized.h" | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | #include <fcntl.h> | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 32 | #include <limits.h> | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 33 | #include <sys/mman.h> | 
| Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 34 | #include <sys/prctl.h> | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 35 | #include <sys/stat.h> | 
|  | 36 | #include <sys/types.h> | 
|  | 37 |  | 
|  | 38 | #include <new> | 
|  | 39 |  | 
|  | 40 | #include <async_safe/log.h> | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 41 | #include <private/android_filesystem_config.h> | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 42 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 43 | #include "system_properties/system_properties.h" | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 44 |  | 
|  | 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) { | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 63 | new (&context_nodes_[i]) ContextNode(property_info_area_file_->context(i), dirname_); | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | return true; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | bool ContextsSerialized::MapSerialPropertyArea(bool access_rw, bool* fsetxattr_failed) { | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 70 | if (access_rw) { | 
| Elliott Hughes | d19f7b1 | 2023-08-30 21:19:55 +0000 | [diff] [blame] | 71 | serial_prop_area_ = prop_area::map_prop_area_rw( | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 72 | serial_filename_.c_str(), "u:object_r:properties_serial:s0", fsetxattr_failed); | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 73 | } else { | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 74 | serial_prop_area_ = prop_area::map_prop_area(serial_filename_.c_str()); | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 75 | } | 
|  | 76 | return serial_prop_area_; | 
|  | 77 | } | 
|  | 78 |  | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 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". | 
|  | 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())) { | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 85 | return false; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | if (!InitializeContextNodes()) { | 
|  | 89 | FreeAndUnmap(); | 
|  | 90 | return false; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | return true; | 
|  | 94 | } | 
|  | 95 |  | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 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". | 
|  | 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)) { | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 105 | return false; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | if (writable) { | 
| Nate Myren | b8c87b1 | 2023-08-28 16:46:39 -0700 | [diff] [blame] | 109 | mkdir(dirname_, S_IRWXU | S_IXGRP | S_IXOTH); | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 110 | bool open_failed = false; | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 111 | if (fsetxattr_failed) { | 
|  | 112 | *fsetxattr_failed = false; | 
|  | 113 | } | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 114 |  | 
|  | 115 | for (size_t i = 0; i < num_context_nodes_; ++i) { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 116 | if (!context_nodes_[i].Open(true, fsetxattr_failed)) { | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 117 | open_failed = true; | 
|  | 118 | } | 
|  | 119 | } | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 120 | if (open_failed || !MapSerialPropertyArea(true, fsetxattr_failed)) { | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 121 | FreeAndUnmap(); | 
|  | 122 | return false; | 
|  | 123 | } | 
| Tom Cherry | 79b724c | 2017-11-17 17:14:05 -0800 | [diff] [blame] | 124 | } else { | 
|  | 125 | if (!MapSerialPropertyArea(false, nullptr)) { | 
|  | 126 | FreeAndUnmap(); | 
|  | 127 | return false; | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 | return true; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 159 | void ContextsSerialized::ResetAccess() { | 
|  | 160 | for (size_t i = 0; i < num_context_nodes_; ++i) { | 
|  | 161 | context_nodes_[i].ResetAccess(); | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
|  | 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 | } |