From nobody Fri May 17 03:01:01 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=1611746578; cv=none; d=zohomail.com; s=zohoarc; b=gmevGaoN5SuEUvPgXQFY2K5QplI2jdgMptGdq98dH1463yRup4uemw/7+IMxPjR/mR7btobx4HlxRCwzFhfi7KNlKebJNGb4kezsI1YLU2qiHnZXI1/0oE3QVgQJKwhch+xXX+X86XQB2tvs5hIMai9LFYaRSwQKnVwgK8+ub5U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611746578; h=Content-Type: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=7/rJJlqF9YgJdNiH6uHzkbQV4PUCaAWgBmA2hQbtM9Q=; b=E/u8mOHHbz2KxYAJN+WDctXI7o2qdrgAjxWZYAJbUXdK99UTf/1He0MwYFklrL/wfCCeolFy6IRCKSomAKcoSJo0JDXpUvVrzPguCARdaxbqSNMfe/l1icNBW+hSsQ/Ji/soZMqZE0wzuI23TdnVWvGa3zVqe47MwsJ/mmXGBcc= 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 161174657807841.7696510343759; Wed, 27 Jan 2021 03:22:58 -0800 (PST) Received: from localhost ([::1]:36880 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l4iuW-0003tG-1x for importer@patchew.org; Wed, 27 Jan 2021 06:22:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46164) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l4itS-0002zz-T7 for qemu-devel@nongnu.org; Wed, 27 Jan 2021 06:21:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:27859) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l4itN-0006kO-3c for qemu-devel@nongnu.org; Wed, 27 Jan 2021 06:21:49 -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-552-qcEJkwPzPxuXnzr5PP2iUA-1; Wed, 27 Jan 2021 06:21:40 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A7AF91015959; Wed, 27 Jan 2021 11:21:33 +0000 (UTC) Received: from localhost (ovpn-115-15.ams2.redhat.com [10.36.115.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 01D655D766; Wed, 27 Jan 2021 11:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611746503; 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; bh=7/rJJlqF9YgJdNiH6uHzkbQV4PUCaAWgBmA2hQbtM9Q=; b=cOk5bUnBlOZLlxszmxQV8VXTxa0zLEKy9cFGfmh3T6RMAs700/nlQ7F1inIO6NXA0ws2t6 kOD3jXw+DMj/LJDYOEo2Mz+jRx4sNMJnZExYx0BLfuey1LJXtrPN6VYfi9FWHyG5tHAfTM zhIjzsF8p8omoQ42SL+1Nxl1guNJWmw= X-MC-Unique: qcEJkwPzPxuXnzr5PP2iUA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH v3] virtiofsd: prevent opening of special files (CVE-2020-35517) Date: Wed, 27 Jan 2021 11:21:31 +0000 Message-Id: <20210127112131.308451-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@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=63.128.21.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.255, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=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: mszeredi@redhat.com, Daniel Berrange , slp@redhat.com, Greg Kurz , "Dr. David Alan Gilbert" , virtio-fs@redhat.com, Alex Xu , Stefan Hajnoczi , P J P , Laszlo Ersek , 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" A well-behaved FUSE client does not attempt to open special files with FUSE_OPEN because they are handled on the client side (e.g. device nodes are handled by client-side device drivers). The check to prevent virtiofsd from opening special files is missing in a few cases, most notably FUSE_OPEN. A malicious client can cause virtiofsd to open a device node, potentially allowing the guest to escape. This can be exploited by a modified guest device driver. It is not exploitable from guest userspace since the guest kernel will handle special files inside the guest instead of sending FUSE requests. This patch adds the missing checks to virtiofsd. This is a short-term solution because it does not prevent a compromised virtiofsd process from opening device nodes on the host. Reported-by: Alex Xu Fixes: CVE-2020-35517 Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Vivek Goyal Signed-off-by: Stefan Hajnoczi --- v3: * Protect lo_create() [Greg] v2: * Add doc comment clarifying that symlinks are traversed client-side [Daniel] This issue was diagnosed on public IRC and is therefore already known and not embargoed. A stronger fix, and the long-term solution, is for users to mount the shared directory and any sub-mounts with nodev, as well as nosuid and noexec. Unfortunately virtiofsd cannot do this automatically because bind mounts added by the user after virtiofsd has launched would not be detected. I suggest the following: 1. Modify libvirt and Kata Containers to explicitly set these mount options. 2. Then modify virtiofsd to check that the shared directory has the necessary options at startup. Refuse to start if the options are missing so that the user is aware of the security requirements. As a bonus this also increases the likelihood that other host processes besides virtiofsd will be protected by nosuid/noexec/nodev so that a malicious guest cannot drop these files in place and then arrange for a host process to come across them. Additionally, user namespaces have been discussed. They seem like a worthwhile addition as an unprivileged or privilege-separated mode although there are limitations with respect to security xattrs and the actual uid/gid stored on the host file system not corresponding to the guest uid/gid. --- tools/virtiofsd/passthrough_ll.c | 104 ++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 30 deletions(-) diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough= _ll.c index 5fb36d9407..054ad439a5 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -555,6 +555,30 @@ static int lo_fd(fuse_req_t req, fuse_ino_t ino) return fd; } =20 +/* + * Open a file descriptor for an inode. Returns -EBADF if the inode is not= a + * regular file or a directory. Use this helper function instead of raw + * openat(2) to prevent security issues when a malicious client opens spec= ial + * files such as block device nodes. Symlink inodes are also rejected since + * symlinks must already have been traversed on the client side. + */ +static int lo_inode_open(struct lo_data *lo, struct lo_inode *inode, + int open_flags) +{ + g_autofree char *fd_str =3D g_strdup_printf("%d", inode->fd); + int fd; + + if (!S_ISREG(inode->filetype) && !S_ISDIR(inode->filetype)) { + return -EBADF; + } + + fd =3D openat(lo->proc_self_fd, fd_str, open_flags); + if (fd < 0) { + return -errno; + } + return fd; +} + static void lo_init(void *userdata, struct fuse_conn_info *conn) { struct lo_data *lo =3D (struct lo_data *)userdata; @@ -684,8 +708,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, = struct stat *attr, if (fi) { truncfd =3D fd; } else { - sprintf(procname, "%i", ifd); - truncfd =3D openat(lo->proc_self_fd, procname, O_RDWR); + truncfd =3D lo_inode_open(lo, inode, O_RDWR); if (truncfd < 0) { goto out_err; } @@ -1654,9 +1677,11 @@ static void update_open_flags(int writeback, int all= ow_direct_io, static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name, mode_t mode, struct fuse_file_info *fi) { + int open_flags =3D (fi->flags | O_CREAT) & ~O_NOFOLLOW; int fd; struct lo_data *lo =3D lo_data(req); struct lo_inode *parent_inode; + struct lo_inode *existing_inode =3D NULL; struct fuse_entry_param e; int err; struct lo_cred old =3D {}; @@ -1682,11 +1707,23 @@ static void lo_create(fuse_req_t req, fuse_ino_t pa= rent, const char *name, =20 update_open_flags(lo->writeback, lo->allow_direct_io, fi); =20 - fd =3D openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOL= LOW, - mode); + /* First, try to create a new file but don't open existing files */ + fd =3D openat(parent_inode->fd, name, open_flags | O_EXCL, mode); err =3D fd =3D=3D -1 ? errno : 0; + lo_restore_cred(&old); =20 + /* Second, open existing files if O_EXCL was not specified */ + if (err =3D=3D EEXIST && !(fi->flags & O_EXCL)) { + existing_inode =3D lookup_name(req, parent, name); + if (existing_inode) { + fd =3D lo_inode_open(lo, existing_inode, open_flags); + if (fd < 0) { + err =3D -fd; + } + } + } + if (!err) { ssize_t fh; =20 @@ -1709,6 +1746,7 @@ static void lo_create(fuse_req_t req, fuse_ino_t pare= nt, const char *name, } =20 out: + lo_inode_put(lo, &existing_inode); lo_inode_put(lo, &parent_inode); =20 if (err) { @@ -1725,7 +1763,6 @@ static struct lo_inode_plock *lookup_create_plock_ctx= (struct lo_data *lo, pid_t pid, int *err) { struct lo_inode_plock *plock; - char procname[64]; int fd; =20 plock =3D @@ -1742,12 +1779,10 @@ static struct lo_inode_plock *lookup_create_plock_c= tx(struct lo_data *lo, } =20 /* Open another instance of file which can be used for ofd locks. */ - sprintf(procname, "%i", inode->fd); - /* TODO: What if file is not writable? */ - fd =3D openat(lo->proc_self_fd, procname, O_RDWR); - if (fd =3D=3D -1) { - *err =3D errno; + fd =3D lo_inode_open(lo, inode, O_RDWR); + if (fd < 0) { + *err =3D -fd; free(plock); return NULL; } @@ -1894,18 +1929,24 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino,= struct fuse_file_info *fi) { int fd; ssize_t fh; - char buf[64]; struct lo_data *lo =3D lo_data(req); + struct lo_inode *inode =3D lo_inode(req, ino); =20 fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=3D%" PRIu64 ", flags=3D%d)\n", i= no, fi->flags); =20 + if (!inode) { + fuse_reply_err(req, EBADF); + return; + } + update_open_flags(lo->writeback, lo->allow_direct_io, fi); =20 - sprintf(buf, "%i", lo_fd(req, ino)); - fd =3D openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW); - if (fd =3D=3D -1) { - return (void)fuse_reply_err(req, errno); + fd =3D lo_inode_open(lo, inode, fi->flags & ~O_NOFOLLOW); + if (fd < 0) { + lo_inode_put(lo, &inode); + fuse_reply_err(req, -fd); + return; } =20 pthread_mutex_lock(&lo->mutex); @@ -1913,6 +1954,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, s= truct fuse_file_info *fi) pthread_mutex_unlock(&lo->mutex); if (fh =3D=3D -1) { close(fd); + lo_inode_put(lo, &inode); fuse_reply_err(req, ENOMEM); return; } @@ -1923,6 +1965,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, s= truct fuse_file_info *fi) } else if (lo->cache =3D=3D CACHE_ALWAYS) { fi->keep_cache =3D 1; } + lo_inode_put(lo, &inode); fuse_reply_open(req, fi); } =20 @@ -1982,39 +2025,40 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino= , struct fuse_file_info *fi) static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info *fi) { + struct lo_inode *inode =3D lo_inode(req, ino); + struct lo_data *lo =3D lo_data(req); int res; int fd; - char *buf; =20 fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=3D%" PRIu64 ", fi=3D0x%p)\n", i= no, (void *)fi); =20 + if (!inode) { + fuse_reply_err(req, EBADF); + return; + } + if (!fi) { - struct lo_data *lo =3D lo_data(req); - - res =3D asprintf(&buf, "%i", lo_fd(req, ino)); - if (res =3D=3D -1) { - return (void)fuse_reply_err(req, errno); - } - - fd =3D openat(lo->proc_self_fd, buf, O_RDWR); - free(buf); - if (fd =3D=3D -1) { - return (void)fuse_reply_err(req, errno); + fd =3D lo_inode_open(lo, inode, O_RDWR); + if (fd < 0) { + res =3D -fd; + goto out; } } else { fd =3D lo_fi_fd(req, fi); } =20 if (datasync) { - res =3D fdatasync(fd); + res =3D fdatasync(fd) =3D=3D -1 ? errno : 0; } else { - res =3D fsync(fd); + res =3D fsync(fd) =3D=3D -1 ? errno : 0; } if (!fi) { close(fd); } - fuse_reply_err(req, res =3D=3D -1 ? errno : 0); +out: + lo_inode_put(lo, &inode); + fuse_reply_err(req, res); } =20 static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off= set, --=20 2.29.2