blob: 3b49b30d36abb8f85ccc014cb1e749336f625af4 [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>
39
40#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
Dan Albertc007bc32015-03-16 10:08:46 -0700171 bool okay = android::base::ReadFdToString(fd, content);
Elliott Hughesf682b472015-02-06 12:19:48 -0800172 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 }
Dan Albertc007bc32015-03-16 10:08:46 -0700184 int result = android::base::WriteStringToFd(content, fd) ? 0 : -errno;
Elliott Hughesf682b472015-02-06 12:19:48 -0800185 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
Elliott Hughesda40c002015-03-27 23:20:44 -0700261time_t gettime() {
262 timespec now;
263 clock_gettime(CLOCK_MONOTONIC, &now);
264 return now.tv_sec;
265}
Colin Cross504bc512010-04-13 19:35:09 -0700266
Elliott Hughesda40c002015-03-27 23:20:44 -0700267uint64_t gettime_ns() {
268 timespec now;
269 clock_gettime(CLOCK_MONOTONIC, &now);
270 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700271}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700272
273int mkdir_recursive(const char *pathname, mode_t mode)
274{
275 char buf[128];
276 const char *slash;
277 const char *p = pathname;
278 int width;
279 int ret;
280 struct stat info;
281
282 while ((slash = strchr(p, '/')) != NULL) {
283 width = slash - pathname;
284 p = slash + 1;
285 if (width < 0)
286 break;
287 if (width == 0)
288 continue;
289 if ((unsigned int)width > sizeof(buf) - 1) {
290 ERROR("path too long for mkdir_recursive\n");
291 return -1;
292 }
293 memcpy(buf, pathname, width);
294 buf[width] = 0;
295 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400296 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700297 if (ret && errno != EEXIST)
298 return ret;
299 }
300 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400301 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700302 if (ret && errno != EEXIST)
303 return ret;
304 return 0;
305}
306
Johan Redestig93ca79b2012-04-18 16:41:19 +0200307/*
308 * replaces any unacceptable characters with '_', the
309 * length of the resulting string is equal to the input string
310 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700311void sanitize(char *s)
312{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200313 const char* accept =
314 "abcdefghijklmnopqrstuvwxyz"
315 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
316 "0123456789"
317 "_-.";
318
Colin Crossb0ab94b2010-04-08 16:16:20 -0700319 if (!s)
320 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200321
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400322 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200323 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400324 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200325 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700326}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200327
Colin Crossb0ab94b2010-04-08 16:16:20 -0700328void make_link(const char *oldpath, const char *newpath)
329{
330 int ret;
331 char buf[256];
332 char *slash;
333 int width;
334
335 slash = strrchr(newpath, '/');
336 if (!slash)
337 return;
338 width = slash - newpath;
339 if (width <= 0 || width > (int)sizeof(buf) - 1)
340 return;
341 memcpy(buf, newpath, width);
342 buf[width] = 0;
343 ret = mkdir_recursive(buf, 0755);
344 if (ret)
345 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
346
347 ret = symlink(oldpath, newpath);
348 if (ret && errno != EEXIST)
349 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
350}
351
352void remove_link(const char *oldpath, const char *newpath)
353{
354 char path[256];
355 ssize_t ret;
356 ret = readlink(newpath, path, sizeof(path) - 1);
357 if (ret <= 0)
358 return;
359 path[ret] = 0;
360 if (!strcmp(path, oldpath))
361 unlink(newpath);
362}
Colin Crosscd0f1732010-04-19 17:10:24 -0700363
364int wait_for_file(const char *filename, int timeout)
365{
366 struct stat info;
367 time_t timeout_time = gettime() + timeout;
368 int ret = -1;
369
370 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
371 usleep(10000);
372
373 return ret;
374}
Colin Crossf83d0b92010-04-21 12:04:20 -0700375
376void open_devnull_stdio(void)
377{
378 int fd;
379 static const char *name = "/dev/__null__";
380 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
381 fd = open(name, O_RDWR);
382 unlink(name);
383 if (fd >= 0) {
384 dup2(fd, 0);
385 dup2(fd, 1);
386 dup2(fd, 2);
387 if (fd > 2) {
388 close(fd);
389 }
390 return;
391 }
392 }
393
394 exit(1);
395}
396
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700397void import_kernel_cmdline(int in_qemu,
398 void (*import_kernel_nv)(char *name, int in_qemu))
399{
Andrew Boie2e63e712013-09-09 13:08:17 -0700400 char cmdline[2048];
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700401 char *ptr;
402 int fd;
403
Nick Kralevich45a884f2015-02-02 14:37:22 -0800404 fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700405 if (fd >= 0) {
Andrew Boie2e63e712013-09-09 13:08:17 -0700406 int n = read(fd, cmdline, sizeof(cmdline) - 1);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700407 if (n < 0) n = 0;
408
409 /* get rid of trailing newline, it happens */
410 if (n > 0 && cmdline[n-1] == '\n') n--;
411
412 cmdline[n] = 0;
413 close(fd);
414 } else {
415 cmdline[0] = 0;
416 }
417
418 ptr = cmdline;
419 while (ptr && *ptr) {
420 char *x = strchr(ptr, ' ');
421 if (x != 0) *x++ = 0;
422 import_kernel_nv(ptr, in_qemu);
423 ptr = x;
424 }
425}
Stephen Smalleye096e362012-06-11 13:37:39 -0400426
427int make_dir(const char *path, mode_t mode)
428{
429 int rc;
430
Stephen Smalleye096e362012-06-11 13:37:39 -0400431 char *secontext = NULL;
432
433 if (sehandle) {
434 selabel_lookup(sehandle, &secontext, path, mode);
435 setfscreatecon(secontext);
436 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400437
438 rc = mkdir(path, mode);
439
Stephen Smalleye096e362012-06-11 13:37:39 -0400440 if (secontext) {
441 int save_errno = errno;
442 freecon(secontext);
443 setfscreatecon(NULL);
444 errno = save_errno;
445 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700446
Stephen Smalleye096e362012-06-11 13:37:39 -0400447 return rc;
448}
449
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500450int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400451{
Stephen Smalley27a93652014-02-07 09:14:13 -0500452 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700453}
454
455int restorecon_recursive(const char* pathname)
456{
Stephen Smalley27a93652014-02-07 09:14:13 -0500457 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700458}