blob: 0f69e1c5fbc1c6fbe89085d6dccdda75a0eccf85 [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
38#include <private/android_filesystem_config.h>
39
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070041#include "log.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070042#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044/*
45 * android_name_to_id - returns the integer uid/gid associated with the given
46 * name, or -1U on error.
47 */
48static unsigned int android_name_to_id(const char *name)
49{
Edwin Vanede7f1ad2012-07-26 14:09:13 -040050 const struct android_id_info *info = android_ids;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 unsigned int n;
52
53 for (n = 0; n < android_id_count; n++) {
54 if (!strcmp(info[n].name, name))
55 return info[n].aid;
56 }
57
58 return -1U;
59}
60
61/*
62 * decode_uid - decodes and returns the given string, which can be either the
63 * numeric or name representation, into the integer uid or gid. Returns -1U on
64 * error.
65 */
66unsigned int decode_uid(const char *s)
67{
68 unsigned int v;
69
70 if (!s || *s == '\0')
71 return -1U;
72 if (isalpha(s[0]))
73 return android_name_to_id(s);
74
75 errno = 0;
76 v = (unsigned int) strtoul(s, 0, 0);
77 if (errno)
78 return -1U;
79 return v;
80}
81
82/*
83 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
84 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
85 * daemon. We communicate the file descriptor's value via the environment
86 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
87 */
Stephen Smalley8348d272013-05-13 12:37:04 -040088int create_socket(const char *name, int type, mode_t perm, uid_t uid,
89 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090{
91 struct sockaddr_un addr;
92 int fd, ret;
Stephen Smalley8348d272013-05-13 12:37:04 -040093 char *filecon;
94
95 if (socketcon)
96 setsockcreatecon(socketcon);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
98 fd = socket(PF_UNIX, type, 0);
99 if (fd < 0) {
100 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
101 return -1;
102 }
103
Stephen Smalley8348d272013-05-13 12:37:04 -0400104 if (socketcon)
105 setsockcreatecon(NULL);
106
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 memset(&addr, 0 , sizeof(addr));
108 addr.sun_family = AF_UNIX;
109 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
110 name);
111
112 ret = unlink(addr.sun_path);
113 if (ret != 0 && errno != ENOENT) {
114 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
115 goto out_close;
116 }
117
Stephen Smalley8348d272013-05-13 12:37:04 -0400118 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500119 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400120 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500121 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400122 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500123 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500124
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
126 if (ret) {
127 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
128 goto out_unlink;
129 }
130
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500131 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400132 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 chown(addr.sun_path, uid, gid);
135 chmod(addr.sun_path, perm);
136
137 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
138 addr.sun_path, perm, uid, gid);
139
140 return fd;
141
142out_unlink:
143 unlink(addr.sun_path);
144out_close:
145 close(fd);
146 return -1;
147}
148
149/* reads a file, making sure it is terminated with \n \0 */
150void *read_file(const char *fn, unsigned *_sz)
151{
152 char *data;
153 int sz;
154 int fd;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800155 struct stat sb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
157 data = 0;
158 fd = open(fn, O_RDONLY);
159 if(fd < 0) return 0;
160
Nick Kralevich38f368c2012-01-18 10:39:01 -0800161 // for security reasons, disallow world-writable
162 // or group-writable files
163 if (fstat(fd, &sb) < 0) {
164 ERROR("fstat failed for '%s'\n", fn);
165 goto oops;
166 }
167 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
168 ERROR("skipping insecure file '%s'\n", fn);
169 goto oops;
170 }
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 sz = lseek(fd, 0, SEEK_END);
173 if(sz < 0) goto oops;
174
175 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
176
177 data = (char*) malloc(sz + 2);
178 if(data == 0) goto oops;
179
180 if(read(fd, data, sz) != sz) goto oops;
181 close(fd);
182 data[sz] = '\n';
183 data[sz+1] = 0;
184 if(_sz) *_sz = sz;
185 return data;
186
187oops:
188 close(fd);
189 if(data != 0) free(data);
190 return 0;
191}
192
Colin Crossf24ed8c2010-04-12 18:51:06 -0700193#define MAX_MTD_PARTITIONS 16
194
195static struct {
196 char name[16];
197 int number;
198} mtd_part_map[MAX_MTD_PARTITIONS];
199
200static int mtd_part_count = -1;
201
202static void find_mtd_partitions(void)
203{
204 int fd;
205 char buf[1024];
206 char *pmtdbufp;
207 ssize_t pmtdsize;
208 int r;
209
210 fd = open("/proc/mtd", O_RDONLY);
211 if (fd < 0)
212 return;
213
214 buf[sizeof(buf) - 1] = '\0';
215 pmtdsize = read(fd, buf, sizeof(buf) - 1);
216 pmtdbufp = buf;
217 while (pmtdsize > 0) {
218 int mtdnum, mtdsize, mtderasesize;
219 char mtdname[16];
220 mtdname[0] = '\0';
221 mtdnum = -1;
222 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
223 &mtdnum, &mtdsize, &mtderasesize, mtdname);
224 if ((r == 4) && (mtdname[0] == '"')) {
225 char *x = strchr(mtdname + 1, '"');
226 if (x) {
227 *x = 0;
228 }
229 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
230 if (mtd_part_count < MAX_MTD_PARTITIONS) {
231 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
232 mtd_part_map[mtd_part_count].number = mtdnum;
233 mtd_part_count++;
234 } else {
235 ERROR("too many mtd partitions\n");
236 }
237 }
238 while (pmtdsize > 0 && *pmtdbufp != '\n') {
239 pmtdbufp++;
240 pmtdsize--;
241 }
242 if (pmtdsize > 0) {
243 pmtdbufp++;
244 pmtdsize--;
245 }
246 }
247 close(fd);
248}
249
250int mtd_name_to_number(const char *name)
251{
252 int n;
253 if (mtd_part_count < 0) {
254 mtd_part_count = 0;
255 find_mtd_partitions();
256 }
257 for (n = 0; n < mtd_part_count; n++) {
258 if (!strcmp(name, mtd_part_map[n].name)) {
259 return mtd_part_map[n].number;
260 }
261 }
262 return -1;
263}
Colin Cross504bc512010-04-13 19:35:09 -0700264
265/*
266 * gettime() - returns the time in seconds of the system's monotonic clock or
267 * zero on error.
268 */
269time_t gettime(void)
270{
271 struct timespec ts;
272 int ret;
273
274 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
275 if (ret < 0) {
276 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
277 return 0;
278 }
279
280 return ts.tv_sec;
281}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700282
283int mkdir_recursive(const char *pathname, mode_t mode)
284{
285 char buf[128];
286 const char *slash;
287 const char *p = pathname;
288 int width;
289 int ret;
290 struct stat info;
291
292 while ((slash = strchr(p, '/')) != NULL) {
293 width = slash - pathname;
294 p = slash + 1;
295 if (width < 0)
296 break;
297 if (width == 0)
298 continue;
299 if ((unsigned int)width > sizeof(buf) - 1) {
300 ERROR("path too long for mkdir_recursive\n");
301 return -1;
302 }
303 memcpy(buf, pathname, width);
304 buf[width] = 0;
305 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400306 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700307 if (ret && errno != EEXIST)
308 return ret;
309 }
310 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400311 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700312 if (ret && errno != EEXIST)
313 return ret;
314 return 0;
315}
316
Johan Redestig93ca79b2012-04-18 16:41:19 +0200317/*
318 * replaces any unacceptable characters with '_', the
319 * length of the resulting string is equal to the input string
320 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700321void sanitize(char *s)
322{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200323 const char* accept =
324 "abcdefghijklmnopqrstuvwxyz"
325 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
326 "0123456789"
327 "_-.";
328
Colin Crossb0ab94b2010-04-08 16:16:20 -0700329 if (!s)
330 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200331
332 for (; *s; s++) {
333 s += strspn(s, accept);
334 if (*s) *s = '_';
335 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700336}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200337
Colin Crossb0ab94b2010-04-08 16:16:20 -0700338void make_link(const char *oldpath, const char *newpath)
339{
340 int ret;
341 char buf[256];
342 char *slash;
343 int width;
344
345 slash = strrchr(newpath, '/');
346 if (!slash)
347 return;
348 width = slash - newpath;
349 if (width <= 0 || width > (int)sizeof(buf) - 1)
350 return;
351 memcpy(buf, newpath, width);
352 buf[width] = 0;
353 ret = mkdir_recursive(buf, 0755);
354 if (ret)
355 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
356
357 ret = symlink(oldpath, newpath);
358 if (ret && errno != EEXIST)
359 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
360}
361
362void remove_link(const char *oldpath, const char *newpath)
363{
364 char path[256];
365 ssize_t ret;
366 ret = readlink(newpath, path, sizeof(path) - 1);
367 if (ret <= 0)
368 return;
369 path[ret] = 0;
370 if (!strcmp(path, oldpath))
371 unlink(newpath);
372}
Colin Crosscd0f1732010-04-19 17:10:24 -0700373
374int wait_for_file(const char *filename, int timeout)
375{
376 struct stat info;
377 time_t timeout_time = gettime() + timeout;
378 int ret = -1;
379
380 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
381 usleep(10000);
382
383 return ret;
384}
Colin Crossf83d0b92010-04-21 12:04:20 -0700385
386void open_devnull_stdio(void)
387{
388 int fd;
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 if (fd >= 0) {
394 dup2(fd, 0);
395 dup2(fd, 1);
396 dup2(fd, 2);
397 if (fd > 2) {
398 close(fd);
399 }
400 return;
401 }
402 }
403
404 exit(1);
405}
406
407void get_hardware_name(char *hardware, unsigned int *revision)
408{
Jon Medhurst229dc352012-12-06 17:00:55 +0000409 const char *cpuinfo = "/proc/cpuinfo";
410 char *data = NULL;
411 size_t len = 0, limit = 1024;
Colin Crossf83d0b92010-04-21 12:04:20 -0700412 int fd, n;
413 char *x, *hw, *rev;
414
415 /* Hardware string was provided on kernel command line */
416 if (hardware[0])
417 return;
418
Jon Medhurst229dc352012-12-06 17:00:55 +0000419 fd = open(cpuinfo, O_RDONLY);
Colin Crossf83d0b92010-04-21 12:04:20 -0700420 if (fd < 0) return;
421
Jon Medhurst229dc352012-12-06 17:00:55 +0000422 for (;;) {
423 x = realloc(data, limit);
424 if (!x) {
425 ERROR("Failed to allocate memory to read %s\n", cpuinfo);
426 goto done;
427 }
428 data = x;
Colin Crossf83d0b92010-04-21 12:04:20 -0700429
Jon Medhurst229dc352012-12-06 17:00:55 +0000430 n = read(fd, data + len, limit - len);
431 if (n < 0) {
432 ERROR("Failed reading %s: %s (%d)\n", cpuinfo, strerror(errno), errno);
433 goto done;
434 }
435 len += n;
436
437 if (len < limit)
438 break;
439
440 /* We filled the buffer, so increase size and loop to read more */
441 limit *= 2;
442 }
443
444 data[len] = 0;
Colin Crossf83d0b92010-04-21 12:04:20 -0700445 hw = strstr(data, "\nHardware");
446 rev = strstr(data, "\nRevision");
447
448 if (hw) {
449 x = strstr(hw, ": ");
450 if (x) {
451 x += 2;
452 n = 0;
The Android Open Source Project742150c2010-07-16 13:37:09 -0700453 while (*x && *x != '\n') {
454 if (!isspace(*x))
455 hardware[n++] = tolower(*x);
Colin Crossf83d0b92010-04-21 12:04:20 -0700456 x++;
457 if (n == 31) break;
458 }
459 hardware[n] = 0;
460 }
461 }
462
463 if (rev) {
464 x = strstr(rev, ": ");
465 if (x) {
466 *revision = strtoul(x + 2, 0, 16);
467 }
468 }
Jon Medhurst229dc352012-12-06 17:00:55 +0000469
470done:
471 close(fd);
472 free(data);
Colin Crossf83d0b92010-04-21 12:04:20 -0700473}
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700474
475void import_kernel_cmdline(int in_qemu,
476 void (*import_kernel_nv)(char *name, int in_qemu))
477{
Andrew Boie2e63e712013-09-09 13:08:17 -0700478 char cmdline[2048];
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700479 char *ptr;
480 int fd;
481
482 fd = open("/proc/cmdline", O_RDONLY);
483 if (fd >= 0) {
Andrew Boie2e63e712013-09-09 13:08:17 -0700484 int n = read(fd, cmdline, sizeof(cmdline) - 1);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700485 if (n < 0) n = 0;
486
487 /* get rid of trailing newline, it happens */
488 if (n > 0 && cmdline[n-1] == '\n') n--;
489
490 cmdline[n] = 0;
491 close(fd);
492 } else {
493 cmdline[0] = 0;
494 }
495
496 ptr = cmdline;
497 while (ptr && *ptr) {
498 char *x = strchr(ptr, ' ');
499 if (x != 0) *x++ = 0;
500 import_kernel_nv(ptr, in_qemu);
501 ptr = x;
502 }
503}
Stephen Smalleye096e362012-06-11 13:37:39 -0400504
505int make_dir(const char *path, mode_t mode)
506{
507 int rc;
508
Stephen Smalleye096e362012-06-11 13:37:39 -0400509 char *secontext = NULL;
510
511 if (sehandle) {
512 selabel_lookup(sehandle, &secontext, path, mode);
513 setfscreatecon(secontext);
514 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400515
516 rc = mkdir(path, mode);
517
Stephen Smalleye096e362012-06-11 13:37:39 -0400518 if (secontext) {
519 int save_errno = errno;
520 freecon(secontext);
521 setfscreatecon(NULL);
522 errno = save_errno;
523 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700524
Stephen Smalleye096e362012-06-11 13:37:39 -0400525 return rc;
526}
527
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500528int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400529{
Stephen Smalley27a93652014-02-07 09:14:13 -0500530 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700531}
532
533int restorecon_recursive(const char* pathname)
534{
Stephen Smalley27a93652014-02-07 09:14:13 -0500535 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700536}