blob: 8216892baf284cf5821821dc674c4008a93a607b [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 Hughes47b01342015-05-15 19:16:40 -0700174 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800175 return okay;
176}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughesf682b472015-02-06 12:19:48 -0800178int write_file(const char* path, const char* content) {
179 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
180 if (fd == -1) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700181 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
182 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800183 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700184 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
185 if (result == -1) {
186 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
187 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700188 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800189 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190}
191
Colin Crossf24ed8c2010-04-12 18:51:06 -0700192#define MAX_MTD_PARTITIONS 16
193
194static struct {
195 char name[16];
196 int number;
197} mtd_part_map[MAX_MTD_PARTITIONS];
198
199static int mtd_part_count = -1;
200
201static void find_mtd_partitions(void)
202{
203 int fd;
204 char buf[1024];
205 char *pmtdbufp;
206 ssize_t pmtdsize;
207 int r;
208
Nick Kralevich45a884f2015-02-02 14:37:22 -0800209 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700210 if (fd < 0)
211 return;
212
213 buf[sizeof(buf) - 1] = '\0';
214 pmtdsize = read(fd, buf, sizeof(buf) - 1);
215 pmtdbufp = buf;
216 while (pmtdsize > 0) {
217 int mtdnum, mtdsize, mtderasesize;
218 char mtdname[16];
219 mtdname[0] = '\0';
220 mtdnum = -1;
221 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
222 &mtdnum, &mtdsize, &mtderasesize, mtdname);
223 if ((r == 4) && (mtdname[0] == '"')) {
224 char *x = strchr(mtdname + 1, '"');
225 if (x) {
226 *x = 0;
227 }
228 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
229 if (mtd_part_count < MAX_MTD_PARTITIONS) {
230 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
231 mtd_part_map[mtd_part_count].number = mtdnum;
232 mtd_part_count++;
233 } else {
234 ERROR("too many mtd partitions\n");
235 }
236 }
237 while (pmtdsize > 0 && *pmtdbufp != '\n') {
238 pmtdbufp++;
239 pmtdsize--;
240 }
241 if (pmtdsize > 0) {
242 pmtdbufp++;
243 pmtdsize--;
244 }
245 }
246 close(fd);
247}
248
249int mtd_name_to_number(const char *name)
250{
251 int n;
252 if (mtd_part_count < 0) {
253 mtd_part_count = 0;
254 find_mtd_partitions();
255 }
256 for (n = 0; n < mtd_part_count; n++) {
257 if (!strcmp(name, mtd_part_map[n].name)) {
258 return mtd_part_map[n].number;
259 }
260 }
261 return -1;
262}
Colin Cross504bc512010-04-13 19:35:09 -0700263
Elliott Hughesda40c002015-03-27 23:20:44 -0700264time_t gettime() {
265 timespec now;
266 clock_gettime(CLOCK_MONOTONIC, &now);
267 return now.tv_sec;
268}
Colin Cross504bc512010-04-13 19:35:09 -0700269
Elliott Hughesda40c002015-03-27 23:20:44 -0700270uint64_t gettime_ns() {
271 timespec now;
272 clock_gettime(CLOCK_MONOTONIC, &now);
273 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700274}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700275
276int mkdir_recursive(const char *pathname, mode_t mode)
277{
278 char buf[128];
279 const char *slash;
280 const char *p = pathname;
281 int width;
282 int ret;
283 struct stat info;
284
285 while ((slash = strchr(p, '/')) != NULL) {
286 width = slash - pathname;
287 p = slash + 1;
288 if (width < 0)
289 break;
290 if (width == 0)
291 continue;
292 if ((unsigned int)width > sizeof(buf) - 1) {
293 ERROR("path too long for mkdir_recursive\n");
294 return -1;
295 }
296 memcpy(buf, pathname, width);
297 buf[width] = 0;
298 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400299 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700300 if (ret && errno != EEXIST)
301 return ret;
302 }
303 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400304 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700305 if (ret && errno != EEXIST)
306 return ret;
307 return 0;
308}
309
Johan Redestig93ca79b2012-04-18 16:41:19 +0200310/*
311 * replaces any unacceptable characters with '_', the
312 * length of the resulting string is equal to the input string
313 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700314void sanitize(char *s)
315{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200316 const char* accept =
317 "abcdefghijklmnopqrstuvwxyz"
318 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
319 "0123456789"
320 "_-.";
321
Colin Crossb0ab94b2010-04-08 16:16:20 -0700322 if (!s)
323 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200324
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400325 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200326 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400327 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200328 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700329}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200330
Colin Crossb0ab94b2010-04-08 16:16:20 -0700331void make_link(const char *oldpath, const char *newpath)
332{
333 int ret;
334 char buf[256];
335 char *slash;
336 int width;
337
338 slash = strrchr(newpath, '/');
339 if (!slash)
340 return;
341 width = slash - newpath;
342 if (width <= 0 || width > (int)sizeof(buf) - 1)
343 return;
344 memcpy(buf, newpath, width);
345 buf[width] = 0;
346 ret = mkdir_recursive(buf, 0755);
347 if (ret)
348 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
349
350 ret = symlink(oldpath, newpath);
351 if (ret && errno != EEXIST)
352 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
353}
354
355void remove_link(const char *oldpath, const char *newpath)
356{
357 char path[256];
358 ssize_t ret;
359 ret = readlink(newpath, path, sizeof(path) - 1);
360 if (ret <= 0)
361 return;
362 path[ret] = 0;
363 if (!strcmp(path, oldpath))
364 unlink(newpath);
365}
Colin Crosscd0f1732010-04-19 17:10:24 -0700366
367int wait_for_file(const char *filename, int timeout)
368{
369 struct stat info;
370 time_t timeout_time = gettime() + timeout;
371 int ret = -1;
372
373 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
374 usleep(10000);
375
376 return ret;
377}
Colin Crossf83d0b92010-04-21 12:04:20 -0700378
379void open_devnull_stdio(void)
380{
Nick Kraleviche34577c2015-04-25 16:24:53 -0700381 // Try to avoid the mknod() call if we can. Since SELinux makes
382 // a /dev/null replacement available for free, let's use it.
383 int fd = open("/sys/fs/selinux/null", O_RDWR);
384 if (fd == -1) {
385 // OOPS, /sys/fs/selinux/null isn't available, likely because
386 // /sys/fs/selinux isn't mounted. Fall back to mknod.
387 static const char *name = "/dev/__null__";
388 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
389 fd = open(name, O_RDWR);
390 unlink(name);
391 }
392 if (fd == -1) {
393 exit(1);
Colin Crossf83d0b92010-04-21 12:04:20 -0700394 }
395 }
396
Nick Kraleviche34577c2015-04-25 16:24:53 -0700397 dup2(fd, 0);
398 dup2(fd, 1);
399 dup2(fd, 2);
400 if (fd > 2) {
401 close(fd);
402 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700403}
404
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700405void import_kernel_cmdline(bool in_qemu,
406 std::function<void(const std::string&, const std::string&, bool)> fn) {
407 std::string cmdline;
408 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700409
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700410 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
411 std::vector<std::string> pieces = android::base::Split(entry, "=");
412 if (pieces.size() == 2) {
413 fn(pieces[0], pieces[1], in_qemu);
414 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700415 }
416}
Stephen Smalleye096e362012-06-11 13:37:39 -0400417
418int make_dir(const char *path, mode_t mode)
419{
420 int rc;
421
Stephen Smalleye096e362012-06-11 13:37:39 -0400422 char *secontext = NULL;
423
424 if (sehandle) {
425 selabel_lookup(sehandle, &secontext, path, mode);
426 setfscreatecon(secontext);
427 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400428
429 rc = mkdir(path, mode);
430
Stephen Smalleye096e362012-06-11 13:37:39 -0400431 if (secontext) {
432 int save_errno = errno;
433 freecon(secontext);
434 setfscreatecon(NULL);
435 errno = save_errno;
436 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700437
Stephen Smalleye096e362012-06-11 13:37:39 -0400438 return rc;
439}
440
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500441int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400442{
Stephen Smalley27a93652014-02-07 09:14:13 -0500443 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700444}
445
446int restorecon_recursive(const char* pathname)
447{
Stephen Smalley27a93652014-02-07 09:14:13 -0500448 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700449}
Andres Moralescb3fce82015-05-08 08:30:33 -0700450
451/*
452 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
453 */
454std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
455 std::string hex("0x");
456 for (size_t i = 0; i < bytes_len; i++)
457 android::base::StringAppendF(&hex, "%02x", bytes[i]);
458 return hex;
459}