Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp726241pxb; Mon, 25 Oct 2021 17:33:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOr7dxYDRLDvPfKzK98/xotrFwu0xoaohdL+NgR0iZE5LvM3N1VzkM05igiq4GlPbV8vUd X-Received: by 2002:a17:902:e851:b0:140:56ff:3e88 with SMTP id t17-20020a170902e85100b0014056ff3e88mr8609892plg.82.1635208402879; Mon, 25 Oct 2021 17:33:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635208402; cv=none; d=google.com; s=arc-20160816; b=TVkeq7jS1M4qR4pXmi2P+nZbPjFrzQuzuYrCkXEBBr2Mj6BMGtj/W79zcADGhPrMFA c4y6aJoX7Gab8RoMhST7rp78FzEPDtiNzUcauUbTAMlNB+mz1vGR5Hk/RBQvuLIR3BMN ZPSic1QHcQh+IHO4qQ//WHfY13dw4ZwPGmMo63TwC7xRl+qSdJvP/NlKiYTJ/3ki9EMT 9P7of3X7w+1dCDa5UJM7/ng7JrqAtDNyFKeiEStpxqei6rbdsrnZjDB1Us63GMShyE32 DXdlKNcxsZUHiIyBXsDjMvpgwFfuJALhd0mHvkNK0edRIyV1Zm4c2N9f/i5sLrhLPkeH Psag== 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=vwbmIzKIhIm4dxtnrPecdARqzBDzluZewZ0Sg40aGfo=; b=RJeuFAOFEhrRS6YYluSqi31tz17EjtlEOlkrV57d7jGxfWHhguTzAkQFUHISZCP2oH pgcuL79sbXPW0pTBumODdatslUJok2J5Ri3t2lsAoSKqU9K1MIN0mxW/aWAnvsBIAgAU ey5Q54zoOCkNMiKwpfAzBNONTQrn6OfEhpUCmuG9/KPRWcmgExbMHvA9EY/mMJfettvL Zg9j2VHLzBSERLSaOQLThfCq43VkhnYK78Y29yTqlX4NrSmDtYtmF2zcdo3BBei46neU MoATcHJ1HSPNuqDepucL106CUXEwCvDHTjYjjjms1HPe2u3unlhe+GkS7TX62lyLgkkB irKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="1/96avNC"; 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 m9si25528662pgv.403.2021.10.25.17.33.09; Mon, 25 Oct 2021 17:33:22 -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="1/96avNC"; 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 S237672AbhJYTzb (ORCPT + 99 others); Mon, 25 Oct 2021 15:55:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:37362 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238601AbhJYTtz (ORCPT ); Mon, 25 Oct 2021 15:49:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 410F560C49; Mon, 25 Oct 2021 19:42:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635190925; bh=O0h+dirvFotMv2Za8enP7HDUcXnoF5nYPQtG5ozqOvM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1/96avNCF1wC8W8+Zhd5kbYKL9QwfvLJj7U41ekvzp2SUAorsuzddpqK6YABFFSTF z1mEQ+TCv3TOxKpbpel1PXny/PTz2fW315BJV4lB+779aG7PG4YLsYA5852Hqh49yA J5lkgN66C6D0sec84UXvBvKuu+auDMss/oxGw8jU= 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 5.14 084/169] vfs: check fd has read access in kernel_read_file_from_fd() Date: Mon, 25 Oct 2021 21:14:25 +0200 Message-Id: <20211025191027.909179636@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025191017.756020307@linuxfoundation.org> References: <20211025191017.756020307@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/kernel_read_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/fs/kernel_read_file.c +++ b/fs/kernel_read_file.c @@ -178,7 +178,7 @@ int kernel_read_file_from_fd(int fd, lof 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, offset, buf, buf_size, file_size, id);