blob: 59642679cb6f3214a0bce493aaf6829be00b6839 [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>
Elliott Hughese5ce30f2015-05-06 19:19:24 -070036#include <base/strings.h>
Dan Albertc007bc32015-03-16 10:08:46 -070037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038/* for ANDROID_SOCKET_* */
39#include <cutils/sockets.h>
Andres Moralescb3fce82015-05-08 08:30:33 -070040#include <base/stringprintf.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42#include <private/android_filesystem_config.h>
43
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070045#include "log.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070046#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048/*
49 * android_name_to_id - returns the integer uid/gid associated with the given
50 * name, or -1U on error.
51 */
52static unsigned int android_name_to_id(const char *name)
53{
Edwin Vanede7f1ad2012-07-26 14:09:13 -040054 const struct android_id_info *info = android_ids;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 unsigned int n;
56
57 for (n = 0; n < android_id_count; n++) {
58 if (!strcmp(info[n].name, name))
59 return info[n].aid;
60 }
61
62 return -1U;
63}
64
65/*
66 * decode_uid - decodes and returns the given string, which can be either the
67 * numeric or name representation, into the integer uid or gid. Returns -1U on
68 * error.
69 */
70unsigned int decode_uid(const char *s)
71{
72 unsigned int v;
73
74 if (!s || *s == '\0')
75 return -1U;
76 if (isalpha(s[0]))
77 return android_name_to_id(s);
78
79 errno = 0;
80 v = (unsigned int) strtoul(s, 0, 0);
81 if (errno)
82 return -1U;
83 return v;
84}
85
86/*
87 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
88 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
89 * daemon. We communicate the file descriptor's value via the environment
90 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
91 */
Stephen Smalley8348d272013-05-13 12:37:04 -040092int create_socket(const char *name, int type, mode_t perm, uid_t uid,
93 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094{
95 struct sockaddr_un addr;
96 int fd, ret;
Stephen Smalley8348d272013-05-13 12:37:04 -040097 char *filecon;
98
99 if (socketcon)
100 setsockcreatecon(socketcon);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
102 fd = socket(PF_UNIX, type, 0);
103 if (fd < 0) {
104 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
105 return -1;
106 }
107
Stephen Smalley8348d272013-05-13 12:37:04 -0400108 if (socketcon)
109 setsockcreatecon(NULL);
110
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 memset(&addr, 0 , sizeof(addr));
112 addr.sun_family = AF_UNIX;
113 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
114 name);
115
116 ret = unlink(addr.sun_path);
117 if (ret != 0 && errno != ENOENT) {
118 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
119 goto out_close;
120 }
121
Stephen Smalley8348d272013-05-13 12:37:04 -0400122 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500123 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400124 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500125 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400126 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500127 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500128
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
130 if (ret) {
131 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
132 goto out_unlink;
133 }
134
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500135 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400136 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 chown(addr.sun_path, uid, gid);
139 chmod(addr.sun_path, perm);
140
141 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
142 addr.sun_path, perm, uid, gid);
143
144 return fd;
145
146out_unlink:
147 unlink(addr.sun_path);
148out_close:
149 close(fd);
150 return -1;
151}
152
Elliott Hughesf682b472015-02-06 12:19:48 -0800153bool read_file(const char* path, std::string* content) {
154 content->clear();
155
156 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
157 if (fd == -1) {
158 return false;
159 }
160
161 // For security reasons, disallow world-writable
162 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800163 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800164 if (fstat(fd, &sb) == -1) {
165 ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
166 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800167 }
168 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800169 ERROR("skipping insecure file '%s'\n", path);
170 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800171 }
172
Dan Albertc007bc32015-03-16 10:08:46 -0700173 bool okay = android::base::ReadFdToString(fd, content);
Elliott Hughesf682b472015-02-06 12:19:48 -0800174 TEMP_FAILURE_RETRY(close(fd));
175 if (okay) {
176 content->append("\n", 1);
177 }
178 return okay;
179}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesf682b472015-02-06 12:19:48 -0800181int write_file(const char* path, const char* content) {
182 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
183 if (fd == -1) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700184 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
185 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800186 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700187 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
188 if (result == -1) {
189 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
190 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800191 TEMP_FAILURE_RETRY(close(fd));
192 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193}
194
Colin Crossf24ed8c2010-04-12 18:51:06 -0700195#define MAX_MTD_PARTITIONS 16
196
197static struct {
198 char name[16];
199 int number;
200} mtd_part_map[MAX_MTD_PARTITIONS];
201
202static int mtd_part_count = -1;
203
204static void find_mtd_partitions(void)
205{
206 int fd;
207 char buf[1024];
208 char *pmtdbufp;
209 ssize_t pmtdsize;
210 int r;
211
Nick Kralevich45a884f2015-02-02 14:37:22 -0800212 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700213 if (fd < 0)
214 return;
215
216 buf[sizeof(buf) - 1] = '\0';
217 pmtdsize = read(fd, buf, sizeof(buf) - 1);
218 pmtdbufp = buf;
219 while (pmtdsize > 0) {
220 int mtdnum, mtdsize, mtderasesize;
221 char mtdname[16];
222 mtdname[0] = '\0';
223 mtdnum = -1;
224 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
225 &mtdnum, &mtdsize, &mtderasesize, mtdname);
226 if ((r == 4) && (mtdname[0] == '"')) {
227 char *x = strchr(mtdname + 1, '"');
228 if (x) {
229 *x = 0;
230 }
231 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
232 if (mtd_part_count < MAX_MTD_PARTITIONS) {
233 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
234 mtd_part_map[mtd_part_count].number = mtdnum;
235 mtd_part_count++;
236 } else {
237 ERROR("too many mtd partitions\n");
238 }
239 }
240 while (pmtdsize > 0 && *pmtdbufp != '\n') {
241 pmtdbufp++;
242 pmtdsize--;
243 }
244 if (pmtdsize > 0) {
245 pmtdbufp++;
246 pmtdsize--;
247 }
248 }
249 close(fd);
250}
251
252int mtd_name_to_number(const char *name)
253{
254 int n;
255 if (mtd_part_count < 0) {
256 mtd_part_count = 0;
257 find_mtd_partitions();
258 }
259 for (n = 0; n < mtd_part_count; n++) {
260 if (!strcmp(name, mtd_part_map[n].name)) {
261 return mtd_part_map[n].number;
262 }
263 }
264 return -1;
265}
Colin Cross504bc512010-04-13 19:35:09 -0700266
Elliott Hughesda40c002015-03-27 23:20:44 -0700267time_t gettime() {
268 timespec now;
269 clock_gettime(CLOCK_MONOTONIC, &now);
270 return now.tv_sec;
271}
Colin Cross504bc512010-04-13 19:35:09 -0700272
Elliott Hughesda40c002015-03-27 23:20:44 -0700273uint64_t gettime_ns() {
274 timespec now;
275 clock_gettime(CLOCK_MONOTONIC, &now);
276 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700277}
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{
Nick Kraleviche34577c2015-04-25 16:24:53 -0700384 // Try to avoid the mknod() call if we can. Since SELinux makes
385 // a /dev/null replacement available for free, let's use it.
386 int fd = open("/sys/fs/selinux/null", O_RDWR);
387 if (fd == -1) {
388 // OOPS, /sys/fs/selinux/null isn't available, likely because
389 // /sys/fs/selinux isn't mounted. Fall back to mknod.
390 static const char *name = "/dev/__null__";
391 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
392 fd = open(name, O_RDWR);
393 unlink(name);
394 }
395 if (fd == -1) {
396 exit(1);
Colin Crossf83d0b92010-04-21 12:04:20 -0700397 }
398 }
399
Nick Kraleviche34577c2015-04-25 16:24:53 -0700400 dup2(fd, 0);
401 dup2(fd, 1);
402 dup2(fd, 2);
403 if (fd > 2) {
404 close(fd);
405 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700406}
407
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700408void import_kernel_cmdline(bool in_qemu,
409 std::function<void(const std::string&, const std::string&, bool)> fn) {
410 std::string cmdline;
411 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700412
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700413 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
414 std::vector<std::string> pieces = android::base::Split(entry, "=");
415 if (pieces.size() == 2) {
416 fn(pieces[0], pieces[1], in_qemu);
417 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700418 }
419}
Stephen Smalleye096e362012-06-11 13:37:39 -0400420
421int make_dir(const char *path, mode_t mode)
422{
423 int rc;
424
Stephen Smalleye096e362012-06-11 13:37:39 -0400425 char *secontext = NULL;
426
427 if (sehandle) {
428 selabel_lookup(sehandle, &secontext, path, mode);
429 setfscreatecon(secontext);
430 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400431
432 rc = mkdir(path, mode);
433
Stephen Smalleye096e362012-06-11 13:37:39 -0400434 if (secontext) {
435 int save_errno = errno;
436 freecon(secontext);
437 setfscreatecon(NULL);
438 errno = save_errno;
439 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700440
Stephen Smalleye096e362012-06-11 13:37:39 -0400441 return rc;
442}
443
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500444int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400445{
Stephen Smalley27a93652014-02-07 09:14:13 -0500446 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700447}
448
449int restorecon_recursive(const char* pathname)
450{
Stephen Smalley27a93652014-02-07 09:14:13 -0500451 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700452}
Andres Moralescb3fce82015-05-08 08:30:33 -0700453
454/*
455 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
456 */
457std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
458 std::string hex("0x");
459 for (size_t i = 0; i < bytes_len; i++)
460 android::base::StringAppendF(&hex, "%02x", bytes[i]);
461 return hex;
462}
463