The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 18 | #include <dirent.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 22 | #include <libgen.h> |
Elliott Hughes | cc86fb2 | 2015-03-17 20:01:13 -0700 | [diff] [blame] | 23 | #include <paths.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 24 | #include <signal.h> |
| 25 | #include <stdarg.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 29 | #include <sys/epoll.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | #include <sys/mount.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | #include <sys/socket.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
Elliott Hughes | 35f5d04 | 2016-07-26 09:32:33 -0700 | [diff] [blame] | 33 | #include <sys/sysmacros.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 34 | #include <sys/types.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | #include <sys/un.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 36 | #include <sys/wait.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 39 | #include <selinux/selinux.h> |
| 40 | #include <selinux/label.h> |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 41 | #include <selinux/android.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 42 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/file.h> |
| 44 | #include <android-base/stringprintf.h> |
| 45 | #include <android-base/strings.h> |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 46 | #include <cutils/fs.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 47 | #include <cutils/iosched_policy.h> |
| 48 | #include <cutils/list.h> |
| 49 | #include <cutils/sockets.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 99dae54 | 2016-11-10 19:22:18 -0800 | [diff] [blame] | 52 | #include <fstream> |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 53 | #include <memory> |
| 54 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 55 | #include "action.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 56 | #include "bootchart.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | #include "devices.h" |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 58 | #include "fs_mgr.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 59 | #include "import_parser.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | #include "init.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 61 | #include "init_parser.h" |
| 62 | #include "keychords.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 63 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | #include "property_service.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 65 | #include "service.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 66 | #include "signal_handler.h" |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 67 | #include "ueventd.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 68 | #include "util.h" |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 69 | #include "watchdogd.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 71 | using android::base::StringPrintf; |
| 72 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 73 | struct selabel_handle *sehandle; |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 74 | struct selabel_handle *sehandle_prop; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 75 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | static int property_triggers_enabled = 0; |
| 77 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | static char qemu[32]; |
| 79 | |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 80 | std::string default_console = "/dev/console"; |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 81 | static time_t process_needs_restart_at; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 83 | const char *ENV[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | |
Wei Wang | 2d0fdaa | 2017-02-02 10:52:39 -0800 | [diff] [blame] | 85 | static std::unique_ptr<Timer> waiting_for_exec(nullptr); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 86 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 87 | static int epoll_fd = -1; |
| 88 | |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 89 | static std::unique_ptr<Timer> waiting_for_prop(nullptr); |
| 90 | static std::string wait_prop_name; |
| 91 | static std::string wait_prop_value; |
| 92 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 93 | void register_epoll_handler(int fd, void (*fn)()) { |
| 94 | epoll_event ev; |
| 95 | ev.events = EPOLLIN; |
| 96 | ev.data.ptr = reinterpret_cast<void*>(fn); |
| 97 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 98 | PLOG(ERROR) << "epoll_ctl failed"; |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | /* add_environment - add "key=value" to the current environment */ |
| 103 | int add_environment(const char *key, const char *val) |
| 104 | { |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 105 | size_t n; |
| 106 | size_t key_len = strlen(key); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 107 | |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 108 | /* The last environment entry is reserved to terminate the list */ |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 109 | for (n = 0; n < (arraysize(ENV) - 1); n++) { |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 110 | |
| 111 | /* Delete any existing entry for this key */ |
| 112 | if (ENV[n] != NULL) { |
| 113 | size_t entry_key_len = strcspn(ENV[n], "="); |
| 114 | if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) { |
| 115 | free((char*)ENV[n]); |
| 116 | ENV[n] = NULL; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /* Add entry if a free slot is available */ |
| 121 | if (ENV[n] == NULL) { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 122 | char* entry; |
| 123 | asprintf(&entry, "%s=%s", key, val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | ENV[n] = entry; |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 129 | LOG(ERROR) << "No env. room to store: '" << key << "':'" << val << "'"; |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 130 | |
| 131 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Wei Wang | 2d0fdaa | 2017-02-02 10:52:39 -0800 | [diff] [blame] | 134 | bool start_waiting_for_exec() |
| 135 | { |
| 136 | if (waiting_for_exec) { |
| 137 | return false; |
| 138 | } |
| 139 | waiting_for_exec.reset(new Timer()); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | void stop_waiting_for_exec() |
| 144 | { |
| 145 | if (waiting_for_exec) { |
| 146 | LOG(INFO) << "Wait for exec took " << *waiting_for_exec; |
| 147 | waiting_for_exec.reset(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | bool start_waiting_for_property(const char *name, const char *value) |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 152 | { |
| 153 | if (waiting_for_prop) { |
| 154 | return false; |
| 155 | } |
| 156 | if (property_get(name) != value) { |
| 157 | // Current property value is not equal to expected value |
| 158 | wait_prop_name = name; |
| 159 | wait_prop_value = value; |
| 160 | waiting_for_prop.reset(new Timer()); |
| 161 | } else { |
Wei Wang | 2d0fdaa | 2017-02-02 10:52:39 -0800 | [diff] [blame] | 162 | LOG(INFO) << "start_waiting_for_property(\"" |
| 163 | << name << "\", \"" << value << "\"): already set"; |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 168 | void property_changed(const char *name, const char *value) |
| 169 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 170 | if (property_triggers_enabled) |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 171 | ActionManager::GetInstance().QueuePropertyTrigger(name, value); |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 172 | if (waiting_for_prop) { |
| 173 | if (wait_prop_name == name && wait_prop_value == value) { |
| 174 | wait_prop_name.clear(); |
| 175 | wait_prop_value.clear(); |
| 176 | LOG(INFO) << "Wait for property took " << *waiting_for_prop; |
| 177 | waiting_for_prop.reset(); |
| 178 | } |
| 179 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 180 | } |
| 181 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 182 | static void restart_processes() |
| 183 | { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 184 | process_needs_restart_at = 0; |
| 185 | ServiceManager::GetInstance().ForEachServiceWithFlags(SVC_RESTARTING, [](Service* s) { |
| 186 | s->RestartIfNeeded(&process_needs_restart_at); |
| 187 | }); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 190 | void handle_control_message(const std::string& msg, const std::string& name) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 191 | Service* svc = ServiceManager::GetInstance().FindServiceByName(name); |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 192 | if (svc == nullptr) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 193 | LOG(ERROR) << "no such service '" << name << "'"; |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 194 | return; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 195 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 196 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 197 | if (msg == "start") { |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 198 | svc->Start(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 199 | } else if (msg == "stop") { |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 200 | svc->Stop(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 201 | } else if (msg == "restart") { |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 202 | svc->Restart(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 203 | } else { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 204 | LOG(ERROR) << "unknown control msg '" << msg << "'"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 208 | static int wait_for_coldboot_done_action(const std::vector<std::string>& args) { |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 209 | Timer t; |
| 210 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 211 | LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE "..."; |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 212 | |
| 213 | // Historically we had a 1s timeout here because we weren't otherwise |
| 214 | // tracking boot time, and many OEMs made their sepolicy regular |
| 215 | // expressions too expensive (http://b/19899875). |
| 216 | |
| 217 | // Now we're tracking boot time, just log the time taken to a system |
| 218 | // property. We still panic if it takes more than a minute though, |
| 219 | // because any build that slow isn't likely to boot at all, and we'd |
| 220 | // rather any test lab devices fail back to the bootloader. |
| 221 | if (wait_for_file(COLDBOOT_DONE, 60s) < 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 222 | LOG(ERROR) << "Timed out waiting for " COLDBOOT_DONE; |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 223 | panic(); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 224 | } |
| 225 | |
James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 226 | property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration_ms()).c_str()); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 227 | return 0; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 230 | /* |
| 231 | * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed |
| 232 | * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom. |
| 233 | * Does nothing if Hardware RNG is not present. |
| 234 | * |
| 235 | * Since we don't yet trust the quality of Hardware RNG, these bytes are not |
| 236 | * mixed into the primary pool of Linux RNG and the entropy estimate is left |
| 237 | * unmodified. |
| 238 | * |
| 239 | * If the HW RNG device /dev/hw_random is present, we require that at least |
| 240 | * 512 bytes read from it are written into Linux RNG. QA is expected to catch |
| 241 | * devices/configurations where these I/O operations are blocking for a long |
| 242 | * time. We do not reboot or halt on failures, as this is a best-effort |
| 243 | * attempt. |
| 244 | */ |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 245 | static int mix_hwrng_into_linux_rng_action(const std::vector<std::string>& args) |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 246 | { |
| 247 | int result = -1; |
| 248 | int hwrandom_fd = -1; |
| 249 | int urandom_fd = -1; |
| 250 | char buf[512]; |
| 251 | ssize_t chunk_size; |
| 252 | size_t total_bytes_written = 0; |
| 253 | |
| 254 | hwrandom_fd = TEMP_FAILURE_RETRY( |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 255 | open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 256 | if (hwrandom_fd == -1) { |
| 257 | if (errno == ENOENT) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 258 | LOG(ERROR) << "/dev/hw_random not found"; |
| 259 | // It's not an error to not have a Hardware RNG. |
| 260 | result = 0; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 261 | } else { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 262 | PLOG(ERROR) << "Failed to open /dev/hw_random"; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 263 | } |
| 264 | goto ret; |
| 265 | } |
| 266 | |
| 267 | urandom_fd = TEMP_FAILURE_RETRY( |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 268 | open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC)); |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 269 | if (urandom_fd == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 270 | PLOG(ERROR) << "Failed to open /dev/urandom"; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 271 | goto ret; |
| 272 | } |
| 273 | |
| 274 | while (total_bytes_written < sizeof(buf)) { |
| 275 | chunk_size = TEMP_FAILURE_RETRY( |
| 276 | read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written)); |
| 277 | if (chunk_size == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 278 | PLOG(ERROR) << "Failed to read from /dev/hw_random"; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 279 | goto ret; |
| 280 | } else if (chunk_size == 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 281 | LOG(ERROR) << "Failed to read from /dev/hw_random: EOF"; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 282 | goto ret; |
| 283 | } |
| 284 | |
| 285 | chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size)); |
| 286 | if (chunk_size == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 287 | PLOG(ERROR) << "Failed to write to /dev/urandom"; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 288 | goto ret; |
| 289 | } |
| 290 | total_bytes_written += chunk_size; |
| 291 | } |
| 292 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 293 | LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom"; |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 294 | result = 0; |
| 295 | |
| 296 | ret: |
| 297 | if (hwrandom_fd != -1) { |
| 298 | close(hwrandom_fd); |
| 299 | } |
| 300 | if (urandom_fd != -1) { |
| 301 | close(urandom_fd); |
| 302 | } |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 306 | static void security_failure() { |
| 307 | LOG(ERROR) << "Security failure..."; |
| 308 | panic(); |
| 309 | } |
| 310 | |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 311 | static bool set_highest_available_option_value(std::string path, int min, int max) |
| 312 | { |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 313 | std::ifstream inf(path, std::fstream::in); |
| 314 | if (!inf) { |
| 315 | LOG(ERROR) << "Cannot open for reading: " << path; |
| 316 | return false; |
| 317 | } |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 318 | |
| 319 | int current = max; |
| 320 | while (current >= min) { |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 321 | // try to write out new value |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 322 | std::string str_val = std::to_string(current); |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 323 | std::ofstream of(path, std::fstream::out); |
| 324 | if (!of) { |
| 325 | LOG(ERROR) << "Cannot open for writing: " << path; |
| 326 | return false; |
| 327 | } |
| 328 | of << str_val << std::endl; |
| 329 | of.close(); |
| 330 | |
| 331 | // check to make sure it was recorded |
| 332 | inf.seekg(0); |
| 333 | std::string str_rec; |
| 334 | inf >> str_rec; |
| 335 | if (str_val.compare(str_rec) == 0) { |
| 336 | break; |
| 337 | } |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 338 | current--; |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 339 | } |
| 340 | inf.close(); |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 341 | |
| 342 | if (current < min) { |
| 343 | LOG(ERROR) << "Unable to set minimum option value " << min << " in " << path; |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 349 | #define MMAP_RND_PATH "/proc/sys/vm/mmap_rnd_bits" |
| 350 | #define MMAP_RND_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits" |
| 351 | |
| 352 | /* __attribute__((unused)) due to lack of mips support: see mips block |
| 353 | * in set_mmap_rnd_bits_action */ |
| 354 | static bool __attribute__((unused)) set_mmap_rnd_bits_min(int start, int min, bool compat) { |
| 355 | std::string path; |
| 356 | if (compat) { |
| 357 | path = MMAP_RND_COMPAT_PATH; |
| 358 | } else { |
| 359 | path = MMAP_RND_PATH; |
| 360 | } |
| 361 | |
| 362 | return set_highest_available_option_value(path, min, start); |
| 363 | } |
| 364 | |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 365 | /* |
| 366 | * Set /proc/sys/vm/mmap_rnd_bits and potentially |
| 367 | * /proc/sys/vm/mmap_rnd_compat_bits to the maximum supported values. |
| 368 | * Returns -1 if unable to set these to an acceptable value. |
| 369 | * |
| 370 | * To support this sysctl, the following upstream commits are needed: |
| 371 | * |
| 372 | * d07e22597d1d mm: mmap: add new /proc tunable for mmap_base ASLR |
| 373 | * e0c25d958f78 arm: mm: support ARCH_MMAP_RND_BITS |
| 374 | * 8f0d3aa9de57 arm64: mm: support ARCH_MMAP_RND_BITS |
| 375 | * 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS |
| 376 | * ec9ee4acd97c drivers: char: random: add get_random_long() |
| 377 | * 5ef11c35ce86 mm: ASLR: use get_random_long() |
| 378 | */ |
| 379 | static int set_mmap_rnd_bits_action(const std::vector<std::string>& args) |
| 380 | { |
| 381 | int ret = -1; |
| 382 | |
| 383 | /* values are arch-dependent */ |
| 384 | #if defined(__aarch64__) |
| 385 | /* arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE */ |
| 386 | if (set_mmap_rnd_bits_min(33, 24, false) |
| 387 | && set_mmap_rnd_bits_min(16, 16, true)) { |
| 388 | ret = 0; |
| 389 | } |
| 390 | #elif defined(__x86_64__) |
| 391 | /* x86_64 supports 28 - 32 bits */ |
| 392 | if (set_mmap_rnd_bits_min(32, 32, false) |
| 393 | && set_mmap_rnd_bits_min(16, 16, true)) { |
| 394 | ret = 0; |
| 395 | } |
| 396 | #elif defined(__arm__) || defined(__i386__) |
| 397 | /* check to see if we're running on 64-bit kernel */ |
| 398 | bool h64 = !access(MMAP_RND_COMPAT_PATH, F_OK); |
| 399 | /* supported 32-bit architecture must have 16 bits set */ |
| 400 | if (set_mmap_rnd_bits_min(16, 16, h64)) { |
| 401 | ret = 0; |
| 402 | } |
| 403 | #elif defined(__mips__) || defined(__mips64__) |
| 404 | // TODO: add mips support b/27788820 |
| 405 | ret = 0; |
| 406 | #else |
dcashman | 41d0b6d | 2016-12-14 08:37:11 -0800 | [diff] [blame] | 407 | LOG(ERROR) << "Unknown architecture"; |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 408 | #endif |
| 409 | |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 410 | if (ret == -1) { |
| 411 | LOG(ERROR) << "Unable to set adequate mmap entropy value!"; |
| 412 | security_failure(); |
| 413 | } |
| 414 | return ret; |
| 415 | } |
| 416 | |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 417 | #define KPTR_RESTRICT_PATH "/proc/sys/kernel/kptr_restrict" |
| 418 | #define KPTR_RESTRICT_MINVALUE 2 |
| 419 | #define KPTR_RESTRICT_MAXVALUE 4 |
| 420 | |
| 421 | /* Set kptr_restrict to the highest available level. |
| 422 | * |
| 423 | * Aborts if unable to set this to an acceptable value. |
| 424 | */ |
| 425 | static int set_kptr_restrict_action(const std::vector<std::string>& args) |
| 426 | { |
| 427 | std::string path = KPTR_RESTRICT_PATH; |
| 428 | |
| 429 | if (!set_highest_available_option_value(path, KPTR_RESTRICT_MINVALUE, KPTR_RESTRICT_MAXVALUE)) { |
| 430 | LOG(ERROR) << "Unable to set adequate kptr_restrict value!"; |
| 431 | security_failure(); |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 436 | static int keychord_init_action(const std::vector<std::string>& args) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 437 | { |
| 438 | keychord_init(); |
| 439 | return 0; |
| 440 | } |
| 441 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 442 | static int console_init_action(const std::vector<std::string>& args) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 443 | { |
Viorel Suman | efc7275 | 2016-03-21 10:23:43 +0200 | [diff] [blame] | 444 | std::string console = property_get("ro.boot.console"); |
| 445 | if (!console.empty()) { |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 446 | default_console = "/dev/" + console; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 447 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 451 | static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) { |
| 452 | if (key.empty()) return; |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 453 | |
| 454 | if (for_emulator) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 455 | // In the emulator, export any kernel option with the "ro.kernel." prefix. |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 456 | property_set(StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str()); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 460 | if (key == "qemu") { |
| 461 | strlcpy(qemu, value.c_str(), sizeof(qemu)); |
| 462 | } else if (android::base::StartsWith(key, "androidboot.")) { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 463 | property_set(StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(), value.c_str()); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Sami Tolvanen | 9e9efca | 2015-12-07 12:33:58 +0000 | [diff] [blame] | 467 | static void export_oem_lock_status() { |
| 468 | if (property_get("ro.oem_unlock_supported") != "1") { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | std::string value = property_get("ro.boot.verifiedbootstate"); |
| 473 | |
| 474 | if (!value.empty()) { |
| 475 | property_set("ro.boot.flash.locked", value == "orange" ? "0" : "1"); |
| 476 | } |
| 477 | } |
| 478 | |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 479 | static void export_kernel_boot_props() { |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 480 | struct { |
| 481 | const char *src_prop; |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 482 | const char *dst_prop; |
| 483 | const char *default_value; |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 484 | } prop_map[] = { |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 485 | { "ro.boot.serialno", "ro.serialno", "", }, |
| 486 | { "ro.boot.mode", "ro.bootmode", "unknown", }, |
| 487 | { "ro.boot.baseband", "ro.baseband", "unknown", }, |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 488 | { "ro.boot.bootloader", "ro.bootloader", "unknown", }, |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 489 | { "ro.boot.hardware", "ro.hardware", "unknown", }, |
| 490 | { "ro.boot.revision", "ro.revision", "0", }, |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 491 | }; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 492 | for (size_t i = 0; i < arraysize(prop_map); i++) { |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 493 | std::string value = property_get(prop_map[i].src_prop); |
| 494 | property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 495 | } |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 496 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 497 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 498 | static constexpr char android_dt_dir[] = "/proc/device-tree/firmware/android"; |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 499 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 500 | static bool is_dt_compatible() { |
| 501 | std::string dt_value; |
| 502 | std::string file_name = StringPrintf("%s/compatible", android_dt_dir); |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 503 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 504 | android::base::ReadFileToString(file_name, &dt_value); |
| 505 | if (!dt_value.compare("android,firmware")) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 506 | LOG(ERROR) << "firmware/android is not compatible with 'android,firmware'"; |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 507 | return false; |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | |
| 513 | static bool is_dt_fstab_compatible() { |
| 514 | std::string dt_value; |
| 515 | std::string file_name = StringPrintf("%s/%s/compatible", android_dt_dir, "fstab"); |
| 516 | |
| 517 | android::base::ReadFileToString(file_name, &dt_value); |
| 518 | if (!dt_value.compare("android,fstab")) { |
| 519 | LOG(ERROR) << "firmware/android/fstab is not compatible with 'android,fstab'"; |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | static void process_kernel_dt() { |
| 527 | if (!is_dt_compatible()) return; |
| 528 | |
| 529 | std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dt_dir), closedir); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 530 | if (!dir) return; |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 531 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 532 | std::string dt_file; |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 533 | struct dirent *dp; |
| 534 | while ((dp = readdir(dir.get())) != NULL) { |
Rom Lemarchand | 1dcf325 | 2015-11-08 17:51:50 -0800 | [diff] [blame] | 535 | if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible") || !strcmp(dp->d_name, "name")) { |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 536 | continue; |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 537 | } |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 538 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 539 | std::string file_name = StringPrintf("%s/%s", android_dt_dir, dp->d_name); |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 540 | |
| 541 | android::base::ReadFileToString(file_name, &dt_file); |
| 542 | std::replace(dt_file.begin(), dt_file.end(), ',', '.'); |
| 543 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 544 | std::string property_name = StringPrintf("ro.boot.%s", dp->d_name); |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 545 | property_set(property_name.c_str(), dt_file.c_str()); |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 549 | static void process_kernel_cmdline() { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 550 | // The first pass does the common stuff, and finds if we are in qemu. |
| 551 | // The second pass is only necessary for qemu to export all kernel params |
| 552 | // as properties. |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 553 | import_kernel_cmdline(false, import_kernel_nv); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 554 | if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 555 | } |
| 556 | |
caozhiyuan | b104c50 | 2016-11-26 21:14:07 +0800 | [diff] [blame] | 557 | static int property_enable_triggers_action(const std::vector<std::string>& args) |
| 558 | { |
| 559 | /* Enable property triggers. */ |
| 560 | property_triggers_enabled = 1; |
| 561 | return 0; |
| 562 | } |
| 563 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 564 | static int queue_property_triggers_action(const std::vector<std::string>& args) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 565 | { |
caozhiyuan | b104c50 | 2016-11-26 21:14:07 +0800 | [diff] [blame] | 566 | ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger"); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 567 | ActionManager::GetInstance().QueueAllPropertyTriggers(); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 571 | static void selinux_init_all_handles(void) |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 572 | { |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 573 | sehandle = selinux_android_file_context_handle(); |
Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 574 | selinux_android_set_sehandle(sehandle); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 575 | sehandle_prop = selinux_android_prop_context_handle(); |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 576 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 577 | |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 578 | enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING }; |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 579 | |
| 580 | static selinux_enforcing_status selinux_status_from_cmdline() { |
| 581 | selinux_enforcing_status status = SELINUX_ENFORCING; |
| 582 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 583 | import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) { |
| 584 | if (key == "androidboot.selinux" && value == "permissive") { |
| 585 | status = SELINUX_PERMISSIVE; |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 586 | } |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 587 | }); |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 588 | |
| 589 | return status; |
| 590 | } |
| 591 | |
Nick Kralevich | 935bd3e | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 592 | static bool selinux_is_enforcing(void) |
| 593 | { |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 594 | if (ALLOW_PERMISSIVE_SELINUX) { |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 595 | return selinux_status_from_cmdline() == SELINUX_ENFORCING; |
Nick Kralevich | 935bd3e | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 596 | } |
Nick Kralevich | 935bd3e | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 597 | return true; |
| 598 | } |
| 599 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 600 | static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) { |
William Roberts | d7aea44 | 2015-10-01 16:03:47 -0700 | [diff] [blame] | 601 | |
| 602 | property_audit_data *d = reinterpret_cast<property_audit_data*>(data); |
| 603 | |
| 604 | if (!d || !d->name || !d->cr) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 605 | LOG(ERROR) << "audit_callback invoked with null data arguments!"; |
William Roberts | d7aea44 | 2015-10-01 16:03:47 -0700 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name, |
| 610 | d->cr->pid, d->cr->uid, d->cr->gid); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 614 | static void selinux_initialize(bool in_kernel_domain) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 615 | Timer t; |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 616 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 617 | selinux_callback cb; |
| 618 | cb.func_log = selinux_klog_callback; |
| 619 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 620 | cb.func_audit = audit_callback; |
| 621 | selinux_set_callback(SELINUX_CB_AUDIT, cb); |
| 622 | |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 623 | if (in_kernel_domain) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 624 | LOG(INFO) << "Loading SELinux policy..."; |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 625 | if (selinux_android_load_policy() < 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 626 | PLOG(ERROR) << "failed to load policy"; |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 627 | security_failure(); |
| 628 | } |
| 629 | |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 630 | bool kernel_enforcing = (security_getenforce() == 1); |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 631 | bool is_enforcing = selinux_is_enforcing(); |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 632 | if (kernel_enforcing != is_enforcing) { |
| 633 | if (security_setenforce(is_enforcing)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 634 | PLOG(ERROR) << "security_setenforce(%s) failed" << (is_enforcing ? "true" : "false"); |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 635 | security_failure(); |
| 636 | } |
| 637 | } |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 638 | |
Jorge Lucangeli Obes | 77f0e9f | 2016-12-28 14:07:02 -0500 | [diff] [blame] | 639 | if (!write_file("/sys/fs/selinux/checkreqprot", "0")) { |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 640 | security_failure(); |
| 641 | } |
| 642 | |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 643 | // init's first stage can't set properties, so pass the time to the second stage. |
James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 644 | setenv("INIT_SELINUX_TOOK", std::to_string(t.duration_ms()).c_str(), 1); |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 645 | } else { |
| 646 | selinux_init_all_handles(); |
Nick Kralevich | 56fa0ac | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 647 | } |
Nick Kralevich | 56fa0ac | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Amit Pundir | d2e74db | 2016-06-29 19:00:00 +0530 | [diff] [blame] | 650 | // Set the UDC controller for the ConfigFS USB Gadgets. |
| 651 | // Read the UDC controller in use from "/sys/class/udc". |
| 652 | // In case of multiple UDC controllers select the first one. |
| 653 | static void set_usb_controller() { |
| 654 | std::unique_ptr<DIR, decltype(&closedir)>dir(opendir("/sys/class/udc"), closedir); |
| 655 | if (!dir) return; |
| 656 | |
| 657 | dirent* dp; |
| 658 | while ((dp = readdir(dir.get())) != nullptr) { |
| 659 | if (dp->d_name[0] == '.') continue; |
| 660 | |
| 661 | property_set("sys.usb.controller", dp->d_name); |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 666 | static std::string import_dt_fstab() { |
| 667 | std::string fstab; |
| 668 | if (!is_dt_compatible() || !is_dt_fstab_compatible()) { |
| 669 | return fstab; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 670 | } |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 671 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 672 | std::string fstabdir_name = StringPrintf("%s/fstab", android_dt_dir); |
| 673 | std::unique_ptr<DIR, int (*)(DIR*)> fstabdir(opendir(fstabdir_name.c_str()), closedir); |
| 674 | if (!fstabdir) return fstab; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 675 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 676 | dirent* dp; |
| 677 | while ((dp = readdir(fstabdir.get())) != NULL) { |
| 678 | // skip over name and compatible |
| 679 | if (dp->d_type != DT_DIR) { |
| 680 | continue; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 681 | } |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 682 | |
| 683 | // skip if its not 'vendor', 'odm' or 'system' |
| 684 | if (strcmp(dp->d_name, "odm") && strcmp(dp->d_name, "system") && |
| 685 | strcmp(dp->d_name, "vendor")) { |
| 686 | continue; |
| 687 | } |
| 688 | |
| 689 | // create <dev> <mnt_point> <type> <mnt_flags> <fsmgr_flags>\n |
| 690 | std::vector<std::string> fstab_entry; |
| 691 | std::string file_name; |
| 692 | std::string value; |
| 693 | file_name = StringPrintf("%s/%s/dev", fstabdir_name.c_str(), dp->d_name); |
| 694 | if (!android::base::ReadFileToString(file_name, &value)) { |
| 695 | LOG(ERROR) << "dt_fstab: Failed to find device for partition " << dp->d_name; |
| 696 | fstab.clear(); |
| 697 | break; |
| 698 | } |
| 699 | // trim the terminating '\0' out |
| 700 | value.resize(value.size() - 1); |
| 701 | fstab_entry.push_back(value); |
| 702 | fstab_entry.push_back(StringPrintf("/%s", dp->d_name)); |
| 703 | |
| 704 | file_name = StringPrintf("%s/%s/type", fstabdir_name.c_str(), dp->d_name); |
| 705 | if (!android::base::ReadFileToString(file_name, &value)) { |
| 706 | LOG(ERROR) << "dt_fstab: Failed to find type for partition " << dp->d_name; |
| 707 | fstab.clear(); |
| 708 | break; |
| 709 | } |
| 710 | value.resize(value.size() - 1); |
| 711 | fstab_entry.push_back(value); |
| 712 | |
| 713 | file_name = StringPrintf("%s/%s/mnt_flags", fstabdir_name.c_str(), dp->d_name); |
| 714 | if (!android::base::ReadFileToString(file_name, &value)) { |
| 715 | LOG(ERROR) << "dt_fstab: Failed to find type for partition " << dp->d_name; |
| 716 | fstab.clear(); |
| 717 | break; |
| 718 | } |
| 719 | value.resize(value.size() - 1); |
| 720 | fstab_entry.push_back(value); |
| 721 | |
| 722 | file_name = StringPrintf("%s/%s/fsmgr_flags", fstabdir_name.c_str(), dp->d_name); |
| 723 | if (!android::base::ReadFileToString(file_name, &value)) { |
| 724 | LOG(ERROR) << "dt_fstab: Failed to find type for partition " << dp->d_name; |
| 725 | fstab.clear(); |
| 726 | break; |
| 727 | } |
| 728 | value.resize(value.size() - 1); |
| 729 | fstab_entry.push_back(value); |
| 730 | |
| 731 | fstab += android::base::Join(fstab_entry, " "); |
| 732 | fstab += '\n'; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 733 | } |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 734 | |
| 735 | return fstab; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 736 | } |
| 737 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 738 | /* Early mount vendor and ODM partitions. The fstab is read from device-tree. */ |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 739 | static bool early_mount() { |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 740 | std::string fstab = import_dt_fstab(); |
| 741 | if (fstab.empty()) { |
| 742 | LOG(INFO) << "Early mount skipped (missing fstab in device tree)"; |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 743 | return true; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 744 | } |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 745 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 746 | std::unique_ptr<FILE, decltype(&fclose)> fstab_file( |
| 747 | fmemopen(static_cast<void*>(const_cast<char*>(fstab.c_str())), fstab.length(), "r"), fclose); |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 748 | if (!fstab_file) { |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 749 | PLOG(ERROR) << "Early mount failed to open fstab file in memory"; |
| 750 | return false; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 751 | } |
| 752 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 753 | std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> tab( |
| 754 | fs_mgr_read_fstab_file(fstab_file.get()), fs_mgr_free_fstab); |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 755 | if (!tab) { |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 756 | LOG(ERROR) << "Early mount fsmgr failed to load fstab from kernel:" << std::endl << fstab; |
| 757 | return false; |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 758 | } |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 759 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 760 | // find out fstab records for odm, system and vendor |
| 761 | fstab_rec* odm_rec = fs_mgr_get_entry_for_mount_point(tab.get(), "/odm"); |
| 762 | fstab_rec* system_rec = fs_mgr_get_entry_for_mount_point(tab.get(), "/system"); |
| 763 | fstab_rec* vendor_rec = fs_mgr_get_entry_for_mount_point(tab.get(), "/vendor"); |
| 764 | if (!odm_rec && !system_rec && !vendor_rec) { |
| 765 | // nothing to early mount |
| 766 | return true; |
| 767 | } |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 768 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 769 | // assume A/B device if we find 'slotselect' in any fstab entry |
| 770 | bool is_ab = ((odm_rec && fs_mgr_is_slotselect(odm_rec)) || |
| 771 | (system_rec && fs_mgr_is_slotselect(system_rec)) || |
| 772 | (vendor_rec && fs_mgr_is_slotselect(vendor_rec))); |
| 773 | bool found_odm = !odm_rec; |
| 774 | bool found_system = !system_rec; |
| 775 | bool found_vendor = !vendor_rec; |
| 776 | int count_odm = 0, count_vendor = 0, count_system = 0; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 777 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 778 | // create the devices we need.. |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 779 | device_init(nullptr, [&](uevent* uevent) -> coldboot_action_t { |
| 780 | if (!strncmp(uevent->subsystem, "firmware", 8)) { |
| 781 | return COLDBOOT_CONTINUE; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 782 | } |
| 783 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 784 | // we need platform devices to create symlinks |
| 785 | if (!strncmp(uevent->subsystem, "platform", 8)) { |
| 786 | return COLDBOOT_CREATE; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 787 | } |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 788 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 789 | // Ignore everything that is not a block device |
| 790 | if (strncmp(uevent->subsystem, "block", 5)) { |
| 791 | return COLDBOOT_CONTINUE; |
| 792 | } |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 793 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 794 | coldboot_action_t ret; |
| 795 | bool create_this_node = false; |
| 796 | if (uevent->partition_name) { |
| 797 | // prefix match partition names so we create device nodes for |
| 798 | // A/B-ed partitions |
| 799 | if (!found_odm && !strncmp(uevent->partition_name, "odm", 3)) { |
| 800 | LOG(VERBOSE) << "early_mount: found (" << uevent->partition_name << ") partition"; |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 801 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 802 | // wait twice for A/B-ed partitions |
| 803 | count_odm++; |
| 804 | if (!is_ab) { |
| 805 | found_odm = true; |
| 806 | } else if (count_odm == 2) { |
| 807 | found_odm = true; |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 808 | } |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 809 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 810 | create_this_node = true; |
| 811 | } else if (!found_system && !strncmp(uevent->partition_name, "system", 6)) { |
| 812 | LOG(VERBOSE) << "early_mount: found (" << uevent->partition_name << ") partition"; |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 813 | |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 814 | count_system++; |
| 815 | if (!is_ab) { |
| 816 | found_system = true; |
| 817 | } else if (count_system == 2) { |
| 818 | found_system = true; |
| 819 | } |
| 820 | |
| 821 | create_this_node = true; |
| 822 | } else if (!found_vendor && !strncmp(uevent->partition_name, "vendor", 6)) { |
| 823 | LOG(VERBOSE) << "early_mount: found (" << uevent->partition_name << ") partition"; |
| 824 | count_vendor++; |
| 825 | if (!is_ab) { |
| 826 | found_vendor = true; |
| 827 | } else if (count_vendor == 2) { |
| 828 | found_vendor = true; |
| 829 | } |
| 830 | |
| 831 | create_this_node = true; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // if we found all other partitions already, create this |
| 836 | // node and stop coldboot. If this is a prefix matched |
| 837 | // partition, create device node and continue. For everything |
| 838 | // else skip the device node |
| 839 | if (found_odm && found_system && found_vendor) { |
| 840 | ret = COLDBOOT_STOP; |
| 841 | } else if (create_this_node) { |
| 842 | ret = COLDBOOT_CREATE; |
| 843 | } else { |
| 844 | ret = COLDBOOT_CONTINUE; |
| 845 | } |
| 846 | |
| 847 | return ret; |
| 848 | }); |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 849 | |
| 850 | // TODO: add support to mount partitions w/ verity |
| 851 | |
| 852 | int ret = 0; |
| 853 | if (odm_rec && |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 854 | (ret = fs_mgr_do_mount(tab.get(), odm_rec->mount_point, odm_rec->blk_device, NULL))) { |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 855 | PLOG(ERROR) << "early_mount: fs_mgr_do_mount returned error for mounting odm"; |
| 856 | return false; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 857 | } |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 858 | |
| 859 | if (vendor_rec && |
Sandeep Patil | fc86f24 | 2017-02-09 19:19:31 -0800 | [diff] [blame] | 860 | (ret = fs_mgr_do_mount(tab.get(), vendor_rec->mount_point, vendor_rec->blk_device, NULL))) { |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 861 | PLOG(ERROR) << "early_mount: fs_mgr_do_mount returned error for mounting vendor"; |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | device_close(); |
| 866 | |
| 867 | return true; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 868 | } |
| 869 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 870 | int main(int argc, char** argv) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 871 | if (!strcmp(basename(argv[0]), "ueventd")) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 872 | return ueventd_main(argc, argv); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 873 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 874 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 875 | if (!strcmp(basename(argv[0]), "watchdogd")) { |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 876 | return watchdogd_main(argc, argv); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 877 | } |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 878 | |
James Hawkins | c8ac067 | 2017-02-14 19:20:20 +0000 | [diff] [blame] | 879 | boot_clock::time_point start_time = boot_clock::now(); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 880 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 881 | // Clear the umask. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 882 | umask(0); |
| 883 | |
Elliott Hughes | cc86fb2 | 2015-03-17 20:01:13 -0700 | [diff] [blame] | 884 | add_environment("PATH", _PATH_DEFPATH); |
| 885 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 886 | bool is_first_stage = (getenv("INIT_SECOND_STAGE") == nullptr); |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 887 | |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 888 | // Don't expose the raw commandline to unprivileged processes. |
| 889 | chmod("/proc/cmdline", 0440); |
| 890 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 891 | // Get the basic filesystem setup we need put together in the initramdisk |
| 892 | // on / and then we'll let the rc file figure out the rest. |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 893 | if (is_first_stage) { |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 894 | mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); |
| 895 | mkdir("/dev/pts", 0755); |
| 896 | mkdir("/dev/socket", 0755); |
| 897 | mount("devpts", "/dev/pts", "devpts", 0, NULL); |
Nick Kralevich | c39ba5a | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 898 | #define MAKE_STR(x) __STRING(x) |
| 899 | mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC)); |
Nick Kralevich | 80960d2 | 2016-10-29 12:20:00 -0700 | [diff] [blame] | 900 | gid_t groups[] = { AID_READPROC }; |
| 901 | setgroups(arraysize(groups), groups); |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 902 | mount("sysfs", "/sys", "sysfs", 0, NULL); |
Nick Kralevich | 3d9e273 | 2016-03-03 10:40:12 -0800 | [diff] [blame] | 903 | mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL); |
Elliott Hughes | 35f5d04 | 2016-07-26 09:32:33 -0700 | [diff] [blame] | 904 | mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11)); |
Evgenii Stepanov | 102daa3 | 2016-11-18 14:58:40 -0800 | [diff] [blame] | 905 | mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)); |
| 906 | mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)); |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 907 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 908 | |
Elliott Hughes | 35f5d04 | 2016-07-26 09:32:33 -0700 | [diff] [blame] | 909 | // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually |
| 910 | // talk to the outside world... |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 911 | InitKernelLogging(argv); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 912 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 913 | LOG(INFO) << "init " << (is_first_stage ? "first" : "second") << " stage started!"; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 914 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 915 | if (is_first_stage) { |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 916 | if (!early_mount()) { |
| 917 | LOG(ERROR) << "Failed to mount required partitions early ..."; |
| 918 | panic(); |
| 919 | } |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 920 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 921 | // Set up SELinux, loading the SELinux policy. |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 922 | selinux_initialize(true); |
| 923 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 924 | // We're in the kernel domain, so re-exec init to transition to the init domain now |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 925 | // that the SELinux policy has been loaded. |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 926 | if (restorecon("/init") == -1) { |
| 927 | PLOG(ERROR) << "restorecon failed"; |
| 928 | security_failure(); |
| 929 | } |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 930 | |
| 931 | setenv("INIT_SECOND_STAGE", "true", 1); |
| 932 | |
James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 933 | static constexpr uint32_t kNanosecondsPerMillisecond = 1e6; |
| 934 | uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond; |
| 935 | setenv("INIT_STARTED_AT", StringPrintf("%" PRIu64, start_ms).c_str(), 1); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 936 | |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 937 | char* path = argv[0]; |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 938 | char* args[] = { path, nullptr }; |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 939 | if (execv(path, args) == -1) { |
| 940 | PLOG(ERROR) << "execv(\"" << path << "\") failed"; |
| 941 | security_failure(); |
| 942 | } |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 943 | } else { |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 944 | // Indicate that booting is in progress to background fw loaders, etc. |
| 945 | close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000)); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 946 | |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 947 | property_init(); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 948 | |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 949 | // If arguments are passed both on the command line and in DT, |
| 950 | // properties set in DT always have priority over the command-line ones. |
| 951 | process_kernel_dt(); |
| 952 | process_kernel_cmdline(); |
| 953 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 954 | // Propagate the kernel variables to internal variables |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 955 | // used by init as well as the current required properties. |
| 956 | export_kernel_boot_props(); |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 957 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 958 | // Make the time that init started available for bootstat to log. |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 959 | property_set("ro.boottime.init", getenv("INIT_STARTED_AT")); |
| 960 | property_set("ro.boottime.init.selinux", getenv("INIT_SELINUX_TOOK")); |
| 961 | |
| 962 | // Clean up our environment. |
| 963 | unsetenv("INIT_SECOND_STAGE"); |
| 964 | unsetenv("INIT_STARTED_AT"); |
| 965 | unsetenv("INIT_SELINUX_TOOK"); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 966 | |
| 967 | // Now set up SELinux for second stage. |
Wei Wang | 313b352 | 2016-10-28 12:22:32 -0700 | [diff] [blame] | 968 | selinux_initialize(false); |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 969 | } |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 970 | |
| 971 | // These directories were necessarily created before initial policy load |
| 972 | // and therefore need their security context restored to the proper value. |
| 973 | // This must happen before /dev is populated by ueventd. |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 974 | LOG(INFO) << "Running restorecon..."; |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 975 | restorecon("/dev"); |
Elliott Hughes | 35f5d04 | 2016-07-26 09:32:33 -0700 | [diff] [blame] | 976 | restorecon("/dev/kmsg"); |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 977 | restorecon("/dev/socket"); |
Evgenii Stepanov | 102daa3 | 2016-11-18 14:58:40 -0800 | [diff] [blame] | 978 | restorecon("/dev/random"); |
| 979 | restorecon("/dev/urandom"); |
Geremy Condra | 8e15eab | 2013-02-28 17:29:58 -0800 | [diff] [blame] | 980 | restorecon("/dev/__properties__"); |
Sandeep Patil | 7a5db4e | 2016-12-27 17:40:53 -0800 | [diff] [blame] | 981 | restorecon("/plat_property_contexts"); |
| 982 | restorecon("/nonplat_property_contexts"); |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 983 | restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE); |
| 984 | restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE); |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 985 | restorecon("/dev/device-mapper"); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 986 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 987 | epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
| 988 | if (epoll_fd == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 989 | PLOG(ERROR) << "epoll_create1 failed"; |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 990 | exit(1); |
| 991 | } |
| 992 | |
| 993 | signal_handler_init(); |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 994 | |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 995 | property_load_boot_defaults(); |
Sami Tolvanen | 9e9efca | 2015-12-07 12:33:58 +0000 | [diff] [blame] | 996 | export_oem_lock_status(); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 997 | start_property_service(); |
Amit Pundir | d2e74db | 2016-06-29 19:00:00 +0530 | [diff] [blame] | 998 | set_usb_controller(); |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 999 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1000 | const BuiltinFunctionMap function_map; |
| 1001 | Action::set_function_map(&function_map); |
| 1002 | |
| 1003 | Parser& parser = Parser::GetInstance(); |
| 1004 | parser.AddSectionParser("service",std::make_unique<ServiceParser>()); |
| 1005 | parser.AddSectionParser("on", std::make_unique<ActionParser>()); |
| 1006 | parser.AddSectionParser("import", std::make_unique<ImportParser>()); |
Hung-ying Tyan | 959aeb1 | 2017-01-18 09:39:36 +0800 | [diff] [blame] | 1007 | std::string bootscript = property_get("ro.boot.init_rc"); |
| 1008 | if (bootscript.empty()) { |
| 1009 | parser.ParseConfig("/init.rc"); |
| 1010 | } else { |
| 1011 | parser.ParseConfig(bootscript); |
| 1012 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1013 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1014 | ActionManager& am = ActionManager::GetInstance(); |
| 1015 | |
| 1016 | am.QueueEventTrigger("early-init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1017 | |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 1018 | // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev... |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1019 | am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done"); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 1020 | // ... so that we can start queuing up actions that require stuff from /dev. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1021 | am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); |
Daniel Cashman | baccc40 | 2016-03-25 23:49:05 +0000 | [diff] [blame] | 1022 | am.QueueBuiltinAction(set_mmap_rnd_bits_action, "set_mmap_rnd_bits"); |
Dave Weinstein | 44f7e4f | 2017-01-19 18:03:34 -0800 | [diff] [blame] | 1023 | am.QueueBuiltinAction(set_kptr_restrict_action, "set_kptr_restrict"); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1024 | am.QueueBuiltinAction(keychord_init_action, "keychord_init"); |
| 1025 | am.QueueBuiltinAction(console_init_action, "console_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1026 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 1027 | // Trigger all the boot actions to get us started. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1028 | am.QueueEventTrigger("init"); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1029 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 1030 | // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random |
| 1031 | // wasn't ready immediately after wait_for_coldboot_done |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1032 | am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1033 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 1034 | // Don't mount filesystems or start core system services in charger mode. |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 1035 | std::string bootmode = property_get("ro.bootmode"); |
| 1036 | if (bootmode == "charger") { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1037 | am.QueueEventTrigger("charger"); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1038 | } else { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1039 | am.QueueEventTrigger("late-init"); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1040 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1041 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 1042 | // Run all property triggers based on current state of the properties. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1043 | am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1044 | |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 1045 | while (true) { |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 1046 | if (!(waiting_for_exec || waiting_for_prop)) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1047 | am.ExecuteOneCommand(); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 1048 | restart_processes(); |
| 1049 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1050 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 1051 | // By default, sleep until something happens. |
| 1052 | int epoll_timeout_ms = -1; |
| 1053 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 1054 | // If there's a process that needs restarting, wake up in time for that. |
| 1055 | if (process_needs_restart_at != 0) { |
| 1056 | epoll_timeout_ms = (process_needs_restart_at - time(nullptr)) * 1000; |
| 1057 | if (epoll_timeout_ms < 0) epoll_timeout_ms = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Wei Wang | 16db434 | 2016-11-30 15:43:42 -0800 | [diff] [blame] | 1060 | // If there's more work to do, wake up again immediately. |
| 1061 | if (am.HasMoreCommands()) epoll_timeout_ms = 0; |
| 1062 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 1063 | epoll_event ev; |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 1064 | int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms)); |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 1065 | if (nr == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1066 | PLOG(ERROR) << "epoll_wait failed"; |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 1067 | } else if (nr == 1) { |
| 1068 | ((void (*)()) ev.data.ptr)(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1069 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |