From patchwork Tue Nov 16 19:42:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thadeu Lima de Souza Cascardo X-Patchwork-Id: 1556102 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=kVwnQp1t; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HvDhp6t0yz9s1l for ; Wed, 17 Nov 2021 18:15:06 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1mnF9l-0005Cj-Rw; Wed, 17 Nov 2021 07:14:57 +0000 Received: from smtp-relay-canonical-1.internal ([10.131.114.174] helo=smtp-relay-canonical-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1mn4Lo-0007sG-TS for kernel-team@lists.ubuntu.com; Tue, 16 Nov 2021 19:42:40 +0000 Received: from mussarela.. (201-43-193-232.dsl.telesp.net.br [201.43.193.232]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id 6DFB8419B8; Tue, 16 Nov 2021 19:42:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1637091760; bh=kzcLOjg5kb6IxTXUpScjR66bWU9pWYlU9H7WOk3tx/A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kVwnQp1tWDcIeL3p45+jAHOR5xj+YeQOkEfCx2+Kd6T+S0p+alTZVFmGxuT+6eFT/ WvSi+9Ow/reSL2Z+g+4ClF3joGmSU4bufxT65P/I0dFF5Rd1mq6HbtoILs22hb61A8 dnwt15r0DWt80L6zsjrpNSOoHSvLLV68XnhHIqkUxdAw4S56+kdeZsEWo9/itZnOD8 nXjvpC3GS4197Va0j7ysc2tX/+us/J482VIocB96s2/0w/Vq+pCY7BGmI+W0/hoS3c +iPmP7BDHhY2JE0vTY5jJP0xxLsegrgRrUsKASjO9UR8vL/bgsJFX1GJ0i7SYfb1xD x5apY399xYhqA== From: Thadeu Lima de Souza Cascardo To: kernel-team@lists.ubuntu.com Subject: [SRU Focal/Bionic] vfs: check fd has read access in kernel_read_file_from_fd() Date: Tue, 16 Nov 2021 16:42:17 -0300 Message-Id: <20211116194217.481966-3-cascardo@canonical.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211116194217.481966-1-cascardo@canonical.com> References: <20211116194217.481966-1-cascardo@canonical.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 17 Nov 2021 07:14:54 +0000 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thadeu Lima de Souza Cascardo , Kees Cook , Greg Kroah-Hartman , Mimi Zohar , Hao Sun , "Matthew Wilcox \(Oracle\)" , stable@vger.kernel.org, Linus Torvalds , Andrew Morton , Al Viro Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: "Matthew Wilcox (Oracle)" BugLink: https://bugs.launchpad.net/bugs/1950644 commit 032146cda85566abcd1c4884d9d23e4e30a07e9a upstream. If we open a file without read access and then pass the fd to a syscall whose implementation calls kernel_read_file_from_fd(), we get a warning from __kernel_read(): if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ))) This currently affects both finit_module() and kexec_file_load(), but it could affect other syscalls in the future. Link: https://lkml.kernel.org/r/20211007220110.600005-1-willy@infradead.org Fixes: b844f0ecbc56 ("vfs: define kernel_copy_file_from_fd()") Signed-off-by: Matthew Wilcox (Oracle) Reported-by: Hao Sun Reviewed-by: Kees Cook Acked-by: Christian Brauner Cc: Al Viro Cc: Mimi Zohar Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman (cherry picked from commit 0f218ba4c8aac7041cd8b81a5a893b0d121e6316 linux-5.4.y) Signed-off-by: Thadeu Lima de Souza Cascardo Acked-by: Stefan Bader --- fs/exec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index eeba096e8a38..006f7fb40b96 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1000,7 +1000,7 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size, struct fd f = fdget(fd); int ret = -EBADF; - if (!f.file) + if (!f.file || !(f.file->f_mode & FMODE_READ)) goto out; ret = kernel_read_file(f.file, buf, size, max_size, id);