From patchwork Mon Oct 31 18:36:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Bryant X-Patchwork-Id: 122915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A6B31B6F87 for ; Tue, 1 Nov 2011 05:48:05 +1100 (EST) Received: from localhost ([::1]:50127 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RKwtu-00082t-BR for incoming@patchwork.ozlabs.org; Mon, 31 Oct 2011 14:48:02 -0400 Received: from eggs.gnu.org ([140.186.70.92]:43640) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RKwto-00082b-89 for qemu-devel@nongnu.org; Mon, 31 Oct 2011 14:47:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RKwtm-0005nt-Lo for qemu-devel@nongnu.org; Mon, 31 Oct 2011 14:47:56 -0400 Received: from e8.ny.us.ibm.com ([32.97.182.138]:50382) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RKwtm-0005nh-IQ for qemu-devel@nongnu.org; Mon, 31 Oct 2011 14:47:54 -0400 Received: from /spool/local by e8.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 31 Oct 2011 14:37:38 -0400 Received: from d01relay04.pok.ibm.com ([9.56.227.236]) by e8.ny.us.ibm.com ([192.168.1.108]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 31 Oct 2011 14:37:16 -0400 Received: from d03av01.boulder.ibm.com (d03av01.boulder.ibm.com [9.17.195.167]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p9VIale4255546 for ; Mon, 31 Oct 2011 14:36:48 -0400 Received: from d03av01.boulder.ibm.com (loopback [127.0.0.1]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p9VIag4x008562 for ; Mon, 31 Oct 2011 12:36:46 -0600 Received: from localhost (sig-9-65-248-137.mts.ibm.com [9.65.248.137]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p9VIae3I008447; Mon, 31 Oct 2011 12:36:41 -0600 From: Corey Bryant To: qemu-devel@nongnu.org Date: Mon, 31 Oct 2011 14:36:30 -0400 Message-Id: <1320086191-23641-4-git-send-email-coreyb@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: <1320086191-23641-1-git-send-email-coreyb@linux.vnet.ibm.com> References: <1320086191-23641-1-git-send-email-coreyb@linux.vnet.ibm.com> x-cbid: 11103118-9360-0000-0000-00000034A787 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.182.138 Cc: aliguori@us.ibm.com, rmarwah@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH v3 3/4] Add cap reduction support to enable use as SUID X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The ideal way to use qemu-bridge-helper is to give it an fscap of using: setcap cap_net_admin=ep qemu-bridge-helper Unfortunately, most distros still do not have a mechanism to package files with fscaps applied. This means they'll have to SUID the qemu-bridge-helper binary. To improve security, use libcap to reduce our capability set to just cap_net_admin, then reduce privileges down to the calling user. This is hopefully close to equivalent to fscap support from a security perspective. Signed-off-by: Anthony Liguori Signed-off-by: Richa Marwaha Signed-off-by: Corey Bryant --- configure | 34 ++++++++++++++++++++++++++++++++++ qemu-bridge-helper.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 0 deletions(-) diff --git a/configure b/configure index 94c89a7..4e49b4b 100755 --- a/configure +++ b/configure @@ -128,6 +128,7 @@ vnc_thread="no" xen="" xen_ctrl_version="" linux_aio="" +cap="" attr="" xfs="" @@ -654,6 +655,10 @@ for opt do ;; --enable-kvm) kvm="yes" ;; + --disable-cap) cap="no" + ;; + --enable-cap) cap="yes" + ;; --disable-spice) spice="no" ;; --enable-spice) spice="yes" @@ -1037,6 +1042,8 @@ echo " --disable-vde disable support for vde network" echo " --enable-vde enable support for vde network" echo " --disable-linux-aio disable Linux AIO support" echo " --enable-linux-aio enable Linux AIO support" +echo " --disable-cap disable libcap-ng support" +echo " --enable-cap enable libcap-ng support" echo " --disable-attr disables attr and xattr support" echo " --enable-attr enable attr and xattr support" echo " --disable-blobs disable installing provided firmware blobs" @@ -1645,6 +1652,29 @@ EOF fi ########################################## +# libcap-ng library probe +if test "$cap" != "no" ; then + cap_libs="-lcap-ng" + cat > $TMPC << EOF +#include +int main(void) +{ + capng_capability_to_name(CAPNG_EFFECTIVE); + return 0; +} +EOF + if compile_prog "" "$cap_libs" ; then + cap=yes + libs_tools="$cap_libs $libs_tools" + else + if test "$cap" = "yes" ; then + feature_not_found "cap" + fi + cap=no + fi +fi + +########################################## # Sound support libraries probe audio_drv_probe() @@ -2761,6 +2791,7 @@ echo "fdatasync $fdatasync" echo "madvise $madvise" echo "posix_madvise $posix_madvise" echo "uuid support $uuid" +echo "libcap-ng support $cap" echo "vhost-net support $vhost_net" echo "Trace backend $trace_backend" echo "Trace output file $trace_file-" @@ -2873,6 +2904,9 @@ fi if test "$vde" = "yes" ; then echo "CONFIG_VDE=y" >> $config_host_mak fi +if test "$cap" = "yes" ; then + echo "CONFIG_LIBCAP=y" >> $config_host_mak +fi for card in $audio_card_list; do def=CONFIG_`echo $card | tr '[:lower:]' '[:upper:]'` echo "$def=y" >> $config_host_mak diff --git a/qemu-bridge-helper.c b/qemu-bridge-helper.c index 4deead7..a318aa2 100644 --- a/qemu-bridge-helper.c +++ b/qemu-bridge-helper.c @@ -36,6 +36,10 @@ #include "net/tap-linux.h" +#ifdef CONFIG_LIBCAP +#include +#endif + #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf" enum { @@ -189,6 +193,27 @@ static int send_fd(int c, int fd) return sendmsg(c, &msg, 0); } +#ifdef CONFIG_LIBCAP +static int drop_privileges(void) +{ + /* clear all capabilities */ + capng_clear(CAPNG_SELECT_BOTH); + + if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, + CAP_NET_ADMIN) < 0) { + return -1; + } + + /* change to calling user's real uid and gid, retaining supplemental + * groups and CAP_NET_ADMIN */ + if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING)) { + return -1; + } + + return 0; +} +#endif + int main(int argc, char **argv) { struct ifreq ifr; @@ -203,6 +228,17 @@ int main(int argc, char **argv) int access_allowed, access_denied; int ret = 0; +#ifdef CONFIG_LIBCAP + /* if we're run from an suid binary, immediately drop privileges preserving + * cap_net_admin */ + if (geteuid() == 0 && getuid() != geteuid()) { + if (drop_privileges() == -1) { + fprintf(stderr, "failed to drop privileges\n"); + return 1; + } + } +#endif + /* parse arguments */ if (argc < 3 || argc > 4) { fprintf(stderr, "Usage: %s [--use-vnet] BRIDGE FD\n", argv[0]);