Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IC0iI-0007XL-UR for qemu-devel@nongnu.org; Fri, 20 Jul 2007 18:12:43 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IC0iH-0007VD-7m for qemu-devel@nongnu.org; Fri, 20 Jul 2007 18:12:41 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IC0iG-0007Us-PB for qemu-devel@nongnu.org; Fri, 20 Jul 2007 18:12:40 -0400 Received: from exchange.columbia.tresys.com ([216.250.243.126]) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1IC0iG-0006o0-EI for qemu-devel@nongnu.org; Fri, 20 Jul 2007 18:12:40 -0400 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-class: urn:content-classes:message Date: Fri, 20 Jul 2007 17:51:10 -0400 Message-ID: In-Reply-To: From: "David Windsor" Subject: [Qemu-devel] [RFC][PATCH 01/01]qemu: add SELinux hook for loading virtual hard disk Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Add infrastructure to qemu to make it an SELinux object manager. = Currently, the AVC is not being used, since only one permission is currently being checked. Index: src/kvm-userspace/qemu/vl.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- src.orig/kvm-userspace/qemu/vl.c +++ src/kvm-userspace/qemu/vl.c @@ -92,6 +92,8 @@ #include "qemu-kvm.h" #endif =20 +#include "selinux.h" + #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" #ifdef __sun__ #define SMBD_COMMAND "/usr/sfw/sbin/smbd" @@ -7522,6 +7524,14 @@ int main(int argc, char **argv) } #endif =20 +#ifdef CONFIG_VM_SECURITY + /* Initialize SELinux */ + if (selinux_init()) { + fprintf(stderr, "Could not initialize SELinux\n"); + exit(1); + } +#endif + /* we always create the cdrom drive, even if no disk is there */ bdrv_init(); if (cdrom_index >=3D 0) { @@ -7537,6 +7547,14 @@ int main(int argc, char **argv) snprintf(buf, sizeof(buf), "hd%c", i + 'a'); bs_table[i] =3D bdrv_new(buf); } + +#ifdef CONFIG_VM_SECURITY + if (selinux_vm_use_block_device(hd_filename[i])) { + fprintf(stderr, "qemu: SELinux denied access to hard = disk image '%s'\n", hd_filename[i]); + exit(1); + } +#endif + if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { fprintf(stderr, "qemu: could not open hard disk image '%s'\n", hd_filename[i]); Index: src/kvm-userspace/qemu/configure =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- src.orig/kvm-userspace/qemu/configure +++ src/kvm-userspace/qemu/configure @@ -90,6 +90,7 @@ bsd=3D"no" linux=3D"no" kqemu=3D"no" kvm=3D"no" +vm_security=3D"no" profiler=3D"no" kernel_path=3D"" cocoa=3D"no" @@ -238,6 +239,8 @@ for opt do ;; --enable-kvm) kvm=3D"yes" ;; + --enable-vm-security) vm_security=3D"yes" + ;; --enable-profiler) profiler=3D"yes" ;; --kernel-path=3D*) kernel_path=3D"$optarg" @@ -287,6 +290,7 @@ echo "kqemu kernel acceleration support: echo " --disable-kqemu disable kqemu support" echo " --kernel-path=3DPATH set the kernel path (configure = probes it)" echo " --enable-kvm enable kernel virtual machine support" +echo " --enable-vm-security enable SELinux enforcement hooks" echo "" echo "Advanced options (experts only):" echo " --source-path=3DPATH path of source code [$source_path]" @@ -868,13 +872,18 @@ bflt=3D"no" interp_prefix1=3D`echo "$interp_prefix" | sed "s/%M/$target_cpu/g"` echo "#define CONFIG_QEMU_PREFIX \"$interp_prefix1\"" >> $config_h =20 +if test $vm_security =3D "yes" ; then + echo "#define CONFIG_VM_SECURITY 1" >> $config_h + echo "CONFIG_VM_SECURITY=3Dyes" >> $config_mak +fi + configure_kvm() { if test $kvm =3D "yes" -a "$target_softmmu" =3D "yes" -a \ \( "$cpu" =3D "i386" -o "$cpu" =3D "x86_64" \); then echo "#define USE_KVM 1" >> $config_h echo "CONFIG_KVM_KERNEL_INC=3D$kernel_path/include" >> $config_mak fi -} + } =20 if test "$target_cpu" =3D "i386" ; then echo "TARGET_ARCH=3Di386" >> $config_mak Index: src/kvm-userspace/configure =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- src.orig/kvm-userspace/configure +++ src/kvm-userspace/configure @@ -5,6 +5,7 @@ kerneldir=3D/lib/modules/$(uname -r)/build want_module=3D1 qemu_cc=3D$(ls /usr/bin/gcc3* /usr/bin/gcc-3* 2>/dev/null | tail -n1) disable_gcc_check=3D +enable_vm_security=3D =20 usage() { cat <<-EOF @@ -18,6 +19,7 @@ usage() { --qemu-cc=3D"$qemu_cc" compiler for qemu (needs gcc3.x) ($qemu_cc) --disable-gcc-check don't insist on gcc-3.x - this will break running without = kvm + --enable-vm-security enable SELinux security enforcement EOF exit 1 } @@ -53,6 +55,9 @@ while [[ "$1" =3D -* ]]; do --disable-gcc-check) disable_gcc_check=3D1 ;; + --enable-vm-security) + enable_vm_security=3D1 + ;; --help) usage ;; @@ -87,6 +92,7 @@ target_cpu() { --enable-kvm --kernel-path=3D"$libkvm_kerneldir" \ --enable-alsa \ ${disable_gcc_check:+"--disable-gcc-check"} \ + ${enable_vm_security:+"--enable-vm-security"}\ --prefix=3D"$prefix" ) =20 Index: src/kvm-userspace/qemu/Makefile.target =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- src.orig/kvm-userspace/qemu/Makefile.target +++ src/kvm-userspace/qemu/Makefile.target @@ -232,7 +232,7 @@ OBJS+=3D libqemu.a =20 # cpu emulator library LIBOBJS=3Dexec.o kqemu.o qemu-kvm.o translate-op.o translate-all.o cpu-exec.o\ - translate.o op.o + translate.o op.o selinux.o ifdef CONFIG_SOFTFLOAT LIBOBJS+=3Dfpu/softfloat.o else @@ -364,6 +364,9 @@ CFLAGS +=3D -I $(CONFIG_KVM_KERNEL_INC) LIBS +=3D -lkvm DEPLIBS +=3D ../user/libkvm.a endif +ifdef CONFIG_VM_SECURITY +LIBS +=3D -lselinux +endif =20 # SCSI layer VL_OBJS+=3D scsi-disk.o cdrom.o lsi53c895a.o Index: src/kvm-userspace/qemu/selinux.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- /dev/null +++ src/kvm-userspace/qemu/selinux.h @@ -0,0 +1,24 @@ +#ifndef _QEMU_SELINUX_H_ +#define _QEMU_SELINUX_H_ + +/* + * Initialize qemu's SELinux subsystem. + * + * Returns 0 on success, non-zero on error. + * This function will only return non-zero when SELinux is + * enabled in the running kernel and is in enforcing mode. + */ +int selinux_init(void); + +/* + * Check the SELinux policy as to whether the current process is = allowed + * to load a virtual disk into a virtual machine. + * This function should only be called after calling selinux_init. + * + * Returns 0 if the operation is allowed, non-zero if the operation is denied. + * This function will only return non-zero if SELinux is enabled + * in the running kernel and is in enforcing mode. + */ +int selinux_vm_use_block_device(const char *hd_filename); + +#endif Index: src/kvm-userspace/qemu/selinux.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- /dev/null +++ src/kvm-userspace/qemu/selinux.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include + +#include "selinux.h" + +static int qemu_selinux_enabled =3D 0; + +/* + * Initialize qemu's SELinux subsystem. + * + * Returns 0 on success, non-zero on error. + * This function will return non-zero only when SELinux is + * enabled in the running kernel and is in enforcing mode. + */ +int selinux_init(void) +{ + int retval =3D -1; + struct security_class_mapping map[] =3D { + { "vm", + { "entrypoint", NULL }}, + { NULL } + }; + + if (qemu_selinux_enabled) { + retval =3D 0; + goto out; + } + + retval =3D is_selinux_enabled(); + if (!retval) + goto out; + + /* Create userspace object class and permission mappings */ + retval =3D selinux_set_mapping(map); + if (retval < 0) { + printf("SELinux: failed to set up security class mapping\n"); + goto out; + } + + qemu_selinux_enabled =3D 1; +out: + if (retval !=3D 0 && !security_getenforce()) + retval =3D 0; + return retval; +} + +/* + * Check the SELinux policy as to whether the current process is + * allowed to load a virtual disk into a virtual machine. + * This function should only be called after calling selinux_init. + * + * Returns 0 if the operation is allowed, non-zero if the operation is denied. + * This function will only return non-zero if SELinux is enabled + * in the running kernel and is in enforcing mode. + */ +int selinux_vm_use_block_device(const char *hd_filename) +{ + security_context_t scon; + security_context_t tcon; + security_class_t vm_class; + access_vector_t vm_perms; + struct av_decision avd; + int retval =3D -1; + =20 + retval =3D is_selinux_enabled(); + if (!retval) { + retval =3D 0; + goto out; + } + + vm_class =3D string_to_security_class("vm"); + if (!vm_class) { + retval =3D -1; + goto out; + } + + vm_perms =3D string_to_av_perm(vm_class, "entrypoint"); + if (!vm_perms) { + retval =3D -1; + goto out; + } + + /* The context of the VM is the context of the current process = */ + retval =3D getcon(&scon); + if (retval) + goto out; + + /* Get the context of the virtual disk */ + retval =3D getfilecon(hd_filename, &tcon); + if (retval < 0) { + freecon(scon); + goto out; + } + + retval =3D security_compute_av(scon, tcon, vm_class, + vm_perms, &avd); + freecon(scon); + freecon(tcon); + if (!retval && ((vm_perms & avd.allowed) =3D=3D vm_perms)) { + goto out; + } else { + /* Access is denied */ + retval =3D 1; + goto out; + } + +out: + if (retval !=3D 0 && !security_getenforce()) + retval =3D 0; + return retval; +}