blob: d6fd0fc9674521d82a9358876bddef2a2063d110 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Cross504bc512010-04-13 19:35:09 -070024#include <time.h>
Nick Kralevichae76f6d2013-07-11 15:38:26 -070025#include <ftw.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Stephen Smalleye46f9d52012-01-13 08:48:47 -050027#include <selinux/label.h>
Stephen Smalleydbd37f22014-01-28 10:34:09 -050028#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050029
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34
Dan Albertc007bc32015-03-16 10:08:46 -070035#include <base/file.h>
36
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037/* for ANDROID_SOCKET_* */
38#include <cutils/sockets.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070039#include <base/stringprintf.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41#include <private/android_filesystem_config.h>
42
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070044#include "log.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070045#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047/*
48 * android_name_to_id - returns the integer uid/gid associated with the given
49 * name, or -1U on error.
50 */
51static unsigned int android_name_to_id(const char *name)
52{
Edwin Vanede7f1ad2012-07-26 14:09:13 -040053 const struct android_id_info *info = android_ids;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 unsigned int n;
55
56 for (n = 0; n < android_id_count; n++) {
57 if (!strcmp(info[n].name, name))
58 return info[n].aid;
59 }
60
61 return -1U;
62}
63
64/*
65 * decode_uid - decodes and returns the given string, which can be either the
66 * numeric or name representation, into the integer uid or gid. Returns -1U on
67 * error.
68 */
69unsigned int decode_uid(const char *s)
70{
71 unsigned int v;
72
73 if (!s || *s == '\0')
74 return -1U;
75 if (isalpha(s[0]))
76 return android_name_to_id(s);
77
78 errno = 0;
79 v = (unsigned int) strtoul(s, 0, 0);
80 if (errno)
81 return -1U;
82 return v;
83}
84
85/*
86 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
87 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
88 * daemon. We communicate the file descriptor's value via the environment
89 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
90 */
Stephen Smalley8348d272013-05-13 12:37:04 -040091int create_socket(const char *name, int type, mode_t perm, uid_t uid,
92 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093{
94 struct sockaddr_un addr;
95 int fd, ret;
Stephen Smalley8348d272013-05-13 12:37:04 -040096 char *filecon;
97
98 if (socketcon)
99 setsockcreatecon(socketcon);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100
101 fd = socket(PF_UNIX, type, 0);
102 if (fd < 0) {
103 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
104 return -1;
105 }
106
Stephen Smalley8348d272013-05-13 12:37:04 -0400107 if (socketcon)
108 setsockcreatecon(NULL);
109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 memset(&addr, 0 , sizeof(addr));
111 addr.sun_family = AF_UNIX;
112 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
113 name);
114
115 ret = unlink(addr.sun_path);
116 if (ret != 0 && errno != ENOENT) {
117 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
118 goto out_close;
119 }
120
Stephen Smalley8348d272013-05-13 12:37:04 -0400121 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500122 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400123 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500124 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400125 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500126 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500127
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
129 if (ret) {
130 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
131 goto out_unlink;
132 }
133
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500134 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400135 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 chown(addr.sun_path, uid, gid);
138 chmod(addr.sun_path, perm);
139
140 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
141 addr.sun_path, perm, uid, gid);
142
143 return fd;
144
145out_unlink:
146 unlink(addr.sun_path);
147out_close:
148 close(fd);
149 return -1;
150}
151
Elliott Hughesf682b472015-02-06 12:19:48 -0800152bool read_file(const char* path, std::string* content) {
153 content->clear();
154
155 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
156 if (fd == -1) {
157 return false;
158 }
159
160 // For security reasons, disallow world-writable
161 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800162 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800163 if (fstat(fd, &sb) == -1) {
164 ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
165 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800166 }
167 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800168 ERROR("skipping insecure file '%s'\n", path);
169 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800170 }
171
Dan Albertc007bc32015-03-16 10:08:46 -0700172 bool okay = android::base::ReadFdToString(fd, content);
Elliott Hughesf682b472015-02-06 12:19:48 -0800173 TEMP_FAILURE_RETRY(close(fd));
174 if (okay) {
175 content->append("\n", 1);
176 }
177 return okay;
178}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Elliott Hughesf682b472015-02-06 12:19:48 -0800180int write_file(const char* path, const char* content) {
181 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
182 if (fd == -1) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700183 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
184 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800185 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700186 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
187 if (result == -1) {
188 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
189 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800190 TEMP_FAILURE_RETRY(close(fd));
191 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192}
193
Colin Crossf24ed8c2010-04-12 18:51:06 -0700194#define MAX_MTD_PARTITIONS 16
195
196static struct {
197 char name[16];
198 int number;
199} mtd_part_map[MAX_MTD_PARTITIONS];
200
201static int mtd_part_count = -1;
202
203static void find_mtd_partitions(void)
204{
205 int fd;
206 char buf[1024];
207 char *pmtdbufp;
208 ssize_t pmtdsize;
209 int r;
210
Nick Kralevich45a884f2015-02-02 14:37:22 -0800211 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700212 if (fd < 0)
213 return;
214
215 buf[sizeof(buf) - 1] = '\0';
216 pmtdsize = read(fd, buf, sizeof(buf) - 1);
217 pmtdbufp = buf;
218 while (pmtdsize > 0) {
219 int mtdnum, mtdsize, mtderasesize;
220 char mtdname[16];
221 mtdname[0] = '\0';
222 mtdnum = -1;
223 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
224 &mtdnum, &mtdsize, &mtderasesize, mtdname);
225 if ((r == 4) && (mtdname[0] == '"')) {
226 char *x = strchr(mtdname + 1, '"');
227 if (x) {
228 *x = 0;
229 }
230 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
231 if (mtd_part_count < MAX_MTD_PARTITIONS) {
232 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
233 mtd_part_map[mtd_part_count].number = mtdnum;
234 mtd_part_count++;
235 } else {
236 ERROR("too many mtd partitions\n");
237 }
238 }
239 while (pmtdsize > 0 && *pmtdbufp != '\n') {
240 pmtdbufp++;
241 pmtdsize--;
242 }
243 if (pmtdsize > 0) {
244 pmtdbufp++;
245 pmtdsize--;
246 }
247 }
248 close(fd);
249}
250
251int mtd_name_to_number(const char *name)
252{
253 int n;
254 if (mtd_part_count < 0) {
255 mtd_part_count = 0;
256 find_mtd_partitions();
257 }
258 for (n = 0; n < mtd_part_count; n++) {
259 if (!strcmp(name, mtd_part_map[n].name)) {
260 return mtd_part_map[n].number;
261 }
262 }
263 return -1;
264}
Colin Cross504bc512010-04-13 19:35:09 -0700265
Elliott Hughesda40c002015-03-27 23:20:44 -0700266time_t gettime() {
267 timespec now;
268 clock_gettime(CLOCK_MONOTONIC, &now);
269 return now.tv_sec;
270}
Colin Cross504bc512010-04-13 19:35:09 -0700271
Elliott Hughesda40c002015-03-27 23:20:44 -0700272uint64_t gettime_ns() {
273 timespec now;
274 clock_gettime(CLOCK_MONOTONIC, &now);
275 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700276}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700277
278int mkdir_recursive(const char *pathname, mode_t mode)
279{
280 char buf[128];
281 const char *slash;
282 const char *p = pathname;
283 int width;
284 int ret;
285 struct stat info;
286
287 while ((slash = strchr(p, '/')) != NULL) {
288 width = slash - pathname;
289 p = slash + 1;
290 if (width < 0)
291 break;
292 if (width == 0)
293 continue;
294 if ((unsigned int)width > sizeof(buf) - 1) {
295 ERROR("path too long for mkdir_recursive\n");
296 return -1;
297 }
298 memcpy(buf, pathname, width);
299 buf[width] = 0;
300 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400301 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700302 if (ret && errno != EEXIST)
303 return ret;
304 }
305 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400306 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700307 if (ret && errno != EEXIST)
308 return ret;
309 return 0;
310}
311
Johan Redestig93ca79b2012-04-18 16:41:19 +0200312/*
313 * replaces any unacceptable characters with '_', the
314 * length of the resulting string is equal to the input string
315 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700316void sanitize(char *s)
317{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200318 const char* accept =
319 "abcdefghijklmnopqrstuvwxyz"
320 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
321 "0123456789"
322 "_-.";
323
Colin Crossb0ab94b2010-04-08 16:16:20 -0700324 if (!s)
325 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200326
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400327 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200328 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400329 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200330 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700331}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200332
Chris Fries79f33842013-09-05 13:19:21 -0500333void make_link_init(const char *oldpath, const char *newpath)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700334{
335 int ret;
336 char buf[256];
337 char *slash;
338 int width;
339
340 slash = strrchr(newpath, '/');
341 if (!slash)
342 return;
343 width = slash - newpath;
344 if (width <= 0 || width > (int)sizeof(buf) - 1)
345 return;
346 memcpy(buf, newpath, width);
347 buf[width] = 0;
348 ret = mkdir_recursive(buf, 0755);
349 if (ret)
350 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
351
352 ret = symlink(oldpath, newpath);
353 if (ret && errno != EEXIST)
354 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
355}
356
357void remove_link(const char *oldpath, const char *newpath)
358{
359 char path[256];
360 ssize_t ret;
361 ret = readlink(newpath, path, sizeof(path) - 1);
362 if (ret <= 0)
363 return;
364 path[ret] = 0;
365 if (!strcmp(path, oldpath))
366 unlink(newpath);
367}
Colin Crosscd0f1732010-04-19 17:10:24 -0700368
369int wait_for_file(const char *filename, int timeout)
370{
371 struct stat info;
372 time_t timeout_time = gettime() + timeout;
373 int ret = -1;
374
375 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
376 usleep(10000);
377
378 return ret;
379}
Colin Crossf83d0b92010-04-21 12:04:20 -0700380
381void open_devnull_stdio(void)
382{
Nick Kraleviche34577c2015-04-25 16:24:53 -0700383 // Try to avoid the mknod() call if we can. Since SELinux makes
384 // a /dev/null replacement available for free, let's use it.
385 int fd = open("/sys/fs/selinux/null", O_RDWR);
386 if (fd == -1) {
387 // OOPS, /sys/fs/selinux/null isn't available, likely because
388 // /sys/fs/selinux isn't mounted. Fall back to mknod.
389 static const char *name = "/dev/__null__";
390 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
391 fd = open(name, O_RDWR);
392 unlink(name);
393 }
394 if (fd == -1) {
395 exit(1);
Colin Crossf83d0b92010-04-21 12:04:20 -0700396 }
397 }
398
Nick Kraleviche34577c2015-04-25 16:24:53 -0700399 dup2(fd, 0);
400 dup2(fd, 1);
401 dup2(fd, 2);
402 if (fd > 2) {
403 close(fd);
404 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700405}
406
Nick Kralevichf667a322015-04-25 17:42:52 -0700407void import_kernel_cmdline(bool in_qemu, std::function<void(char*,bool)> import_kernel_nv)
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700408{
Andrew Boie2e63e712013-09-09 13:08:17 -0700409 char cmdline[2048];
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700410 char *ptr;
411 int fd;
412
Nick Kralevich45a884f2015-02-02 14:37:22 -0800413 fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700414 if (fd >= 0) {
Andrew Boie2e63e712013-09-09 13:08:17 -0700415 int n = read(fd, cmdline, sizeof(cmdline) - 1);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700416 if (n < 0) n = 0;
417
418 /* get rid of trailing newline, it happens */
419 if (n > 0 && cmdline[n-1] == '\n') n--;
420
421 cmdline[n] = 0;
422 close(fd);
423 } else {
424 cmdline[0] = 0;
425 }
426
427 ptr = cmdline;
428 while (ptr && *ptr) {
429 char *x = strchr(ptr, ' ');
430 if (x != 0) *x++ = 0;
431 import_kernel_nv(ptr, in_qemu);
432 ptr = x;
433 }
434}
Stephen Smalleye096e362012-06-11 13:37:39 -0400435
436int make_dir(const char *path, mode_t mode)
437{
438 int rc;
439
Stephen Smalleye096e362012-06-11 13:37:39 -0400440 char *secontext = NULL;
441
442 if (sehandle) {
443 selabel_lookup(sehandle, &secontext, path, mode);
444 setfscreatecon(secontext);
445 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400446
447 rc = mkdir(path, mode);
448
Stephen Smalleye096e362012-06-11 13:37:39 -0400449 if (secontext) {
450 int save_errno = errno;
451 freecon(secontext);
452 setfscreatecon(NULL);
453 errno = save_errno;
454 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700455
Stephen Smalleye096e362012-06-11 13:37:39 -0400456 return rc;
457}
458
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500459int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400460{
Stephen Smalley27a93652014-02-07 09:14:13 -0500461 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700462}
463
464int restorecon_recursive(const char* pathname)
465{
Stephen Smalley27a93652014-02-07 09:14:13 -0500466 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700467}
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700468
469/*
470 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
471 */
472std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
473 std::string hex("0x");
474 for (size_t i = 0; i < bytes_len; i++)
475 android::base::StringAppendF(&hex, "%02x", bytes[i]);
476 return hex;
477}
478