Skip to contentSkip to conversation

dm: Check for device sector overflow if CONFIG_LBDAF is not set

RFC PATCH Superseded Milan Broz 2 participants 6 messages Nov 4, 2018 – Nov 15, 2018 Device mapper

lore
Share & tools
lore

Milan Broz

author
#0 lore raw

RFC PATCH 1/1 dm: Check for device sector overflow if CONFIG_LBDAF is not set

Reference to a device in device-mapper table contains offset in sectors.

If the sector_t is 32bit integer (CONFIG_LBDAF is not set), then several device-mapper targets can overflow this offset and validity check is then performad on wrong offset and wrong table is activated.

See for example (on 32bit without CONFIG_LBDAF) this overflow:

  # dmsetup create test --table "0 2048 linear /dev/sdg 4294967297"
  # dmsetup table test
  0 2048 linear 8:96 1

In this patch I tried to add check for this problem to dm-linear and dm-crypt, but I am sure there are more places and I am not sure this is the proper way.

Should we use uint64_t in DM internally for device offset instead?

There are probably some internal calculations in dm-table.c that can overflow as well.

NOTE: it is a RFC patch that is incomplete (more targets need fixes).

---
 drivers/md/dm-crypt.c  | 4 ++++
 drivers/md/dm-linear.c | 4 ++++
 2 files changed, 8 insertions(+)
drivers/md/dm-crypt.c +4 -0
index 49be7a6a2e81..008fc40ef84b 100644
@@ -2786,6 +2786,10 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto bad;
 	}
 	cc->start = tmpll;
+	if (sizeof(cc->start) < sizeof(tmpll) && cc->start != tmpll) {
+		ti->error = "Device sector overflow";
+		goto bad;
+	}
 
 	if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
 		ret = crypt_integrity_ctr(cc, ti);
drivers/md/dm-linear.c +4 -0
index 8d7ddee6ac4d..b5a0065d1436 100644
@@ -50,6 +50,10 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto bad;
 	}
 	lc->start = tmp;
+	if (sizeof(lc->start) < sizeof(tmp) && lc->start != tmp) {
+		ti->error = "Device sector overflow";
+		goto bad;
+	}
 
 	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev);
 	if (ret) {
Signature · 1 line
2.19.1
maintainer
#1 lore raw
Quoted 15 lines · Milan Broz

Reference to a device in device-mapper table contains offset in sectors.

If the sector_t is 32bit integer (CONFIG_LBDAF is not set), then several device-mapper targets can overflow this offset and validity check is then performad on wrong offset and wrong table is activated.

See for example (on 32bit without CONFIG_LBDAF) this overflow:

  # dmsetup create test --table "0 2048 linear /dev/sdg 4294967297"
  # dmsetup table test
  0 2048 linear 8:96 1

In this patch I tried to add check for this problem to dm-linear and dm-crypt, but I am sure there are more places and I am not sure this is the proper way.

Quoted 6 lines

Should we use uint64_t in DM internally for device offset instead?

There are probably some internal calculations in dm-table.c that can overflow as well.

NOTE: it is a RFC patch that is incomplete (more targets need fixes).

OK.

But the condition "sizeof(cc->start) < sizeof(tmpll)" could be dropped, the compiler will optimize out "cc->start != tmpll" if the types have the same width.

Mikulas

Quoted 39 lines
---
 drivers/md/dm-crypt.c  | 4 ++++
 drivers/md/dm-linear.c | 4 ++++
 2 files changed, 8 insertions(+)
drivers/md/dm-crypt.c +4 -0
index 49be7a6a2e81..008fc40ef84b 100644
@@ -2786,6 +2786,10 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto bad;
 	}
 	cc->start = tmpll;
+	if (sizeof(cc->start) < sizeof(tmpll) && cc->start != tmpll) {
+		ti->error = "Device sector overflow";
+		goto bad;
+	}
 
 	if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
 		ret = crypt_integrity_ctr(cc, ti);
drivers/md/dm-linear.c +4 -0
index 8d7ddee6ac4d..b5a0065d1436 100644
@@ -50,6 +50,10 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto bad;
 	}
 	lc->start = tmp;
+	if (sizeof(lc->start) < sizeof(tmp) && lc->start != tmp) {
+		ti->error = "Device sector overflow";
+		goto bad;
+	}
 
 	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev);
 	if (ret) {
Signature · 1 line
2.19.1
author
#2 lore raw
Quoted 3 lines · Mikulas Patocka

But the condition "sizeof(cc->start) < sizeof(tmpll)" could be dropped, the compiler will optimize out "cc->start != tmpll" if the types have the same width.

Yes, the intention here is that in 64bit env. the whole if condition is not compiled in. If it happens without "sizeof(cc->start) < sizeof(tmpll)", then we can drop it.

So, does it make sense to add this to all dm targets? Or any better idea?

Milan

maintainer
#3 lore raw
Quoted 5 lines · Milan Broz
Quoted 3 lines · Mikulas Patocka

But the condition "sizeof(cc->start) < sizeof(tmpll)" could be dropped, the compiler will optimize out "cc->start != tmpll" if the types have the same width.

Quoted 7 lines

Yes, the intention here is that in 64bit env. the whole if condition is not compiled in. If it happens without "sizeof(cc->start) < sizeof(tmpll)", then we can drop it.

So, does it make sense to add this to all dm targets? Or any better idea?

Milan

In the targets that I have written, I tried to protect against this:

	if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start)

	ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors)
	if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {

	wc->start_sector = start_sector;
	if (wc->start_sector != start_sector

If you find some more unprotected cases, it's OK to convert them.

Mikulas

author
#4 lore raw

PATCH v2 1/1 dm: Check for device sector overflow if CONFIG_LBDAF is not set

Reference to a device in device-mapper table contains offset in sectors.

If the sector_t is 32bit integer (CONFIG_LBDAF is not set), then several device-mapper targets can overflow this offset and validity check is then performed on a wrong offset and a wrong table is activated.

See for example (on 32bit without CONFIG_LBDAF) this overflow:

  # dmsetup create test --table "0 2048 linear /dev/sdg 4294967297"
  # dmsetup table test
  0 2048 linear 8:96 1

This patch adds explicit check for overflow if the offset is sector_t type.

---
 drivers/md/dm-crypt.c    | 2 +-
 drivers/md/dm-delay.c    | 2 +-
 drivers/md/dm-flakey.c   | 2 +-
 drivers/md/dm-linear.c   | 2 +-
 drivers/md/dm-raid1.c    | 3 ++-
 drivers/md/dm-unstripe.c | 2 +-
 6 files changed, 7 insertions(+), 6 deletions(-)
drivers/md/dm-crypt.c +1 -1
index 49be7a6a2e81..a41fe7975dc6 100644
@@ -2781,7 +2781,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	}
 
 	ret = -EINVAL;
-	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
+	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
drivers/md/dm-delay.c +1 -1
index 2fb7bb4304ad..fddffe251bf6 100644
@@ -141,7 +141,7 @@ static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **a
 	unsigned long long tmpll;
 	char dummy;
 
-	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
+	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
 		ti->error = "Invalid device sector";
 		return -EINVAL;
 	}
drivers/md/dm-flakey.c +1 -1
index 3cb97fa4c11d..8261aa8c7fe1 100644
@@ -213,7 +213,7 @@ static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	devname = dm_shift_arg(&as);
 
 	r = -EINVAL;
-	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
+	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
drivers/md/dm-linear.c +1 -1
index 8d7ddee6ac4d..ad980a38fb1e 100644
@@ -45,7 +45,7 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	}
 
 	ret = -EINVAL;
-	if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1) {
+	if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1 || tmp != (sector_t)tmp) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
drivers/md/dm-raid1.c +2 -1
index 79eab1071ec2..5a51151f680d 100644
@@ -943,7 +943,8 @@ static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
 	char dummy;
 	int ret;
 
-	if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
+	if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
+	    offset != (sector_t)offset) {
 		ti->error = "Invalid offset";
 		return -EINVAL;
 	}
drivers/md/dm-unstripe.c +1 -1
index 954b7ab4e684..e673dacf6418 100644
@@ -78,7 +78,7 @@ static int unstripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto err;
 	}
 
-	if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1) {
+	if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
 		ti->error = "Invalid striped device offset";
 		goto err;
 	}
Signature · 1 line
2.19.1
maintainer Reviewed-by on v2 1/1
#5 lore raw
Quoted 8 lines · Milan Broz

Reference to a device in device-mapper table contains offset in sectors.

If the sector_t is 32bit integer (CONFIG_LBDAF is not set), then several device-mapper targets can overflow this offset and validity check is then performed on a wrong offset and a wrong table is activated.

See for example (on 32bit without CONFIG_LBDAF) this overflow:

Quoted 7 lines
  # dmsetup create test --table "0 2048 linear /dev/sdg 4294967297"
  # dmsetup table test
  0 2048 linear 8:96 1

This patch adds explicit check for overflow if the offset is sector_t type.

Quoted 91 lines
---
 drivers/md/dm-crypt.c    | 2 +-
 drivers/md/dm-delay.c    | 2 +-
 drivers/md/dm-flakey.c   | 2 +-
 drivers/md/dm-linear.c   | 2 +-
 drivers/md/dm-raid1.c    | 3 ++-
 drivers/md/dm-unstripe.c | 2 +-
 6 files changed, 7 insertions(+), 6 deletions(-)
drivers/md/dm-crypt.c +1 -1
index 49be7a6a2e81..a41fe7975dc6 100644
@@ -2781,7 +2781,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	}
 
 	ret = -EINVAL;
-	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
+	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
drivers/md/dm-delay.c +1 -1
index 2fb7bb4304ad..fddffe251bf6 100644
@@ -141,7 +141,7 @@ static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **a
 	unsigned long long tmpll;
 	char dummy;
 
-	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
+	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
 		ti->error = "Invalid device sector";
 		return -EINVAL;
 	}
drivers/md/dm-flakey.c +1 -1
index 3cb97fa4c11d..8261aa8c7fe1 100644
@@ -213,7 +213,7 @@ static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	devname = dm_shift_arg(&as);
 
 	r = -EINVAL;
-	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
+	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
drivers/md/dm-linear.c +1 -1
index 8d7ddee6ac4d..ad980a38fb1e 100644
@@ -45,7 +45,7 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	}
 
 	ret = -EINVAL;
-	if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1) {
+	if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1 || tmp != (sector_t)tmp) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
drivers/md/dm-raid1.c +2 -1
index 79eab1071ec2..5a51151f680d 100644
@@ -943,7 +943,8 @@ static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
 	char dummy;
 	int ret;
 
-	if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
+	if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
+	    offset != (sector_t)offset) {
 		ti->error = "Invalid offset";
 		return -EINVAL;
 	}
drivers/md/dm-unstripe.c +1 -1
index 954b7ab4e684..e673dacf6418 100644
@@ -78,7 +78,7 @@ static int unstripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto err;
 	}
 
-	if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1) {
+	if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
 		ti->error = "Invalid striped device offset";
 		goto err;
 	}
Signature · 1 line
2.19.1