From nobody Sat Apr 5 16:12:51 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1738051179587697.7316335779004; Mon, 27 Jan 2025 23:59:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tcgUS-0007cC-JX; Tue, 28 Jan 2025 02:58:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tcgUA-0007Pz-LI; Tue, 28 Jan 2025 02:58:18 -0500 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tcgU8-0000uQ-O5; Tue, 28 Jan 2025 02:58:14 -0500 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id B4909E1AF8; Tue, 28 Jan 2025 10:54:25 +0300 (MSK) Received: from localhost.tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with ESMTP id 2F9AA1A62DF; Tue, 28 Jan 2025 10:54:51 +0300 (MSK) Received: by localhost.tls.msk.ru (Postfix, from userid 1000) id E920252055; Tue, 28 Jan 2025 10:54:50 +0300 (MSK) To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Christian Schoenebeck , Dirk Herrendorfer , Greg Kurz , Michael Tokarev Subject: [Stable-8.2.9 23/45] 9pfs: fix regression regarding CVE-2023-2861 Date: Mon, 27 Jan 2025 23:26:04 +0300 Message-Id: <20250127202630.3724367-23-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.5 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Tokarev 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=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -53 X-Spam_score: -5.4 X-Spam_bar: ----- X-Spam_report: (-5.4 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_06_12=1.543, RCVD_IN_DNSWL_HI=-5, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1738051180813019000 Content-Type: text/plain; charset="utf-8" The released fix for this CVE: f6b0de53fb8 ("9pfs: prevent opening special files (CVE-2023-2861)") caused a regression with security_model=3Dpassthrough. When handling a 'Tmknod' request there was a side effect that 'Tmknod' request could fail as 9p server was trying to adjust permissions: #6 close_if_special_file (fd=3D30) at ../hw/9pfs/9p-util.h:140 #7 openat_file (mode=3D, flags=3D2228224, name=3D, dirfd=3D) at ../hw/9pfs/9p-util.h:181 #8 fchmodat_nofollow (dirfd=3Ddirfd@entry=3D31, name=3Dname@entry=3D0x5555577ea6e0 "mysocket", mode=3D493) at ../hw/9pfs/9p-local.c:360 #9 local_set_cred_passthrough (credp=3D0x7ffbbc4ace10, name=3D0x5555577e= a6e0 "mysocket", dirfd=3D31, fs_ctx=3D0x55555811f528) at ../hw/9pfs/9p-local.c:457 #10 local_mknod (fs_ctx=3D0x55555811f528, dir_path=3D, name=3D0x5555577ea6e0 "mysocket", credp=3D0x7ffbbc4ace10) at ../hw/9pfs/9p-local.c:702 #11 v9fs_co_mknod (pdu=3Dpdu@entry=3D0x555558121140, fidp=3Dfidp@entry=3D0x5555574c46c0, name=3Dname@entry=3D0x7ffbbc4aced= 0, uid=3D1000, gid=3D1000, dev=3D, mode=3D49645, stbuf=3D0x7ffbbc4acef0) at ../hw/9pfs/cofs.c:205 #12 v9fs_mknod (opaque=3D0x555558121140) at ../hw/9pfs/9p.c:3711 That's because server was opening the special file to adjust permissions, however it was using O_PATH and it would have not returned the file descriptor to guest. So the call to close_if_special_file() on that branch was incorrect. Let's lift the restriction introduced by f6b0de53fb8 such that it would allow to open special files on host if O_PATH flag is supplied, not only for 9p server's own operations as described above, but also for any client 'Topen' request. It is safe to allow opening special files with O_PATH on host, because O_PATH only allows path based operations on the resulting file descriptor and prevents I/O such as read() and write() on that file descriptor. Fixes: f6b0de53fb8 ("9pfs: prevent opening special files (CVE-2023-2861)") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2337 Reported-by: Dirk Herrendorfer Signed-off-by: Christian Schoenebeck Reviewed-by: Greg Kurz Tested-by: Dirk Herrendorfer Message-Id: (cherry picked from commit d06a9d843fb65351e0e4dc42ba0c404f01ea92b3) Signed-off-by: Michael Tokarev diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h index 51c94b0116..95ee4da9bd 100644 --- a/hw/9pfs/9p-util.h +++ b/hw/9pfs/9p-util.h @@ -177,20 +177,27 @@ again: return -1; } =20 - if (close_if_special_file(fd) < 0) { - return -1; - } - - serrno =3D errno; - /* O_NONBLOCK was only needed to open the file. Let's drop it. We don't - * do that with O_PATH since fcntl(F_SETFL) isn't supported, and opena= t() - * ignored it anyway. - */ + /* Only if O_PATH is not set ... */ if (!(flags & O_PATH_9P_UTIL)) { + /* + * Prevent I/O on special files (device files, etc.) on host side, + * however it is safe and required to allow opening them with O_PA= TH, + * as this is limited to (required) path based operations only. + */ + if (close_if_special_file(fd) < 0) { + return -1; + } + + serrno =3D errno; + /* + * O_NONBLOCK was only needed to open the file. Let's drop it. We = don't + * do that with O_PATH since fcntl(F_SETFL) isn't supported, and + * openat() ignored it anyway. + */ ret =3D fcntl(fd, F_SETFL, flags); assert(!ret); + errno =3D serrno; } - errno =3D serrno; return fd; } =20 --=20 2.39.5