Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1246677ybl; Fri, 23 Aug 2019 16:07:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqzesm4jgHcLNpbWgK273WZz8yWDKSrPT5IZ+7n/dy4Ed9UnaG7Pdd949Doj5IASgnOEB0ZJ X-Received: by 2002:a17:902:e30b:: with SMTP id cg11mr7551748plb.335.1566601651367; Fri, 23 Aug 2019 16:07:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566601651; cv=none; d=google.com; s=arc-20160816; b=TFxxA8ctmCmPMxk4ex8hZlN1R/I6kITem1QbXKHjzTesncq0CmTnxzJeWTZrewT3rv v4tXKwxFE1eyXJj3RKRiLH+C6DCC9SZbQ3yEY/dqU92t7ioILiFEUwzhb6FHdKpmWmqh YpoaokOyemuMxy6N4tHYotYRCAXgHcgAlTmGj4gpge6UD5ue9YoljnOEjbKb+qYHB6Fz oUiicbLaNLBPutcntfym6ZlCsFSZncU6HPWoOXCXrJly/Kz58fZv65L7jPNNMaEczmx2 KfiIOPsXvQ0Ddt4iYGkxDgw/TEKN0h+0yP6DY0P6+77H3+UV2NtY0oO44FG2MpUKVQ8j TQHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=x4yLJiKXtzzpojZPeE920WUaz2kXg9WvCvfPgzn0aF8=; b=fdwMw8WaiDJbkHQzWdzFaKX61F/Jmg3oiBFbh9xiPA40varDldGBeywwn66xOOwGTz gOyyRFBjU5m0TI3XJvX4/W5bTTQyptABRBJtzUY52sMBnlxwpfheYWlUSvYxbMhNlNwW qj7GaFSaJUlQUeafoexFXG4WtXKqq8gRalS82RHaucFgJ2bdO5IdvDL/qrl3wNChiA2l tXwoubQAAEnx3BQ539cguvba2Oz3DL2ibFDkNblpm6XH5PjUlZ1HwHbxdEaGXUCM/iZk qyxrTecTxE5Tme9M0PeN+1E/N11izZuAo/UFBkI2h2KMHTL/kxENLDH8sRMI0MDOlCoO taFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x139si3123268pgx.251.2019.08.23.16.07.16; Fri, 23 Aug 2019 16:07:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389739AbfHWL2W (ORCPT + 99 others); Fri, 23 Aug 2019 07:28:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49116 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388570AbfHWL2W (ORCPT ); Fri, 23 Aug 2019 07:28:22 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 50611308FBA7; Fri, 23 Aug 2019 11:28:21 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E1AB15D6B2; Fri, 23 Aug 2019 11:28:16 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id x7NBSGO7008523; Fri, 23 Aug 2019 07:28:16 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id x7NBSGxR008520; Fri, 23 Aug 2019 07:28:16 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Fri, 23 Aug 2019 07:28:16 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Zhang Tao cc: agk@redhat.com, snitzer@redhat.com, Zhang Tao , dm-devel@redhat.com, linux-kernel@vger.kernel.org Subject: Re: [dm-devel] [PATCH] dm table: fix a potential array out of bounds In-Reply-To: <1566351211-13280-1-git-send-email-kontais@zoho.com> Message-ID: References: <1566351211-13280-1-git-send-email-kontais@zoho.com> User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Fri, 23 Aug 2019 11:28:21 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi I tested it and the bug is real - with some table sizes, dm_table_find_target will access memory out of bounds if the sector argument is beyond limit. Your patch fixes some of these cases, but not all of them. I used this script to test all possible table sizes: #!/bin/bash -e sync dmsetup remove_all || true rmmod dm_mod || true >t.txt for i in `seq 1 10000`; do echo $i echo $((i-1)) 1 error >>t.txt dmsetup create error From: Zhang Tao > > allocate num + 1 for target and offset array, n_highs need num + 1 > elements, the last element will be used for node lookup in function > dm_table_find_target. > > Signed-off-by: Zhang Tao > --- > drivers/md/dm-table.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c > index 7b6c3ee..fd7f604 100644 > --- a/drivers/md/dm-table.c > +++ b/drivers/md/dm-table.c > @@ -160,20 +160,22 @@ static int alloc_targets(struct dm_table *t, unsigned int num) > { > sector_t *n_highs; > struct dm_target *n_targets; > + unsigned int alloc_num; > > /* > * Allocate both the target array and offset array at once. > * Append an empty entry to catch sectors beyond the end of > * the device. > */ > - n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) + > + alloc_num = num + 1; > + n_highs = (sector_t *) dm_vcalloc(alloc_num, sizeof(struct dm_target) + > sizeof(sector_t)); > if (!n_highs) > return -ENOMEM; > > - n_targets = (struct dm_target *) (n_highs + num); > + n_targets = (struct dm_target *) (n_highs + alloc_num); > > - memset(n_highs, -1, sizeof(*n_highs) * num); > + memset(n_highs, -1, sizeof(*n_highs) * alloc_num); > vfree(t->highs); > > t->num_allocated = num; > -- > 1.8.3.1 > > > -- > dm-devel mailing list > dm-devel@redhat.com > https://www.redhat.com/mailman/listinfo/dm-devel >