]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
regmap: debugfs: check count when read regmap file
authorPeng Fan <peng.fan@nxp.com>
Fri, 13 Mar 2020 01:58:07 +0000 (09:58 +0800)
committerMark Brown <broonie@kernel.org>
Fri, 13 Mar 2020 16:33:58 +0000 (16:33 +0000)
When executing the following command, we met kernel dump.
dmesg -c > /dev/null; cd /sys;
for i in `ls /sys/kernel/debug/regmap/* -d`; do
echo "Checking regmap in $i";
cat $i/registers;
done && grep -ri "0x02d0" *;

It is because the count value is too big, and kmalloc fails. So add an
upper bound check to allow max size `PAGE_SIZE << (MAX_ORDER - 1)`.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Link: https://lore.kernel.org/r/1584064687-12964-1-git-send-email-peng.fan@nxp.com
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/base/regmap/regmap-debugfs.c

index e72843fe41dfeea565b2c0dc2722688561670228..089e5dc7144a6a1a090cbe61dd04fcc0b9fd6c38 100644 (file)
@@ -227,6 +227,9 @@ static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
        if (*ppos < 0 || !count)
                return -EINVAL;
 
+       if (count > (PAGE_SIZE << (MAX_ORDER - 1)))
+               count = PAGE_SIZE << (MAX_ORDER - 1);
+
        buf = kmalloc(count, GFP_KERNEL);
        if (!buf)
                return -ENOMEM;
@@ -371,6 +374,9 @@ static ssize_t regmap_reg_ranges_read_file(struct file *file,
        if (*ppos < 0 || !count)
                return -EINVAL;
 
+       if (count > (PAGE_SIZE << (MAX_ORDER - 1)))
+               count = PAGE_SIZE << (MAX_ORDER - 1);
+
        buf = kmalloc(count, GFP_KERNEL);
        if (!buf)
                return -ENOMEM;