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 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame^] | 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <ftw.h> |
| 21 | #include <pwd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdarg.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 26 | #include <time.h> |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame^] | 27 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 29 | #include <selinux/android.h> |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame^] | 30 | #include <selinux/label.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 31 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame^] | 32 | #include <sys/socket.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include <sys/un.h> |
| 36 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 37 | #include <android-base/file.h> |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 38 | #include <android-base/logging.h> |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame^] | 39 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 40 | #include <android-base/strings.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | /* for ANDROID_SOCKET_* */ |
| 42 | #include <cutils/sockets.h> |
| 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 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame^] | 167 | /* |
| 168 | * create_file - opens and creates a file as dictated in init.rc. |
| 169 | * This file is inherited by the daemon. We communicate the file |
| 170 | * descriptor's value via the environment variable ANDROID_FILE_<basename> |
| 171 | */ |
| 172 | int create_file(const char *path, int flags, mode_t perm, uid_t uid, |
| 173 | gid_t gid, const char *filecon) |
| 174 | { |
| 175 | char *secontext = NULL; |
| 176 | int ret; |
| 177 | |
| 178 | if (filecon) { |
| 179 | if (setsockcreatecon(filecon) == -1) { |
| 180 | PLOG(ERROR) << "setsockcreatecon(\"" << filecon << "\") failed"; |
| 181 | return -1; |
| 182 | } |
| 183 | } else if (sehandle) { |
| 184 | ret = selabel_lookup(sehandle, &secontext, path, perm); |
| 185 | if (ret != -1) { |
| 186 | ret = setfscreatecon(secontext); |
| 187 | if (ret == -1) { |
| 188 | freecon(secontext); |
| 189 | PLOG(ERROR) << "setfscreatecon(\"" << secontext << "\") failed"; |
| 190 | return -1; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | int fd = TEMP_FAILURE_RETRY(open(path, flags | O_NDELAY, perm)); |
| 196 | |
| 197 | if (filecon) { |
| 198 | setsockcreatecon(NULL); |
| 199 | lsetfilecon(path, filecon); |
| 200 | } else { |
| 201 | setfscreatecon(NULL); |
| 202 | freecon(secontext); |
| 203 | } |
| 204 | |
| 205 | if (fd == -1) { |
| 206 | PLOG(ERROR) << "Failed to open/create file '" << path << "'"; |
| 207 | goto out_close; |
| 208 | } |
| 209 | |
| 210 | if (!(flags & O_NDELAY)) fcntl(fd, F_SETFD, flags); |
| 211 | |
| 212 | ret = lchown(path, uid, gid); |
| 213 | if (ret) { |
| 214 | PLOG(ERROR) << "Failed to lchown file '" << path << "'"; |
| 215 | goto out_close; |
| 216 | } |
| 217 | if (perm != static_cast<mode_t>(-1)) { |
| 218 | ret = fchmodat(AT_FDCWD, path, perm, AT_SYMLINK_NOFOLLOW); |
| 219 | if (ret) { |
| 220 | PLOG(ERROR) << "Failed to fchmodat file '" << path << "'"; |
| 221 | goto out_close; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | LOG(INFO) << "Created file '" << path << "'" |
| 226 | << ", mode " << std::oct << perm << std::dec |
| 227 | << ", user " << uid |
| 228 | << ", group " << gid; |
| 229 | |
| 230 | return fd; |
| 231 | |
| 232 | out_close: |
| 233 | if (fd >= 0) close(fd); |
| 234 | return -1; |
| 235 | } |
| 236 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 237 | bool read_file(const char* path, std::string* content) { |
| 238 | content->clear(); |
| 239 | |
| 240 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC)); |
| 241 | if (fd == -1) { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | // For security reasons, disallow world-writable |
| 246 | // or group-writable files. |
Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 247 | struct stat sb; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 248 | if (fstat(fd, &sb) == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 249 | PLOG(ERROR) << "fstat failed for '" << path << "'"; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 250 | return false; |
Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 251 | } |
| 252 | if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 253 | PLOG(ERROR) << "skipping insecure file '" << path << "'"; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 254 | return false; |
Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 257 | bool okay = android::base::ReadFdToString(fd, content); |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 258 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 259 | return okay; |
| 260 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 262 | int write_file(const char* path, const char* content) { |
| 263 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600)); |
| 264 | if (fd == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 265 | PLOG(ERROR) << "write_file: Unable to open '" << path << "'"; |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 266 | return -1; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 267 | } |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 268 | int result = android::base::WriteStringToFd(content, fd) ? 0 : -1; |
| 269 | if (result == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 270 | PLOG(ERROR) << "write_file: Unable to write to '" << path << "'"; |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 271 | } |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 272 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 273 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 276 | time_t gettime() { |
| 277 | timespec now; |
| 278 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 279 | return now.tv_sec; |
| 280 | } |
Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 281 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 282 | uint64_t gettime_ns() { |
| 283 | timespec now; |
| 284 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 285 | 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] | 286 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 287 | |
| 288 | int mkdir_recursive(const char *pathname, mode_t mode) |
| 289 | { |
| 290 | char buf[128]; |
| 291 | const char *slash; |
| 292 | const char *p = pathname; |
| 293 | int width; |
| 294 | int ret; |
| 295 | struct stat info; |
| 296 | |
| 297 | while ((slash = strchr(p, '/')) != NULL) { |
| 298 | width = slash - pathname; |
| 299 | p = slash + 1; |
| 300 | if (width < 0) |
| 301 | break; |
| 302 | if (width == 0) |
| 303 | continue; |
| 304 | if ((unsigned int)width > sizeof(buf) - 1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 305 | LOG(ERROR) << "path too long for mkdir_recursive"; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | memcpy(buf, pathname, width); |
| 309 | buf[width] = 0; |
| 310 | if (stat(buf, &info) != 0) { |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 311 | ret = make_dir(buf, mode); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 312 | if (ret && errno != EEXIST) |
| 313 | return ret; |
| 314 | } |
| 315 | } |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 316 | ret = make_dir(pathname, mode); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 317 | if (ret && errno != EEXIST) |
| 318 | return ret; |
| 319 | return 0; |
| 320 | } |
| 321 | |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 322 | /* |
| 323 | * replaces any unacceptable characters with '_', the |
| 324 | * length of the resulting string is equal to the input string |
| 325 | */ |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 326 | void sanitize(char *s) |
| 327 | { |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 328 | const char* accept = |
| 329 | "abcdefghijklmnopqrstuvwxyz" |
| 330 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 331 | "0123456789" |
| 332 | "_-."; |
| 333 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 334 | if (!s) |
| 335 | return; |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 336 | |
Christopher R. Palmer | 07f3fee | 2014-09-22 14:35:54 -0400 | [diff] [blame] | 337 | while (*s) { |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 338 | s += strspn(s, accept); |
Christopher R. Palmer | 07f3fee | 2014-09-22 14:35:54 -0400 | [diff] [blame] | 339 | if (*s) *s++ = '_'; |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 340 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 341 | } |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 342 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 343 | int wait_for_file(const char *filename, int timeout) |
| 344 | { |
| 345 | struct stat info; |
Thierry Strudel | 91cf41c | 2015-05-22 15:56:14 -0700 | [diff] [blame] | 346 | uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000); |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 347 | int ret = -1; |
| 348 | |
Thierry Strudel | 91cf41c | 2015-05-22 15:56:14 -0700 | [diff] [blame] | 349 | while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0)) |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 350 | usleep(10000); |
| 351 | |
| 352 | return ret; |
| 353 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 354 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 355 | void import_kernel_cmdline(bool in_qemu, |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 356 | const std::function<void(const std::string&, const std::string&, bool)>& fn) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 357 | std::string cmdline; |
| 358 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 359 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 360 | for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) { |
| 361 | std::vector<std::string> pieces = android::base::Split(entry, "="); |
| 362 | if (pieces.size() == 2) { |
| 363 | fn(pieces[0], pieces[1], in_qemu); |
| 364 | } |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 367 | |
| 368 | int make_dir(const char *path, mode_t mode) |
| 369 | { |
| 370 | int rc; |
| 371 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 372 | char *secontext = NULL; |
| 373 | |
| 374 | if (sehandle) { |
| 375 | selabel_lookup(sehandle, &secontext, path, mode); |
| 376 | setfscreatecon(secontext); |
| 377 | } |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 378 | |
| 379 | rc = mkdir(path, mode); |
| 380 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 381 | if (secontext) { |
| 382 | int save_errno = errno; |
| 383 | freecon(secontext); |
| 384 | setfscreatecon(NULL); |
| 385 | errno = save_errno; |
| 386 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 387 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 388 | return rc; |
| 389 | } |
| 390 | |
Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 391 | int restorecon(const char* pathname) |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 392 | { |
Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 393 | return selinux_android_restorecon(pathname, 0); |
Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | int restorecon_recursive(const char* pathname) |
| 397 | { |
Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 398 | return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE); |
Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 399 | } |
Andres Morales | db5f5d4 | 2015-05-08 08:30:33 -0700 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * Writes hex_len hex characters (1/2 byte) to hex from bytes. |
| 403 | */ |
| 404 | std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) { |
| 405 | std::string hex("0x"); |
| 406 | for (size_t i = 0; i < bytes_len; i++) |
| 407 | android::base::StringAppendF(&hex, "%02x", bytes[i]); |
| 408 | return hex; |
| 409 | } |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * Returns true is pathname is a directory |
| 413 | */ |
| 414 | bool is_dir(const char* pathname) { |
| 415 | struct stat info; |
| 416 | if (stat(pathname, &info) == -1) { |
| 417 | return false; |
| 418 | } |
| 419 | return S_ISDIR(info.st_mode); |
| 420 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 421 | |
| 422 | bool expand_props(const std::string& src, std::string* dst) { |
| 423 | const char* src_ptr = src.c_str(); |
| 424 | |
| 425 | if (!dst) { |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | /* - variables can either be $x.y or ${x.y}, in case they are only part |
| 430 | * of the string. |
| 431 | * - will accept $$ as a literal $. |
| 432 | * - no nested property expansion, i.e. ${foo.${bar}} is not supported, |
| 433 | * bad things will happen |
Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 434 | * - ${x.y:-default} will return default value if property empty. |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 435 | */ |
| 436 | while (*src_ptr) { |
| 437 | const char* c; |
| 438 | |
| 439 | c = strchr(src_ptr, '$'); |
| 440 | if (!c) { |
| 441 | dst->append(src_ptr); |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | dst->append(src_ptr, c); |
| 446 | c++; |
| 447 | |
| 448 | if (*c == '$') { |
| 449 | dst->push_back(*(c++)); |
| 450 | src_ptr = c; |
| 451 | continue; |
| 452 | } else if (*c == '\0') { |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | std::string prop_name; |
Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 457 | std::string def_val; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 458 | if (*c == '{') { |
| 459 | c++; |
| 460 | const char* end = strchr(c, '}'); |
| 461 | if (!end) { |
| 462 | // failed to find closing brace, abort. |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 463 | LOG(ERROR) << "unexpected end of string in '" << src << "', looking for }"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | prop_name = std::string(c, end); |
| 467 | c = end + 1; |
Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 468 | size_t def = prop_name.find(":-"); |
| 469 | if (def < prop_name.size()) { |
| 470 | def_val = prop_name.substr(def + 2); |
| 471 | prop_name = prop_name.substr(0, def); |
| 472 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 473 | } else { |
| 474 | prop_name = c; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 475 | LOG(ERROR) << "using deprecated syntax for specifying property '" << c << "', use ${name} instead"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 476 | c += prop_name.size(); |
| 477 | } |
| 478 | |
| 479 | if (prop_name.empty()) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 480 | LOG(ERROR) << "invalid zero-length property name in '" << src << "'"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 481 | return false; |
| 482 | } |
| 483 | |
| 484 | std::string prop_val = property_get(prop_name.c_str()); |
| 485 | if (prop_val.empty()) { |
Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 486 | if (def_val.empty()) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 487 | LOG(ERROR) << "property '" << prop_name << "' doesn't exist while expanding '" << src << "'"; |
Mark Salyzyn | 4b56162 | 2016-06-07 08:49:01 -0700 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | prop_val = def_val; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | dst->append(prop_val); |
| 494 | src_ptr = c; |
| 495 | } |
| 496 | |
| 497 | return true; |
| 498 | } |