From: Mikulas Patocka <mpatocka@redhat.com>
To: Zhang Tao <kontais@zoho.com>
Cc: agk@redhat.com, snitzer@redhat.com,
Zhang Tao <zhangtao27@lenovo.com>,
dm-devel@redhat.com, linux-kernel@vger.kernel.org
Subject: Re: [dm-devel] [PATCH] dm table: fix a potential array out of bounds
Date: Fri, 23 Aug 2019 07:28:16 -0400 (EDT) [thread overview]
Message-ID: <alpine.LRH.2.02.1908230705510.5296@file01.intranet.prod.int.rdu2.redhat.com> (raw)
In-Reply-To: <1566351211-13280-1-git-send-email-kontais@zoho.com>
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 <t.txt
dmsetup remove error
done
and I modified dm_table_find_target to call dm_table_find_target with too
high sector number. Without your patch, it fails on table with 16 entries;
with your patch applied, it fails on 144 entries.
I'll make another patch that passes the test.
Mikulas
On Wed, 21 Aug 2019, Zhang Tao wrote:
> From: Zhang Tao <zhangtao27@lenovo.com>
>
> 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 <zhangtao27@lenovo.com>
> ---
> 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
>
next prev parent reply other threads:[~2019-08-23 11:28 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-21 1:33 [PATCH] dm table: fix a potential array out of bounds Zhang Tao
2019-08-23 11:28 ` Mikulas Patocka [this message]
2019-08-23 13:54 ` [PATCH 1/2] dm table: fix invalid memory accesses with too high sector number Mikulas Patocka
2019-08-23 13:55 ` [PATCH 2/2] dm: make dm_table_find_target return NULL Mikulas Patocka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.LRH.2.02.1908230705510.5296@file01.intranet.prod.int.rdu2.redhat.com \
--to=mpatocka@redhat.com \
--cc=agk@redhat.com \
--cc=dm-devel@redhat.com \
--cc=kontais@zoho.com \
--cc=linux-kernel@vger.kernel.org \
--cc=snitzer@redhat.com \
--cc=zhangtao27@lenovo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).