blob: fdbdc1cddd2e3c9f5cafc044a1d36c201a6628aa [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 Moralesdb5f5d42015-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
Nick Kralevichd2104df2015-06-18 17:46:54 -070050 * name, or UINT_MAX on error.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 */
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
Nick Kralevichd2104df2015-06-18 17:46:54 -070062 return UINT_MAX;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063}
64
Nick Kralevichd2104df2015-06-18 17:46:54 -070065static unsigned int do_decode_uid(const char *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066{
67 unsigned int v;
68
69 if (!s || *s == '\0')
Nick Kralevichd2104df2015-06-18 17:46:54 -070070 return UINT_MAX;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071 if (isalpha(s[0]))
72 return android_name_to_id(s);
73
74 errno = 0;
75 v = (unsigned int) strtoul(s, 0, 0);
76 if (errno)
Nick Kralevichd2104df2015-06-18 17:46:54 -070077 return UINT_MAX;
78 return v;
79}
80
81/*
82 * decode_uid - decodes and returns the given string, which can be either the
83 * numeric or name representation, into the integer uid or gid. Returns
84 * UINT_MAX on error.
85 */
86unsigned int decode_uid(const char *s) {
87 unsigned int v = do_decode_uid(s);
88 if (v == UINT_MAX) {
89 ERROR("decode_uid: Unable to find UID for '%s'. Returning UINT_MAX\n", s);
90 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 return v;
92}
93
94/*
95 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
96 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
97 * daemon. We communicate the file descriptor's value via the environment
98 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
99 */
Stephen Smalley8348d272013-05-13 12:37:04 -0400100int create_socket(const char *name, int type, mode_t perm, uid_t uid,
101 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102{
103 struct sockaddr_un addr;
104 int fd, ret;
Stephen Smalley8348d272013-05-13 12:37:04 -0400105 char *filecon;
106
107 if (socketcon)
108 setsockcreatecon(socketcon);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109
110 fd = socket(PF_UNIX, type, 0);
111 if (fd < 0) {
112 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
113 return -1;
114 }
115
Stephen Smalley8348d272013-05-13 12:37:04 -0400116 if (socketcon)
117 setsockcreatecon(NULL);
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 memset(&addr, 0 , sizeof(addr));
120 addr.sun_family = AF_UNIX;
121 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
122 name);
123
124 ret = unlink(addr.sun_path);
125 if (ret != 0 && errno != ENOENT) {
126 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
127 goto out_close;
128 }
129
Stephen Smalley8348d272013-05-13 12:37:04 -0400130 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500131 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400132 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500133 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400134 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500135 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
138 if (ret) {
139 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
140 goto out_unlink;
141 }
142
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500143 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400144 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 chown(addr.sun_path, uid, gid);
147 chmod(addr.sun_path, perm);
148
149 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
150 addr.sun_path, perm, uid, gid);
151
152 return fd;
153
154out_unlink:
155 unlink(addr.sun_path);
156out_close:
157 close(fd);
158 return -1;
159}
160
Elliott Hughesf682b472015-02-06 12:19:48 -0800161bool read_file(const char* path, std::string* content) {
162 content->clear();
163
164 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
165 if (fd == -1) {
166 return false;
167 }
168
169 // For security reasons, disallow world-writable
170 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800171 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800172 if (fstat(fd, &sb) == -1) {
173 ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
174 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800175 }
176 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800177 ERROR("skipping insecure file '%s'\n", path);
178 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800179 }
180
Dan Albertc007bc32015-03-16 10:08:46 -0700181 bool okay = android::base::ReadFdToString(fd, content);
Elliott Hughes9fc83432015-05-15 19:16:40 -0700182 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800183 return okay;
184}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughesf682b472015-02-06 12:19:48 -0800186int write_file(const char* path, const char* content) {
187 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
188 if (fd == -1) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700189 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
190 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800191 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700192 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
193 if (result == -1) {
194 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
195 }
Elliott Hughes9fc83432015-05-15 19:16:40 -0700196 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800197 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198}
199
Colin Crossf24ed8c2010-04-12 18:51:06 -0700200#define MAX_MTD_PARTITIONS 16
201
202static struct {
203 char name[16];
204 int number;
205} mtd_part_map[MAX_MTD_PARTITIONS];
206
207static int mtd_part_count = -1;
208
209static void find_mtd_partitions(void)
210{
211 int fd;
212 char buf[1024];
213 char *pmtdbufp;
214 ssize_t pmtdsize;
215 int r;
216
Nick Kralevich45a884f2015-02-02 14:37:22 -0800217 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700218 if (fd < 0)
219 return;
220
221 buf[sizeof(buf) - 1] = '\0';
222 pmtdsize = read(fd, buf, sizeof(buf) - 1);
223 pmtdbufp = buf;
224 while (pmtdsize > 0) {
225 int mtdnum, mtdsize, mtderasesize;
226 char mtdname[16];
227 mtdname[0] = '\0';
228 mtdnum = -1;
229 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
230 &mtdnum, &mtdsize, &mtderasesize, mtdname);
231 if ((r == 4) && (mtdname[0] == '"')) {
232 char *x = strchr(mtdname + 1, '"');
233 if (x) {
234 *x = 0;
235 }
236 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
237 if (mtd_part_count < MAX_MTD_PARTITIONS) {
238 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
239 mtd_part_map[mtd_part_count].number = mtdnum;
240 mtd_part_count++;
241 } else {
242 ERROR("too many mtd partitions\n");
243 }
244 }
245 while (pmtdsize > 0 && *pmtdbufp != '\n') {
246 pmtdbufp++;
247 pmtdsize--;
248 }
249 if (pmtdsize > 0) {
250 pmtdbufp++;
251 pmtdsize--;
252 }
253 }
254 close(fd);
255}
256
257int mtd_name_to_number(const char *name)
258{
259 int n;
260 if (mtd_part_count < 0) {
261 mtd_part_count = 0;
262 find_mtd_partitions();
263 }
264 for (n = 0; n < mtd_part_count; n++) {
265 if (!strcmp(name, mtd_part_map[n].name)) {
266 return mtd_part_map[n].number;
267 }
268 }
269 return -1;
270}
Colin Cross504bc512010-04-13 19:35:09 -0700271
Elliott Hughesda40c002015-03-27 23:20:44 -0700272time_t gettime() {
273 timespec now;
274 clock_gettime(CLOCK_MONOTONIC, &now);
275 return now.tv_sec;
276}
Colin Cross504bc512010-04-13 19:35:09 -0700277
Elliott Hughesda40c002015-03-27 23:20:44 -0700278uint64_t gettime_ns() {
279 timespec now;
280 clock_gettime(CLOCK_MONOTONIC, &now);
281 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700282}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700283
284int mkdir_recursive(const char *pathname, mode_t mode)
285{
286 char buf[128];
287 const char *slash;
288 const char *p = pathname;
289 int width;
290 int ret;
291 struct stat info;
292
293 while ((slash = strchr(p, '/')) != NULL) {
294 width = slash - pathname;
295 p = slash + 1;
296 if (width < 0)
297 break;
298 if (width == 0)
299 continue;
300 if ((unsigned int)width > sizeof(buf) - 1) {
301 ERROR("path too long for mkdir_recursive\n");
302 return -1;
303 }
304 memcpy(buf, pathname, width);
305 buf[width] = 0;
306 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400307 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700308 if (ret && errno != EEXIST)
309 return ret;
310 }
311 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400312 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700313 if (ret && errno != EEXIST)
314 return ret;
315 return 0;
316}
317
Johan Redestig93ca79b2012-04-18 16:41:19 +0200318/*
319 * replaces any unacceptable characters with '_', the
320 * length of the resulting string is equal to the input string
321 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700322void sanitize(char *s)
323{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200324 const char* accept =
325 "abcdefghijklmnopqrstuvwxyz"
326 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
327 "0123456789"
328 "_-.";
329
Colin Crossb0ab94b2010-04-08 16:16:20 -0700330 if (!s)
331 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200332
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400333 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200334 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400335 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200336 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700337}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200338
Chris Fries79f33842013-09-05 13:19:21 -0500339void make_link_init(const char *oldpath, const char *newpath)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700340{
341 int ret;
342 char buf[256];
343 char *slash;
344 int width;
345
346 slash = strrchr(newpath, '/');
347 if (!slash)
348 return;
349 width = slash - newpath;
350 if (width <= 0 || width > (int)sizeof(buf) - 1)
351 return;
352 memcpy(buf, newpath, width);
353 buf[width] = 0;
354 ret = mkdir_recursive(buf, 0755);
355 if (ret)
356 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
357
358 ret = symlink(oldpath, newpath);
359 if (ret && errno != EEXIST)
360 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
361}
362
363void remove_link(const char *oldpath, const char *newpath)
364{
365 char path[256];
366 ssize_t ret;
367 ret = readlink(newpath, path, sizeof(path) - 1);
368 if (ret <= 0)
369 return;
370 path[ret] = 0;
371 if (!strcmp(path, oldpath))
372 unlink(newpath);
373}
Colin Crosscd0f1732010-04-19 17:10:24 -0700374
375int wait_for_file(const char *filename, int timeout)
376{
377 struct stat info;
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700378 uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000);
Colin Crosscd0f1732010-04-19 17:10:24 -0700379 int ret = -1;
380
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700381 while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0))
Colin Crosscd0f1732010-04-19 17:10:24 -0700382 usleep(10000);
383
384 return ret;
385}
Colin Crossf83d0b92010-04-21 12:04:20 -0700386
387void open_devnull_stdio(void)
388{
Nick Kraleviche34577c2015-04-25 16:24:53 -0700389 // Try to avoid the mknod() call if we can. Since SELinux makes
390 // a /dev/null replacement available for free, let's use it.
391 int fd = open("/sys/fs/selinux/null", O_RDWR);
392 if (fd == -1) {
393 // OOPS, /sys/fs/selinux/null isn't available, likely because
394 // /sys/fs/selinux isn't mounted. Fall back to mknod.
395 static const char *name = "/dev/__null__";
396 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
397 fd = open(name, O_RDWR);
398 unlink(name);
399 }
400 if (fd == -1) {
401 exit(1);
Colin Crossf83d0b92010-04-21 12:04:20 -0700402 }
403 }
404
Nick Kraleviche34577c2015-04-25 16:24:53 -0700405 dup2(fd, 0);
406 dup2(fd, 1);
407 dup2(fd, 2);
408 if (fd > 2) {
409 close(fd);
410 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700411}
412
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700413void import_kernel_cmdline(bool in_qemu,
414 std::function<void(const std::string&, const std::string&, bool)> fn) {
415 std::string cmdline;
416 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700417
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700418 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
419 std::vector<std::string> pieces = android::base::Split(entry, "=");
420 if (pieces.size() == 2) {
421 fn(pieces[0], pieces[1], in_qemu);
422 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700423 }
424}
Stephen Smalleye096e362012-06-11 13:37:39 -0400425
426int make_dir(const char *path, mode_t mode)
427{
428 int rc;
429
Stephen Smalleye096e362012-06-11 13:37:39 -0400430 char *secontext = NULL;
431
432 if (sehandle) {
433 selabel_lookup(sehandle, &secontext, path, mode);
434 setfscreatecon(secontext);
435 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400436
437 rc = mkdir(path, mode);
438
Stephen Smalleye096e362012-06-11 13:37:39 -0400439 if (secontext) {
440 int save_errno = errno;
441 freecon(secontext);
442 setfscreatecon(NULL);
443 errno = save_errno;
444 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700445
Stephen Smalleye096e362012-06-11 13:37:39 -0400446 return rc;
447}
448
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500449int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400450{
Stephen Smalley27a93652014-02-07 09:14:13 -0500451 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700452}
453
454int restorecon_recursive(const char* pathname)
455{
Stephen Smalley27a93652014-02-07 09:14:13 -0500456 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700457}
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700458
459/*
460 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
461 */
462std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
463 std::string hex("0x");
464 for (size_t i = 0; i < bytes_len; i++)
465 android::base::StringAppendF(&hex, "%02x", bytes[i]);
466 return hex;
467}
Lee Campbellf13b1b32015-07-24 16:57:14 -0700468
469/*
470 * Returns true is pathname is a directory
471 */
472bool is_dir(const char* pathname) {
473 struct stat info;
474 if (stat(pathname, &info) == -1) {
475 return false;
476 }
477 return S_ISDIR(info.st_mode);
478}