Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp677437pxb; Mon, 25 Oct 2021 16:29:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzM3+AyFJ21UETTQbql+6Kl+5QdcbW4ud9hj9Jb7B7lP+l29zqlHSvvgm13aPDqriZFU7Pf X-Received: by 2002:a50:8e05:: with SMTP id 5mr29890701edw.76.1635204556157; Mon, 25 Oct 2021 16:29:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635204556; cv=none; d=google.com; s=arc-20160816; b=zjWm4PqIcBghJ55bCzjN/RIRCX8pYRF+60ofyFMHkYCdmQN3bwyX2ZuIOrg2dqGza5 vHWfq70KQairY9BvIUYeDlrQ662MGss3Y4uyhDXJBRsLzu4wRsIAzVypct84iXtXF9QZ pJ47k45O3p2jgqUXeUYY/Mi5MfSkQ5Cf8y6G1JsSMbzhX7BDIPCU1Jz243/0RDgErrSO QXxblp2US5ID4XI5xj/71rLmtE4otpxpMB+fbCiKapZ7XhBm3UaqPZarhDZNLokF4VXu yK+D5HoaWEXWO6wdaO5k+QeKqB/hb3uJaIxGFLbfb4gGsI9m2B6hak++ERE2apiKqj36 7sGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6pa+DsF9h4QaG3f0oFnRS+9pv8rYYbvaLUgXA69PtGQ=; b=SMY4zq7B2BopDFLqaFPKe8Lzs+mMC0IW2RsgCRASXuvwKGzMOMPIvm/1LMHQGA4Il9 eS1fK2Yi1d79urvYxU/Yi9jXHouY/sLfzDhWOXvSQ+Q9h3+eLy8KfG01ZjgRTCh285su DORQT6wbmcctQ9amPqbtv+/BOMnzLCPjixLGyNcedLXbjh34OpGE2+c47UWFFBoyPhGi elbtmL8hYI+kZl9AGCkE61W2jokyvYYAPUVWmLGi2JYwLy9X/ZRQu4JbKzpR2f5gscp0 vY7weX97TOkUe4ahumjBTDTqYmauvNCm+VIr4bgABe5ClD7+voF8P64HGiaCPwScD/ec nlQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=njaqvQ6j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p5si22584766edi.454.2021.10.25.16.28.52; Mon, 25 Oct 2021 16:29:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=njaqvQ6j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235053AbhJYTaH (ORCPT + 99 others); Mon, 25 Oct 2021 15:30:07 -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);