From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756719Ab2IXQRu (ORCPT ); Mon, 24 Sep 2012 12:17:50 -0400 Received: from twin.jikos.cz ([89.185.236.188]:36108 "EHLO twin.jikos.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756510Ab2IXQRs (ORCPT ); Mon, 24 Sep 2012 12:17:48 -0400 Date: Mon, 24 Sep 2012 18:17:42 +0200 From: David Sterba To: Wang Sheng-Hui Cc: chris.mason@fusionio.com, jbacik@fusionio.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: Btrfs: check range early in map_private_extent_buffer Message-ID: <20120924161742.GL14582@twin.jikos.cz> Reply-To: dave@jikos.cz Mail-Followup-To: Wang Sheng-Hui , chris.mason@fusionio.com, jbacik@fusionio.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org References: <505FE3AF.2090302@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <505FE3AF.2090302@gmail.com> User-Agent: Mutt/1.5.21 (2011-07-01) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 24, 2012 at 12:38:07PM +0800, Wang Sheng-Hui wrote: > Check range early to avoid further check/compute in case > of range error. > > Signed-off-by: Wang Sheng-Hui > --- > fs/btrfs/extent_io.c | 16 ++++++++-------- > 1 files changed, 8 insertions(+), 8 deletions(-) > > diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c > index 4c87847..9250cf5 100644 > --- a/fs/btrfs/extent_io.c > +++ b/fs/btrfs/extent_io.c > @@ -4643,6 +4643,14 @@ int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, > unsigned long end_i = (start_offset + start + min_len - 1) >> > PAGE_CACHE_SHIFT; > > + if (start + min_len > eb->len) { > + printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, " > + "wanted %lu %lu\n", (unsigned long long)eb->start, > + eb->len, start, min_len); > + WARN_ON(1); > + return -EINVAL; > + } > + > if (i != end_i) > return -EINVAL; 4665 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 4666 unsigned long end_i = (start_offset + start + min_len - 1) >> 4667 PAGE_CACHE_SHIFT; so the check above effectively verifies that min_len - 1 < PAGE_CACHE_SIZE AND is within the same page The other check if (start + min_len > eb->len) { looks if the requested data do not lie out of the bounds of the extent buffer, where min_len is filled with sizeof(something). So, both the checks look for corrupted metadata, I don't see the need to swap them. david