From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29344C433FE for ; Mon, 25 Oct 2021 19:27:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1292B610F8 for ; Mon, 25 Oct 2021 19:27:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233544AbhJYTaJ (ORCPT ); Mon, 25 Oct 2021 15:30:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:41026 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235318AbhJYT1U (ORCPT ); Mon, 25 Oct 2021 15:27:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DE5DA610FD; Mon, 25 Oct 2021 19:23:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635189831; bh=hQJPHq4qgyQXrxad6e6ocRLz8J9mShDe681FBv+TYfc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=njaqvQ6juZubVF72avRRgbl5sfPraEo80CMf4RqkW8F9gO8QEgpQHFscBvThD4+m5 8ztCy2zBjqdondf1cuU4EVwKrOFFUrwCXsq1ZaUyQh7RnwpSkg2YhOB9KrS7CXcAwm pziJT5hcI31CX73Px+0s8znQKJqS4f0Yc6EQkK70= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Matthew Wilcox (Oracle)" , Hao Sun , Kees Cook , Christian Brauner , Al Viro , Mimi Zohar , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 18/37] vfs: check fd has read access in kernel_read_file_from_fd() Date: Mon, 25 Oct 2021 21:14:43 +0200 Message-Id: <20211025190931.974851341@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025190926.680827862@linuxfoundation.org> References: <20211025190926.680827862@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox (Oracle) 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 --- fs/exec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/fs/exec.c +++ b/fs/exec.c @@ -984,7 +984,7 @@ int kernel_read_file_from_fd(int fd, voi 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);