Skip to contentSkip to conversation

image: Protect against overflow in unknown_msg()

PATCH Superseded Simon Glass 2 participants 3 messages Oct 28, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 2/2 image: Protect against overflow in unknown_msg()

Coverity complains that this can overflow. If we later increase the size of one of the strings in the table, it could happen.

Adjust the code to protect against this.

Reported-by Coverity (CID: 150964)

---

Changes in v2:
- Drop unwanted #include

 common/image.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/image.c +1 -1
index 0e86c13..4255267 100644
@@ -590,7 +590,7 @@ static const char *unknown_msg(enum ih_category category)
 	static char msg[30];
 
 	strcpy(msg, "Unknown ");
-	strcat(msg, table_info[category].desc);
+	strncat(msg, table_info[category].desc, sizeof(msg) - 1);
 
 	return msg;
 }
Signature · 1 line
2.8.0.rc3.226.g39d4020
#1 lore raw

PATCH v2 2/2 image: Protect against overflow in unknown_msg()

Quoted 20 lines · On Thu, 27 Oct 2016 18:38:40 -0600 Simon Glass <[email protected]> wrote:

Coverity complains that this can overflow. If we later increase the size of one of the strings in the table, it could happen.

Adjust the code to protect against this.

Reported-by Coverity (CID: 150964)

---

Changes in v2:
- Drop unwanted #include

 common/image.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/image.c +0 -0
index 0e86c13..4255267 100644

common/image.c static const char *unknown_msg(enum ih_category category) · line 593

Quoted 5 lines
common/image.c +1 -1
@@ -590,7 +590,7 @@ static const char *unknown_msg(enum ih_category category)
 	static char msg[30];
 
 	strcpy(msg, "Unknown ");
-	strcat(msg, table_info[category].desc);
+	strncat(msg, table_info[category].desc, sizeof(msg) - 1);

"man strncat" on my system says:

char *strncat(char *dest, const char *src, size_t n);

...

If src contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.

Quoted 3 lines
common/image.c +0 -0
 	return msg;
 }
Signature · 2 lines
Best regards,
Siarhei Siamashka
#2 lore raw

PATCH v2 2/2 image: Protect against overflow in unknown_msg()

Hi,

Quoted 31 lines · Siarhei Siamashka
Quoted 20 lines · On Thu, 27 Oct 2016 18:38:40 -0600 Simon Glass <[email protected]> wrote:

Coverity complains that this can overflow. If we later increase the size of one of the strings in the table, it could happen.

Adjust the code to protect against this.

Reported-by Coverity (CID: 150964)

---

Changes in v2:
- Drop unwanted #include

 common/image.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/image.c +0 -0
index 0e86c13..4255267 100644

common/image.c static const char *unknown_msg(enum ih_category category) · line 593

Quoted 5 lines
common/image.c +1 -1
@@ -590,7 +590,7 @@ static const char *unknown_msg(enum ih_category category)
      static char msg[30];
 
      strcpy(msg, "Unknown ");
-     strcat(msg, table_info[category].desc);
+     strncat(msg, table_info[category].desc, sizeof(msg) - 1);

"man strncat" on my system says:

Quoted 12 lines

char *strncat(char *dest, const char *src, size_t n);

...

If src contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.

Quoted 3 lines
common/image.c +0 -0
      return msg;
 }

That's odd as it does not seem to match the U-Boot implementation. But neither does my code in fact. I'll try v3.

I suspect U-Boot's string functions could use some tests!

Regards,
Simon