blob: 332aa2aa03132876f40cbfccfed7bcb3eecb5cd0 [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) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700182 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
183 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800184 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700185 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
186 if (result == -1) {
187 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
188 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800189 TEMP_FAILURE_RETRY(close(fd));
190 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191}
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
Nick Kralevich45a884f2015-02-02 14:37:22 -0800210 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700211 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
Elliott Hughesda40c002015-03-27 23:20:44 -0700265time_t gettime() {
266 timespec now;
267 clock_gettime(CLOCK_MONOTONIC, &now);
268 return now.tv_sec;
269}
Colin Cross504bc512010-04-13 19:35:09 -0700270
Elliott Hughesda40c002015-03-27 23:20:44 -0700271uint64_t gettime_ns() {
272 timespec now;
273 clock_gettime(CLOCK_MONOTONIC, &now);
274 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700275}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700276
277int mkdir_recursive(const char *pathname, mode_t mode)
278{
279 char buf[128];
280 const char *slash;
281 const char *p = pathname;
282 int width;
283 int ret;
284 struct stat info;
285
286 while ((slash = strchr(p, '/')) != NULL) {
287 width = slash - pathname;
288 p = slash + 1;
289 if (width < 0)
290 break;
291 if (width == 0)
292 continue;
293 if ((unsigned int)width > sizeof(buf) - 1) {
294 ERROR("path too long for mkdir_recursive\n");
295 return -1;
296 }
297 memcpy(buf, pathname, width);
298 buf[width] = 0;
299 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400300 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700301 if (ret && errno != EEXIST)
302 return ret;
303 }
304 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400305 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700306 if (ret && errno != EEXIST)
307 return ret;
308 return 0;
309}
310
Johan Redestig93ca79b2012-04-18 16:41:19 +0200311/*
312 * replaces any unacceptable characters with '_', the
313 * length of the resulting string is equal to the input string
314 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700315void sanitize(char *s)
316{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200317 const char* accept =
318 "abcdefghijklmnopqrstuvwxyz"
319 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
320 "0123456789"
321 "_-.";
322
Colin Crossb0ab94b2010-04-08 16:16:20 -0700323 if (!s)
324 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200325
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400326 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200327 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400328 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200329 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700330}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200331
Colin Crossb0ab94b2010-04-08 16:16:20 -0700332void make_link(const char *oldpath, const char *newpath)
333{
334 int ret;
335 char buf[256];
336 char *slash;
337 int width;
338
339 slash = strrchr(newpath, '/');
340 if (!slash)
341 return;
342 width = slash - newpath;
343 if (width <= 0 || width > (int)sizeof(buf) - 1)
344 return;
345 memcpy(buf, newpath, width);
346 buf[width] = 0;
347 ret = mkdir_recursive(buf, 0755);
348 if (ret)
349 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
350
351 ret = symlink(oldpath, newpath);
352 if (ret && errno != EEXIST)
353 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
354}
355
356void remove_link(const char *oldpath, const char *newpath)
357{
358 char path[256];
359 ssize_t ret;
360 ret = readlink(newpath, path, sizeof(path) - 1);
361 if (ret <= 0)
362 return;
363 path[ret] = 0;
364 if (!strcmp(path, oldpath))
365 unlink(newpath);
366}
Colin Crosscd0f1732010-04-19 17:10:24 -0700367
368int wait_for_file(const char *filename, int timeout)
369{
370 struct stat info;
371 time_t timeout_time = gettime() + timeout;
372 int ret = -1;
373
374 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
375 usleep(10000);
376
377 return ret;
378}
Colin Crossf83d0b92010-04-21 12:04:20 -0700379
380void open_devnull_stdio(void)
381{
382 int fd;
383 static const char *name = "/dev/__null__";
384 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
385 fd = open(name, O_RDWR);
386 unlink(name);
387 if (fd >= 0) {
388 dup2(fd, 0);
389 dup2(fd, 1);
390 dup2(fd, 2);
391 if (fd > 2) {
392 close(fd);
393 }
394 return;
395 }
396 }
397
398 exit(1);
399}
400
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700401void import_kernel_cmdline(int in_qemu,
402 void (*import_kernel_nv)(char *name, int in_qemu))
403{
Andrew Boie2e63e712013-09-09 13:08:17 -0700404 char cmdline[2048];
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700405 char *ptr;
406 int fd;
407
Nick Kralevich45a884f2015-02-02 14:37:22 -0800408 fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700409 if (fd >= 0) {
Andrew Boie2e63e712013-09-09 13:08:17 -0700410 int n = read(fd, cmdline, sizeof(cmdline) - 1);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700411 if (n < 0) n = 0;
412
413 /* get rid of trailing newline, it happens */
414 if (n > 0 && cmdline[n-1] == '\n') n--;
415
416 cmdline[n] = 0;
417 close(fd);
418 } else {
419 cmdline[0] = 0;
420 }
421
422 ptr = cmdline;
423 while (ptr && *ptr) {
424 char *x = strchr(ptr, ' ');
425 if (x != 0) *x++ = 0;
426 import_kernel_nv(ptr, in_qemu);
427 ptr = x;
428 }
429}
Stephen Smalleye096e362012-06-11 13:37:39 -0400430
431int make_dir(const char *path, mode_t mode)
432{
433 int rc;
434
Stephen Smalleye096e362012-06-11 13:37:39 -0400435 char *secontext = NULL;
436
437 if (sehandle) {
438 selabel_lookup(sehandle, &secontext, path, mode);
439 setfscreatecon(secontext);
440 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400441
442 rc = mkdir(path, mode);
443
Stephen Smalleye096e362012-06-11 13:37:39 -0400444 if (secontext) {
445 int save_errno = errno;
446 freecon(secontext);
447 setfscreatecon(NULL);
448 errno = save_errno;
449 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700450
Stephen Smalleye096e362012-06-11 13:37:39 -0400451 return rc;
452}
453
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500454int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400455{
Stephen Smalley27a93652014-02-07 09:14:13 -0500456 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700457}
458
459int restorecon_recursive(const char* pathname)
460{
Stephen Smalley27a93652014-02-07 09:14:13 -0500461 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700462}