blob: a5392c686b082b9bbdb9c53c7a0a67b9d33426de [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 Hughes9fc83432015-05-15 19:16:40 -0700173 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800174 return okay;
175}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughesf682b472015-02-06 12:19:48 -0800177int write_file(const char* path, const char* content) {
178 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
179 if (fd == -1) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700180 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
181 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800182 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700183 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
184 if (result == -1) {
185 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
186 }
Elliott Hughes9fc83432015-05-15 19:16:40 -0700187 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800188 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189}
190
Colin Crossf24ed8c2010-04-12 18:51:06 -0700191#define MAX_MTD_PARTITIONS 16
192
193static struct {
194 char name[16];
195 int number;
196} mtd_part_map[MAX_MTD_PARTITIONS];
197
198static int mtd_part_count = -1;
199
200static void find_mtd_partitions(void)
201{
202 int fd;
203 char buf[1024];
204 char *pmtdbufp;
205 ssize_t pmtdsize;
206 int r;
207
Nick Kralevich45a884f2015-02-02 14:37:22 -0800208 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700209 if (fd < 0)
210 return;
211
212 buf[sizeof(buf) - 1] = '\0';
213 pmtdsize = read(fd, buf, sizeof(buf) - 1);
214 pmtdbufp = buf;
215 while (pmtdsize > 0) {
216 int mtdnum, mtdsize, mtderasesize;
217 char mtdname[16];
218 mtdname[0] = '\0';
219 mtdnum = -1;
220 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
221 &mtdnum, &mtdsize, &mtderasesize, mtdname);
222 if ((r == 4) && (mtdname[0] == '"')) {
223 char *x = strchr(mtdname + 1, '"');
224 if (x) {
225 *x = 0;
226 }
227 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
228 if (mtd_part_count < MAX_MTD_PARTITIONS) {
229 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
230 mtd_part_map[mtd_part_count].number = mtdnum;
231 mtd_part_count++;
232 } else {
233 ERROR("too many mtd partitions\n");
234 }
235 }
236 while (pmtdsize > 0 && *pmtdbufp != '\n') {
237 pmtdbufp++;
238 pmtdsize--;
239 }
240 if (pmtdsize > 0) {
241 pmtdbufp++;
242 pmtdsize--;
243 }
244 }
245 close(fd);
246}
247
248int mtd_name_to_number(const char *name)
249{
250 int n;
251 if (mtd_part_count < 0) {
252 mtd_part_count = 0;
253 find_mtd_partitions();
254 }
255 for (n = 0; n < mtd_part_count; n++) {
256 if (!strcmp(name, mtd_part_map[n].name)) {
257 return mtd_part_map[n].number;
258 }
259 }
260 return -1;
261}
Colin Cross504bc512010-04-13 19:35:09 -0700262
Elliott Hughesda40c002015-03-27 23:20:44 -0700263time_t gettime() {
264 timespec now;
265 clock_gettime(CLOCK_MONOTONIC, &now);
266 return now.tv_sec;
267}
Colin Cross504bc512010-04-13 19:35:09 -0700268
Elliott Hughesda40c002015-03-27 23:20:44 -0700269uint64_t gettime_ns() {
270 timespec now;
271 clock_gettime(CLOCK_MONOTONIC, &now);
272 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700273}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700274
275int mkdir_recursive(const char *pathname, mode_t mode)
276{
277 char buf[128];
278 const char *slash;
279 const char *p = pathname;
280 int width;
281 int ret;
282 struct stat info;
283
284 while ((slash = strchr(p, '/')) != NULL) {
285 width = slash - pathname;
286 p = slash + 1;
287 if (width < 0)
288 break;
289 if (width == 0)
290 continue;
291 if ((unsigned int)width > sizeof(buf) - 1) {
292 ERROR("path too long for mkdir_recursive\n");
293 return -1;
294 }
295 memcpy(buf, pathname, width);
296 buf[width] = 0;
297 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400298 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700299 if (ret && errno != EEXIST)
300 return ret;
301 }
302 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400303 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700304 if (ret && errno != EEXIST)
305 return ret;
306 return 0;
307}
308
Johan Redestig93ca79b2012-04-18 16:41:19 +0200309/*
310 * replaces any unacceptable characters with '_', the
311 * length of the resulting string is equal to the input string
312 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700313void sanitize(char *s)
314{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200315 const char* accept =
316 "abcdefghijklmnopqrstuvwxyz"
317 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
318 "0123456789"
319 "_-.";
320
Colin Crossb0ab94b2010-04-08 16:16:20 -0700321 if (!s)
322 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200323
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400324 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200325 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400326 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200327 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700328}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200329
Chris Fries79f33842013-09-05 13:19:21 -0500330void make_link_init(const char *oldpath, const char *newpath)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700331{
332 int ret;
333 char buf[256];
334 char *slash;
335 int width;
336
337 slash = strrchr(newpath, '/');
338 if (!slash)
339 return;
340 width = slash - newpath;
341 if (width <= 0 || width > (int)sizeof(buf) - 1)
342 return;
343 memcpy(buf, newpath, width);
344 buf[width] = 0;
345 ret = mkdir_recursive(buf, 0755);
346 if (ret)
347 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
348
349 ret = symlink(oldpath, newpath);
350 if (ret && errno != EEXIST)
351 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
352}
353
354void remove_link(const char *oldpath, const char *newpath)
355{
356 char path[256];
357 ssize_t ret;
358 ret = readlink(newpath, path, sizeof(path) - 1);
359 if (ret <= 0)
360 return;
361 path[ret] = 0;
362 if (!strcmp(path, oldpath))
363 unlink(newpath);
364}
Colin Crosscd0f1732010-04-19 17:10:24 -0700365
366int wait_for_file(const char *filename, int timeout)
367{
368 struct stat info;
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700369 uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000);
Colin Crosscd0f1732010-04-19 17:10:24 -0700370 int ret = -1;
371
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700372 while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0))
Colin Crosscd0f1732010-04-19 17:10:24 -0700373 usleep(10000);
374
375 return ret;
376}
Colin Crossf83d0b92010-04-21 12:04:20 -0700377
378void open_devnull_stdio(void)
379{
Nick Kraleviche34577c2015-04-25 16:24:53 -0700380 // Try to avoid the mknod() call if we can. Since SELinux makes
381 // a /dev/null replacement available for free, let's use it.
382 int fd = open("/sys/fs/selinux/null", O_RDWR);
383 if (fd == -1) {
384 // OOPS, /sys/fs/selinux/null isn't available, likely because
385 // /sys/fs/selinux isn't mounted. Fall back to mknod.
386 static const char *name = "/dev/__null__";
387 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
388 fd = open(name, O_RDWR);
389 unlink(name);
390 }
391 if (fd == -1) {
392 exit(1);
Colin Crossf83d0b92010-04-21 12:04:20 -0700393 }
394 }
395
Nick Kraleviche34577c2015-04-25 16:24:53 -0700396 dup2(fd, 0);
397 dup2(fd, 1);
398 dup2(fd, 2);
399 if (fd > 2) {
400 close(fd);
401 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700402}
403
Nick Kralevichf667a322015-04-25 17:42:52 -0700404void import_kernel_cmdline(bool in_qemu, std::function<void(char*,bool)> import_kernel_nv)
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700405{
Andrew Boie2e63e712013-09-09 13:08:17 -0700406 char cmdline[2048];
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700407 char *ptr;
408 int fd;
409
Nick Kralevich45a884f2015-02-02 14:37:22 -0800410 fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700411 if (fd >= 0) {
Andrew Boie2e63e712013-09-09 13:08:17 -0700412 int n = read(fd, cmdline, sizeof(cmdline) - 1);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700413 if (n < 0) n = 0;
414
415 /* get rid of trailing newline, it happens */
416 if (n > 0 && cmdline[n-1] == '\n') n--;
417
418 cmdline[n] = 0;
419 close(fd);
420 } else {
421 cmdline[0] = 0;
422 }
423
424 ptr = cmdline;
425 while (ptr && *ptr) {
426 char *x = strchr(ptr, ' ');
427 if (x != 0) *x++ = 0;
428 import_kernel_nv(ptr, in_qemu);
429 ptr = x;
430 }
431}
Stephen Smalleye096e362012-06-11 13:37:39 -0400432
433int make_dir(const char *path, mode_t mode)
434{
435 int rc;
436
Stephen Smalleye096e362012-06-11 13:37:39 -0400437 char *secontext = NULL;
438
439 if (sehandle) {
440 selabel_lookup(sehandle, &secontext, path, mode);
441 setfscreatecon(secontext);
442 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400443
444 rc = mkdir(path, mode);
445
Stephen Smalleye096e362012-06-11 13:37:39 -0400446 if (secontext) {
447 int save_errno = errno;
448 freecon(secontext);
449 setfscreatecon(NULL);
450 errno = save_errno;
451 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700452
Stephen Smalleye096e362012-06-11 13:37:39 -0400453 return rc;
454}
455
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500456int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400457{
Stephen Smalley27a93652014-02-07 09:14:13 -0500458 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700459}
460
461int restorecon_recursive(const char* pathname)
462{
Stephen Smalley27a93652014-02-07 09:14:13 -0500463 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700464}
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700465
466/*
467 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
468 */
469std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
470 std::string hex("0x");
471 for (size_t i = 0; i < bytes_len; i++)
472 android::base::StringAppendF(&hex, "%02x", bytes[i]);
473 return hex;
474}