Skip to contentSkip to conversation

regmap: Check for out-of-range offsets before mapping them

PATCH New Pratyush Yadav 3 participants 6 messages May 26, 2020 – Jun 17, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regmap: Check for out-of-range offsets before mapping them

In regmap_raw_{read,write}_range(), offsets are checked to make sure they aren't out of range. But this check happens _after_ the address is mapped from physical memory. Input should be sanity-checked before using it. Mapping the address before validating it leaves the door open to passing an invalid address to map_physmem(). So check for out of range offsets _before_ mapping them.

This fixes a segmentation fault in sandbox when -1 is used as an offset to regmap_{read,write}().

---
 drivers/core/regmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/core/regmap.c +4 -4
index 4a214eff7c..a67a237b88 100644
@@ -310,13 +310,13 @@ int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
 	}
 	range = &map->ranges[range_num];
 
-	ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
-
 	if (offset + val_len > range->size) {
 		debug("%s: offset/size combination invalid\n", __func__);
 		return -ERANGE;
 	}
 
+	ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
+
 	switch (val_len) {
 	case REGMAP_SIZE_8:
 		*((u8 *)valp) = __read_8(ptr, map->endianness);
@@ -419,13 +419,13 @@ int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
 	}
 	range = &map->ranges[range_num];
 
-	ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
-
 	if (offset + val_len > range->size) {
 		debug("%s: offset/size combination invalid\n", __func__);
 		return -ERANGE;
 	}
 
+	ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
+
 	switch (val_len) {
 	case REGMAP_SIZE_8:
 		__write_8(ptr, val, map->endianness);
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/1 regmap: Check for out-of-range offsets before mapping them

Hi Pratyush,

Quoted 8 lines · Pratyush Yadav

In regmap_raw_{read,write}_range(), offsets are checked to make sure they aren't out of range. But this check happens _after_ the address is mapped from physical memory. Input should be sanity-checked before using it. Mapping the address before validating it leaves the door open to passing an invalid address to map_physmem(). So check for out of range offsets _before_ mapping them.

Quoted 8 lines

This fixes a segmentation fault in sandbox when -1 is used as an offset to regmap_{read,write}().

---
 drivers/core/regmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Please add a sandbox test to catch this problem.

Regards,
Simon

#2 lore raw

PATCH 1/1 regmap: Check for out-of-range offsets before mapping them

Hi Simon,

Quoted 14 lines · Simon Glass

Hi Pratyush,

Quoted 11 lines · Pratyush Yadav

In regmap_raw_{read,write}_range(), offsets are checked to make sure they aren't out of range. But this check happens _after_ the address is mapped from physical memory. Input should be sanity-checked before using it. Mapping the address before validating it leaves the door open to passing an invalid address to map_physmem(). So check for out of range offsets _before_ mapping them.

This fixes a segmentation fault in sandbox when -1 is used as an offset to regmap_{read,write}().

Quoted 7 lines
Quoted 5 lines
---
 drivers/core/regmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Thanks.

Quoted 1 line

Please add a sandbox test to catch this problem.

The test "dm_test_devm_regmap" proposed in [0] should catch this:

    ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, -1, val));
    ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, &val));

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200605203025.15466-9-p.yadav at ti.com/

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#3 lore raw

PATCH 1/1 regmap: Check for out-of-range offsets before mapping them

Hi Simon,

Quoted 14 lines · Simon Glass

Hi Pratyush,

Quoted 11 lines · Pratyush Yadav

In regmap_raw_{read,write}_range(), offsets are checked to make sure they aren't out of range. But this check happens _after_ the address is mapped from physical memory. Input should be sanity-checked before using it. Mapping the address before validating it leaves the door open to passing an invalid address to map_physmem(). So check for out of range offsets _before_ mapping them.

This fixes a segmentation fault in sandbox when -1 is used as an offset to regmap_{read,write}().

Quoted 7 lines
Quoted 5 lines
---
 drivers/core/regmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Thanks.

Quoted 1 line

Please add a sandbox test to catch this problem.

The test "dm_test_devm_regmap" proposed in [0] should catch this:

    ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, -1, val));
    ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, &val));

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200605203025.15466-9-p.yadav at ti.com/

Signature · 5 lines
Regards,
Pratyush Yadav
Texas Instruments India

Applied to u-boot-dm, thanks!
#4 lore raw

PATCH 1/1 regmap: Check for out-of-range offsets before mapping them

Quoted 27 lines · sjg at google.com

Hi Simon,

Quoted 14 lines · Simon Glass

Hi Pratyush,

Quoted 11 lines · Pratyush Yadav

In regmap_raw_{read,write}_range(), offsets are checked to make sure they aren't out of range. But this check happens _after_ the address is mapped from physical memory. Input should be sanity-checked before using it. Mapping the address before validating it leaves the door open to passing an invalid address to map_physmem(). So check for out of range offsets _before_ mapping them.

This fixes a segmentation fault in sandbox when -1 is used as an offset to regmap_{read,write}().

Quoted 7 lines
Quoted 5 lines
---
 drivers/core/regmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Thanks.

Quoted 15 lines
Quoted 1 line

Please add a sandbox test to catch this problem.

The test "dm_test_devm_regmap" proposed in [0] should catch this:

    ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, -1, val));
    ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, &val));

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200605203025.15466-9-p.yadav at ti.com/

Signature · 5 lines
Regards,
Pratyush Yadav
Texas Instruments India

Applied to u-boot-dm, thanks!

Thanks. BTW, your script seems to be misbehaving. It didn't quote the text in my original email with "> ".

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#5 lore raw

PATCH 1/1 regmap: Check for out-of-range offsets before mapping them

Hi Pratyush,

Quoted 43 lines · Pratyush Yadav
Quoted 41 lines · sjg at google.com

Hi Simon,

Quoted 14 lines · Simon Glass

Hi Pratyush,

Quoted 11 lines · Pratyush Yadav

In regmap_raw_{read,write}_range(), offsets are checked to make sure they aren't out of range. But this check happens _after_ the address is mapped from physical memory. Input should be sanity-checked before using it. Mapping the address before validating it leaves the door open to passing an invalid address to map_physmem(). So check for out of range offsets _before_ mapping them.

This fixes a segmentation fault in sandbox when -1 is used as an offset to regmap_{read,write}().

Quoted 7 lines
Quoted 5 lines
---
 drivers/core/regmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Thanks.

Quoted 1 line

Please add a sandbox test to catch this problem.

The test "dm_test_devm_regmap" proposed in [0] should catch this:

    ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, -1, val));
    ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, &val));

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200605203025.15466-9-p.yadav at ti.com/

--
Regards,
Pratyush Yadav
Texas Instruments India

Quoted 4 lines
Quoted 1 line

Applied to u-boot-dm, thanks!

Thanks. BTW, your script seems to be misbehaving. It didn't quote the text in my original email with "> ".

Oh dear, and it didn't parse it correctly either.

I suppose at some point I'll take another look at it. Thanks for letting me know.

Regards,
Simon