From nobody Fri May 17 11:28:42 2024 Delivered-To: importer@patchew.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=1614854448; cv=none; d=zohomail.com; s=zohoarc; b=I7eFmMi7u6/if0ZA5h6eWwIO9lMvVnUid/prbQX67Etfpz2PnHUmvMWNMQ1gkWXWwpN34dFxxf2axKCx6X6bfKBMzqcSdukCWlep4i2rwcVVEAUVK3yyLjIQdFAke4WuRsmcKnYPp+4NIdYvIavF77XtO7lXjypKRXRqLkqgWLw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614854448; h=Content-Type:Content-Transfer-Encoding:Cc: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=dhJnHEMUbA/gFGRds5Kz7ihWFbobZqDoQ9kWt0GQy1g=; b=O9AUWdpbPtp7NpRMPgEneMniuagTPWt6IsHto64Ejjq8bcPRoGHekmMFqzT5mr9GgbUD7rFkbMczSFuJPkUvWSqJFcElJ8adKKkNjZgrWiR9+jPVQmri6sF96mrl+OxOe15j/nB1pT1H2S20UO/V9a2UrxsXzKu1DlvZpLWXymQ= 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 1614854448582666.3326369968105; Thu, 4 Mar 2021 02:40:48 -0800 (PST) Received: from localhost ([::1]:48446 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lHlPS-0000gy-Ud for importer@patchew.org; Thu, 04 Mar 2021 05:40:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54352) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lHlNw-0007lf-6W for qemu-devel@nongnu.org; Thu, 04 Mar 2021 05:39:12 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50823) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lHlNt-0003Ki-06 for qemu-devel@nongnu.org; Thu, 04 Mar 2021 05:39:11 -0500 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-437-enNtq1f-NjuWYMHs83BGVA-1; Thu, 04 Mar 2021 05:39:06 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 436351B18BC1 for ; Thu, 4 Mar 2021 10:39:05 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-114-208.ams2.redhat.com [10.36.114.208]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4361739A71; Thu, 4 Mar 2021 10:39:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614854347; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dhJnHEMUbA/gFGRds5Kz7ihWFbobZqDoQ9kWt0GQy1g=; b=LpCamMMpMTTjhbm3miiGiaPEuiGT38pV2NxMkRd1dHT/9aN7t8Bt9DVl7C+9H2aXAE63Ig A7Lq3Wc07rN3/y8jutRWFiss5R9MVual6pqggO/aw7vOTr94E0jz81pxTAzgh15HpOv/Ko WNupCqJMKHl0rawe87VoE8xwAXswYcY= X-MC-Unique: enNtq1f-NjuWYMHs83BGVA-1 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org Subject: [PULL 1/1] virtiofs: drop remapped security.capability xattr as needed Date: Thu, 4 Mar 2021 10:38:58 +0000 Message-Id: <20210304103858.38400-2-dgilbert@redhat.com> In-Reply-To: <20210304103858.38400-1-dgilbert@redhat.com> References: <20210304103858.38400-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dgilbert@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=216.205.24.124; envelope-from=dgilbert@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: mcascell@redhat.com, stefanha@redhat.com, vgoyal@redhat.com 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" From: "Dr. David Alan Gilbert" On Linux, the 'security.capability' xattr holds a set of capabilities that can change when an executable is run, giving a limited form of privilege escalation to those programs that the writer of the file deemed worthy. Any write causes the 'security.capability' xattr to be dropped, stopping anyone from gaining privilege by modifying a blessed file. Fuse relies on the daemon to do this dropping, and in turn the daemon relies on the host kernel to drop the xattr for it. However, with the addition of -o xattrmap, the xattr that the guest stores its capabilities in is now not the same as the one that the host kernel automatically clears. Where the mapping changes 'security.capability', explicitly clear the remapped name to preserve the same behaviour. This bug is assigned CVE-2021-20263. Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Vivek Goyal --- docs/tools/virtiofsd.rst | 4 ++ tools/virtiofsd/passthrough_ll.c | 77 +++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/docs/tools/virtiofsd.rst b/docs/tools/virtiofsd.rst index 866b7db3ee..00554c75bd 100644 --- a/docs/tools/virtiofsd.rst +++ b/docs/tools/virtiofsd.rst @@ -228,6 +228,10 @@ The 'map' type adds a number of separate rules to add = **prepend** as a prefix to the matched **key** (or all attributes if **key** is empty). There may be at most one 'map' rule and it must be the last rule in the se= t. =20 +Note: When the 'security.capability' xattr is remapped, the daemon has to = do +extra work to remove it during many operations, which the host kernel norm= ally +does itself. + xattr-mapping Examples ---------------------- =20 diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough= _ll.c index 58d24c0010..fc7e1b1e8e 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -148,6 +148,7 @@ struct lo_data { int posix_lock; int xattr; char *xattrmap; + char *xattr_security_capability; char *source; char *modcaps; double timeout; @@ -217,6 +218,8 @@ static __thread bool cap_loaded =3D 0; =20 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st, uint64_t mnt_id); +static int xattr_map_client(const struct lo_data *lo, const char *client_n= ame, + char **out_name); =20 static int is_dot_or_dotdot(const char *name) { @@ -356,6 +359,37 @@ out: return ret; } =20 +/* + * The host kernel normally drops security.capability xattr's on + * any write, however if we're remapping xattr names we need to drop + * whatever the clients security.capability is actually stored as. + */ +static int drop_security_capability(const struct lo_data *lo, int fd) +{ + if (!lo->xattr_security_capability) { + /* We didn't remap the name, let the host kernel do it */ + return 0; + } + if (!fremovexattr(fd, lo->xattr_security_capability)) { + /* All good */ + return 0; + } + + switch (errno) { + case ENODATA: + /* Attribute didn't exist, that's fine */ + return 0; + + case ENOTSUP: + /* FS didn't support attribute anyway, also fine */ + return 0; + + default: + /* Hmm other error */ + return errno; + } +} + static void lo_map_init(struct lo_map *map) { map->elems =3D NULL; @@ -737,6 +771,11 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino,= struct stat *attr, uid_t uid =3D (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)= -1; gid_t gid =3D (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)= -1; =20 + saverr =3D drop_security_capability(lo, ifd); + if (saverr) { + goto out_err; + } + res =3D fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOF= OLLOW); if (res =3D=3D -1) { saverr =3D errno; @@ -759,6 +798,14 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino,= struct stat *attr, } } =20 + saverr =3D drop_security_capability(lo, truncfd); + if (saverr) { + if (!fi) { + close(truncfd); + } + goto out_err; + } + if (kill_suidgid) { res =3D drop_effective_cap("FSETID", &cap_fsetid_dropped); if (res !=3D 0) { @@ -1784,6 +1831,13 @@ static int lo_do_open(struct lo_data *lo, struct lo_= inode *inode, if (fd < 0) { return -fd; } + if (fi->flags & (O_TRUNC)) { + int err =3D drop_security_capability(lo, fd); + if (err) { + close(fd); + return err; + } + } } =20 pthread_mutex_lock(&lo->mutex); @@ -2191,6 +2245,12 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t = ino, "lo_write_buf(ino=3D%" PRIu64 ", size=3D%zd, off=3D%lu kill_p= riv=3D%d)\n", ino, out_buf.buf[0].size, (unsigned long)off, fi->kill_priv); =20 + res =3D drop_security_capability(lo_data(req), out_buf.buf[0].fd); + if (res) { + fuse_reply_err(req, res); + return; + } + /* * If kill_priv is set, drop CAP_FSETID which should lead to kernel * clearing setuid/setgid on file. Note, for WRITE, we need to do @@ -2432,6 +2492,7 @@ static void parse_xattrmap(struct lo_data *lo) { const char *map =3D lo->xattrmap; const char *tmp; + int ret; =20 lo->xattr_map_nentries =3D 0; while (*map) { @@ -2462,7 +2523,7 @@ static void parse_xattrmap(struct lo_data *lo) * the last entry. */ parse_xattrmap_map(lo, map, sep); - return; + break; } else { fuse_log(FUSE_LOG_ERR, "%s: Unexpected type;" @@ -2531,6 +2592,19 @@ static void parse_xattrmap(struct lo_data *lo) fuse_log(FUSE_LOG_ERR, "Empty xattr map\n"); exit(1); } + + ret =3D xattr_map_client(lo, "security.capability", + &lo->xattr_security_capability); + if (ret) { + fuse_log(FUSE_LOG_ERR, "Failed to map security.capability: %s\n", + strerror(ret)); + exit(1); + } + if (!strcmp(lo->xattr_security_capability, "security.capability")) { + /* 1-1 mapping, don't need to do anything */ + free(lo->xattr_security_capability); + lo->xattr_security_capability =3D NULL; + } } =20 /* @@ -3588,6 +3662,7 @@ static void fuse_lo_data_cleanup(struct lo_data *lo) =20 free(lo->xattrmap); free_xattrmap(lo); + free(lo->xattr_security_capability); free(lo->source); } =20 --=20 2.29.2