Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2317484ybz; Sat, 18 Apr 2020 21:56:44 -0700 (PDT) X-Google-Smtp-Source: APiQypL/12F9A3uqsvB3obP844eVAUkZgZg65GanNReKi0KHo6z9LJ7ZDKREyJnwuiMYtmBbUg6O X-Received: by 2002:a05:6402:3121:: with SMTP id dd1mr3858781edb.168.1587272204298; Sat, 18 Apr 2020 21:56:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587272204; cv=none; d=google.com; s=arc-20160816; b=UHUF4DIiylO5jsvyXura2cxfIM/1WRaIPaghs2Mr59kiQW2xxiiNFfKyyTcoJpdeiV OtvywpZ4eN9HO5Sd0jAqdfADoA6ZpsPs+7x51YU5UWPqMqo3mkkiBylvG9J4ou5u6nvw fgBDGHq/trIvlO0PX1mpnlFi1KRHjBlS35TffrEzPWXXB+EVyr0u0jfBFHAscrMRWm9K igaV81RMlrjGK3UAySoGE6GuG5G5gp72M2ha5pnndU+iyc6q4RaQiS5u76VHnmPfKIEU gpZRJQMlw2nyPhhV8XfiY2zqmg7usImQ6voKpguaSyTHFyrVEWlUNnF8w6p+NFY00gYc 3ZWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=2MpbYTGkc7oKWXa8Hd4m9UTb3wiRllqzrdwDgrmpM3Q=; b=AYnfOVYgKg992h/a2KE5rGgxXuJvrMynhqfI0xA7Cvv/PhaJhkurQn+H+rX41JtkIW 5YnvrL34rU7wFokPPsoBM2OEZpWpPixA6vi0jZVZh/13Ceeysj+q/aUYxPMSqtg53QoA aMcU+qRAYTND50QwgQUa8mLeEsr6K92/YVkLz43nOY79CjMP40AwZZ2pvo5BIazKzQ39 uBx8fJaMQJz/iyOkC7IMC5WSpfR4K1Obnf+jU5FcHjah9MNNDuyihoqm87otu8td+Ve0 ytTVaS0L5UmIokY/ipRd7G1JvVmDMIWcqPcxUNLf6YDWjrqAYXU2nvtX8aTHX5pWqOXn Gong== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k11si14840303ejp.508.2020.04.18.21.56.20; Sat, 18 Apr 2020 21:56:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725947AbgDSEqT (ORCPT + 99 others); Sun, 19 Apr 2020 00:46:19 -0400 Received: from outgoing-auth-1.mit.edu ([18.9.28.11]:40704 "EHLO outgoing.mit.edu" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725763AbgDSEqS (ORCPT ); Sun, 19 Apr 2020 00:46:18 -0400 Received: from callcc.thunk.org (pool-100-0-195-244.bstnma.fios.verizon.net [100.0.195.244]) (authenticated bits=0) (User authenticated as tytso@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 03J4kCT4021519 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 19 Apr 2020 00:46:13 -0400 Received: by callcc.thunk.org (Postfix, from userid 15806) id A725642013B; Sun, 19 Apr 2020 00:46:12 -0400 (EDT) Date: Sun, 19 Apr 2020 00:46:12 -0400 From: "Theodore Y. Ts'o" To: Ritesh Harjani Cc: Murphy Zhou , Ext4 Developers List Subject: Re: [PATCH] ext4: validate fiemap iomap begin offset and length value Message-ID: <20200419044612.GB311394@mit.edu> References: <20200418233231.z767yvfiupy7hwgp@xzhoux.usersys.redhat.com> <20200419015654.F2061A4051@d06av23.portsmouth.uk.ibm.com> <20200419044224.GA311394@mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200419044224.GA311394@mit.edu> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Sun, Apr 19, 2020 at 12:42:24AM -0400, Theodore Y. Ts'o wrote: > I think we need to take his patch, and make a simialr change to > ext4_iomap_begin(). Ritesh, do you agree? For example... diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 2a4aae6acdcb..adce3339d697 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3424,8 +3424,10 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, int ret; struct ext4_map_blocks map; u8 blkbits = inode->i_blkbits; + ext4_lblk_t lblk = offset >> blkbits; + ext4_lblk_t last_lblk = (offset + length - 1) >> blkbits; - if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) + if (lblk > EXT4_MAX_LOGICAL_BLOCK) return -EINVAL; if (WARN_ON_ONCE(ext4_has_inline_data(inode))) @@ -3434,9 +3436,15 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, /* * Calculate the first and last logical blocks respectively. */ - map.m_lblk = offset >> blkbits; - map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, - EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; + if (last_lblk >= EXT4_MAX_LOGICAL_BLOCK) + last_lblk = EXT4_MAX_LOGICAL_BLOCK - 1; + if (lblk >= EXT4_MAX_LOGICAL_BLOCK) + lblk = EXT4_MAX_LOGICAL_BLOCK - 1; + + map.m_lblk = lblk; + map.m_len = last_lblk - lblk + 1; + if (map.m_len == 0 ) + map.m_len = 1; if (flags & IOMAP_WRITE) ret = ext4_iomap_alloc(inode, &map, flags); @@ -3524,8 +3532,10 @@ static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, bool delalloc = false; struct ext4_map_blocks map; u8 blkbits = inode->i_blkbits; + ext4_lblk_t lblk = offset >> blkbits; + ext4_lblk_t last_lblk = (offset + length - 1) >> blkbits; - if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) + if (lblk > EXT4_MAX_LOGICAL_BLOCK) return -EINVAL; if (ext4_has_inline_data(inode)) { @@ -3540,9 +3550,15 @@ static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, /* * Calculate the first and last logical block respectively. */ - map.m_lblk = offset >> blkbits; - map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, - EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; + if (last_lblk >= EXT4_MAX_LOGICAL_BLOCK) + last_lblk = EXT4_MAX_LOGICAL_BLOCK - 1; + if (lblk >= EXT4_MAX_LOGICAL_BLOCK) + lblk = EXT4_MAX_LOGICAL_BLOCK - 1; + + map.m_lblk = lblk; + map.m_len = last_lblk - lblk + 1; + if (map.m_len == 0 ) + map.m_len = 1; /* * Fiemap callers may call for offset beyond s_bitmap_maxbytes.