| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [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 |  | 
|  | 17 | #include <stdarg.h> | 
|  | 18 | #include <stdlib.h> | 
|  | 19 | #include <stdio.h> | 
|  | 20 | #include <string.h> | 
|  | 21 | #include <fcntl.h> | 
|  | 22 | #include <ctype.h> | 
|  | 23 | #include <errno.h> | 
| Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 24 | #include <time.h> | 
| Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 25 | #include <ftw.h> | 
| William Roberts | 3792e6c | 2016-04-06 19:18:50 -0700 | [diff] [blame] | 26 | #include <pwd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 28 | #include <selinux/label.h> | 
| Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 29 | #include <selinux/android.h> | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 30 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> | 
|  | 32 | #include <sys/types.h> | 
|  | 33 | #include <sys/socket.h> | 
|  | 34 | #include <sys/un.h> | 
|  | 35 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 37 | #include <android-base/logging.h> | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 38 | #include <android-base/strings.h> | 
| Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 39 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | /* for ANDROID_SOCKET_* */ | 
|  | 41 | #include <cutils/sockets.h> | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 44 | #include "init.h" | 
| Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 45 | #include "log.h" | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 46 | #include "property_service.h" | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 47 | #include "util.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 |  | 
| Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 49 | static unsigned int do_decode_uid(const char *s) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | { | 
|  | 51 | unsigned int v; | 
|  | 52 |  | 
|  | 53 | if (!s || *s == '\0') | 
| Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 54 | return UINT_MAX; | 
| William Roberts | 3792e6c | 2016-04-06 19:18:50 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | if (isalpha(s[0])) { | 
|  | 57 | struct passwd* pwd = getpwnam(s); | 
|  | 58 | if (!pwd) | 
|  | 59 | return UINT_MAX; | 
|  | 60 | return pwd->pw_uid; | 
|  | 61 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 |  | 
|  | 63 | errno = 0; | 
|  | 64 | v = (unsigned int) strtoul(s, 0, 0); | 
|  | 65 | if (errno) | 
| Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 66 | return UINT_MAX; | 
|  | 67 | return v; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | /* | 
|  | 71 | * decode_uid - decodes and returns the given string, which can be either the | 
|  | 72 | * numeric or name representation, into the integer uid or gid. Returns | 
|  | 73 | * UINT_MAX on error. | 
|  | 74 | */ | 
|  | 75 | unsigned int decode_uid(const char *s) { | 
|  | 76 | unsigned int v = do_decode_uid(s); | 
|  | 77 | if (v == UINT_MAX) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 78 | LOG(ERROR) << "decode_uid: Unable to find UID for '" << s << "'; returning UINT_MAX"; | 
| Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 79 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | return v; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | /* | 
|  | 84 | * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR | 
|  | 85 | * ("/dev/socket") as dictated in init.rc. This socket is inherited by the | 
|  | 86 | * daemon. We communicate the file descriptor's value via the environment | 
|  | 87 | * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo"). | 
|  | 88 | */ | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 89 | int create_socket(const char *name, int type, mode_t perm, uid_t uid, | 
|  | 90 | gid_t gid, const char *socketcon) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | { | 
|  | 92 | struct sockaddr_un addr; | 
| Nick Kralevich | 9bcfd64 | 2016-02-24 15:50:52 -0800 | [diff] [blame] | 93 | int fd, ret, savederrno; | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 94 | char *filecon; | 
|  | 95 |  | 
| Nick Kralevich | 83ccb1c | 2015-11-23 16:26:42 -0800 | [diff] [blame] | 96 | if (socketcon) { | 
|  | 97 | if (setsockcreatecon(socketcon) == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 98 | PLOG(ERROR) << "setsockcreatecon(\"" << socketcon << "\") failed"; | 
| Nick Kralevich | 83ccb1c | 2015-11-23 16:26:42 -0800 | [diff] [blame] | 99 | return -1; | 
|  | 100 | } | 
|  | 101 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 |  | 
|  | 103 | fd = socket(PF_UNIX, type, 0); | 
|  | 104 | if (fd < 0) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 105 | PLOG(ERROR) << "Failed to open socket '" << name << "'"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | return -1; | 
|  | 107 | } | 
|  | 108 |  | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 109 | if (socketcon) | 
|  | 110 | setsockcreatecon(NULL); | 
|  | 111 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | memset(&addr, 0 , sizeof(addr)); | 
|  | 113 | addr.sun_family = AF_UNIX; | 
|  | 114 | snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s", | 
|  | 115 | name); | 
|  | 116 |  | 
|  | 117 | ret = unlink(addr.sun_path); | 
|  | 118 | if (ret != 0 && errno != ENOENT) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 119 | PLOG(ERROR) << "Failed to unlink old socket '" << name << "'"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | goto out_close; | 
|  | 121 | } | 
|  | 122 |  | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 123 | filecon = NULL; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 124 | if (sehandle) { | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 125 | ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK); | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 126 | if (ret == 0) | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 127 | setfscreatecon(filecon); | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 128 | } | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 129 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr)); | 
| Nick Kralevich | 9bcfd64 | 2016-02-24 15:50:52 -0800 | [diff] [blame] | 131 | savederrno = errno; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 133 | setfscreatecon(NULL); | 
| Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 134 | freecon(filecon); | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 135 |  | 
| Nick Kralevich | 9bcfd64 | 2016-02-24 15:50:52 -0800 | [diff] [blame] | 136 | if (ret) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 137 | errno = savederrno; | 
|  | 138 | PLOG(ERROR) << "Failed to bind socket '" << name << "'"; | 
| Nick Kralevich | 9bcfd64 | 2016-02-24 15:50:52 -0800 | [diff] [blame] | 139 | goto out_unlink; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | ret = lchown(addr.sun_path, uid, gid); | 
|  | 143 | if (ret) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 144 | PLOG(ERROR) << "Failed to lchown socket '" << addr.sun_path << "'"; | 
| Nick Kralevich | 9bcfd64 | 2016-02-24 15:50:52 -0800 | [diff] [blame] | 145 | goto out_unlink; | 
|  | 146 | } | 
|  | 147 | ret = fchmodat(AT_FDCWD, addr.sun_path, perm, AT_SYMLINK_NOFOLLOW); | 
|  | 148 | if (ret) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 149 | PLOG(ERROR) << "Failed to fchmodat socket '" << addr.sun_path << "'"; | 
| Nick Kralevich | 9bcfd64 | 2016-02-24 15:50:52 -0800 | [diff] [blame] | 150 | goto out_unlink; | 
|  | 151 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 |  | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 153 | LOG(INFO) << "Created socket '" << addr.sun_path << "'" | 
|  | 154 | << ", mode " << std::oct << perm << std::dec | 
|  | 155 | << ", user " << uid | 
|  | 156 | << ", group " << gid; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 |  | 
|  | 158 | return fd; | 
|  | 159 |  | 
|  | 160 | out_unlink: | 
|  | 161 | unlink(addr.sun_path); | 
|  | 162 | out_close: | 
|  | 163 | close(fd); | 
|  | 164 | return -1; | 
|  | 165 | } | 
|  | 166 |  | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 167 | bool read_file(const char* path, std::string* content) { | 
|  | 168 | content->clear(); | 
|  | 169 |  | 
|  | 170 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC)); | 
|  | 171 | if (fd == -1) { | 
|  | 172 | return false; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | // For security reasons, disallow world-writable | 
|  | 176 | // or group-writable files. | 
| Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 177 | struct stat sb; | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 178 | if (fstat(fd, &sb) == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 179 | PLOG(ERROR) << "fstat failed for '" << path << "'"; | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 180 | return false; | 
| Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 181 | } | 
|  | 182 | if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 183 | PLOG(ERROR) << "skipping insecure file '" << path << "'"; | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 184 | return false; | 
| Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 187 | bool okay = android::base::ReadFdToString(fd, content); | 
| Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 188 | close(fd); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 189 | return okay; | 
|  | 190 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 |  | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 192 | int write_file(const char* path, const char* content) { | 
|  | 193 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600)); | 
|  | 194 | if (fd == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 195 | PLOG(ERROR) << "write_file: Unable to open '" << path << "'"; | 
| Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 196 | return -1; | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 197 | } | 
| Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 198 | int result = android::base::WriteStringToFd(content, fd) ? 0 : -1; | 
|  | 199 | if (result == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 200 | PLOG(ERROR) << "write_file: Unable to write to '" << path << "'"; | 
| Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 201 | } | 
| Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 202 | close(fd); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 203 | return result; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 206 | time_t gettime() { | 
|  | 207 | timespec now; | 
|  | 208 | clock_gettime(CLOCK_MONOTONIC, &now); | 
|  | 209 | return now.tv_sec; | 
|  | 210 | } | 
| Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 211 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 212 | uint64_t gettime_ns() { | 
|  | 213 | timespec now; | 
|  | 214 | clock_gettime(CLOCK_MONOTONIC, &now); | 
|  | 215 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec; | 
| Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 216 | } | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 217 |  | 
|  | 218 | int mkdir_recursive(const char *pathname, mode_t mode) | 
|  | 219 | { | 
|  | 220 | char buf[128]; | 
|  | 221 | const char *slash; | 
|  | 222 | const char *p = pathname; | 
|  | 223 | int width; | 
|  | 224 | int ret; | 
|  | 225 | struct stat info; | 
|  | 226 |  | 
|  | 227 | while ((slash = strchr(p, '/')) != NULL) { | 
|  | 228 | width = slash - pathname; | 
|  | 229 | p = slash + 1; | 
|  | 230 | if (width < 0) | 
|  | 231 | break; | 
|  | 232 | if (width == 0) | 
|  | 233 | continue; | 
|  | 234 | if ((unsigned int)width > sizeof(buf) - 1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 235 | LOG(ERROR) << "path too long for mkdir_recursive"; | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 236 | return -1; | 
|  | 237 | } | 
|  | 238 | memcpy(buf, pathname, width); | 
|  | 239 | buf[width] = 0; | 
|  | 240 | if (stat(buf, &info) != 0) { | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 241 | ret = make_dir(buf, mode); | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 242 | if (ret && errno != EEXIST) | 
|  | 243 | return ret; | 
|  | 244 | } | 
|  | 245 | } | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 246 | ret = make_dir(pathname, mode); | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 247 | if (ret && errno != EEXIST) | 
|  | 248 | return ret; | 
|  | 249 | return 0; | 
|  | 250 | } | 
|  | 251 |  | 
| Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 252 | /* | 
|  | 253 | * replaces any unacceptable characters with '_', the | 
|  | 254 | * length of the resulting string is equal to the input string | 
|  | 255 | */ | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 256 | void sanitize(char *s) | 
|  | 257 | { | 
| Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 258 | const char* accept = | 
|  | 259 | "abcdefghijklmnopqrstuvwxyz" | 
|  | 260 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 
|  | 261 | "0123456789" | 
|  | 262 | "_-."; | 
|  | 263 |  | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 264 | if (!s) | 
|  | 265 | return; | 
| Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 266 |  | 
| Christopher R. Palmer | 07f3fee | 2014-09-22 14:35:54 -0400 | [diff] [blame] | 267 | while (*s) { | 
| Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 268 | s += strspn(s, accept); | 
| Christopher R. Palmer | 07f3fee | 2014-09-22 14:35:54 -0400 | [diff] [blame] | 269 | if (*s) *s++ = '_'; | 
| Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 270 | } | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 271 | } | 
| Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 272 |  | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 273 | int wait_for_file(const char *filename, int timeout) | 
|  | 274 | { | 
|  | 275 | struct stat info; | 
| Thierry Strudel | 91cf41c | 2015-05-22 15:56:14 -0700 | [diff] [blame] | 276 | uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000); | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 277 | int ret = -1; | 
|  | 278 |  | 
| Thierry Strudel | 91cf41c | 2015-05-22 15:56:14 -0700 | [diff] [blame] | 279 | while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0)) | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 280 | usleep(10000); | 
|  | 281 |  | 
|  | 282 | return ret; | 
|  | 283 | } | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 284 |  | 
| Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 285 | void import_kernel_cmdline(bool in_qemu, | 
| Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 286 | const std::function<void(const std::string&, const std::string&, bool)>& fn) { | 
| Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 287 | std::string cmdline; | 
|  | 288 | android::base::ReadFileToString("/proc/cmdline", &cmdline); | 
| Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 289 |  | 
| Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 290 | for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) { | 
|  | 291 | std::vector<std::string> pieces = android::base::Split(entry, "="); | 
|  | 292 | if (pieces.size() == 2) { | 
|  | 293 | fn(pieces[0], pieces[1], in_qemu); | 
|  | 294 | } | 
| Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 295 | } | 
|  | 296 | } | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 297 |  | 
|  | 298 | int make_dir(const char *path, mode_t mode) | 
|  | 299 | { | 
|  | 300 | int rc; | 
|  | 301 |  | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 302 | char *secontext = NULL; | 
|  | 303 |  | 
|  | 304 | if (sehandle) { | 
|  | 305 | selabel_lookup(sehandle, &secontext, path, mode); | 
|  | 306 | setfscreatecon(secontext); | 
|  | 307 | } | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 308 |  | 
|  | 309 | rc = mkdir(path, mode); | 
|  | 310 |  | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 311 | if (secontext) { | 
|  | 312 | int save_errno = errno; | 
|  | 313 | freecon(secontext); | 
|  | 314 | setfscreatecon(NULL); | 
|  | 315 | errno = save_errno; | 
|  | 316 | } | 
| Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 317 |  | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 318 | return rc; | 
|  | 319 | } | 
|  | 320 |  | 
| Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 321 | int restorecon(const char* pathname) | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 322 | { | 
| Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 323 | return selinux_android_restorecon(pathname, 0); | 
| Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | int restorecon_recursive(const char* pathname) | 
|  | 327 | { | 
| Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 328 | return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE); | 
| Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 329 | } | 
| Andres Morales | db5f5d4 | 2015-05-08 08:30:33 -0700 | [diff] [blame] | 330 |  | 
| Jeff Sharkey | 1635afe | 2016-07-15 16:21:34 -0600 | [diff] [blame] | 331 | int restorecon_recursive_skipce(const char* pathname) | 
|  | 332 | { | 
|  | 333 | return selinux_android_restorecon(pathname, | 
|  | 334 | SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIPCE); | 
|  | 335 | } | 
|  | 336 |  | 
| Andres Morales | db5f5d4 | 2015-05-08 08:30:33 -0700 | [diff] [blame] | 337 | /* | 
|  | 338 | * Writes hex_len hex characters (1/2 byte) to hex from bytes. | 
|  | 339 | */ | 
|  | 340 | std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) { | 
|  | 341 | std::string hex("0x"); | 
|  | 342 | for (size_t i = 0; i < bytes_len; i++) | 
|  | 343 | android::base::StringAppendF(&hex, "%02x", bytes[i]); | 
|  | 344 | return hex; | 
|  | 345 | } | 
| Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 346 |  | 
|  | 347 | /* | 
|  | 348 | * Returns true is pathname is a directory | 
|  | 349 | */ | 
|  | 350 | bool is_dir(const char* pathname) { | 
|  | 351 | struct stat info; | 
|  | 352 | if (stat(pathname, &info) == -1) { | 
|  | 353 | return false; | 
|  | 354 | } | 
|  | 355 | return S_ISDIR(info.st_mode); | 
|  | 356 | } | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 357 |  | 
|  | 358 | bool expand_props(const std::string& src, std::string* dst) { | 
|  | 359 | const char* src_ptr = src.c_str(); | 
|  | 360 |  | 
|  | 361 | if (!dst) { | 
|  | 362 | return false; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | /* - variables can either be $x.y or ${x.y}, in case they are only part | 
|  | 366 | *   of the string. | 
|  | 367 | * - will accept $$ as a literal $. | 
|  | 368 | * - no nested property expansion, i.e. ${foo.${bar}} is not supported, | 
|  | 369 | *   bad things will happen | 
| Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 370 | * - ${x.y:-default} will return default value if property empty. | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 371 | */ | 
|  | 372 | while (*src_ptr) { | 
|  | 373 | const char* c; | 
|  | 374 |  | 
|  | 375 | c = strchr(src_ptr, '$'); | 
|  | 376 | if (!c) { | 
|  | 377 | dst->append(src_ptr); | 
|  | 378 | return true; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | dst->append(src_ptr, c); | 
|  | 382 | c++; | 
|  | 383 |  | 
|  | 384 | if (*c == '$') { | 
|  | 385 | dst->push_back(*(c++)); | 
|  | 386 | src_ptr = c; | 
|  | 387 | continue; | 
|  | 388 | } else if (*c == '\0') { | 
|  | 389 | return true; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | std::string prop_name; | 
| Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 393 | std::string def_val; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 394 | if (*c == '{') { | 
|  | 395 | c++; | 
|  | 396 | const char* end = strchr(c, '}'); | 
|  | 397 | if (!end) { | 
|  | 398 | // failed to find closing brace, abort. | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 399 | LOG(ERROR) << "unexpected end of string in '" << src << "', looking for }"; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 400 | return false; | 
|  | 401 | } | 
|  | 402 | prop_name = std::string(c, end); | 
|  | 403 | c = end + 1; | 
| Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 404 | size_t def = prop_name.find(":-"); | 
|  | 405 | if (def < prop_name.size()) { | 
|  | 406 | def_val = prop_name.substr(def + 2); | 
|  | 407 | prop_name = prop_name.substr(0, def); | 
|  | 408 | } | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 409 | } else { | 
|  | 410 | prop_name = c; | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 411 | LOG(ERROR) << "using deprecated syntax for specifying property '" << c << "', use ${name} instead"; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 412 | c += prop_name.size(); | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | if (prop_name.empty()) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 416 | LOG(ERROR) << "invalid zero-length property name in '" << src << "'"; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 417 | return false; | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | std::string prop_val = property_get(prop_name.c_str()); | 
|  | 421 | if (prop_val.empty()) { | 
| Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 422 | if (def_val.empty()) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 423 | LOG(ERROR) << "property '" << prop_name << "' doesn't exist while expanding '" << src << "'"; | 
| Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 424 | return false; | 
|  | 425 | } | 
|  | 426 | prop_val = def_val; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
|  | 429 | dst->append(prop_val); | 
|  | 430 | src_ptr = c; | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | return true; | 
|  | 434 | } |