Index: lib/libvmmapi/vmmapi.c =================================================================== --- lib/libvmmapi/vmmapi.c +++ lib/libvmmapi/vmmapi.c @@ -1719,7 +1719,7 @@ VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SUSPEND_CPU, VM_RESUME_CPU, VM_SET_INTINFO, VM_GET_INTINFO, VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME, - VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY }; + VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY, VM_SNAPSHOT_REQ }; if (len == NULL) { cmds = malloc(sizeof(vm_ioctl_cmds)); Index: usr.sbin/bhyve/Makefile =================================================================== --- usr.sbin/bhyve/Makefile +++ usr.sbin/bhyve/Makefile @@ -91,8 +91,9 @@ LIBADD+= casper LIBADD+= cap_pwd LIBADD+= cap_grp +LIBADD+= cap_sysctl # Temporary disable capsicum, until we integrate checkpoint code with it. -#CFLAGS+=-DWITH_CASPER +CFLAGS+=-DWITH_CASPER .endif .if ${MK_BHYVE_SNAPSHOT} != "no" @@ -121,9 +122,6 @@ .if ${MK_BHYVE_SNAPSHOT} != "no" CFLAGS+= -I${SRCTOP}/contrib/libucl/include -# Temporary disable capsicum, until we integrate checkpoint code with it. -CFLAGS+= -DWITHOUT_CAPSICUM - CFLAGS+= -DBHYVE_SNAPSHOT .endif Index: usr.sbin/bhyve/bhyverun.c =================================================================== --- usr.sbin/bhyve/bhyverun.c +++ usr.sbin/bhyve/bhyverun.c @@ -34,6 +34,8 @@ #include #ifndef WITHOUT_CAPSICUM #include +#include +#include #endif #include #ifdef BHYVE_SNAPSHOT @@ -1244,13 +1246,14 @@ restore_file = NULL; #endif + char *ckp_path = NULL; init_config(); set_defaults(); progname = basename(argv[0]); #ifdef BHYVE_SNAPSHOT - optstr = "aehuwxACDHIPSWYk:o:p:G:c:s:m:l:K:U:r:"; + optstr = "aehuwxACDHIPSWYk:o:p:G:c:s:m:l:K:U:r:t:"; #else optstr = "aehuwxACDHIPSWYk:o:p:G:c:s:m:l:K:U:"; #endif @@ -1302,6 +1305,9 @@ case 'r': restore_file = optarg; break; + case 't': + ckp_path = optarg; + break; #endif case 's': if (strncmp(optarg, "help", strlen(optarg)) == 0) { @@ -1547,16 +1553,6 @@ */ setproctitle("%s", vmname); -#ifndef WITHOUT_CAPSICUM - caph_cache_catpages(); - - if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1) - errx(EX_OSERR, "Unable to apply rights for sandbox"); - - if (caph_enter() == -1) - errx(EX_OSERR, "cap_enter() failed"); -#endif - #ifdef BHYVE_SNAPSHOT if (restore_file != NULL) destroy_restore_state(&rstate); @@ -1564,6 +1560,9 @@ /* initialize mutex/cond variables */ init_snapshot(); + /* initialize snapshot files parent directory and casper channel */ + init_capsicum_info(ckp_path); + /* * checkpointing thread for communication with bhyvectl */ @@ -1574,6 +1573,16 @@ vm_restore_time(ctx); #endif +#ifndef WITHOUT_CAPSICUM + caph_cache_catpages(); + + if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); + + if (caph_enter() == -1) + errx(EX_OSERR, "cap_enter() failed"); +#endif + /* * Add CPU 0 */ Index: usr.sbin/bhyve/snapshot.h =================================================================== --- usr.sbin/bhyve/snapshot.h +++ usr.sbin/bhyve/snapshot.h @@ -42,6 +42,11 @@ #include #include +#ifndef WITHOUT_CAPSICUM +#include +#include +#endif + #define BHYVE_RUN_DIR "/var/run/bhyve/" #define MAX_SNAPSHOT_FILENAME PATH_MAX @@ -101,8 +106,9 @@ int get_checkpoint_msg(int conn_fd, struct vmctx *ctx); void *checkpoint_thread(void *param); -int init_checkpoint_thread(struct vmctx *ctx); void init_snapshot(void); +int init_checkpoint_thread(struct vmctx *ctx); +void init_capsicum_info(char *ckp_path); int load_restore_file(const char *filename, struct restore_state *rstate); Index: usr.sbin/bhyve/snapshot.c =================================================================== --- usr.sbin/bhyve/snapshot.c +++ usr.sbin/bhyve/snapshot.c @@ -171,6 +171,9 @@ static pthread_cond_t vcpus_idle, vcpus_can_run; static bool checkpoint_active; +static int cdir_fd = AT_FDCWD; +static cap_channel_t *casper_channel = NULL; + /* * TODO: Harden this function and all of its callers since 'base_str' is a user * provided string. @@ -224,6 +227,38 @@ ucl_parser_free(rstate->meta_parser); } +#ifndef WITHOUT_CAPSICUM +static void +limit_vmmem_socket(int s) +{ + cap_rights_t rights; + + cap_rights_init(&rights, CAP_FSTAT, CAP_MMAP_R, CAP_IOCTL, CAP_READ); + if (caph_rights_limit(s, &rights) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); +} + +static void +limit_kernel_socket(int s) +{ + cap_rights_t rights; + + cap_rights_init(&rights, CAP_FSTAT, CAP_MMAP_R, CAP_READ); + if (caph_rights_limit(s, &rights) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); +} + +static void +limit_metadata_socket(int s) +{ + cap_rights_t rights; + + cap_rights_init(&rights, CAP_FSTAT, CAP_MMAP_R, CAP_READ); + if (caph_rights_limit(s, &rights) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); +} +#endif + static int load_vmmem_file(const char *filename, struct restore_state *rstate) { @@ -236,6 +271,10 @@ return (-1); } +#ifndef WITHOUT_CAPSICUM + limit_vmmem_socket(rstate->vmmem_fd); +#endif + err = fstat(rstate->vmmem_fd, &sb); if (err < 0) { perror("Failed to stat restore file"); @@ -269,6 +308,10 @@ return (-1); } +#ifndef WITHOUT_CAPSICUM + limit_kernel_socket(rstate->kdata_fd); +#endif + err = fstat(rstate->kdata_fd, &sb); if (err < 0) { perror("Failed to stat kernel data file"); @@ -301,6 +344,7 @@ { const ucl_object_t *obj; struct ucl_parser *parser; + int md_fd = -1; int err; parser = ucl_parser_new(UCL_PARSER_DEFAULT); @@ -309,7 +353,13 @@ goto err_load_metadata; } - err = ucl_parser_add_file(parser, filename); + md_fd = open(filename, O_RDONLY); + +#ifndef WITHOUT_CAPSICUM + limit_metadata_socket(md_fd); +#endif + + err = ucl_parser_add_fd(parser, md_fd); if (err == 0) { fprintf(stderr, "Failed to parse metadata file: '%s'\n", filename); @@ -330,6 +380,8 @@ return (0); err_load_metadata: + if (md_fd > 0) + close(md_fd); if (parser != NULL) ucl_parser_free(parser); return (err); @@ -1304,10 +1356,16 @@ static void vm_vcpu_pause(struct vmctx *ctx) { + int err; pthread_mutex_lock(&vcpu_lock); checkpoint_active = true; - vm_suspend_cpu(ctx, -1); + err = vm_suspend_cpu(ctx, -1); + if (err != 0) { + fprintf(stderr, "%s: Could not suspend vcpus\r\n", __func__); + pthread_mutex_unlock(&vcpu_lock); + return; + } while (CPU_CMP(&vcpus_active, &vcpus_suspended) != 0) pthread_cond_wait(&vcpus_idle, &vcpu_lock); pthread_mutex_unlock(&vcpu_lock); @@ -1324,10 +1382,42 @@ pthread_cond_broadcast(&vcpus_can_run); } +#ifndef WITHOUT_CAPSICUM +#define DESTROY(vm, ch, err, LABEL) \ +do { \ + cap_channel_t *capsysctl = NULL; \ + char *name = "hw.vmm.destroy"; \ + void *limit; \ + \ + /* Create capability to the system.sysctl service with Casper. */ \ + capsysctl = cap_service_open(ch, "system.sysctl"); \ + if (capsysctl == NULL) \ + fprintf(stderr, "%s: Unable to open system.sysctl service", __func__); \ + \ + cap_close(ch); \ + \ + /* Create limit for one MIB with write access only. */ \ + limit = cap_sysctl_limit_init(capsysctl); \ + (void)cap_sysctl_limit_name(limit, name, CAP_SYSCTL_WRITE); \ + \ + /* Limit system.sysctl. */ \ + if (cap_sysctl_limit(limit) < 0) \ + fprintf(stderr, "%s: Unable to set limits", __func__); \ + \ + err = cap_sysctlbyname(capsysctl, name, NULL, NULL, (vm), strlen((vm))); \ + \ + cap_close(capsysctl); \ + if (err != 0) { \ + fprintf(stderr, "%s: err is %d\r\n", __func__, errno); \ + goto LABEL; \ + } \ +} while(0) +#endif + static int vm_checkpoint(struct vmctx *ctx, const char *checkpoint_file, bool stop_vm) { - int fd_checkpoint = 0, kdata_fd = 0; + int fd_checkpoint = 0, kdata_fd = 0, meta_fd = 0; int ret = 0; int error = 0; size_t memsz; @@ -1335,6 +1425,7 @@ char *meta_filename = NULL; char *kdata_filename = NULL; FILE *meta_file = NULL; + char vmname[MAX_VMNAME]; kdata_filename = strcat_extension(checkpoint_file, ".kern"); if (kdata_filename == NULL) { @@ -1342,15 +1433,14 @@ return (-1); } - kdata_fd = open(kdata_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700); + kdata_fd = openat(cdir_fd, kdata_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700); if (kdata_fd < 0) { perror("Failed to open kernel data snapshot file."); error = -1; goto done; } - fd_checkpoint = open(checkpoint_file, O_RDWR | O_CREAT | O_TRUNC, 0700); - + fd_checkpoint = openat(cdir_fd, checkpoint_file, O_RDWR | O_CREAT | O_TRUNC, 0700); if (fd_checkpoint < 0) { perror("Failed to create checkpoint file"); error = -1; @@ -1363,7 +1453,13 @@ goto done; } - meta_file = fopen(meta_filename, "w"); + meta_fd = openat(cdir_fd, meta_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700); + if (meta_fd < 0) { + perror("Failed to open vm metadata snapshot file descriptor."); + goto done; + } + + meta_file = fdopen(meta_fd, "w"); if (meta_file == NULL) { perror("Failed to open vm metadata snapshot file."); goto done; @@ -1398,7 +1494,6 @@ goto done; } - ret = vm_snapshot_kern_structs(ctx, kdata_fd, xop); if (ret != 0) { fprintf(stderr, "Failed to snapshot vm kernel data.\n"); @@ -1415,8 +1510,19 @@ xo_finish_h(xop); + if (stop_vm) { - vm_destroy(ctx); + if (casper_channel != NULL) { + error = vm_get_name(ctx, vmname, MAX_VMNAME - 1); + if (error != 0) { + fprintf(stderr, "%s: Failed to get VM name", __func__); + goto done; + } + DESTROY(vmname, casper_channel, error, done); + free(ctx); + } else + vm_destroy(ctx); + exit(0); } @@ -1437,10 +1543,14 @@ fclose(meta_file); if (kdata_fd > 0) close(kdata_fd); +#ifndef WITHOUT_CAPSICUM + if (cdir_fd > 0) + close(cdir_fd); +#endif return (error); } -static int +int handle_message(struct vmctx *ctx, nvlist_t *nvl) { int err; @@ -1453,13 +1563,13 @@ if (strcmp(cmd, "checkpoint") == 0) { if (!nvlist_exists_string(nvl, "filename") || !nvlist_exists_bool(nvl, "suspend")) - err = -1; + err = -1; else - err = vm_checkpoint(ctx, nvlist_get_string(nvl, "filename"), - nvlist_get_bool(nvl, "suspend")); + err = vm_checkpoint(ctx, nvlist_get_string(nvl, "filename"), + nvlist_get_bool(nvl, "suspend")); } else { - EPRINTLN("Unrecognized checkpoint operation\n"); - err = -1; + EPRINTLN("Unrecognized checkpoint operation\n"); + err = -1; } if (err != 0) @@ -1483,6 +1593,11 @@ for (;;) { nvl = nvlist_recv(thread_info->socket_fd, 0); + + /* + * slight sanity check: see if there's enough data to at + * least determine the type of message. + */ if (nvl != NULL) handle_message(thread_info->ctx, nvl); else @@ -1508,6 +1623,51 @@ errc(1, err, "checkpoint cv init (vcpus_can_run)"); } +#ifndef WITHOUT_CAPSICUM +static void +limit_control_socket(int s) +{ + cap_rights_t rights; + + cap_rights_init(&rights, CAP_BIND, CAP_READ, CAP_GETSOCKOPT); + if (caph_rights_limit(s, &rights) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); +} + +static void +limit_file_operations() +{ + cap_rights_t rights; + + cap_rights_init(&rights, CAP_LOOKUP, CAP_FTRUNCATE, CAP_PWRITE, CAP_PREAD, CAP_FCNTL, CAP_CREATE); + if (caph_rights_limit(cdir_fd, &rights) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); +} + +void +init_capsicum_info(char *ckp_path) +{ + /* Open capability to Casper. */ + casper_channel = cap_init(); + if (casper_channel == NULL) + errx(EX_OSERR, "cap_init() failed"); + + /* + * If the path for the parent directory is not specified then + * the directory where the bhyve command is called will be used + */ + if (ckp_path == NULL) { + ckp_path = "."; + } + + cdir_fd = open(ckp_path, O_RDONLY | O_DIRECTORY); + if (cdir_fd < 0) + errc(1, cdir_fd, "open snapshot files directory"); + limit_file_operations(); +} + +#endif + /* * Create the listening socket for IPC with bhyvectl */ @@ -1519,7 +1679,7 @@ int socket_fd; pthread_t checkpoint_pthread; char vmname_buf[MAX_VMNAME]; - int err; + int err = 0; memset(&addr, 0, sizeof(addr)); @@ -1530,6 +1690,7 @@ goto fail; } + limit_control_socket(socket_fd); addr.sun_family = AF_UNIX; err = vm_get_name(ctx, vmname_buf, MAX_VMNAME - 1);