Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6683899ybi; Wed, 29 May 2019 11:25:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqwLoq2hfOyG0+cEkkuMX3uFrsPIRryd0DhlMtzUQkTtkjCqGE+BvCJ1Blu0LsTEcxo5jUgN X-Received: by 2002:a17:90a:1ac8:: with SMTP id p66mr14000547pjp.1.1559154359287; Wed, 29 May 2019 11:25:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559154359; cv=none; d=google.com; s=arc-20160816; b=RgaMq7DDDVmyv7BDtw0Ykb1m/etDWkJYYi0TUF4auhsSlYMkJNCmuyCTlaPLAR6B5G 1+DqhD79OvlRLvmPdWD01i7oaf2wjtDBV8eFClZCQdclzITbsMUqJkkxXbgOrJnW2782 MET79q8lM0JZCdP6LeYsHnWXSWrVNjF5L72iR7dg0mQs4DwFRRppVsjgqIIH72ZeGeu0 9JxIrYEVh2cAkgYBmaWllRoEt7Pyn1UJUWHO2kcuRA53SVlDDPNbF2XG1DpqYmiwVnpy u9z4U7kpf1WSiYqXZi2LONPy5drW5EozXXj0iRpBrfPAZEv+zL/9YbFjPF5n4UKnhn6o K29g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Id2h4/LUEmkIwtGMeQrsxQRh2WfTng2MUmJNlVZJqjg=; b=KUQkuj3WxyUVClHeF0WUT9DZvfmczV4T2GOC3mM9w2OODsEODOuLIzPsOLLjqC4SEd D5HZA2dcbbokarvLpMr4e78dGcQsSwF1NB/WJQqErKHSFyw1o8Ppb/MJAQQNv87WEiZs s5f1Jad1m3XYZtx0LOMDwh0VzZBNpV4TWAzCcbMWMdhFA7D+c+tHkzQ4TRdhu93aPNzN vb8YoDukBAVEHnO2K3X6GMjpEocrTaqku3anhz6CH2xBbOwdw9w003G+BPbb5QraZknl jdKYhjq9gq0jimSWt8ghA5OdfYWjoAuufn8/HHKjLdBoYO28jWqZAQX/5qofwfHsg9r0 Ow7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=YsWW5FsG; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t9si159153pjb.25.2019.05.29.11.25.45; Wed, 29 May 2019 11:25:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=YsWW5FsG; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727089AbfE2SZe (ORCPT + 99 others); Wed, 29 May 2019 14:25:34 -0400 Received: from aserp2130.oracle.com ([141.146.126.79]:43174 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725917AbfE2SZd (ORCPT ); Wed, 29 May 2019 14:25:33 -0400 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x4TIO1m6061503; Wed, 29 May 2019 18:24:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=Id2h4/LUEmkIwtGMeQrsxQRh2WfTng2MUmJNlVZJqjg=; b=YsWW5FsGZfyCAeeNvk4lGeT4g8EFkAkUsErDxtHwVvVsehawm51m3KSBg+UixAoC49L8 aqY747FUfjIIwEP62j9fYZEOcRBV9AiXwzJFaxjzZvK3pXWvbfUIEJV4QP3DoxVhoF8w lelTGQpLOhUXR/l6ZjgzgAulm6mIOHLX6GM4yEcLth5IiXW2YTnkBx+ja86o4vv5+kVY RQNDf4ouHWIFSNPt5nlvqAlvszJv4xg2Thv07pZ+7hEmWS4uARAkqNW9FdXTYb1nIvlu OJHmxF/BFXTynmbK/rr4mF5a1Fbk2CSyLulqfR8ZacSbjlN7RFp+4yF1wr6rWyLpb28t Dg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2130.oracle.com with ESMTP id 2spu7dky8u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 29 May 2019 18:24:52 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x4TIO13V007852; Wed, 29 May 2019 18:24:51 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3020.oracle.com with ESMTP id 2sr31ve1p7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 29 May 2019 18:24:51 +0000 Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x4TIOnkH011817; Wed, 29 May 2019 18:24:49 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 29 May 2019 11:24:48 -0700 Date: Wed, 29 May 2019 11:24:47 -0700 From: "Darrick J. Wong" To: Amir Goldstein Cc: Dave Chinner , Christoph Hellwig , linux-xfs@vger.kernel.org, Olga Kornievskaia , Luis Henriques , Al Viro , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, ceph-devel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, Dave Chinner Subject: Re: [PATCH v3 05/13] vfs: add missing checks to copy_file_range Message-ID: <20190529182447.GE5231@magnolia> References: <20190529174318.22424-1-amir73il@gmail.com> <20190529174318.22424-6-amir73il@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190529174318.22424-6-amir73il@gmail.com> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9272 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905290118 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9272 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905290119 Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org On Wed, May 29, 2019 at 08:43:09PM +0300, Amir Goldstein wrote: > Like the clone and dedupe interfaces we've recently fixed, the > copy_file_range() implementation is missing basic sanity, limits and > boundary condition tests on the parameters that are passed to it > from userspace. Create a new "generic_copy_file_checks()" function > modelled on the generic_remap_checks() function to provide this > missing functionality. > > [Amir] Shorten copy length instead of checking pos_in limits > because input file size already abides by the limits. > > Signed-off-by: Dave Chinner > Signed-off-by: Amir Goldstein Looks ok, Reviewed-by: Darrick J. Wong --D > --- > fs/read_write.c | 3 ++- > include/linux/fs.h | 3 +++ > mm/filemap.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 58 insertions(+), 1 deletion(-) > > diff --git a/fs/read_write.c b/fs/read_write.c > index f1900bdb3127..b0fb1176b628 100644 > --- a/fs/read_write.c > +++ b/fs/read_write.c > @@ -1626,7 +1626,8 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, > if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) > return -EXDEV; > > - ret = generic_file_rw_checks(file_in, file_out); > + ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len, > + flags); > if (unlikely(ret)) > return ret; > > diff --git a/include/linux/fs.h b/include/linux/fs.h > index 89b9b73eb581..e4d382c4342a 100644 > --- a/include/linux/fs.h > +++ b/include/linux/fs.h > @@ -3050,6 +3050,9 @@ extern int generic_remap_checks(struct file *file_in, loff_t pos_in, > struct file *file_out, loff_t pos_out, > loff_t *count, unsigned int remap_flags); > extern int generic_file_rw_checks(struct file *file_in, struct file *file_out); > +extern int generic_copy_file_checks(struct file *file_in, loff_t pos_in, > + struct file *file_out, loff_t pos_out, > + size_t *count, unsigned int flags); > extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *); > extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *); > extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *); > diff --git a/mm/filemap.c b/mm/filemap.c > index 44361928bbb0..aac71aef4c61 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -3056,6 +3056,59 @@ int generic_file_rw_checks(struct file *file_in, struct file *file_out) > return 0; > } > > +/* > + * Performs necessary checks before doing a file copy > + * > + * Can adjust amount of bytes to copy via @req_count argument. > + * Returns appropriate error code that caller should return or > + * zero in case the copy should be allowed. > + */ > +int generic_copy_file_checks(struct file *file_in, loff_t pos_in, > + struct file *file_out, loff_t pos_out, > + size_t *req_count, unsigned int flags) > +{ > + struct inode *inode_in = file_inode(file_in); > + struct inode *inode_out = file_inode(file_out); > + uint64_t count = *req_count; > + loff_t size_in; > + int ret; > + > + ret = generic_file_rw_checks(file_in, file_out); > + if (ret) > + return ret; > + > + /* Don't touch certain kinds of inodes */ > + if (IS_IMMUTABLE(inode_out)) > + return -EPERM; > + > + if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out)) > + return -ETXTBSY; > + > + /* Ensure offsets don't wrap. */ > + if (pos_in + count < pos_in || pos_out + count < pos_out) > + return -EOVERFLOW; > + > + /* Shorten the copy to EOF */ > + size_in = i_size_read(inode_in); > + if (pos_in >= size_in) > + count = 0; > + else > + count = min(count, size_in - (uint64_t)pos_in); > + > + ret = generic_write_check_limits(file_out, pos_out, &count); > + if (ret) > + return ret; > + > + /* Don't allow overlapped copying within the same file. */ > + if (inode_in == inode_out && > + pos_out + count > pos_in && > + pos_out < pos_in + count) > + return -EINVAL; > + > + *req_count = count; > + return 0; > +} > + > int pagecache_write_begin(struct file *file, struct address_space *mapping, > loff_t pos, unsigned len, unsigned flags, > struct page **pagep, void **fsdata) > -- > 2.17.1 >