From 52bc052e1a28a8f143559ddeda67f4b30f39b654 Mon Sep 17 00:00:00 2001 From: Balazs Gerofi Date: Fri, 16 Jun 2017 17:54:48 +0900 Subject: [PATCH] mcexec: recursively bind mount $prefix/rootfs/ on / --- kernel/include/config.h.in => config.h.in | 5 +- configure | 12 ++- configure.ac | 4 +- executer/config.h.in | 97 ------------------- executer/kernel/mcctrl/arch/x86_64/archdeps.c | 2 +- executer/kernel/mcctrl/control.c | 2 +- executer/kernel/mcctrl/sysfs_files.c | 2 +- executer/user/mcexec.c | 82 +++++++++++++++- kernel/include/rusage.h | 2 +- 9 files changed, 96 insertions(+), 112 deletions(-) rename kernel/include/config.h.in => config.h.in (95%) delete mode 100644 executer/config.h.in diff --git a/kernel/include/config.h.in b/config.h.in similarity index 95% rename from kernel/include/config.h.in rename to config.h.in index cf52bbcf..6954ee91 100644 --- a/kernel/include/config.h.in +++ b/config.h.in @@ -1,4 +1,4 @@ -/* kernel/include/config.h.in. Generated from configure.ac by autoheader. */ +/* config.h.in. Generated from configure.ac by autoheader. */ /* whether mcoverlayfs is enabled */ #undef ENABLE_MCOVERLAYFS @@ -96,5 +96,8 @@ /* Define to the version of this package. */ #undef PACKAGE_VERSION +/* Path of bind-mount source directory */ +#undef ROOTFSDIR + /* Define to 1 if you have the ANSI C header files. */ #undef STDC_HEADERS diff --git a/configure b/configure index 68321ff0..114db8b8 100755 --- a/configure +++ b/configure @@ -3400,6 +3400,11 @@ fi test "x$prefix" = xNONE && prefix="$ac_default_prefix" +cat >>confdefs.h <<_ACEOF +#define ROOTFSDIR "$prefix/rootfs" +_ACEOF + + case $WITH_TARGET in attached-mic|builtin-x86|smp-x86) ARCH=`uname -m` @@ -4576,9 +4581,7 @@ fi -ac_config_headers="$ac_config_headers kernel/include/config.h" - -ac_config_headers="$ac_config_headers executer/config.h" +ac_config_headers="$ac_config_headers config.h" ac_config_files="$ac_config_files Makefile executer/user/Makefile executer/kernel/mcctrl/Makefile executer/kernel/mcctrl/arch/x86_64/Makefile executer/kernel/mcoverlayfs/Makefile executer/kernel/mcoverlayfs/linux-3.10.0-327.36.1.el7/Makefile executer/kernel/mcoverlayfs/linux-4.0.9/Makefile executer/kernel/mcoverlayfs/linux-4.6.7/Makefile kernel/Makefile kernel/Makefile.build arch/x86/tools/mcreboot-attached-mic.sh arch/x86/tools/mcshutdown-attached-mic.sh arch/x86/tools/mcreboot-builtin-x86.sh arch/x86/tools/mcreboot-smp-x86.sh arch/x86/tools/mcstop+release-smp-x86.sh arch/x86/tools/eclair-dump-backtrace.exp arch/x86/tools/mcshutdown-builtin-x86.sh arch/x86/tools/mcreboot.1:arch/x86/tools/mcreboot.1in arch/x86/tools/irqbalance_mck.service arch/x86/tools/irqbalance_mck.in" @@ -5278,8 +5281,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 for ac_config_target in $ac_config_targets do case $ac_config_target in - "kernel/include/config.h") CONFIG_HEADERS="$CONFIG_HEADERS kernel/include/config.h" ;; - "executer/config.h") CONFIG_HEADERS="$CONFIG_HEADERS executer/config.h" ;; + "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;; "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;; "executer/user/Makefile") CONFIG_FILES="$CONFIG_FILES executer/user/Makefile" ;; "executer/kernel/mcctrl/Makefile") CONFIG_FILES="$CONFIG_FILES executer/kernel/mcctrl/Makefile" ;; diff --git a/configure.ac b/configure.ac index ae1a1b7b..842a371d 100644 --- a/configure.ac +++ b/configure.ac @@ -88,6 +88,7 @@ if test "X$WITH_TARGET" = Xyes -o "X$WITH_TARGET" = Xno; then fi test "x$prefix" = xNONE && prefix="$ac_default_prefix" +AC_DEFINE_UNQUOTED(ROOTFSDIR,"$prefix/rootfs",[Path of bind-mount source directory]) case $WITH_TARGET in attached-mic|builtin-x86|smp-x86) @@ -351,8 +352,7 @@ AC_SUBST(MCKERNEL_RELEASE_DATE) AC_SUBST(DCFA_RESEASE_DATE) AC_SUBST(uncomment_if_ENABLE_MEMDUMP) -AC_CONFIG_HEADERS([kernel/include/config.h]) -AC_CONFIG_HEADERS([executer/config.h]) +AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([ Makefile executer/user/Makefile diff --git a/executer/config.h.in b/executer/config.h.in deleted file mode 100644 index 32db25b3..00000000 --- a/executer/config.h.in +++ /dev/null @@ -1,97 +0,0 @@ -/* executer/config.h.in. Generated from configure.ac by autoheader. */ - -/* whether mcoverlayfs is enabled */ -#undef ENABLE_MCOVERLAYFS - -/* whether memdump feature is enabled */ -#undef ENABLE_MEMDUMP - -/* Define to 1 if you have the header file. */ -#undef HAVE_INTTYPES_H - -/* Define to 1 if you have the `bfd' library (-lbfd). */ -#undef HAVE_LIBBFD - -/* Define to 1 if you have the header file. */ -#undef HAVE_MEMORY_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDINT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDLIB_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRINGS_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRING_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_STAT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_TYPES_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_UNISTD_H - -/* Define to address of kernel symbol __vvar_page, or 0 if exported */ -#undef MCCTRL_KSYM___vvar_page - -/* Define to address of kernel symbol hpet_address, or 0 if exported */ -#undef MCCTRL_KSYM_hpet_address - -/* Define to address of kernel symbol hv_clock, or 0 if exported */ -#undef MCCTRL_KSYM_hv_clock - -/* Define to address of kernel symbol sys_mount, or 0 if exported */ -#undef MCCTRL_KSYM_sys_mount - -/* Define to address of kernel symbol sys_readlink, or 0 if exported */ -#undef MCCTRL_KSYM_sys_readlink - -/* Define to address of kernel symbol sys_umount, or 0 if exported */ -#undef MCCTRL_KSYM_sys_umount - -/* Define to address of kernel symbol sys_unshare, or 0 if exported */ -#undef MCCTRL_KSYM_sys_unshare - -/* Define to address of kernel symbol vdso_end, or 0 if exported */ -#undef MCCTRL_KSYM_vdso_end - -/* Define to address of kernel symbol vdso_image_64, or 0 if exported */ -#undef MCCTRL_KSYM_vdso_image_64 - -/* Define to address of kernel symbol vdso_pages, or 0 if exported */ -#undef MCCTRL_KSYM_vdso_pages - -/* Define to address of kernel symbol vdso_start, or 0 if exported */ -#undef MCCTRL_KSYM_vdso_start - -/* Define to address of kernel symbol zap_page_range, or 0 if exported */ -#undef MCCTRL_KSYM_zap_page_range - -/* McKernel specific libraries */ -#undef MCKERNEL_LIBDIR - -/* Define to the address where bug reports for this package should be sent. */ -#undef PACKAGE_BUGREPORT - -/* Define to the full name of this package. */ -#undef PACKAGE_NAME - -/* Define to the full name and version of this package. */ -#undef PACKAGE_STRING - -/* Define to the one symbol short name of this package. */ -#undef PACKAGE_TARNAME - -/* Define to the home page for this package. */ -#undef PACKAGE_URL - -/* Define to the version of this package. */ -#undef PACKAGE_VERSION - -/* Define to 1 if you have the ANSI C header files. */ -#undef STDC_HEADERS diff --git a/executer/kernel/mcctrl/arch/x86_64/archdeps.c b/executer/kernel/mcctrl/arch/x86_64/archdeps.c index 0a96b397..81090411 100644 --- a/executer/kernel/mcctrl/arch/x86_64/archdeps.c +++ b/executer/kernel/mcctrl/arch/x86_64/archdeps.c @@ -1,5 +1,5 @@ #include -#include "../../config.h" +#include "../../../config.h" #include "../../mcctrl.h" #ifdef MCCTRL_KSYM_vdso_image_64 diff --git a/executer/kernel/mcctrl/control.c b/executer/kernel/mcctrl/control.c index 2ffdcb8c..1132f1f2 100644 --- a/executer/kernel/mcctrl/control.c +++ b/executer/kernel/mcctrl/control.c @@ -38,7 +38,7 @@ #include #include #include -#include "../../config.h" +#include "../../../config.h" #include "mcctrl.h" #include diff --git a/executer/kernel/mcctrl/sysfs_files.c b/executer/kernel/mcctrl/sysfs_files.c index f52ce799..aefd9651 100644 --- a/executer/kernel/mcctrl/sysfs_files.c +++ b/executer/kernel/mcctrl/sysfs_files.c @@ -14,7 +14,7 @@ #include #include #include -#include "../../config.h" +#include "../../../config.h" #include "mcctrl.h" #include "sysfs_msg.h" diff --git a/executer/user/mcexec.c b/executer/user/mcexec.c index 36f2818c..3867a0d3 100644 --- a/executer/user/mcexec.c +++ b/executer/user/mcexec.c @@ -65,7 +65,7 @@ #include #include "../include/uprotocol.h" #include -#include "../config.h" +#include "../../config.h" #include #include @@ -1379,6 +1379,65 @@ static struct option mcexec_options[] = { #define MCEXEC_DEF_CUR_STACK_SIZE (2 * 1024 * 1024) /* 2 MiB */ #define MCEXEC_DEF_MAX_STACK_SIZE (64 * 1024 * 1024) /* 64 MiB */ +#ifdef ENABLE_MCOVERLAYFS +void bind_mount_recursive(const char *root, char *prefix) +{ + DIR *dir; + struct dirent *entry; + char path[PATH_MAX]; + int len; + + len = snprintf(path, sizeof(path) - 1, "%s/%s", root, prefix); + path[len] = 0; + + if (!(dir = opendir(path))) { + return; + } + + if (!(entry = readdir(dir))) { + return; + } + + do { + len = snprintf(path, sizeof(path) - 1, + "%s/%s", prefix, entry->d_name); + path[len] = 0; + + if (entry->d_type == DT_DIR) { + if (strcmp(entry->d_name, ".") == 0 || + strcmp(entry->d_name, "..") == 0) + continue; + + bind_mount_recursive(root, path); + } + else if (entry->d_type == DT_REG) { + int ret; + struct sys_mount_desc mount_desc; + memset(&mount_desc, '\0', sizeof mount_desc); + char bind_path[PATH_MAX]; + + len = snprintf(bind_path, sizeof(bind_path) - 1, + "%s/%s/%s", root, prefix, entry->d_name); + bind_path[len] = 0; + + mount_desc.dev_name = bind_path; + mount_desc.dir_name = path; + mount_desc.type = NULL; + mount_desc.flags = MS_BIND | MS_PRIVATE; + mount_desc.data = NULL; + if ((ret = ioctl(fd, MCEXEC_UP_SYS_MOUNT, + (unsigned long)&mount_desc)) != 0) { + fprintf(stderr, "WARNING: failed to bind mount %s over %s: %d\n", + bind_path, path, ret); + } + } + } + while ((entry = readdir(dir)) != NULL); + + closedir(dir); +} +#endif + int main(int argc, char **argv) { // int fd; @@ -1521,12 +1580,26 @@ int main(int argc, char **argv) struct sys_mount_desc mount_desc; struct sys_umount_desc umount_desc; + /* Unshare mount namespace */ memset(&unshare_desc, '\0', sizeof unshare_desc); memset(&mount_desc, '\0', sizeof mount_desc); unshare_desc.unshare_flags = CLONE_NEWNS; - if (ioctl(fd, MCEXEC_UP_SYS_UNSHARE, + if (ioctl(fd, MCEXEC_UP_SYS_UNSHARE, (unsigned long)&unshare_desc) != 0) { - fprintf(stderr, "Error: Failed to unshare. (%s)\n", + fprintf(stderr, "Error: Failed to unshare. (%s)\n", + strerror(errno)); + return 1; + } + + /* Privatize mount namespace */ + mount_desc.dev_name = NULL; + mount_desc.dir_name = "/"; + mount_desc.type = NULL; + mount_desc.flags = MS_PRIVATE | MS_REC; + mount_desc.data = NULL; + if (ioctl(fd, MCEXEC_UP_SYS_MOUNT, + (unsigned long)&mount_desc) != 0) { + fprintf(stderr, "Error: Failed to privatize mounts. (%s)\n", strerror(errno)); return 1; } @@ -1603,6 +1676,9 @@ int main(int argc, char **argv) strerror(errno)); return 1; } + + bind_mount_recursive(ROOTFSDIR, ""); + } else if (error == -1) { return 1; } diff --git a/kernel/include/rusage.h b/kernel/include/rusage.h index 65f1cd01..4a550166 100644 --- a/kernel/include/rusage.h +++ b/kernel/include/rusage.h @@ -1,7 +1,7 @@ #ifndef __RUSAGE_H #define __RUSAGE_H -#include "config.h" +#include "../../config.h" #define RUSAGE_DEFAULT_SIZE 10