From patchwork Wed Oct 10 00:11:20 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: 10633471 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 DC2C016B1 for ; Wed, 10 Oct 2018 00:11:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CBDCB29C64 for ; Wed, 10 Oct 2018 00:11:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9CDA29CA1; Wed, 10 Oct 2018 00:11:28 +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 51AC129C64 for ; Wed, 10 Oct 2018 00:11:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 713556B026D; Tue, 9 Oct 2018 20:11:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 69DC06B026E; Tue, 9 Oct 2018 20:11:26 -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 51C816B026F; Tue, 9 Oct 2018 20:11:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id 029696B026D for ; Tue, 9 Oct 2018 20:11:26 -0400 (EDT) Received: by mail-pf1-f200.google.com with SMTP id 8-v6so3237783pfr.0 for ; Tue, 09 Oct 2018 17:11:25 -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:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=UPZ0d5EyyxBIdyb+/QZEk2VIco+d0Uqne8UVSJH846Y=; b=hmVBCh2ltfbUkvcI2RfXxixbn6lcQRr6aVX7BPPx23YCN9XG8DaOo542iKSna9Srdu SZdQrYSP19dhZ8HupOLQw0mlzzKLWSZXUdJY7hhXQRNjKV90yvU4GtE75sj/0tfY7bvB eVvNfJq3LC1T3ki7Tfm89J78h11VQ0EAsGBCVMBdu3kVhcAtB/qZBOnPqSJavJ6AA/9Y LykVsDIOOjdbGDldLKOtZhrhgNRbwnSdaNWnOvTbXD366GWJcqwpLV4v59B/YkaOeop6 WCZpFYAJmO3j1GFqhyk9SSoWQdbzXyW/WoNkfZs/OAanc6TAEUldjhoynKcs3CcyyLqI HiPA== X-Gm-Message-State: ABuFfog4vX/PWA8oojl4QskfzWMFkiCqpgLcu0GhqOxu0cRvLVBshyAl qU8vk19sRpJVy6ztqucaG+PGUXPpZMZDByoGnwKeUQOfDgmRAC2acEe1q8kEHoo4st8Im1AzqSq UUvCjdQGdjlOwSHu8h7iHo1EEkKkJ6/yC1tsdKuKvKrkHsG2FGhSDvvFJu9IUvPelvw== X-Received: by 2002:a62:4ec9:: with SMTP id c192-v6mr32139868pfb.221.1539130285672; Tue, 09 Oct 2018 17:11:25 -0700 (PDT) X-Google-Smtp-Source: ACcGV62nsCUMLiZyOMaf2EYSFPqfgsGN1ncd7yki0LAs8fjhPMolzpFRDtvwOzdyZKWjiEIgNMIw X-Received: by 2002:a62:4ec9:: with SMTP id c192-v6mr32139814pfb.221.1539130284746; Tue, 09 Oct 2018 17:11:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539130284; cv=none; d=google.com; s=arc-20160816; b=P8BdM/xhcwPC4B5aHkcO6cRhyA4tVAYx4hWdZgRmHihILFS+s7y2KtoIqwtp2LyQku YvDMjILG3QCZXg+QqLoPekq1ZBn822wVHayRp792C14cH5c2/pooX4YQo4QcUQLsgeUm TlUc5Esgaj+zaBfowNx98Z4JYtjC4if04/a22kJC1KoKvwOy5t7bdJhYjOnsJy2aYby/ SZWBEgULFyBfWkbWMfYVYKh2QOyemkrBqtc7mvS8YcLneJb9AJSh6HacaiiX0BcPbhX8 YrRkWMFSvFTvr1vBDptdUJXrHh1q6h5Lw2YoFiwl+Wy63/ZZ/s6gSq/aMaNr3GtVqYzd Vw9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=UPZ0d5EyyxBIdyb+/QZEk2VIco+d0Uqne8UVSJH846Y=; b=HYLYrtjzviVCs/G6ZT6Vo46+/eAUdA4UNwd4kKIzRTjzWZeFO41vQslQTMjTmTA6D4 MivqzdA18dDpnVktxjAiBo7Xo/2HlylfjUBtX8k6Z4oa9d2L/IYeOIsMjZ8z6PChFns8 g7FnmEzmChpca8T3qzJ2qbfL6PupM/006vYEilFExKCgb2RT/hCzoqNqPCoqCGWQdOYm +IDy7xwpteOo129D0MyjT0S0pmB/wA/aaAylPqIaWHSPYBpoIPIFWx0BQLjc+l1EzbNt c2a+hn8vo+R1F52uhqGBezxZF5Sq53pYHmCdAIZIjOHpe5d63+nmhCv3jP1vUSRVDYLh XoKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=zijFNAT9; spf=pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.78 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from aserp2120.oracle.com (aserp2120.oracle.com. [141.146.126.78]) by mx.google.com with ESMTPS id f9-v6si25660578plm.126.2018.10.09.17.11.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Oct 2018 17:11:24 -0700 (PDT) Received-SPF: pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.78 as permitted sender) client-ip=141.146.126.78; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=zijFNAT9; spf=pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.78 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 (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w9A08ekx138543; Wed, 10 Oct 2018 00:11:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=UPZ0d5EyyxBIdyb+/QZEk2VIco+d0Uqne8UVSJH846Y=; b=zijFNAT9z+gw/MI8d1eVXf3jMsCTgjhryhyCdeq6HhqEzQ1EtXkYHagwhAVTNqJJkaJo R9ZE0JZiE0Oa9smd0U066iCUML432vrYFnjzYfCnsHQQ2Fluq1zANB0SuT8talkjS/l4 AFwIV8HLs5vBTY3V1YHgZjNfCfU1t6OqLbqHEtwrd6k+ccJbluWbQblZSFU6KORkLLfW jm4l9tdM+OSKrpky4EitobilSWcztg/PUhzQuPrteT8r3d+SjXkkgDXXr6KS1ZmuFuXP EA5ILI/AeOFLm4ZtxS/FQAvuiHvAo1X5y5+WkhJnAOYg9RlqNDQsbSSuDg16hhMqOU9x Ew== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp2120.oracle.com with ESMTP id 2mxn0q0tgx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 10 Oct 2018 00:11:23 +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 w9A0BMFB023816 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 10 Oct 2018 00:11:23 GMT Received: from abhmp0011.oracle.com (abhmp0011.oracle.com [141.146.116.17]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w9A0BMw9020784; Wed, 10 Oct 2018 00:11:22 GMT Received: from localhost (/10.159.249.114) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 10 Oct 2018 00:11:21 +0000 Subject: [PATCH 06/25] vfs: strengthen checking of file range inputs to generic_remap_checks From: "Darrick J. Wong" To: david@fromorbit.com, darrick.wong@oracle.com Cc: sandeen@redhat.com, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, linux-unionfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-mm@kvack.org, linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, ocfs2-devel@oss.oracle.com Date: Tue, 09 Oct 2018 17:11:20 -0700 Message-ID: <153913028015.32295.15993665528948323051.stgit@magnolia> In-Reply-To: <153913023835.32295.13962696655740190941.stgit@magnolia> References: <153913023835.32295.13962696655740190941.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9041 signatures=668706 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-1807170000 definitions=main-1810100000 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 --- mm/filemap.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index 14041a8468ba..59056bd9c58a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2974,6 +2974,27 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) } EXPORT_SYMBOL(generic_write_checks); +static int +generic_remap_check_limits(struct file *file, loff_t pos, uint64_t *count) +{ + struct inode *inode = file->f_mapping->host; + + /* Don't exceed the LFS limits. */ + if (unlikely(pos + *count > MAX_NON_LFS && + !(file->f_flags & O_LARGEFILE))) { + if (pos >= MAX_NON_LFS) + return -EFBIG; + *count = min(*count, MAX_NON_LFS - (uint64_t)pos); + } + + /* Don't operate on ranges the page cache doesn't support. */ + if (unlikely(pos >= inode->i_sb->s_maxbytes)) + return -EFBIG; + + *count = min(*count, inode->i_sb->s_maxbytes - (uint64_t)pos); + return 0; +} + /* * Performs necessary checks before doing a clone. * @@ -2992,6 +3013,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)) @@ -3015,6 +3037,23 @@ int generic_remap_checks(struct file *file_in, loff_t pos_in, return -EINVAL; count = min(count, size_in - (uint64_t)pos_in); + /* Don't exceed RLMIT_FSIZE in the file we're writing into. */ + if (limit != RLIM_INFINITY) { + if (pos_out >= limit) { + send_sig(SIGXFSZ, current, 0); + return -EFBIG; + } + count = min(count, limit - (uint64_t)pos_out); + } + + ret = generic_remap_check_limits(file_in, pos_in, &count); + if (ret) + return ret; + + ret = generic_remap_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.