123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- #include <sys/vfs.h>
- #include <sys/mount.h>
- #if ENABLE_RUN_INIT
- # include <sys/prctl.h>
- # include <linux/capability.h>
- extern int capset(cap_user_header_t header, cap_user_data_t data);
- extern int capget(cap_user_header_t header, const cap_user_data_t data);
- #endif
- #include "libbb.h"
- #ifndef RAMFS_MAGIC
- # define RAMFS_MAGIC ((unsigned)0x858458f6)
- #endif
- #ifndef TMPFS_MAGIC
- # define TMPFS_MAGIC ((unsigned)0x01021994)
- #endif
- #ifndef MS_MOVE
- # define MS_MOVE 8192
- #endif
- static void delete_contents(const char *directory, dev_t rootdev)
- {
- DIR *dir;
- struct dirent *d;
- struct stat st;
-
- if (lstat(directory, &st) || st.st_dev != rootdev)
- return;
-
- if (S_ISDIR(st.st_mode)) {
- dir = opendir(directory);
- if (dir) {
- while ((d = readdir(dir))) {
- char *newdir = d->d_name;
-
- if (DOT_OR_DOTDOT(newdir))
- continue;
-
- newdir = concat_path_file(directory, newdir);
- delete_contents(newdir, rootdev);
- free(newdir);
- }
- closedir(dir);
-
- rmdir(directory);
- }
- } else {
-
- unlink(directory);
- }
- }
- #if ENABLE_RUN_INIT
- DEFINE_STRUCT_CAPS;
- static void drop_capset(int cap_idx)
- {
- struct caps caps;
- getcaps(&caps);
- caps.data[CAP_TO_INDEX(cap_idx)].inheritable &= ~CAP_TO_MASK(cap_idx);
- if (capset(&caps.header, caps.data) != 0)
- bb_perror_msg_and_die("capset");
- }
- static void drop_bounding_set(int cap_idx)
- {
- int ret;
- ret = prctl(PR_CAPBSET_READ, cap_idx, 0, 0, 0);
- if (ret < 0)
- bb_perror_msg_and_die("prctl: %s", "PR_CAPBSET_READ");
- if (ret == 1) {
- ret = prctl(PR_CAPBSET_DROP, cap_idx, 0, 0, 0);
- if (ret != 0)
- bb_perror_msg_and_die("prctl: %s", "PR_CAPBSET_DROP");
- }
- }
- static void drop_usermodehelper(const char *filename, int cap_idx)
- {
- unsigned lo, hi;
- char buf[sizeof(int)*3 * 2 + 8];
- int fd;
- int ret;
- ret = open_read_close(filename, buf, sizeof(buf) - 1);
- if (ret < 0)
- return;
- buf[ret] = '\0';
- ret = sscanf(buf, "%u %u", &lo, &hi);
- if (ret != 2)
- bb_perror_msg_and_die("can't parse file '%s'", filename);
- if (cap_idx < 32)
- lo &= ~(1 << cap_idx);
- else
- hi &= ~(1 << (cap_idx - 32));
- fd = xopen(filename, O_WRONLY);
- fdprintf(fd, "%u %u", lo, hi);
- close(fd);
- }
- static void drop_capabilities(char *string)
- {
- char *cap;
- cap = strtok(string, ",");
- while (cap) {
- unsigned cap_idx;
- cap_idx = cap_name_to_number(cap);
- drop_usermodehelper("/proc/sys/kernel/usermodehelper/bset", cap_idx);
- drop_usermodehelper("/proc/sys/kernel/usermodehelper/inheritable", cap_idx);
- drop_bounding_set(cap_idx);
- drop_capset(cap_idx);
- bb_error_msg("dropped capability: %s", cap);
- cap = strtok(NULL, ",");
- }
- }
- #endif
- int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int switch_root_main(int argc UNUSED_PARAM, char **argv)
- {
- char *newroot, *console = NULL;
- struct stat st;
- struct statfs stfs;
- unsigned dry_run = 0;
- dev_t rootdev;
-
- if (ENABLE_SWITCH_ROOT && (!ENABLE_RUN_INIT || applet_name[0] == 's')) {
- getopt32(argv, "^+"
- "c:"
- "\0" "-2" ,
- &console
- );
- } else {
- #if ENABLE_RUN_INIT
- char *cap_list = NULL;
- dry_run = getopt32(argv, "^+"
- "c:d:n"
- "\0" "-2" ,
- &console,
- &cap_list
- );
- dry_run >>= 2;
- if (cap_list)
- drop_capabilities(cap_list);
- #endif
- }
- argv += optind;
- newroot = *argv++;
-
- xchdir(newroot);
- xstat("/", &st);
- rootdev = st.st_dev;
- xstat(".", &st);
- if (st.st_dev == rootdev) {
-
- bb_show_usage();
- }
- if (!dry_run && getpid() != 1) {
-
- bb_show_usage();
- }
-
-
-
- if (stat("/init", &st) != 0 || !S_ISREG(st.st_mode)) {
- bb_error_msg_and_die("'%s' is not a regular file", "/init");
- }
- statfs("/", &stfs);
- if ((unsigned)stfs.f_type != RAMFS_MAGIC
- && (unsigned)stfs.f_type != TMPFS_MAGIC
- ) {
- bb_error_msg_and_die("root filesystem is not ramfs/tmpfs");
- }
- if (!dry_run) {
-
- delete_contents("/", rootdev);
-
- if (mount(".", "/", NULL, MS_MOVE, NULL)) {
-
- bb_perror_msg_and_die("error moving root");
- }
- }
- xchroot(".");
-
-
-
- if (console) {
- int fd = open_or_warn(console, O_RDWR);
- if (fd >= 0) {
- xmove_fd(fd, 0);
- xdup2(0, 1);
- xdup2(0, 2);
- }
- }
- if (dry_run) {
-
-
-
-
- if (access(argv[0], X_OK) == 0)
- return 0;
- } else {
-
- execv(argv[0], argv);
- }
- bb_perror_msg_and_die("can't execute '%s'", argv[0]);
- }
|