From nobody Sat Apr 20 14:12:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575029047; cv=none; d=zohomail.com; s=zohoarc; b=FzEa4bDeuRDXejDzU/liVuRZFtvLMc3d/X+e1N91kxVl6pr2oS6tkMk6a4zPqCe7x1EUWqWG/+lKfMlHWBBYrHI92EPXaywRAWGn73PhhQLmJ/uHJjT+0y7UdK2NP/FJbYPLrcMtIjD3A0q2ONhCy3J2XsDhgjKDDSOpD9o8wmI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575029047; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=y+JITu/sXR0tEv1HQ2+1vlvrpnR0cdQPnZCIBN6cwjA=; b=cDnZmTLyNxIaVNaDTmiSFn8Dq0Lw6yh92VPekpuj5EMXDld1V2HPNNT5nFwcuLXY73MK8WB+ZKZF5KiFDvb8yXGIWpqNcpds792jSWKyHviqooViZG2+/aVnzxhVA2TkxSUcoDiAqWNAU8Aan8x2vdEK/KmIiGJYm9UTCBAZiiU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1575029047553559.0118048278821; Fri, 29 Nov 2019 04:04:07 -0800 (PST) Received: from localhost ([::1]:57940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iaf0F-0004Fu-RS for importer@patchew.org; Fri, 29 Nov 2019 07:04:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57204) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iaeGS-00056H-Kh for qemu-devel@nongnu.org; Fri, 29 Nov 2019 06:16:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iaeGN-0001wx-96 for qemu-devel@nongnu.org; Fri, 29 Nov 2019 06:16:40 -0500 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:43507) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iaeGM-0001iN-2B for qemu-devel@nongnu.org; Fri, 29 Nov 2019 06:16:39 -0500 Received: by mail-wr1-x443.google.com with SMTP id n1so34713627wra.10 for ; Fri, 29 Nov 2019 03:16:35 -0800 (PST) Received: from donizetti.redhat.com ([2001:b07:6468:f312:459f:99a9:39f1:65ba]) by smtp.gmail.com with ESMTPSA id i71sm29752969wri.68.2019.11.29.03.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Nov 2019 03:16:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=y+JITu/sXR0tEv1HQ2+1vlvrpnR0cdQPnZCIBN6cwjA=; b=OOPRyXAc3Ro9oA6WLST4+OOQgDtD4dwsCAxWZp0AhRM1lVNXATlWG8vjdABp9ewPYJ JIlcqjudnUNSFXaGxnPYqkOYi8w4NChvIxEwrqJo1AAPUh6jyiz0S4JS49NwtVC9R/bM XqdTUkCbbjCcqgTHtxEUUF05ZFGJLkagAPdBogWObijGv/N707HrKj1zFgbuH85mi9au ANuckbEDS3ExXbk8kNhoIQVTbrnfplovaraWQdtChsUvL4ZZiny3FTvAt69LhGnFhp6i 1MtO4KD8gXoO8BOTKBUwMcYG+Y4Gs9MqhFokEmmcI7l8hthhgVsABGSd0X/nTdujJEhi vmaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=y+JITu/sXR0tEv1HQ2+1vlvrpnR0cdQPnZCIBN6cwjA=; b=b0xe8nkVWBW1h6/xQw7aP8A7nLqrU6eSJnGzReXtl2GZvdj5SRxrzWSZuuXJhfL5Zx xOoRbP2D2hCrESzWDKBnRHcoYKDJrPR3iJjNFnqjM2pW/J8Uy3bGBjgBFz4GilNgKqd9 Dy3pvqon+I2GqHcN9x3YE35MEMqPx8VJTfqX/J49Zx7h/eHRzCvAqoosCemrpuNTG9BW J272Whg9WYkuAMnqxoCkKk4ZpZM/GH4DeTST1KGd5bWVXTHX+DYaJH+E05ME50+zOHzk 5fUxXQyfjUHYZg0hw9uHVxsQZEA52+LIoBMPmXAdFOQN7SGNJjLU5eL2vYuq+JZNnvTm CKMQ== X-Gm-Message-State: APjAAAXrN+p/v/OV7OMdswLkbrxkVpR6V/KQmHpBUVmfd3BE9o5DTL+i 2nEflinPqgX/41D/yl0tTyZLOAmx X-Google-Smtp-Source: APXvYqw9CvgSU9VxwILc34y0kBf8LcoD5JYSStQc+C8JFLTlvPR73eOtm6dbWmSzdtvHgZZrMdXZMw== X-Received: by 2002:adf:8044:: with SMTP id 62mr53030115wrk.244.1575026194602; Fri, 29 Nov 2019 03:16:34 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH] virtfs-proxy-helper: switch from libcap to libcap-ng Date: Fri, 29 Nov 2019 12:16:32 +0100 Message-Id: <20191129111632.22840-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::443 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: berrange@redhat.com, dgilbert@redhat.com, groug@kaod.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" virtfs-proxy-helper is the only user of libcap; everyone else is using the simpler libcap-ng API. Switch and remove the configure code to detect libcap. Signed-off-by: Paolo Bonzini Acked-by: Greg Kurz Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Greg Kurz --- configure | 18 +------ fsdev/virtfs-proxy-helper.c | 100 ++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 72 deletions(-) diff --git a/configure b/configure index afe9393f04..2216662bf6 100755 --- a/configure +++ b/configure @@ -3863,22 +3863,6 @@ else mpathpersist=3Dno fi =20 -########################################## -# libcap probe - -if test "$cap" !=3D "no" ; then - cat > $TMPC < -#include -int main(void) { cap_t caps; caps =3D cap_init(); return caps !=3D NULL; } -EOF - if compile_prog "" "-lcap" ; then - cap=3Dyes - else - cap=3Dno - fi -fi - ########################################## # pthread probe PTHREADLIBS_LIST=3D"-pthread -lpthread -lpthreadGC2" @@ -6204,7 +6188,7 @@ if test "$want_tools" =3D "yes" ; then fi if test "$softmmu" =3D yes ; then if test "$linux" =3D yes; then - if test "$virtfs" !=3D no && test "$cap" =3D yes && test "$attr" =3D y= es ; then + if test "$virtfs" !=3D no && test "$cap_ng" =3D yes && test "$attr" = =3D yes ; then virtfs=3Dyes tools=3D"$tools fsdev/virtfs-proxy-helper\$(EXESUF)" else diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c index 6f132c5ff1..0d4de49dcf 100644 --- a/fsdev/virtfs-proxy-helper.c +++ b/fsdev/virtfs-proxy-helper.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -21,6 +20,7 @@ #ifdef CONFIG_LINUX_MAGIC_H #include #endif +#include #include "qemu-common.h" #include "qemu/sockets.h" #include "qemu/xattr.h" @@ -79,49 +79,10 @@ static void do_perror(const char *string) } } =20 -static int do_cap_set(cap_value_t *cap_value, int size, int reset) -{ - cap_t caps; - if (reset) { - /* - * Start with an empty set and set permitted and effective - */ - caps =3D cap_init(); - if (caps =3D=3D NULL) { - do_perror("cap_init"); - return -1; - } - if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < = 0) { - do_perror("cap_set_flag"); - goto error; - } - } else { - caps =3D cap_get_proc(); - if (!caps) { - do_perror("cap_get_proc"); - return -1; - } - } - if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) { - do_perror("cap_set_flag"); - goto error; - } - if (cap_set_proc(caps) < 0) { - do_perror("cap_set_proc"); - goto error; - } - cap_free(caps); - return 0; - -error: - cap_free(caps); - return -1; -} - static int init_capabilities(void) { /* helper needs following capabilities only */ - cap_value_t cap_list[] =3D { + int cap_list[] =3D { CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_FOWNER, @@ -130,7 +91,34 @@ static int init_capabilities(void) CAP_MKNOD, CAP_SETUID, }; - return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1); + int i; + + capng_clear(CAPNG_SELECT_BOTH); + for (i =3D 0; i < ARRAY_SIZE(cap_list); i++) { + if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, + cap_list[i]) < 0) { + do_perror("capng_update"); + return -1; + } + } + if (capng_apply(CAPNG_SELECT_BOTH) < 0) { + do_perror("capng_apply"); + return -1; + } + + /* Prepare effective set for setugid. */ + for (i =3D 0; i < ARRAY_SIZE(cap_list); i++) { + if (cap_list[i] =3D=3D CAP_DAC_OVERRIDE) { + continue; + } + + if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, + cap_list[i]) < 0) { + do_perror("capng_update"); + return -1; + } + } + return 0; } =20 static int socket_read(int sockfd, void *buff, ssize_t size) @@ -295,14 +283,6 @@ static int setugid(int uid, int gid, int *suid, int *s= gid) { int retval; =20 - /* - * We still need DAC_OVERRIDE because we don't change - * supplementary group ids, and hence may be subjected DAC rules - */ - cap_value_t cap_list[] =3D { - CAP_DAC_OVERRIDE, - }; - *suid =3D geteuid(); *sgid =3D getegid(); =20 @@ -316,11 +296,21 @@ static int setugid(int uid, int gid, int *suid, int *= sgid) goto err_sgid; } =20 - if (uid !=3D 0 || gid !=3D 0) { - if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) { - retval =3D -errno; - goto err_suid; - } + if (uid =3D=3D 0 && gid =3D=3D 0) { + /* Linux has already copied the permitted set to the effective set= . */ + return 0; + } + + /* + * All capabilities have been cleared from the effective set. However + * we still need DAC_OVERRIDE because we don't change supplementary + * group ids, and hence may be subject to DAC rules. init_capabilities + * left the set of capabilities that we want in libcap-ng's state. + */ + if (capng_apply(CAPNG_SELECT_CAPS) < 0) { + retval =3D -errno; + do_perror("capng_apply"); + goto err_suid; } return 0; =20 --=20 2.21.0