Skip to contentSkip to conversation

common: state: check length

PATCH Superseded Jan Remmet 2 participants 4 messages May 21, 2019 – May 23, 2019

lore
Share & tools
lore

Jan Remmet

author
#0 lore raw

PATCH 1/1 common: state: check length

if written_length is read from a partial written bucket it may be to big and xmalloc will panic barebox.

Check if the value is sane.

---
 common/state/backend_bucket_direct.c | 4 ++++
 1 file changed, 4 insertions(+)
common/state/backend_bucket_direct.c +4 -0
index 95ddb9310685..fc633eea8dec 100644
@@ -67,6 +67,10 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 	}
 	if (meta.magic == direct_magic) {
 		read_len = meta.written_length;
+		if (read_len < 0 || read_len > direct->max_size) {
+			dev_err(direct->dev, "Wrong length in meta data\n");
+			return -EINVAL;
+		}
 	} else {
 		if (meta.magic != ~0 && !!meta.magic)
 			bucket->wrong_magic = 1;
Signature · 7 lines
2.7.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw
Quoted 15 lines · Jan Remmet

if written_length is read from a partial written bucket it may be to big and xmalloc will panic barebox.

Check if the value is sane.

---
 common/state/backend_bucket_direct.c | 4 ++++
 1 file changed, 4 insertions(+)
common/state/backend_bucket_direct.c +0 -0
index 95ddb9310685..fc633eea8dec 100644

common/state/backend_bucket_direct.c static int state_backend_bucket_direct_read(struct state_backend_storage_bucket · line 70

Quoted 4 lines
common/state/backend_bucket_direct.c +1 -0
@@ -67,6 +67,10 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 	}
 	if (meta.magic == direct_magic) {
 		read_len = meta.written_length;
+		if (read_len < 0 || read_len > direct->max_size) {

written_length in struct state_backend_storage_bucket_direct_meta is unsigned and so should be read_len. Then you can skip the read_len < 0 check.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author
#2 lore raw

PATCH v2 1/1 common: state: check length

if written_length is read from a partial written bucket it may be to big and xmalloc will panic barebox.

Check if the value is sane. Make read_len unsigned to avoid negative values.

---
v2: replace compare < 0 by making read_len unsigned

 common/state/backend_bucket_direct.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
common/state/backend_bucket_direct.c +6 -1
index 95ddb9310685..0dbd334db821 100644
@@ -52,7 +52,7 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 	struct state_backend_storage_bucket_direct *direct =
 	    get_bucket_direct(bucket);
 	struct state_backend_storage_bucket_direct_meta meta;
-	ssize_t read_len;
+	uint32_t read_len;
 	void *buf;
 	int ret;
 
@@ -67,6 +67,11 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 	}
 	if (meta.magic == direct_magic) {
 		read_len = meta.written_length;
+		if (read_len > direct->max_size) {
+			dev_err(direct->dev, "Wrong length in meta data\n");
+			return -EINVAL;
+
+		}
 	} else {
 		if (meta.magic != ~0 && !!meta.magic)
 			bucket->wrong_magic = 1;
Signature · 7 lines
2.7.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#3 lore raw
Quoted 9 lines · Jan Remmet

if written_length is read from a partial written bucket it may be to big and xmalloc will panic barebox.

Check if the value is sane. Make read_len unsigned to avoid negative values.

---
v2: replace compare < 0 by making read_len unsigned

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox