From patchwork Tue Sep 6 06:42:04 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukas Czerner X-Patchwork-Id: 113486 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id E43E3B6F77 for ; Tue, 6 Sep 2011 16:42:13 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752803Ab1IFGmM (ORCPT ); Tue, 6 Sep 2011 02:42:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:23807 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752268Ab1IFGmL (ORCPT ); Tue, 6 Sep 2011 02:42:11 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p866g9Qt025865 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 6 Sep 2011 02:42:09 -0400 Received: from dhcp-27-109.brq.redhat.com (vpn-11-235.rdu.redhat.com [10.11.11.235]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p866g75K031545; Tue, 6 Sep 2011 02:42:07 -0400 From: Lukas Czerner To: linux-ext4@vger.kernel.org Cc: tm@tao.ma, tytso@mit.edu, Lukas Czerner Subject: [PATCH v2] ext4: fix possible overflow in ext4_trim_fs() Date: Tue, 6 Sep 2011 08:42:04 +0200 Message-Id: <1315291324-6864-1-git-send-email-lczerner@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org The overflow can happen when we are calling get_group_no_and_offset() which stores the result of do_div() in 32 bit long type. However the result might be bigger than that if big blocknr is passed in. This will most likely happen when calling FITRIM with the default argument len = ULLONG_MAX. Fix it by decrementing the len so that start+len equals to the file system size in the worst case. Signed-off-by: Lukas Czerner --- v2 - Change commit description to better describe the problem fs/ext4/mballoc.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index 17a5a57..d86dc14 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -4952,14 +4952,18 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) uint64_t start, len, minlen, trimmed = 0; ext4_fsblk_t first_data_blk = le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); + ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es); int ret = 0; start = range->start >> sb->s_blocksize_bits; len = range->len >> sb->s_blocksize_bits; minlen = range->minlen >> sb->s_blocksize_bits; - if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb))) + if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)) || + unlikely(start > max_blks)) return -EINVAL; + if (unlikely(len > max_blks)) + len = max_blks - start; if (start + len <= first_data_blk) goto out; if (start < first_data_blk) {