installd.cpp revision 9a034c2ada5bd513619b72729071a10528f158ad
1/*
2** Copyright 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 <fcntl.h>
18#include <selinux/android.h>
19#include <selinux/avc.h>
20#include <sys/capability.h>
21#include <sys/fsuid.h>
22#include <sys/prctl.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25
26#include <android-base/logging.h>
27#include <cutils/fs.h>
28#include <cutils/log.h>               // TODO: Move everything to base::logging.
29#include <cutils/properties.h>
30#include <cutils/sockets.h>
31#include <private/android_filesystem_config.h>
32
33#include <commands.h>
34#include <globals.h>
35#include <installd_constants.h>
36#include <installd_deps.h>  // Need to fill in requirements of commands.
37#include <utils.h>
38
39#ifndef LOG_TAG
40#define LOG_TAG "installd"
41#endif
42#define SOCKET_PATH "installd"
43
44#define BUFFER_MAX    1024  /* input buffer for commands */
45#define TOKEN_MAX     16    /* max number of arguments in buffer */
46#define REPLY_MAX     256   /* largest reply allowed */
47
48namespace android {
49namespace installd {
50
51// Check that installd-deps sizes match cutils sizes.
52static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
53static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
54
55////////////////////////
56// Plug-in functions. //
57////////////////////////
58
59int get_property(const char *key, char *value, const char *default_value) {
60    return property_get(key, value, default_value);
61}
62
63// Compute the output path of
64bool calculate_oat_file_path(char path[PKG_PATH_MAX],
65                             const char *oat_dir,
66                             const char *apk_path,
67                             const char *instruction_set) {
68    char *file_name_start;
69    char *file_name_end;
70
71    file_name_start = strrchr(apk_path, '/');
72    if (file_name_start == NULL) {
73        ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
74        return false;
75    }
76    file_name_end = strrchr(apk_path, '.');
77    if (file_name_end < file_name_start) {
78        ALOGE("apk_path '%s' has no extension\n", apk_path);
79        return false;
80    }
81
82    // Calculate file_name
83    int file_name_len = file_name_end - file_name_start - 1;
84    char file_name[file_name_len + 1];
85    memcpy(file_name, file_name_start + 1, file_name_len);
86    file_name[file_name_len] = '\0';
87
88    // <apk_parent_dir>/oat/<isa>/<file_name>.odex
89    snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
90    return true;
91}
92
93/*
94 * Computes the odex file for the given apk_path and instruction_set.
95 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
96 *
97 * Returns false if it failed to determine the odex file path.
98 */
99bool calculate_odex_file_path(char path[PKG_PATH_MAX],
100                              const char *apk_path,
101                              const char *instruction_set) {
102    if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
103            + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
104        ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
105        return false;
106    }
107
108    strcpy(path, apk_path);
109    char *end = strrchr(path, '/');
110    if (end == NULL) {
111        ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
112        return false;
113    }
114    const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
115
116    strcpy(end + 1, "oat/");       // path = /system/framework/oat/\0
117    strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
118    strcat(path, apk_end);         // path = /system/framework/oat/<isa>/whatever.jar\0
119    end = strrchr(path, '.');
120    if (end == NULL) {
121        ALOGE("apk_path '%s' has no extension.\n", apk_path);
122        return false;
123    }
124    strcpy(end + 1, "odex");
125    return true;
126}
127
128bool create_cache_path(char path[PKG_PATH_MAX],
129                       const char *src,
130                       const char *instruction_set) {
131    size_t srclen = strlen(src);
132
133        /* demand that we are an absolute path */
134    if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
135        return false;
136    }
137
138    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
139        return false;
140    }
141
142    size_t dstlen =
143        android_data_dir.len +
144        strlen(DALVIK_CACHE) +
145        1 +
146        strlen(instruction_set) +
147        srclen +
148        strlen(DALVIK_CACHE_POSTFIX) + 2;
149
150    if (dstlen > PKG_PATH_MAX) {
151        return false;
152    }
153
154    sprintf(path,"%s%s/%s/%s%s",
155            android_data_dir.path,
156            DALVIK_CACHE,
157            instruction_set,
158            src + 1, /* skip the leading / */
159            DALVIK_CACHE_POSTFIX);
160
161    char* tmp =
162            path +
163            android_data_dir.len +
164            strlen(DALVIK_CACHE) +
165            1 +
166            strlen(instruction_set) + 1;
167
168    for(; *tmp; tmp++) {
169        if (*tmp == '/') {
170            *tmp = '@';
171        }
172    }
173
174    return true;
175}
176
177
178static char* parse_null(char* arg) {
179    if (strcmp(arg, "!") == 0) {
180        return nullptr;
181    } else {
182        return arg;
183    }
184}
185
186static int do_ping(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
187{
188    return 0;
189}
190
191static int do_create_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
192    /* const char *uuid, const char *pkgname, userid_t userid, int flags,
193            appid_t appid, const char* seinfo, int target_sdk_version */
194    return create_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]),
195                           atoi(arg[4]), arg[5], atoi(arg[6]));
196}
197
198static int do_restorecon_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
199    /* const char* uuid, const char* pkgName, userid_t userid, int flags,
200            appid_t appid, const char* seinfo */
201    return restorecon_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]);
202}
203
204static int do_clear_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
205    /* const char *uuid, const char *pkgname, userid_t userid, int flags */
206    return clear_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
207}
208
209static int do_destroy_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
210    /* const char *uuid, const char *pkgname, userid_t userid, int flags */
211    return destroy_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
212}
213
214static int do_dexopt(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
215{
216    /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags, volume_uuid,
217            use_profiles */
218    return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]),
219                  arg[5], atoi(arg[6]), parse_null(arg[7]), (atoi(arg[8]) == 0 ? false : true));
220}
221
222static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
223{
224    return mark_boot_complete(arg[0] /* instruction set */);
225}
226
227static int do_rm_dex(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
228{
229    return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
230}
231
232static int do_free_cache(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) /* TODO int:free_size */
233{
234    return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
235}
236
237static int do_get_app_size(char **arg, char reply[REPLY_MAX]) {
238    int64_t codesize = 0;
239    int64_t datasize = 0;
240    int64_t cachesize = 0;
241    int64_t asecsize = 0;
242    int res = 0;
243
244    /* const char *uuid, const char *pkgname, userid_t userid, int flags,
245            const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath,
246            const char *asecpath, const char *instruction_set */
247    res = get_app_size(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4], arg[5],
248            arg[6], arg[7], arg[8], &codesize, &datasize, &cachesize, &asecsize);
249
250    /*
251     * Each int64_t can take up 22 characters printed out. Make sure it
252     * doesn't go over REPLY_MAX in the future.
253     */
254    snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
255            codesize, datasize, cachesize, asecsize);
256    return res;
257}
258
259static int do_move_complete_app(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
260    /* const char* from_uuid, const char *to_uuid, const char *package_name,
261            const char *data_app_name, appid_t appid, const char* seinfo,
262            int target_sdk_version */
263    return move_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3],
264                             atoi(arg[4]), arg[5], atoi(arg[6]));
265}
266
267static int do_mk_user_config(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
268{
269    return make_user_config(atoi(arg[0])); /* userid */
270}
271
272static int do_rm_user(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
273{
274    return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
275}
276
277static int do_movefiles(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
278{
279    return movefiles();
280}
281
282static int do_linklib(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
283{
284    return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
285}
286
287static int do_idmap(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
288{
289    return idmap(arg[0], arg[1], atoi(arg[2]));
290}
291
292static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
293{
294    /* oat_dir, instruction_set */
295    return create_oat_dir(arg[0], arg[1]);
296}
297
298static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
299{
300    /* oat_dir */
301    return rm_package_dir(arg[0]);
302}
303
304static int do_link_file(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
305{
306    /* relative_path, from_base, to_base */
307    return link_file(arg[0], arg[1], arg[2]);
308}
309
310struct cmdinfo {
311    const char *name;
312    unsigned numargs;
313    int (*func)(char **arg, char reply[REPLY_MAX]);
314};
315
316struct cmdinfo cmds[] = {
317    { "ping",                 0, do_ping },
318
319    { "create_app_data",      7, do_create_app_data },
320    { "restorecon_app_data",  6, do_restorecon_app_data },
321    { "clear_app_data",       4, do_clear_app_data },
322    { "destroy_app_data",     4, do_destroy_app_data },
323    { "move_complete_app",    7, do_move_complete_app },
324    { "get_app_size",         9, do_get_app_size },
325
326    { "dexopt",               9, do_dexopt },
327    { "markbootcomplete",     1, do_mark_boot_complete },
328    { "rmdex",                2, do_rm_dex },
329    { "freecache",            2, do_free_cache },
330    { "movefiles",            0, do_movefiles },
331    { "linklib",              4, do_linklib },
332    { "mkuserconfig",         1, do_mk_user_config },
333    { "rmuser",               2, do_rm_user },
334    { "idmap",                3, do_idmap },
335    { "createoatdir",         2, do_create_oat_dir },
336    { "rmpackagedir",         1, do_rm_package_dir },
337    { "linkfile",             3, do_link_file },
338};
339
340static int readx(int s, void *_buf, int count)
341{
342    char *buf = (char *) _buf;
343    int n = 0, r;
344    if (count < 0) return -1;
345    while (n < count) {
346        r = read(s, buf + n, count - n);
347        if (r < 0) {
348            if (errno == EINTR) continue;
349            ALOGE("read error: %s\n", strerror(errno));
350            return -1;
351        }
352        if (r == 0) {
353            ALOGE("eof\n");
354            return -1; /* EOF */
355        }
356        n += r;
357    }
358    return 0;
359}
360
361static int writex(int s, const void *_buf, int count)
362{
363    const char *buf = (const char *) _buf;
364    int n = 0, r;
365    if (count < 0) return -1;
366    while (n < count) {
367        r = write(s, buf + n, count - n);
368        if (r < 0) {
369            if (errno == EINTR) continue;
370            ALOGE("write error: %s\n", strerror(errno));
371            return -1;
372        }
373        n += r;
374    }
375    return 0;
376}
377
378
379/* Tokenize the command buffer, locate a matching command,
380 * ensure that the required number of arguments are provided,
381 * call the function(), return the result.
382 */
383static int execute(int s, char cmd[BUFFER_MAX])
384{
385    char reply[REPLY_MAX];
386    char *arg[TOKEN_MAX+1];
387    unsigned i;
388    unsigned n = 0;
389    unsigned short count;
390    int ret = -1;
391
392    // ALOGI("execute('%s')\n", cmd);
393
394        /* default reply is "" */
395    reply[0] = 0;
396
397        /* n is number of args (not counting arg[0]) */
398    arg[0] = cmd;
399    while (*cmd) {
400        if (isspace(*cmd)) {
401            *cmd++ = 0;
402            n++;
403            arg[n] = cmd;
404            if (n == TOKEN_MAX) {
405                ALOGE("too many arguments\n");
406                goto done;
407            }
408        }
409        if (*cmd) {
410          cmd++;
411        }
412    }
413
414    for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
415        if (!strcmp(cmds[i].name,arg[0])) {
416            if (n != cmds[i].numargs) {
417                ALOGE("%s requires %d arguments (%d given)\n",
418                     cmds[i].name, cmds[i].numargs, n);
419            } else {
420                ret = cmds[i].func(arg + 1, reply);
421            }
422            goto done;
423        }
424    }
425    ALOGE("unsupported command '%s'\n", arg[0]);
426
427done:
428    if (reply[0]) {
429        n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
430    } else {
431        n = snprintf(cmd, BUFFER_MAX, "%d", ret);
432    }
433    if (n > BUFFER_MAX) n = BUFFER_MAX;
434    count = n;
435
436    // ALOGI("reply: '%s'\n", cmd);
437    if (writex(s, &count, sizeof(count))) return -1;
438    if (writex(s, cmd, count)) return -1;
439    return 0;
440}
441
442bool initialize_globals() {
443    const char* data_path = getenv("ANDROID_DATA");
444    if (data_path == nullptr) {
445        ALOGE("Could not find ANDROID_DATA");
446        return false;
447    }
448    const char* root_path = getenv("ANDROID_ROOT");
449    if (root_path == nullptr) {
450        ALOGE("Could not find ANDROID_ROOT");
451        return false;
452    }
453
454    return init_globals_from_data_and_root(data_path, root_path);
455}
456
457static int initialize_directories() {
458    int res = -1;
459
460    // Read current filesystem layout version to handle upgrade paths
461    char version_path[PATH_MAX];
462    snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
463
464    int oldVersion;
465    if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
466        oldVersion = 0;
467    }
468    int version = oldVersion;
469
470    if (version < 2) {
471        SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
472        version = 2;
473    }
474
475    if (ensure_config_user_dirs(0) == -1) {
476        ALOGE("Failed to setup misc for user 0");
477        goto fail;
478    }
479
480    if (version == 2) {
481        ALOGD("Upgrading to /data/misc/user directories");
482
483        char misc_dir[PATH_MAX];
484        snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
485
486        char keychain_added_dir[PATH_MAX];
487        snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
488
489        char keychain_removed_dir[PATH_MAX];
490        snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
491
492        DIR *dir;
493        struct dirent *dirent;
494        dir = opendir("/data/user");
495        if (dir != NULL) {
496            while ((dirent = readdir(dir))) {
497                const char *name = dirent->d_name;
498
499                // skip "." and ".."
500                if (name[0] == '.') {
501                    if (name[1] == 0) continue;
502                    if ((name[1] == '.') && (name[2] == 0)) continue;
503                }
504
505                uint32_t user_id = atoi(name);
506
507                // /data/misc/user/<user_id>
508                if (ensure_config_user_dirs(user_id) == -1) {
509                    goto fail;
510                }
511
512                char misc_added_dir[PATH_MAX];
513                snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
514
515                char misc_removed_dir[PATH_MAX];
516                snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
517
518                uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
519                gid_t gid = uid;
520                if (access(keychain_added_dir, F_OK) == 0) {
521                    if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
522                        ALOGE("Some files failed to copy");
523                    }
524                }
525                if (access(keychain_removed_dir, F_OK) == 0) {
526                    if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
527                        ALOGE("Some files failed to copy");
528                    }
529                }
530            }
531            closedir(dir);
532
533            if (access(keychain_added_dir, F_OK) == 0) {
534                delete_dir_contents(keychain_added_dir, 1, 0);
535            }
536            if (access(keychain_removed_dir, F_OK) == 0) {
537                delete_dir_contents(keychain_removed_dir, 1, 0);
538            }
539        }
540
541        version = 3;
542    }
543
544    // Persist layout version if changed
545    if (version != oldVersion) {
546        if (fs_write_atomic_int(version_path, version) == -1) {
547            ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
548            goto fail;
549        }
550    }
551
552    // Success!
553    res = 0;
554
555fail:
556    return res;
557}
558
559static int log_callback(int type, const char *fmt, ...) {
560    va_list ap;
561    int priority;
562
563    switch (type) {
564    case SELINUX_WARNING:
565        priority = ANDROID_LOG_WARN;
566        break;
567    case SELINUX_INFO:
568        priority = ANDROID_LOG_INFO;
569        break;
570    default:
571        priority = ANDROID_LOG_ERROR;
572        break;
573    }
574    va_start(ap, fmt);
575    LOG_PRI_VA(priority, "SELinux", fmt, ap);
576    va_end(ap);
577    return 0;
578}
579
580static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
581    char buf[BUFFER_MAX];
582    struct sockaddr addr;
583    socklen_t alen;
584    int lsocket, s;
585    int selinux_enabled = (is_selinux_enabled() > 0);
586
587    setenv("ANDROID_LOG_TAGS", "*:v", 1);
588    android::base::InitLogging(argv);
589
590    ALOGI("installd firing up\n");
591
592    union selinux_callback cb;
593    cb.func_log = log_callback;
594    selinux_set_callback(SELINUX_CB_LOG, cb);
595
596    if (!initialize_globals()) {
597        ALOGE("Could not initialize globals; exiting.\n");
598        exit(1);
599    }
600
601    if (initialize_directories() < 0) {
602        ALOGE("Could not create directories; exiting.\n");
603        exit(1);
604    }
605
606    if (selinux_enabled && selinux_status_open(true) < 0) {
607        ALOGE("Could not open selinux status; exiting.\n");
608        exit(1);
609    }
610
611    lsocket = android_get_control_socket(SOCKET_PATH);
612    if (lsocket < 0) {
613        ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
614        exit(1);
615    }
616    if (listen(lsocket, 5)) {
617        ALOGE("Listen on socket failed: %s\n", strerror(errno));
618        exit(1);
619    }
620    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
621
622    for (;;) {
623        alen = sizeof(addr);
624        s = accept(lsocket, &addr, &alen);
625        if (s < 0) {
626            ALOGE("Accept failed: %s\n", strerror(errno));
627            continue;
628        }
629        fcntl(s, F_SETFD, FD_CLOEXEC);
630
631        ALOGI("new connection\n");
632        for (;;) {
633            unsigned short count;
634            if (readx(s, &count, sizeof(count))) {
635                ALOGE("failed to read size\n");
636                break;
637            }
638            if ((count < 1) || (count >= BUFFER_MAX)) {
639                ALOGE("invalid size %d\n", count);
640                break;
641            }
642            if (readx(s, buf, count)) {
643                ALOGE("failed to read command\n");
644                break;
645            }
646            buf[count] = 0;
647            if (selinux_enabled && selinux_status_updated() > 0) {
648                selinux_android_seapp_context_reload();
649            }
650            if (execute(s, buf)) break;
651        }
652        ALOGI("closing connection\n");
653        close(s);
654    }
655
656    return 0;
657}
658
659}  // namespace installd
660}  // namespace android
661
662int main(const int argc, char *argv[]) {
663    return android::installd::installd_main(argc, argv);
664}
665