From patchwork Fri May 8 09:22:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536129 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 65288913 for ; Fri, 8 May 2020 09:23:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 41AFE21582 for ; Fri, 8 May 2020 09:23:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="O9BYEco6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726843AbgEHJW2 (ORCPT ); Fri, 8 May 2020 05:22:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726746AbgEHJW1 (ORCPT ); Fri, 8 May 2020 05:22:27 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA2C6C05BD43; Fri, 8 May 2020 02:22:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=xPhGP2jiYAjHpxSk/ggsWDYG1ptmeqnodXQwagIjz80=; b=O9BYEco6eWqsAs1PWLYmxNpjmD PoBKDzzLadPZjumipcrGt30j682jPS2N/LZPgAjG+d9zE7N64mb9uCLbtiC1w0iSlPNe4pHCebO9J ucCuOlaBfVYZPQ9xMintsgxB3rniNoIJPGggd5MGpbFnGdD8R0JnTVnRa9bt/5C/5036+UID/T3mk dS69zQ2VmCyxnm2Kk6pB3iTZDO3ddwPfeHSrh6Vhj/gNcSUJpttTphD4m9/pRK0ZKO5mdeBscV8AH Ow2hy6r/9QNcOKCd/xVixYFQcSeOEWmZ7zZK+tLbWQE+kZ2ueXryc5miatQD0NAdu+j0jmzAvzqjH 5Gn3wVYQ==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzD9-0008Ii-AY; Fri, 08 May 2020 09:22:27 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 01/11] fs: call file_{start,end}_write from __kernel_write Date: Fri, 8 May 2020 11:22:12 +0200 Message-Id: <20200508092222.2097-2-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: We always need to take a reference on the file system we are writing to. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/read_write.c b/fs/read_write.c index bbfa9b12b15eb..d5aaf3a4198b9 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -508,6 +508,7 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t if (!(file->f_mode & FMODE_CAN_WRITE)) return -EINVAL; + file_start_write(file); old_fs = get_fs(); set_fs(KERNEL_DS); p = (__force const char __user *)buf; @@ -520,6 +521,7 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t add_wchar(current, ret); } inc_syscw(current); + file_end_write(file); return ret; } EXPORT_SYMBOL(__kernel_write); From patchwork Fri May 8 09:22:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536125 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1D25C913 for ; Fri, 8 May 2020 09:23:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F1499208DB for ; Fri, 8 May 2020 09:23:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="jvP7+X5X" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726746AbgEHJWb (ORCPT ); Fri, 8 May 2020 05:22:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725379AbgEHJWa (ORCPT ); Fri, 8 May 2020 05:22:30 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49B98C05BD43; Fri, 8 May 2020 02:22:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Sender:Reply-To:Content-ID:Content-Description; bh=w8+H/WdMyxnhfLBjBxD8TC1RnBnytabQkaUnuZkvk7I=; b=jvP7+X5X7VRIMwCYgT7LNK0AFl XUjK6E9jLYjSPhdxBhk4e8cjUgzYRdfPmgcekEHtrCLFzVPVYOmflEAT2tytuFBAuKLvPZ7vgUuHV QxZDMpCoN65mGRvFviWLQwZpYYk3P5CsK/ASiV/YnKIG47vuwlVqXAwfcZsYnDtD0k2LIT4i3cv7q ucYZnJ3na83YU8pBMu9hGVaES8VRMgpT72itohPncZ+s92Bgutanx8IQ714uW/Yfx0SDflH/R8uz6 QdXGcn6rKYt/thKQ3KxVSThheOBqc+odYus2izfMSzKAfdauHUpKYV+EI1/LJxXhiNo0WOgpTCJie heeDjR6g==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDB-0008J4-QF; Fri, 08 May 2020 09:22:30 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 02/11] fs: check FMODE_WRITE in __kernel_write Date: Fri, 8 May 2020 11:22:13 +0200 Message-Id: <20200508092222.2097-3-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: We still need to check if the fѕ is open write, even for the low-level helper. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/read_write.c b/fs/read_write.c index d5aaf3a4198b9..d5c754080e5a5 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -505,6 +505,8 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t const char __user *p; ssize_t ret; + if (!(file->f_mode & FMODE_WRITE)) + return -EBADF; if (!(file->f_mode & FMODE_CAN_WRITE)) return -EINVAL; From patchwork Fri May 8 09:22:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536123 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B9B5F159A for ; Fri, 8 May 2020 09:23:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 98876208DB for ; Fri, 8 May 2020 09:23:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="TU8+oMbh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727082AbgEHJWe (ORCPT ); Fri, 8 May 2020 05:22:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727067AbgEHJWd (ORCPT ); Fri, 8 May 2020 05:22:33 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E190BC05BD43; Fri, 8 May 2020 02:22:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=0lEo6gqn07GiGCkWQW5GaafQ9aQ+OilbTHmYkhl6q0Y=; b=TU8+oMbh9/SrFZqoIP1O6ndAHF Q4mmC0Rr/S2/uMkTPXXhKV626GJndr59xNMGAyyKRBsVB8XJDmviHBuwR9abew4SvtstEFHCO0jTp hIYOin8/+t2TF4JzQtDySwrcMsAqGShLK93k41y6qby8luj4FqS8O3zd81JakUtB6OjrFxQaGxfzN hfauYeAjLC+WbZ/aAc7J8RIbxGqgB2TbC0u4/hVHlent0q7qPDLQjc3DnMP9wBEXVh9QmepT7a0nE bHYtoEnfh7b7aS5b4WFSLW3XgL7epq+4p/OOWaRtcEdlhLmr+KZtSfH8hUHPLwoOmjpKyTs3apszi A1Xbi+2g==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDE-0008Jh-Dk; Fri, 08 May 2020 09:22:32 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 03/11] fs: remove the call_{read,write}_iter functions Date: Fri, 8 May 2020 11:22:14 +0200 Message-Id: <20200508092222.2097-4-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: Just open coding the methods calls is a lot easier to follow. Signed-off-by: Christoph Hellwig --- drivers/block/loop.c | 4 ++-- drivers/target/target_core_file.c | 4 ++-- fs/aio.c | 4 ++-- fs/io_uring.c | 4 ++-- fs/read_write.c | 12 ++++++------ fs/splice.c | 2 +- include/linux/fs.h | 12 ------------ 7 files changed, 15 insertions(+), 27 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index da693e6a834e5..ad167050a4ec4 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -572,9 +572,9 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, kthread_associate_blkcg(cmd->css); if (rw == WRITE) - ret = call_write_iter(file, &cmd->iocb, &iter); + ret = file->f_op->write_iter(&cmd->iocb, &iter); else - ret = call_read_iter(file, &cmd->iocb, &iter); + ret = file->f_op->read_iter(&cmd->iocb, &iter); lo_rw_aio_do_completion(cmd); kthread_associate_blkcg(NULL); diff --git a/drivers/target/target_core_file.c b/drivers/target/target_core_file.c index 7143d03f0e027..79f0707877917 100644 --- a/drivers/target/target_core_file.c +++ b/drivers/target/target_core_file.c @@ -303,9 +303,9 @@ fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, aio_cmd->iocb.ki_flags |= IOCB_DSYNC; if (is_write) - ret = call_write_iter(file, &aio_cmd->iocb, &iter); + ret = file->f_op->write_iter(&aio_cmd->iocb, &iter); else - ret = call_read_iter(file, &aio_cmd->iocb, &iter); + ret = file->f_op->read_iter(&aio_cmd->iocb, &iter); kfree(bvec); diff --git a/fs/aio.c b/fs/aio.c index 5f3d3d8149287..1ccc0efdc357d 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1540,7 +1540,7 @@ static int aio_read(struct kiocb *req, const struct iocb *iocb, return ret; ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter)); if (!ret) - aio_rw_done(req, call_read_iter(file, req, &iter)); + aio_rw_done(req, file->f_op->read_iter(req, &iter)); kfree(iovec); return ret; } @@ -1580,7 +1580,7 @@ static int aio_write(struct kiocb *req, const struct iocb *iocb, __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE); } req->ki_flags |= IOCB_WRITE; - aio_rw_done(req, call_write_iter(file, req, &iter)); + aio_rw_done(req, file->f_op->write_iter(req, &iter)); } kfree(iovec); return ret; diff --git a/fs/io_uring.c b/fs/io_uring.c index 0b91b06311735..ecad3dba1b23f 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2590,7 +2590,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) ssize_t ret2; if (req->file->f_op->read_iter) - ret2 = call_read_iter(req->file, kiocb, &iter); + ret2 = req->file->f_op->read_iter(kiocb, &iter); else ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); @@ -2705,7 +2705,7 @@ static int io_write(struct io_kiocb *req, bool force_nonblock) current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize; if (req->file->f_op->write_iter) - ret2 = call_write_iter(req->file, kiocb, &iter); + ret2 = req->file->f_op->write_iter(kiocb, &iter); else ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); diff --git a/fs/read_write.c b/fs/read_write.c index d5c754080e5a5..d91fe7ff6cc55 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -412,7 +412,7 @@ static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, lo kiocb.ki_pos = (ppos ? *ppos : 0); iov_iter_init(&iter, READ, &iov, 1, len); - ret = call_read_iter(filp, &kiocb, &iter); + ret = filp->f_op->read_iter(&kiocb, &iter); BUG_ON(ret == -EIOCBQUEUED); if (ppos) *ppos = kiocb.ki_pos; @@ -481,7 +481,7 @@ static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t kiocb.ki_pos = (ppos ? *ppos : 0); iov_iter_init(&iter, WRITE, &iov, 1, len); - ret = call_write_iter(filp, &kiocb, &iter); + ret = filp->f_op->write_iter(&kiocb, &iter); BUG_ON(ret == -EIOCBQUEUED); if (ret > 0 && ppos) *ppos = kiocb.ki_pos; @@ -693,9 +693,9 @@ static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter, kiocb.ki_pos = (ppos ? *ppos : 0); if (type == READ) - ret = call_read_iter(filp, &kiocb, iter); + ret = filp->f_op->read_iter(&kiocb, iter); else - ret = call_write_iter(filp, &kiocb, iter); + ret = filp->f_op->write_iter(&kiocb, iter); BUG_ON(ret == -EIOCBQUEUED); if (ppos) *ppos = kiocb.ki_pos; @@ -964,7 +964,7 @@ ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb, if (ret < 0) return ret; - ret = call_read_iter(file, iocb, iter); + ret = file->f_op->read_iter(iocb, iter); out: if (ret >= 0) fsnotify_access(file); @@ -1028,7 +1028,7 @@ ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb, if (ret < 0) return ret; - ret = call_write_iter(file, iocb, iter); + ret = file->f_op->write_iter(iocb, iter); if (ret > 0) fsnotify_modify(file); diff --git a/fs/splice.c b/fs/splice.c index 4735defc46ee6..05f52b02320b4 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -310,7 +310,7 @@ ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, i_head = to.head; init_sync_kiocb(&kiocb, in); kiocb.ki_pos = *ppos; - ret = call_read_iter(in, &kiocb, &to); + ret = in->f_op->read_iter(&kiocb, &to); if (ret > 0) { *ppos = kiocb.ki_pos; file_accessed(in); diff --git a/include/linux/fs.h b/include/linux/fs.h index 45cc10cdf6ddd..21f126957c2cf 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1895,18 +1895,6 @@ struct inode_operations { int (*set_acl)(struct inode *, struct posix_acl *, int); } ____cacheline_aligned; -static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio, - struct iov_iter *iter) -{ - return file->f_op->read_iter(kio, iter); -} - -static inline ssize_t call_write_iter(struct file *file, struct kiocb *kio, - struct iov_iter *iter) -{ - return file->f_op->write_iter(kio, iter); -} - static inline int call_mmap(struct file *file, struct vm_area_struct *vma) { return file->f_op->mmap(file, vma); From patchwork Fri May 8 09:22:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536121 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 27A6C159A for ; Fri, 8 May 2020 09:23:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 076B3208DB for ; Fri, 8 May 2020 09:23:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="I2fV/Il/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727100AbgEHJWh (ORCPT ); Fri, 8 May 2020 05:22:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727095AbgEHJWf (ORCPT ); Fri, 8 May 2020 05:22:35 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E100C05BD43; Fri, 8 May 2020 02:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=/Vw1xBDqcg+had1H7ldfhCCl+Ssv4JonY1VaXlhtXBk=; b=I2fV/Il/XU+eajjiZQcidKGoMT QP56q4Zq+TTHB6ncYlzWGgrpbxz/uo9TmD78E/YEV0jmqMMkBZVYJZIntxXpZfkLbPa4d7mth9gUn jL9SFH0ecXzMGl24/Jr7EUTaNpPsHZVuoHecEHc1rrZ6iVoRTtJifMi5x9wsVtXzbtr2rzQY3JUYD VScP/8znNa9NNBap7UDX3bA8i7GgKHkmUaAtJ6Gs/LAj+D5O3sakvOdDonXtn/i0F9csiafQ15TOy 1kfHXRJWsiFsUb22CIeRZHzRx6fsUz9VlHejkw2ivZBeLYbXlJTmf8KOYI9nW1DizG07e2Z6vTpOJ Bie1R+kQ==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDG-0008KE-UI; Fri, 08 May 2020 09:22:35 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 04/11] fs: implement kernel_write using __kernel_write Date: Fri, 8 May 2020 11:22:15 +0200 Message-Id: <20200508092222.2097-5-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: Consolidate the two in-kernel write helpers to make upcoming changes easier. The only difference are the missing call to rw_verify_area in kernel_write, and an access_ok check that doesn't make sense for kernel buffers to start with. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index d91fe7ff6cc55..6b456a257b31c 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -531,16 +531,13 @@ EXPORT_SYMBOL(__kernel_write); ssize_t kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos) { - mm_segment_t old_fs; - ssize_t res; - old_fs = get_fs(); - set_fs(KERNEL_DS); - /* The cast to a user pointer is valid due to the set_fs() */ - res = vfs_write(file, (__force const char __user *)buf, count, pos); - set_fs(old_fs); + ssize_t ret; - return res; + ret = rw_verify_area(WRITE, file, pos, count); + if (ret) + return ret; + return __kernel_write(file, buf, count, pos); } EXPORT_SYMBOL(kernel_write); From patchwork Fri May 8 09:22:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536119 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6BDCF17EF for ; Fri, 8 May 2020 09:23:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 54D2F21582 for ; Fri, 8 May 2020 09:23:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="PrGhWrIl" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727113AbgEHJWj (ORCPT ); Fri, 8 May 2020 05:22:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727110AbgEHJWi (ORCPT ); Fri, 8 May 2020 05:22:38 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 060A2C05BD43; Fri, 8 May 2020 02:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=V/EdMUvxufPYsTf8+BVqK5Uvn4IVRJmx9LBhx14QLjI=; b=PrGhWrIlreWoD+QPgc7MN98VLi jZsvdj+NuPyAUvmNMQOzBl5CuowJnr/4G3hDaf+jQ7AaBXIauqNtel/e5bfGLrhPLAVe/qFgIUGAI CVAjwM5GVTZVfr/twjqvFVtm2KPz96Qv3KyzxH9bDRXnfAW+d8khqkW9vHfdWT16oZ+a1MQKqxDQZ hjLpxSFtL1VnmlNo5SWIl1yCghd831FgjviXBpxAr+bxJ4r1LX8ssLQ+nCUXhdrHdWPmRKHC+4/iM kY3pEDQpZyiOKOnJtEYZCVN83ByaCl6Rqwczy3RTkR9+AqbfW0gLvQ1u7EqJ2MtXrIQjiwRpONNW/ qD/uubrw==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDJ-0008Ke-Ey; Fri, 08 May 2020 09:22:37 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 05/11] fs: remove __vfs_write Date: Fri, 8 May 2020 11:22:16 +0200 Message-Id: <20200508092222.2097-6-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: Fold it into the two callers. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 6b456a257b31c..67a035782874b 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -488,17 +488,6 @@ static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t return ret; } -static ssize_t __vfs_write(struct file *file, const char __user *p, - size_t count, loff_t *pos) -{ - if (file->f_op->write) - return file->f_op->write(file, p, count, pos); - else if (file->f_op->write_iter) - return new_sync_write(file, p, count, pos); - else - return -EINVAL; -} - ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos) { mm_segment_t old_fs; @@ -516,7 +505,12 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t p = (__force const char __user *)buf; if (count > MAX_RW_COUNT) count = MAX_RW_COUNT; - ret = __vfs_write(file, p, count, pos); + if (file->f_op->write) + ret = file->f_op->write(file, p, count, pos); + else if (file->f_op->write_iter) + ret = new_sync_write(file, p, count, pos); + else + ret = -EINVAL; set_fs(old_fs); if (ret > 0) { fsnotify_modify(file); @@ -553,19 +547,23 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_ return -EFAULT; ret = rw_verify_area(WRITE, file, pos, count); - if (!ret) { - if (count > MAX_RW_COUNT) - count = MAX_RW_COUNT; - file_start_write(file); - ret = __vfs_write(file, buf, count, pos); - if (ret > 0) { - fsnotify_modify(file); - add_wchar(current, ret); - } - inc_syscw(current); - file_end_write(file); + if (ret) + return ret; + if (count > MAX_RW_COUNT) + count = MAX_RW_COUNT; + file_start_write(file); + if (file->f_op->write) + ret = file->f_op->write(file, buf, count, pos); + else if (file->f_op->write_iter) + ret = new_sync_write(file, buf, count, pos); + else + ret = -EINVAL; + if (ret > 0) { + fsnotify_modify(file); + add_wchar(current, ret); } - + inc_syscw(current); + file_end_write(file); return ret; } From patchwork Fri May 8 09:22:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536115 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C3E4C17EF for ; Fri, 8 May 2020 09:23:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ACE3A21582 for ; Fri, 8 May 2020 09:23:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="olPPvebg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727768AbgEHJWl (ORCPT ); Fri, 8 May 2020 05:22:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727110AbgEHJWk (ORCPT ); Fri, 8 May 2020 05:22:40 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C541C05BD43; Fri, 8 May 2020 02:22:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=dQWwowQZzF+sZwOgwUAsHjLJmSCQ+nMIyBlQWLSvPPg=; b=olPPvebgLSHNjuC6Rk6QAPIcKP BoVfkzI516eXvEO2vp1hVAtK5STLXpFefNyIXwN7JszvH9FXoVr/CdLDykp71LE5v5nPxCvwzumqI 1ypE2tmEHIvuKCr6M7W94ITB/ZpInQlvXvI06NvooHcDsb6YY8DU59jDcvGwOuvoFoAAdS1y3yrfi q/hKup0NEAX9Kx6htRi4ZaTl7q2zNNC0q6eD+qC/oL/kaONcnl7JVO9yoPHeOtagp9ypu2T3ngBe/ wcLge5FQ7NLaPiJTbycJFn/qrj6naIyi3yrGo6heJNMbEFCl/XSBT82D+7vT2WOHQekJR8zf9EKI8 mtOkdS5g==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDM-0008LT-07; Fri, 08 May 2020 09:22:40 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 06/11] fs: don't change the address limit for ->write_iter in __kernel_write Date: Fri, 8 May 2020 11:22:17 +0200 Message-Id: <20200508092222.2097-7-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: If we write to a file that implements ->write_iter there is no need to change the address limit if we send a kvec down. Implement that case, and prefer it over using plain ->write with a changed address limit if available. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 67a035782874b..8a55e81bd9ac7 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -488,10 +488,9 @@ static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t return ret; } -ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos) +ssize_t __kernel_write(struct file *file, const void *buf, size_t count, + loff_t *pos) { - mm_segment_t old_fs; - const char __user *p; ssize_t ret; if (!(file->f_mode & FMODE_WRITE)) @@ -500,18 +499,29 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t return -EINVAL; file_start_write(file); - old_fs = get_fs(); - set_fs(KERNEL_DS); - p = (__force const char __user *)buf; if (count > MAX_RW_COUNT) count = MAX_RW_COUNT; - if (file->f_op->write) - ret = file->f_op->write(file, p, count, pos); - else if (file->f_op->write_iter) - ret = new_sync_write(file, p, count, pos); - else + if (file->f_op->write_iter) { + struct kvec iov = { .iov_base = (void *)buf, .iov_len = count }; + struct kiocb kiocb; + struct iov_iter iter; + + init_sync_kiocb(&kiocb, file); + kiocb.ki_pos = *pos; + iov_iter_kvec(&iter, WRITE, &iov, 1, count); + ret = file->f_op->write_iter(&kiocb, &iter); + if (ret > 0) + *pos = kiocb.ki_pos; + } else if (file->f_op->write) { + mm_segment_t old_fs = get_fs(); + + set_fs(KERNEL_DS); + ret = file->f_op->write(file, (__force const char __user *)buf, + count, pos); + set_fs(old_fs); + } else { ret = -EINVAL; - set_fs(old_fs); + } if (ret > 0) { fsnotify_modify(file); add_wchar(current, ret); From patchwork Fri May 8 09:22:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536111 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9AC78913 for ; Fri, 8 May 2020 09:23:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7669921582 for ; Fri, 8 May 2020 09:23:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="p26D5tuX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727786AbgEHJWn (ORCPT ); Fri, 8 May 2020 05:22:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727780AbgEHJWn (ORCPT ); Fri, 8 May 2020 05:22:43 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 058F7C05BD43; Fri, 8 May 2020 02:22:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=2xa/wiG5agEUFc0wQaCp1rgH/MqxYHnGtxkOZ1B6lq8=; b=p26D5tuXJEYyjY6ZosGiYBDB3E 1nq9PJ5KJeCChaYv93o+077l05GSOwqJwF9js9042LPhMGecjuUw3dSEd2lBlM2vKuXbHEs0gsisq 2O36MwsQAOBupXMVoqH4mu6d6eRo2O6pbGMg2XtWDpUmNueG88exR3Bs90oRCAiVNaZg6Zp8HMyid +zvsgsCGSVci3FwA9zH/YN/KDCYr5X+/IG8NMXSRU1pCN1D2V/z0tR5AULuo4aJITTLkT05Pn0kBK NH6igeEEFYWgwB/VtYEPddtbFsH57WTUTKFzvpOLCD0xSrVlwHlMa1l96ijBep7ssATWjLZz5Jmcw 4GyQbx2A==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDO-0008TT-Go; Fri, 08 May 2020 09:22:42 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 07/11] fs: add a __kernel_read helper Date: Fri, 8 May 2020 11:22:18 +0200 Message-Id: <20200508092222.2097-8-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: This is the counterpart to __kernel_write, and skip the rw_verify_area call compared to kernel_read. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 21 +++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 22 insertions(+) diff --git a/fs/read_write.c b/fs/read_write.c index 8a55e81bd9ac7..93f5724b4837d 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -430,6 +430,27 @@ ssize_t __vfs_read(struct file *file, char __user *buf, size_t count, return -EINVAL; } +ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) +{ + mm_segment_t old_fs = get_fs(); + ssize_t ret; + + if (!(file->f_mode & FMODE_CAN_READ)) + return -EINVAL; + + if (count > MAX_RW_COUNT) + count = MAX_RW_COUNT; + set_fs(KERNEL_DS); + ret = __vfs_read(file, (void __user *)buf, count, pos); + set_fs(old_fs); + if (ret > 0) { + fsnotify_access(file); + add_rchar(current, ret); + } + inc_syscr(current); + return ret; +} + ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) { mm_segment_t old_fs; diff --git a/include/linux/fs.h b/include/linux/fs.h index 21f126957c2cf..6441aaa25f8f2 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3011,6 +3011,7 @@ extern int kernel_read_file_from_path_initns(const char *, void **, loff_t *, lo extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t, enum kernel_read_file_id); extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *); +ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos); extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *); extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *); extern struct file * open_exec(const char *); From patchwork Fri May 8 09:22:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536109 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9390E913 for ; Fri, 8 May 2020 09:23:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E06721582 for ; Fri, 8 May 2020 09:23:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="hy8jbfxB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727805AbgEHJWr (ORCPT ); Fri, 8 May 2020 05:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727802AbgEHJWp (ORCPT ); Fri, 8 May 2020 05:22:45 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EC34C05BD43; Fri, 8 May 2020 02:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=v2d8pva8LsFTXWqDIrLlQ4NNcDJbw9ZGB5N3mHruM2Q=; b=hy8jbfxBdHRLA63hXlAM8XAENS gGYr4RCSkaLOb23GrcvL4ap/hgUcbFR8px9M6FibgZvYzAORKvw5/sirIfzb5hrJzqKwdHvTH+U5u bgYMzjpOchyC1Pz5lekkCleC8Dz+efnHbIdY/QNpHZ81n0St9GOqsfAhf5C7zW2mWet1EG9W6P56g vWlKWTXudBRjvFIR7/a62Ghnrn4rjNJg0YslRaiU4qwZC0jHHKt0VNdk1GRK1ttGG564UqqnqtLAN 55ahucBgZ5nf0aZHmcGyH2MErwpXIjAl9pV7vyPv3caHKDphDIeyRJRyhoGuDCJw/druVeDSa2FSR jSvB6nXA==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDR-000084-0I; Fri, 08 May 2020 09:22:45 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 08/11] integrity/ima: switch to using __kernel_read Date: Fri, 8 May 2020 11:22:19 +0200 Message-Id: <20200508092222.2097-9-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: __kernel_read has a bunch of additional sanity checks, and this moves the set_fs out of non-core code. Signed-off-by: Christoph Hellwig --- security/integrity/iint.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/security/integrity/iint.c b/security/integrity/iint.c index e12c4900510f6..1d20003243c3f 100644 --- a/security/integrity/iint.c +++ b/security/integrity/iint.c @@ -188,19 +188,7 @@ DEFINE_LSM(integrity) = { int integrity_kernel_read(struct file *file, loff_t offset, void *addr, unsigned long count) { - mm_segment_t old_fs; - char __user *buf = (char __user *)addr; - ssize_t ret; - - if (!(file->f_mode & FMODE_READ)) - return -EBADF; - - old_fs = get_fs(); - set_fs(KERNEL_DS); - ret = __vfs_read(file, buf, count, &offset); - set_fs(old_fs); - - return ret; + return __kernel_read(file, addr, count, &offset); } /* From patchwork Fri May 8 09:22:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536103 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EE526913 for ; Fri, 8 May 2020 09:22:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CA4EB208DB for ; Fri, 8 May 2020 09:22:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="rjM5axrY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727802AbgEHJWu (ORCPT ); Fri, 8 May 2020 05:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727812AbgEHJWs (ORCPT ); Fri, 8 May 2020 05:22:48 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0027AC05BD43; Fri, 8 May 2020 02:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=vlc4eNvi5wfwGR6DQMeyDB/cyssj3gSmf75wshA0KHE=; b=rjM5axrYMXR1Bc2ojkvRameTtm y0O2ynzTxvyUdJ+FiZOd1ptslY6lVW5XZ4WAceALRvYsBWTTFkvwSNvC+FNApfE8OuB598OwEdSVk aGBrexMIWI+h+wQfqo8X2w6BcSaTAIh5HIJm32kP43k+oo81WhxWvSkoW0OsmvbHwqk+92OizqGRL CECyLWPkwuvnBioFD9juRCpS4LjiWAyl6J1H2hi0DvmJHH0rqiKXxWr/sLYgClwcEY+WG4+ZXg9Or WiKD0pXbngdXYnWolI+baqZiWiI9i0nl166GHeucTwu9uCZvVBmxEBzt2PpRLHbNVOiN+faSMp/Wx ObpNpkiA==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDT-00008p-GE; Fri, 08 May 2020 09:22:47 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 09/11] fs: implement kernel_read using __kernel_read Date: Fri, 8 May 2020 11:22:20 +0200 Message-Id: <20200508092222.2097-10-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: Consolidate the two in-kernel read helpers to make upcoming changes easier. The only difference are the missing call to rw_verify_area in kernel_read, and an access_ok check that doesn't make sense for kernel buffers to start with. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 93f5724b4837d..0ffbed5fd8136 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -453,15 +453,12 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) { - mm_segment_t old_fs; - ssize_t result; + ssize_t ret; - old_fs = get_fs(); - set_fs(KERNEL_DS); - /* The cast to a user pointer is valid due to the set_fs() */ - result = vfs_read(file, (void __user *)buf, count, pos); - set_fs(old_fs); - return result; + ret = rw_verify_area(READ, file, pos, count); + if (ret) + return ret; + return __kernel_read(file, buf, count, pos); } EXPORT_SYMBOL(kernel_read); From patchwork Fri May 8 09:22:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536107 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7245617EF for ; Fri, 8 May 2020 09:23:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 57E5F216FD for ; Fri, 8 May 2020 09:23:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="KUF8uiBM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727857AbgEHJXA (ORCPT ); Fri, 8 May 2020 05:23:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727827AbgEHJWu (ORCPT ); Fri, 8 May 2020 05:22:50 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CE43C05BD43; Fri, 8 May 2020 02:22:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=ny/dUp3EeGrcoMfUS16dK3e2zcCNlcPi/UI1wWuauls=; b=KUF8uiBMZClAsuWmDnrErAMQbC EKAF+mkVMdsLTctvZJU45i1SIYJ89Qc54JXieAHoSbTXuzM7Yk2NZeFBG67kO7VKeuqCAFoFt5vFa AvvZ4zo1QxIcc9uOZM9BpE+JBNEqxcPt5v76f7GWFsb+w36vfaoIv3whFv4YNTT0G7sVAdD/uy9+Y kKeSSCxNdKEuhODE8gc6GmOFpwIH6YLI6Rsq/UU9v1Xp+t6JW7h3TDYomR3nh6a5nUfj4j370wzWS Ts5AN1FifFjGLPBq0u15w/ZAdmrOpphgm/S+lhWkRP5cmushbj6cJxTy6PFLwrRJgCu42TAQZ40vf +LS1OufA==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDW-00009a-18; Fri, 08 May 2020 09:22:50 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 10/11] fs: remove __vfs_read Date: Fri, 8 May 2020 11:22:21 +0200 Message-Id: <20200508092222.2097-11-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: Fold it into the two callers. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 43 +++++++++++++++++++++---------------------- include/linux/fs.h | 1 - 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 0ffbed5fd8136..f0009b506014c 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -419,17 +419,6 @@ static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, lo return ret; } -ssize_t __vfs_read(struct file *file, char __user *buf, size_t count, - loff_t *pos) -{ - if (file->f_op->read) - return file->f_op->read(file, buf, count, pos); - else if (file->f_op->read_iter) - return new_sync_read(file, buf, count, pos); - else - return -EINVAL; -} - ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) { mm_segment_t old_fs = get_fs(); @@ -441,7 +430,12 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) if (count > MAX_RW_COUNT) count = MAX_RW_COUNT; set_fs(KERNEL_DS); - ret = __vfs_read(file, (void __user *)buf, count, pos); + if (file->f_op->read) + ret = file->f_op->read(file, (void __user *)buf, count, pos); + else if (file->f_op->read_iter) + ret = new_sync_read(file, (void __user *)buf, count, pos); + else + ret = -EINVAL; set_fs(old_fs); if (ret > 0) { fsnotify_access(file); @@ -474,17 +468,22 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) return -EFAULT; ret = rw_verify_area(READ, file, pos, count); - if (!ret) { - if (count > MAX_RW_COUNT) - count = MAX_RW_COUNT; - ret = __vfs_read(file, buf, count, pos); - if (ret > 0) { - fsnotify_access(file); - add_rchar(current, ret); - } - inc_syscr(current); - } + if (ret) + return ret; + if (count > MAX_RW_COUNT) + count = MAX_RW_COUNT; + if (file->f_op->read) + ret = file->f_op->read(file, buf, count, pos); + else if (file->f_op->read_iter) + ret = new_sync_read(file, buf, count, pos); + else + ret = -EINVAL; + if (ret > 0) { + fsnotify_access(file); + add_rchar(current, ret); + } + inc_syscr(current); return ret; } diff --git a/include/linux/fs.h b/include/linux/fs.h index 6441aaa25f8f2..4c10a07a36178 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1905,7 +1905,6 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, struct iovec *fast_pointer, struct iovec **ret_pointer); -extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *); extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *); extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *); extern ssize_t vfs_readv(struct file *, const struct iovec __user *, From patchwork Fri May 8 09:22:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 11536101 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6F5C7159A for ; Fri, 8 May 2020 09:22:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 56FE2208DB for ; Fri, 8 May 2020 09:22:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="GOOO9v2Q" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727838AbgEHJWy (ORCPT ); Fri, 8 May 2020 05:22:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727812AbgEHJWx (ORCPT ); Fri, 8 May 2020 05:22:53 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F17C9C05BD43; Fri, 8 May 2020 02:22:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=8pX7i8uPtHU7HN4LiIFYJSz1zLla2TEkp0jZ2qu15RI=; b=GOOO9v2QplkrXMye0xGfmThwzE LGK1+j6gYEzDAXuvhQpa/icS2f9t6+y/sGTeDAbK4JB48nRHYec3N3jCxQMp5ZASxOwKtypYCiIRK Vbgj1OUJEBE6WBmO0V8r7rFvcZ50o52awLIsNYLtHAnGRVPckLfIaFQnGqI3v26xkvkXZ4VxVOgz4 PfFZFXHAysCwvDXLM3+BcgTb8Ee2RmT/K3MJbXwSauYE4wznfb6Z5k2dShehfOa0xlZgWaOPGrDMu lWQxBdehatdCSXws7x/KS3vTZ9W61+y8P1jRcDPrSjhQzfW715yqccPAFizZ4Ufa7eTJEtfnjoFgh DDr9S7Pg==; Received: from [2001:4bb8:180:9d3f:90d7:9df8:7cd:3504] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jWzDY-0000AI-Ek; Fri, 08 May 2020 09:22:52 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 11/11] fs: don't change the address limit for ->read_iter in __kernel_read Date: Fri, 8 May 2020 11:22:22 +0200 Message-Id: <20200508092222.2097-12-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508092222.2097-1-hch@lst.de> References: <20200508092222.2097-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: If we read to a file that implements ->read_iter there is no need to change the address limit if we send a kvec down. Implement that case, and prefer it over using plain ->read with a changed address limit if available. Signed-off-by: Christoph Hellwig --- fs/read_write.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index f0009b506014c..70715a0e2375d 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -421,7 +421,6 @@ static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, lo ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) { - mm_segment_t old_fs = get_fs(); ssize_t ret; if (!(file->f_mode & FMODE_CAN_READ)) @@ -429,14 +428,25 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) if (count > MAX_RW_COUNT) count = MAX_RW_COUNT; - set_fs(KERNEL_DS); - if (file->f_op->read) + if (file->f_op->read_iter) { + struct kvec iov = { .iov_base = buf, .iov_len = count }; + struct kiocb kiocb; + struct iov_iter iter; + + init_sync_kiocb(&kiocb, file); + kiocb.ki_pos = *pos; + iov_iter_kvec(&iter, READ, &iov, 1, count); + ret = file->f_op->read_iter(&kiocb, &iter); + *pos = kiocb.ki_pos; + } else if (file->f_op->read) { + mm_segment_t old_fs = get_fs(); + + set_fs(KERNEL_DS); ret = file->f_op->read(file, (void __user *)buf, count, pos); - else if (file->f_op->read_iter) - ret = new_sync_read(file, (void __user *)buf, count, pos); - else + set_fs(old_fs); + } else { ret = -EINVAL; - set_fs(old_fs); + } if (ret > 0) { fsnotify_access(file); add_rchar(current, ret);