From nobody Sat Feb 7 09:36:35 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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=pass; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571918632; cv=none; d=zoho.com; s=zohoarc; b=WYL8jQRf8tlduAPP/trcw29fxOrHkKN/BWa1qe1xR5zZxes9VxwvVvy7zDRAGSUCyDJCGPohJmQxDHRmRqXgI/or+U4x9kLr6M1wubsUyYzk+Ph9f/Rs0IgZ85mupoL74yGddf4SIzC9ieBeCVTd9hbp8SZ8T0CGOMCQAr2Fa1o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571918632; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iix76qz+D3wDmXlNY++4q7SA/90h60kTtY/hGCP0X6c=; b=e4FkmLvsQ0WBjj9+iB9gVLFW4lxDu3BKVUWjETQ54uKFTY6SDYW30uYpZMTfX0kk7ITpF49MpQtguNWEmLPY8epDR/RiFAYzPKRbPFj5anj8xwVa2aSDUHOm0rJ+hH4KVCvp1jUdoqIpbOODr7JI2u4+OLh6uFejZANApmzEj1s= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass 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 1571918632410359.1250648246547; Thu, 24 Oct 2019 05:03:52 -0700 (PDT) Received: from localhost ([::1]:40526 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNbqI-0000Qe-Ad for importer@patchew.org; Thu, 24 Oct 2019 08:03:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39540) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNbIX-0004T2-Ai for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:28:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNbIV-0000Eo-TB for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:28:57 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55264 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNbIV-0000EO-Or for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:28:55 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-344-gFI1Ck6KPZmbKdaMff5QkQ-1; Thu, 24 Oct 2019 07:28:50 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2FD39800D49; Thu, 24 Oct 2019 11:28:49 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-117-248.ams2.redhat.com [10.36.117.248]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F999196B2; Thu, 24 Oct 2019 11:28:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571916534; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iix76qz+D3wDmXlNY++4q7SA/90h60kTtY/hGCP0X6c=; b=SCabeUcaUS5ULMzHhBxPRJqeneNkJihmbsQmQoiEHMvJmapa+E1ldA5YknOOdrvnCL+ASN 4lmV3LAIA+AenQonXLVHTjsUCj4L25tNgPbdnld3bdFPQHNNblruPBIAbr4LnwAWRxa9nf x8juoKdmsA+kidPkrm64+4Y3pe/CaAs= From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, renzhen@linux.alibaba.com, eguan@linux.alibaba.com, ganesh.mahalingam@intel.com, m.mizuma@jp.fujitsu.com, mszeredi@redhat.com, misono.tomohiro@jp.fujitsu.com, tao.peng@linux.alibaba.com, piaojun@huawei.com, stefanha@redhat.com, vgoyal@redhat.com, mst@redhat.com, berrange@redhat.com Subject: [PATCH 21/25] virtiofsd: Drop CAP_FSETID if client asked for it Date: Thu, 24 Oct 2019 12:27:14 +0100 Message-Id: <20191024112718.34657-22-dgilbert@redhat.com> In-Reply-To: <20191024112718.34657-1-dgilbert@redhat.com> References: <20191024112718.34657-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: gFI1Ck6KPZmbKdaMff5QkQ-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Vivek Goyal If client requested killing setuid/setgid bits on file being written, drop CAP_FSETID capability so that setuid/setgid bits are cleared upon write automatically. pjdfstest chown/12.t needs this. Signed-off-by: Vivek Goyal Reviewed-by: Dr. David Alan Gilbert --- contrib/virtiofsd/Makefile.objs | 2 + contrib/virtiofsd/passthrough_ll.c | 127 +++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/contrib/virtiofsd/Makefile.objs b/contrib/virtiofsd/Makefile.o= bjs index 941b19f18e..25f1e8dd73 100644 --- a/contrib/virtiofsd/Makefile.objs +++ b/contrib/virtiofsd/Makefile.objs @@ -11,3 +11,5 @@ virtiofsd-obj-y =3D buffer.o \ =20 seccomp.o-cflags :=3D $(SECCOMP_CFLAGS) seccomp.o-libs :=3D $(SECCOMP_LIBS) + +passthrough_ll.o-libs +=3D -lcap diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthr= ough_ll.c index 93873bf6f4..fe46b25fb6 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -174,6 +175,115 @@ static struct lo_data *lo_data(fuse_req_t req) return (struct lo_data *) fuse_req_userdata(req); } =20 +/* Helpers for dropping and regaining effective capabilities. Returns 0 + * on success, error otherwise */ +static int drop_effective_cap(const char *cap_name, bool *cap_dropped) +{ + cap_t caps; + cap_value_t cap; + cap_flag_value_t cap_value; + int ret =3D 0; + + ret =3D cap_from_name(cap_name, &cap); + if (ret =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_from_name(%s) failed:%s\n", cap_name, + strerror(errno)); + goto out; + } + + if (!CAP_IS_SUPPORTED(cap)) { + fuse_log(FUSE_LOG_ERR, "capability(%s) is not supported\n", cap_name); + return EINVAL; + } + + caps =3D cap_get_proc(); + if (caps =3D=3D NULL) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_get_proc() failed\n"); + goto out; + } + + if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &cap_value) =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_get_flag() failed\n"); + goto out_cap_free; + } + + /* We dont have this capability in effective set already. */ + if (cap_value =3D=3D CAP_CLEAR) { + ret =3D 0; + goto out_cap_free; + } + + if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap, CAP_CLEAR) =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_set_flag() failed\n"); + goto out_cap_free; + } + + if (cap_set_proc(caps) =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_set_procs() failed\n"); + goto out_cap_free; + } + + ret =3D 0; + if (cap_dropped) + *cap_dropped =3D true; + +out_cap_free: + cap_free(caps); +out: + return ret; +} + +static int gain_effective_cap(const char *cap_name) +{ + cap_t caps; + cap_value_t cap; + int ret =3D 0; + + ret =3D cap_from_name(cap_name, &cap); + if (ret =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_from_name(%s) failed:%s\n", cap_name, + strerror(errno)); + goto out; + } + + if (!CAP_IS_SUPPORTED(cap)) { + fuse_log(FUSE_LOG_ERR, "capability(%s) is not supported\n", cap_name); + return EINVAL; + } + + caps =3D cap_get_proc(); + if (caps =3D=3D NULL) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_get_proc() failed\n"); + goto out; + } + + + if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap, CAP_SET) =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_set_flag() failed\n"); + goto out_cap_free; + } + + if (cap_set_proc(caps) =3D=3D -1) { + ret =3D errno; + fuse_log(FUSE_LOG_ERR, "cap_set_procs() failed\n"); + goto out_cap_free; + } + ret =3D 0; + +out_cap_free: + cap_free(caps); +out: + return ret; +} + static void lo_map_init(struct lo_map *map) { map->elems =3D NULL; @@ -1447,6 +1557,7 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t i= no, (void) ino; ssize_t res; struct fuse_bufvec out_buf =3D FUSE_BUFVEC_INIT(fuse_buf_size(in_buf)); + bool cap_fsetid_dropped =3D false; =20 out_buf.buf[0].flags =3D FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK; out_buf.buf[0].fd =3D lo_fi_fd(req, fi); @@ -1456,11 +1567,27 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t= ino, fuse_log(FUSE_LOG_DEBUG, "lo_write(ino=3D%" PRIu64 ", size=3D%zd, off=3D= %lu)\n", ino, out_buf.buf[0].size, (unsigned long) off); =20 + /* + * If kill_priv is set, drop CAP_FSETID which should lead to kernel + * clearing setuid/setgid on file. + */ + if (fi->kill_priv) { + res =3D drop_effective_cap("cap_fsetid", &cap_fsetid_dropped); + if (res !=3D 0) + fuse_reply_err(req, res); + } + res =3D fuse_buf_copy(&out_buf, in_buf, 0); if(res < 0) fuse_reply_err(req, -res); else fuse_reply_write(req, (size_t) res); + + if (cap_fsetid_dropped) { + res =3D gain_effective_cap("cap_fsetid"); + if (res) + fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n"); + } } =20 static void lo_statfs(fuse_req_t req, fuse_ino_t ino) --=20 2.23.0