diff options
author | Mikulas Patocka <mpatocka@redhat.com> | 2012-03-28 13:41:26 -0400 |
---|---|---|
committer | Alasdair G Kergon <agk@redhat.com> | 2012-03-28 13:41:26 -0400 |
commit | 31998ef19385c944600d9a981b96252f98204bee (patch) | |
tree | ab757b8d6e3d349cf42827354e594687dcf6c5c8 /drivers/md/dm-linear.c | |
parent | 0447568fc51e0268e201f7086d2450cf986e0411 (diff) |
dm: reject trailing characters in sccanf input
Device mapper uses sscanf to convert arguments to numbers. The problem is that
the way we use it ignores additional unmatched characters in the scanned string.
For example, this `if (sscanf(string, "%d", &number) == 1)' will match a number,
but also it will match number with some garbage appended, like "123abc".
As a result, device mapper accepts garbage after some numbers. For example
the command `dmsetup create vg1-new --table "0 16384 linear 254:1bla 34816bla"'
will pass without an error.
This patch fixes all sscanf uses in device mapper. It appends "%c" with
a pointer to a dummy character variable to every sscanf statement.
The construct `if (sscanf(string, "%d%c", &number, &dummy) == 1)' succeeds
only if string is a null-terminated number (optionally preceded by some
whitespace characters). If there is some character appended after the number,
sscanf matches "%c", writes the character to the dummy variable and returns 2.
We check the return value for 1 and consequently reject numbers with some
garbage appended.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Diffstat (limited to 'drivers/md/dm-linear.c')
-rw-r--r-- | drivers/md/dm-linear.c | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/drivers/md/dm-linear.c b/drivers/md/dm-linear.c index 9728839f844a..3639eeab6042 100644 --- a/drivers/md/dm-linear.c +++ b/drivers/md/dm-linear.c | |||
@@ -29,6 +29,7 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |||
29 | { | 29 | { |
30 | struct linear_c *lc; | 30 | struct linear_c *lc; |
31 | unsigned long long tmp; | 31 | unsigned long long tmp; |
32 | char dummy; | ||
32 | 33 | ||
33 | if (argc != 2) { | 34 | if (argc != 2) { |
34 | ti->error = "Invalid argument count"; | 35 | ti->error = "Invalid argument count"; |
@@ -41,7 +42,7 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |||
41 | return -ENOMEM; | 42 | return -ENOMEM; |
42 | } | 43 | } |
43 | 44 | ||
44 | if (sscanf(argv[1], "%llu", &tmp) != 1) { | 45 | if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1) { |
45 | ti->error = "dm-linear: Invalid device sector"; | 46 | ti->error = "dm-linear: Invalid device sector"; |
46 | goto bad; | 47 | goto bad; |
47 | } | 48 | } |