Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp643012pxb; Mon, 25 Oct 2021 15:41:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0UOnkj3CH7EzPddURv7CcmyhWbDyAVejORoll2kG/FcpD7RWmhrBsSmx/zWQZ3L8g90g+ X-Received: by 2002:a05:6a00:10d2:b0:44d:f03e:46c7 with SMTP id d18-20020a056a0010d200b0044df03e46c7mr21303695pfu.0.1635201664468; Mon, 25 Oct 2021 15:41:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635201664; cv=none; d=google.com; s=arc-20160816; b=eWJ6sfTMU9BgIj+yF8WrEmcntmfbFyC2xiAgBTMJYk4pdIS2iiUlgmjqHtbsxxVXxm po5SwUxY/0sGFtmpus0I1U+QwmJ3ds5xJ8fs04DllP9mkfLkiDeGdActeNnuV55sRvlx JVdO8LP+qOUK/h3xPjzZ+Q8FMF1er+3ZmYj4DwukHkIsnqUTlLKkCkpYc7w6cIaAtwWX 86rCXns7jGnAsfo4I9m4ToUAPZY9FJrDhhx66SCvd0oZGaE+2RtzDu5e0tDkYMR/S/H8 wIJFiksVpqZPZ2PvahgnkxYlFnPd5j30rjKk5+CzagOi9grgpbqkXGuSvZ5fF4SUlxNv 3FgA== 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=taEED1jZWkVN0WXY4SiigdsDjCh38H5xmdhBqSHhbHs=; b=VV8H/NvaUnGjhpAR7EZiQ0jcAmezvRKI4deSEVw4ubQgwUUJJjhT19f3MszQHRlL2P YY0M483WhQh5BPy99Pc+n2t1D22gvIzv4DwcodRXhkF8Z+EioShZZ9Nh+Q8TEaDjfJ0K j9nmYbgX3e6JXIeJpYyWxLOcAneuMQ9+KcSKxKVLqhoMbkL5N8FTjJKVYI/Ymxvtu8tJ tqhwcB4xI08uiAUiMo3V2pPWGUOLz2QV3DWLQWl1u6KHss0mMYwe8qR0VcZBGE+5Gm59 Iooif2kwYIdLN44hrAgLoRtPSv3zVgS6FkKMd3RBC597yNKafk0KpVbABrl1DX3r5rVK so5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Rh77+fDo; 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 m14si114976pgb.43.2021.10.25.15.40.47; Mon, 25 Oct 2021 15:41:04 -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=Rh77+fDo; 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 S235145AbhJYT07 (ORCPT + 99 others); Mon, 25 Oct 2021 15:26:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:40718 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235043AbhJYTZJ (ORCPT ); Mon, 25 Oct 2021 15:25:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2704860FE8; Mon, 25 Oct 2021 19:22:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635189738; bh=xQH6xbKIA9JfXpgVrSV2Ie+26/isb91g+KzUq6/V17g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rh77+fDoP86grwXiF5SXWUNQGQPO2UxaLM1swlNizm/rKNzK/scb/XSfVhE1EbvA6 jekaFOnc6itIH0S9N7unGebyOL5KrsiQ7PMQ8Ny/Zas5Xcw0lIvsqdSslH0EHyKCg/ l6c+qoTKx+KJdXEmVcb6qAGBjMPBfmTt6VTXDOZI= 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.14 14/30] vfs: check fd has read access in kernel_read_file_from_fd() Date: Mon, 25 Oct 2021 21:14:34 +0200 Message-Id: <20211025190926.501953774@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025190922.089277904@linuxfoundation.org> References: <20211025190922.089277904@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 @@ -980,7 +980,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);