blob: 3dddb15d412af2446ab3f8940985a0657a713d6e [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
35/* for ANDROID_SOCKET_* */
36#include <cutils/sockets.h>
37
Elliott Hughesf682b472015-02-06 12:19:48 -080038#include <utils/file.h>
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <private/android_filesystem_config.h>
41
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070043#include "log.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070044#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046/*
47 * android_name_to_id - returns the integer uid/gid associated with the given
48 * name, or -1U on error.
49 */
50static unsigned int android_name_to_id(const char *name)
51{
Edwin Vanede7f1ad2012-07-26 14:09:13 -040052 const struct android_id_info *info = android_ids;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053 unsigned int n;
54
55 for (n = 0; n < android_id_count; n++) {
56 if (!strcmp(info[n].name, name))
57 return info[n].aid;
58 }
59
60 return -1U;
61}
62
63/*
64 * decode_uid - decodes and returns the given string, which can be either the
65 * numeric or name representation, into the integer uid or gid. Returns -1U on
66 * error.
67 */
68unsigned int decode_uid(const char *s)
69{
70 unsigned int v;
71
72 if (!s || *s == '\0')
73 return -1U;
74 if (isalpha(s[0]))
75 return android_name_to_id(s);
76
77 errno = 0;
78 v = (unsigned int) strtoul(s, 0, 0);
79 if (errno)
80 return -1U;
81 return v;
82}
83
84/*
85 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
86 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
87 * daemon. We communicate the file descriptor's value via the environment
88 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
89 */
Stephen Smalley8348d272013-05-13 12:37:04 -040090int create_socket(const char *name, int type, mode_t perm, uid_t uid,
91 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092{
93 struct sockaddr_un addr;
94 int fd, ret;
Stephen Smalley8348d272013-05-13 12:37:04 -040095 char *filecon;
96
97 if (socketcon)
98 setsockcreatecon(socketcon);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
100 fd = socket(PF_UNIX, type, 0);
101 if (fd < 0) {
102 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
103 return -1;
104 }
105
Stephen Smalley8348d272013-05-13 12:37:04 -0400106 if (socketcon)
107 setsockcreatecon(NULL);
108
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 memset(&addr, 0 , sizeof(addr));
110 addr.sun_family = AF_UNIX;
111 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
112 name);
113
114 ret = unlink(addr.sun_path);
115 if (ret != 0 && errno != ENOENT) {
116 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
117 goto out_close;
118 }
119
Stephen Smalley8348d272013-05-13 12:37:04 -0400120 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500121 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400122 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500123 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400124 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500125 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500126
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
128 if (ret) {
129 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
130 goto out_unlink;
131 }
132
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500133 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400134 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 chown(addr.sun_path, uid, gid);
137 chmod(addr.sun_path, perm);
138
139 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
140 addr.sun_path, perm, uid, gid);
141
142 return fd;
143
144out_unlink:
145 unlink(addr.sun_path);
146out_close:
147 close(fd);
148 return -1;
149}
150
Elliott Hughesf682b472015-02-06 12:19:48 -0800151bool read_file(const char* path, std::string* content) {
152 content->clear();
153
154 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
155 if (fd == -1) {
156 return false;
157 }
158
159 // For security reasons, disallow world-writable
160 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800161 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800162 if (fstat(fd, &sb) == -1) {
163 ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
164 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800165 }
166 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800167 ERROR("skipping insecure file '%s'\n", path);
168 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800169 }
170
Elliott Hughesf682b472015-02-06 12:19:48 -0800171 bool okay = android::ReadFdToString(fd, content);
172 TEMP_FAILURE_RETRY(close(fd));
173 if (okay) {
174 content->append("\n", 1);
175 }
176 return okay;
177}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Elliott Hughesf682b472015-02-06 12:19:48 -0800179int write_file(const char* path, const char* content) {
180 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
181 if (fd == -1) {
182 return -errno;
183 }
184 int result = android::WriteStringToFd(content, fd) ? 0 : -errno;
185 TEMP_FAILURE_RETRY(close(fd));
186 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187}
188
Colin Crossf24ed8c2010-04-12 18:51:06 -0700189#define MAX_MTD_PARTITIONS 16
190
191static struct {
192 char name[16];
193 int number;
194} mtd_part_map[MAX_MTD_PARTITIONS];
195
196static int mtd_part_count = -1;
197
198static void find_mtd_partitions(void)
199{
200 int fd;
201 char buf[1024];
202 char *pmtdbufp;
203 ssize_t pmtdsize;
204 int r;
205
Nick Kralevich45a884f2015-02-02 14:37:22 -0800206 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700207 if (fd < 0)
208 return;
209
210 buf[sizeof(buf) - 1] = '\0';
211 pmtdsize = read(fd, buf, sizeof(buf) - 1);
212 pmtdbufp = buf;
213 while (pmtdsize > 0) {
214 int mtdnum, mtdsize, mtderasesize;
215 char mtdname[16];
216 mtdname[0] = '\0';
217 mtdnum = -1;
218 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
219 &mtdnum, &mtdsize, &mtderasesize, mtdname);
220 if ((r == 4) && (mtdname[0] == '"')) {
221 char *x = strchr(mtdname + 1, '"');
222 if (x) {
223 *x = 0;
224 }
225 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
226 if (mtd_part_count < MAX_MTD_PARTITIONS) {
227 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
228 mtd_part_map[mtd_part_count].number = mtdnum;
229 mtd_part_count++;
230 } else {
231 ERROR("too many mtd partitions\n");
232 }
233 }
234 while (pmtdsize > 0 && *pmtdbufp != '\n') {
235 pmtdbufp++;
236 pmtdsize--;
237 }
238 if (pmtdsize > 0) {
239 pmtdbufp++;
240 pmtdsize--;
241 }
242 }
243 close(fd);
244}
245
246int mtd_name_to_number(const char *name)
247{
248 int n;
249 if (mtd_part_count < 0) {
250 mtd_part_count = 0;
251 find_mtd_partitions();
252 }
253 for (n = 0; n < mtd_part_count; n++) {
254 if (!strcmp(name, mtd_part_map[n].name)) {
255 return mtd_part_map[n].number;
256 }
257 }
258 return -1;
259}
Colin Cross504bc512010-04-13 19:35:09 -0700260
261/*
262 * gettime() - returns the time in seconds of the system's monotonic clock or
263 * zero on error.
264 */
265time_t gettime(void)
266{
267 struct timespec ts;
268 int ret;
269
270 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
271 if (ret < 0) {
272 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
273 return 0;
274 }
275
276 return ts.tv_sec;
277}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700278
279int mkdir_recursive(const char *pathname, mode_t mode)
280{
281 char buf[128];
282 const char *slash;
283 const char *p = pathname;
284 int width;
285 int ret;
286 struct stat info;
287
288 while ((slash = strchr(p, '/')) != NULL) {
289 width = slash - pathname;
290 p = slash + 1;
291 if (width < 0)
292 break;
293 if (width == 0)
294 continue;
295 if ((unsigned int)width > sizeof(buf) - 1) {
296 ERROR("path too long for mkdir_recursive\n");
297 return -1;
298 }
299 memcpy(buf, pathname, width);
300 buf[width] = 0;
301 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400302 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700303 if (ret && errno != EEXIST)
304 return ret;
305 }
306 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400307 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700308 if (ret && errno != EEXIST)
309 return ret;
310 return 0;
311}
312
Johan Redestig93ca79b2012-04-18 16:41:19 +0200313/*
314 * replaces any unacceptable characters with '_', the
315 * length of the resulting string is equal to the input string
316 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700317void sanitize(char *s)
318{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200319 const char* accept =
320 "abcdefghijklmnopqrstuvwxyz"
321 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
322 "0123456789"
323 "_-.";
324
Colin Crossb0ab94b2010-04-08 16:16:20 -0700325 if (!s)
326 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200327
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400328 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200329 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400330 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200331 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700332}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200333
Colin Crossb0ab94b2010-04-08 16:16:20 -0700334void make_link(const char *oldpath, const char *newpath)
335{
336 int ret;
337 char buf[256];
338 char *slash;
339 int width;
340
341 slash = strrchr(newpath, '/');
342 if (!slash)
343 return;
344 width = slash - newpath;
345 if (width <= 0 || width > (int)sizeof(buf) - 1)
346 return;
347 memcpy(buf, newpath, width);
348 buf[width] = 0;
349 ret = mkdir_recursive(buf, 0755);
350 if (ret)
351 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
352
353 ret = symlink(oldpath, newpath);
354 if (ret && errno != EEXIST)
355 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
356}
357
358void remove_link(const char *oldpath, const char *newpath)
359{
360 char path[256];
361 ssize_t ret;
362 ret = readlink(newpath, path, sizeof(path) - 1);
363 if (ret <= 0)
364 return;
365 path[ret] = 0;
366 if (!strcmp(path, oldpath))
367 unlink(newpath);
368}
Colin Crosscd0f1732010-04-19 17:10:24 -0700369
370int wait_for_file(const char *filename, int timeout)
371{
372 struct stat info;
373 time_t timeout_time = gettime() + timeout;
374 int ret = -1;
375
376 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
377 usleep(10000);
378
379 return ret;
380}
Colin Crossf83d0b92010-04-21 12:04:20 -0700381
382void open_devnull_stdio(void)
383{
384 int fd;
385 static const char *name = "/dev/__null__";
386 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
387 fd = open(name, O_RDWR);
388 unlink(name);
389 if (fd >= 0) {
390 dup2(fd, 0);
391 dup2(fd, 1);
392 dup2(fd, 2);
393 if (fd > 2) {
394 close(fd);
395 }
396 return;
397 }
398 }
399
400 exit(1);
401}
402
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800403void get_hardware_name(char *hardware, unsigned int *revision) {
404 // Hardware string was provided on kernel command line.
405 if (hardware[0]) {
406 return;
407 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700408
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800409 FILE* fp = fopen("/proc/cpuinfo", "re");
410 if (fp == NULL) {
411 return;
412 }
413 char buf[1024];
414 while (fgets(buf, sizeof(buf), fp) != NULL) {
415 if (strncmp(buf, "Hardware", 8) == 0) {
416 const char* hw = strstr(buf, ": ");
417 if (hw) {
418 hw += 2;
419 size_t n = 0;
420 while (*hw) {
421 if (!isspace(*hw)) {
422 hardware[n++] = tolower(*hw);
423 }
424 hw++;
425 if (n == 31) break;
Jon Medhurst229dc352012-12-06 17:00:55 +0000426 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800427 hardware[n] = 0;
428 }
429 } else if (strncmp(buf, "Revision", 8) == 0) {
Elliott Hughes24627902015-02-04 10:25:09 -0800430 sscanf(buf, "Revision : %ux", revision);
Jon Medhurst229dc352012-12-06 17:00:55 +0000431 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800432 }
433 fclose(fp);
Colin Crossf83d0b92010-04-21 12:04:20 -0700434}
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700435
436void import_kernel_cmdline(int in_qemu,
437 void (*import_kernel_nv)(char *name, int in_qemu))
438{
Andrew Boie2e63e712013-09-09 13:08:17 -0700439 char cmdline[2048];
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700440 char *ptr;
441 int fd;
442
Nick Kralevich45a884f2015-02-02 14:37:22 -0800443 fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700444 if (fd >= 0) {
Andrew Boie2e63e712013-09-09 13:08:17 -0700445 int n = read(fd, cmdline, sizeof(cmdline) - 1);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700446 if (n < 0) n = 0;
447
448 /* get rid of trailing newline, it happens */
449 if (n > 0 && cmdline[n-1] == '\n') n--;
450
451 cmdline[n] = 0;
452 close(fd);
453 } else {
454 cmdline[0] = 0;
455 }
456
457 ptr = cmdline;
458 while (ptr && *ptr) {
459 char *x = strchr(ptr, ' ');
460 if (x != 0) *x++ = 0;
461 import_kernel_nv(ptr, in_qemu);
462 ptr = x;
463 }
464}
Stephen Smalleye096e362012-06-11 13:37:39 -0400465
466int make_dir(const char *path, mode_t mode)
467{
468 int rc;
469
Stephen Smalleye096e362012-06-11 13:37:39 -0400470 char *secontext = NULL;
471
472 if (sehandle) {
473 selabel_lookup(sehandle, &secontext, path, mode);
474 setfscreatecon(secontext);
475 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400476
477 rc = mkdir(path, mode);
478
Stephen Smalleye096e362012-06-11 13:37:39 -0400479 if (secontext) {
480 int save_errno = errno;
481 freecon(secontext);
482 setfscreatecon(NULL);
483 errno = save_errno;
484 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700485
Stephen Smalleye096e362012-06-11 13:37:39 -0400486 return rc;
487}
488
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500489int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400490{
Stephen Smalley27a93652014-02-07 09:14:13 -0500491 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700492}
493
494int restorecon_recursive(const char* pathname)
495{
Stephen Smalley27a93652014-02-07 09:14:13 -0500496 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700497}