From patchwork Thu Oct 18 18:56:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 10647853 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4B30D14E2 for ; Thu, 18 Oct 2018 18:56:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 38EA92886F for ; Thu, 18 Oct 2018 18:56:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B0F728B12; Thu, 18 Oct 2018 18:56:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 88F482886F for ; Thu, 18 Oct 2018 18:56:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E35A6B0003; Thu, 18 Oct 2018 14:56:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 592526B0006; Thu, 18 Oct 2018 14:56:18 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4335A6B0010; Thu, 18 Oct 2018 14:56:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id F2A3F6B0003 for ; Thu, 18 Oct 2018 14:56:17 -0400 (EDT) Received: by mail-pg1-f198.google.com with SMTP id n5-v6so1098760pgv.6 for ; Thu, 18 Oct 2018 11:56:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :message-id:references:mime-version:content-disposition:in-reply-to :user-agent; bh=9Cj+DLe0Sln1e0/R/XAZSWL1COfZTGIQu59QcFECiYk=; b=ugoXVlcH24h58/UEefIP+CUfMLCaaaQp4H3GSBnC+Ty46dUc9rvl6CmcrhW7qahHDS H37dPEQiAuKTr01WBxKxIr8VkqRiy5B1vc/3uSb4MTSuBJTQ+elIMunDjlZSiyNe6eVq qkiX+/JMT1gq0/It8CNf9aj+stJfVQsLJzFlwXcGhA+XrtrhFf0jMo7kgKUiumb43FvS lKYXceMw4xhh01HN2aUx8cpv8qjQy2SIdN3bqpNo6WKuFQ+7Y94e1otxrCsUL/eyIDLW IrI/Wj/k+LWfCJWx/F0gragoJkj985c2Sct38QahbHRuyeeg6ubc13HaWlVMXNo7rQks JFjA== X-Gm-Message-State: ABuFfoj3j8T7ob24pvJqsjYtgLZEKL+wbBoQfx/772FaWeFqqKCqEINI whV+vjGvogR9K6ow/u864IYWhQwJgcH44jrgBXLc0JhhOT6/exMyfLujA3Zv+YqHLIfnH4hsLRr i33soxryUQqe5RUiaNDMB6HmN80a0F4dgHV+SMyzZCLDqTqdQPDhjwbqS209F/j6s9A== X-Received: by 2002:a65:63c3:: with SMTP id n3-v6mr1735091pgv.5.1539888977634; Thu, 18 Oct 2018 11:56:17 -0700 (PDT) X-Google-Smtp-Source: ACcGV61KMDv55T0IyCmrlB6SAdzLfSgGxk20b2GOHKQgpBEEIyzIY7k6PFbwPC9IRThuI/t+jxse X-Received: by 2002:a65:63c3:: with SMTP id n3-v6mr1735044pgv.5.1539888976541; Thu, 18 Oct 2018 11:56:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539888976; cv=none; d=google.com; s=arc-20160816; b=eovoip6dmiM8WFjnnwM1ZkFcDkjrFWMAA40cALCGtfpAZaTg6OigKd9PLk4vMsrmnA Eca48ZnbN/3KAi8BWjjz3GDBIMvYSx1hDeD7n6xcevjox8Ue9JCtIKfLyijc/04kt+WG zZAAtaUdFnZDhQ2pcV/eQ9F8Zd5i8zBeB5tU4SzleUE2f3AMMHX0QnwtW9p9C1TwCZWc XtL7FHxev5lDLnua3+p9Y9JjWDpfgcH8WNlTjMUX2AMQ2C3dTE29sLCFodnZV/i/ltMv fY2kQIXQxbo0DxTlCtw3TrmQJdljYImwCOFl2ZGYJjnV4x5yld8s+96PSkPPWFt7ACqh SivA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:dkim-signature; bh=9Cj+DLe0Sln1e0/R/XAZSWL1COfZTGIQu59QcFECiYk=; b=w8tgLzaNR1FJBIGNgawmtL2q/WNAqJKfGtDItqPFF2zIrHutR+VcOfFE3hTEY8rpjs FxvU16FDD2c4sq8jXRFri8r+8D69DGXduyZaMECTpO3VEMDYLEHYBOzRarvz36heYIR+ iqs7feY37epJI/BqVUC2W8Qi3086qTBniIJ9IEv7zPlt1T2jNY9VYRCCFP8hqA50iGAh tC+HZDxV3AWqXvOysUOni6v1JEniEqOPsynK/PxLNxThq4u3IKxxkLozn5rrVU5QrKZB HEG1RI9stBkrEO9YGHBf5pff1N7BdFD80iD4vpq6dtXOmKYbqmBdGyeN2mEDl5pgKPBa moGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=bAITFBpB; spf=pass (google.com: domain of darrick.wong@oracle.com designates 156.151.31.86 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from userp2130.oracle.com (userp2130.oracle.com. [156.151.31.86]) by mx.google.com with ESMTPS id 12-v6si22162038pgq.337.2018.10.18.11.56.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Oct 2018 11:56:16 -0700 (PDT) Received-SPF: pass (google.com: domain of darrick.wong@oracle.com designates 156.151.31.86 as permitted sender) client-ip=156.151.31.86; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=bAITFBpB; spf=pass (google.com: domain of darrick.wong@oracle.com designates 156.151.31.86 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w9IInMKn143759; Thu, 18 Oct 2018 18:56:14 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=9Cj+DLe0Sln1e0/R/XAZSWL1COfZTGIQu59QcFECiYk=; b=bAITFBpBt7cT5N9nFgczVrdWqQCF0RWbCg6oripYnhQMPpSKLaWRHYtY4sFuzQ/0w7GF vfkjSWgXvx/BkRXcm4RMkfSmw4YEqBqZilhe32tpZ3090oOcORj8N5rkpT5WEW4xF9ml 0ytU+FHBZAb+wOu9RiC0bKL2xgAJQYjf3rILzp1QXBjpake5fUtHY63QsEQAl3H+chpq gnQgjDFjDXkFkN0k/F1bBSYB3xN4GnU1pqyo/fmJObWIDQmlkPUALRzbEx8XbkxgK6Sr 5ROw2ZxrBDKp8zWzFxepz6R/TQza1FNrGMAkd5Bl7wz3DrXxc6epLaRw88wArehUvj6h 8A== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2130.oracle.com with ESMTP id 2n384ugjmk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Oct 2018 18:56:14 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w9IIuC5I008572 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Oct 2018 18:56:12 GMT Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w9IIuBEd025211; Thu, 18 Oct 2018 18:56:11 GMT Received: from localhost (/10.145.178.77) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 18 Oct 2018 11:56:10 -0700 Date: Thu, 18 Oct 2018 11:56:07 -0700 From: "Darrick J. Wong" To: david@fromorbit.com Cc: sandeen@redhat.com, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, Amir Goldstein , linux-unionfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-mm@kvack.org, linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, Christoph Hellwig , ocfs2-devel@oss.oracle.com Subject: [PATCH v2 04/29] vfs: strengthen checking of file range inputs to generic_remap_checks Message-ID: <20181018185607.GV28243@magnolia> References: <153981625504.5568.2708520119290577378.stgit@magnolia> <153981628292.5568.2466587869276881561.stgit@magnolia> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <153981628292.5568.2466587869276881561.stgit@magnolia> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9050 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=1 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-1807170000 definitions=main-1810180157 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong File range remapping, if allowed to run past the destination file's EOF, is an optimization on a regular file write. Regular file writes that extend the file length are subject to various constraints which are not checked by range cloning. This is a correctness problem because we're never allowed to touch ranges that the page cache can't support (s_maxbytes); we're not supposed to deal with large offsets (MAX_NON_LFS) if O_LARGEFILE isn't set; and we must obey resource limits (RLIMIT_FSIZE). Therefore, add these checks to the new generic_remap_checks function so that we curtail unexpected behavior. Signed-off-by: Darrick J. Wong Reviewed-by: Amir Goldstein Reviewed-by: Christoph Hellwig --- v2: restructurings as recommended by Al Viro --- mm/filemap.c | 84 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 47e6bfd45a91..aab149fc1faa 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2915,6 +2915,42 @@ struct page *read_cache_page_gfp(struct address_space *mapping, } EXPORT_SYMBOL(read_cache_page_gfp); +/* + * Don't operate on ranges the page cache doesn't support, and don't exceed the + * LFS limits. If pos is under the limit it becomes a short access. If it + * exceeds the limit we return -EFBIG. + */ +static int generic_access_check_limits(struct file *file, loff_t pos, + loff_t *count) +{ + struct inode *inode = file->f_mapping->host; + loff_t max_size = inode->i_sb->s_maxbytes; + + if (!(file->f_flags & O_LARGEFILE)) + max_size = MAX_NON_LFS; + + if (unlikely(pos >= max_size)) + return -EFBIG; + *count = min(*count, max_size - pos); + return 0; +} + +static int generic_write_check_limits(struct file *file, loff_t pos, + loff_t *count) +{ + loff_t limit = rlimit(RLIMIT_FSIZE); + + if (limit != RLIM_INFINITY) { + if (pos >= limit) { + send_sig(SIGXFSZ, current, 0); + return -EFBIG; + } + *count = min(*count, limit - pos); + } + + return generic_access_check_limits(file, pos, count); +} + /* * Performs necessary checks before doing a write * @@ -2926,8 +2962,8 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) { struct file *file = iocb->ki_filp; struct inode *inode = file->f_mapping->host; - unsigned long limit = rlimit(RLIMIT_FSIZE); - loff_t pos; + loff_t count; + int ret; if (!iov_iter_count(from)) return 0; @@ -2936,40 +2972,15 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) if (iocb->ki_flags & IOCB_APPEND) iocb->ki_pos = i_size_read(inode); - pos = iocb->ki_pos; - if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) return -EINVAL; - if (limit != RLIM_INFINITY) { - if (iocb->ki_pos >= limit) { - send_sig(SIGXFSZ, current, 0); - return -EFBIG; - } - iov_iter_truncate(from, limit - (unsigned long)pos); - } - - /* - * LFS rule - */ - if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS && - !(file->f_flags & O_LARGEFILE))) { - if (pos >= MAX_NON_LFS) - return -EFBIG; - iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos); - } - - /* - * Are we about to exceed the fs block limit ? - * - * If we have written data it becomes a short write. If we have - * exceeded without writing data we send a signal and return EFBIG. - * Linus frestrict idea will clean these up nicely.. - */ - if (unlikely(pos >= inode->i_sb->s_maxbytes)) - return -EFBIG; + count = iov_iter_count(from); + ret = generic_write_check_limits(file, iocb->ki_pos, &count); + if (ret) + return ret; - iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos); + iov_iter_truncate(from, count); return iov_iter_count(from); } EXPORT_SYMBOL(generic_write_checks); @@ -2991,6 +3002,7 @@ int generic_remap_checks(struct file *file_in, loff_t pos_in, uint64_t bcount; loff_t size_in, size_out; loff_t bs = inode_out->i_sb->s_blocksize; + int ret; /* The start of both ranges must be aligned to an fs block. */ if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs)) @@ -3014,6 +3026,14 @@ int generic_remap_checks(struct file *file_in, loff_t pos_in, return -EINVAL; count = min(count, size_in - (uint64_t)pos_in); + ret = generic_access_check_limits(file_in, pos_in, &count); + if (ret) + return ret; + + ret = generic_write_check_limits(file_out, pos_out, &count); + if (ret) + return ret; + /* * If the user wanted us to link to the infile's EOF, round up to the * next block boundary for this check.