2022-04-01 09:16:53

by Ildar Kamaletdinov

[permalink] [raw]
Subject: [PATCH BlueZ 4/7] tools: Prevent infinity loops in bluemoon.c

In case FW size is too big we can face with infinity while() loops.
According to C99 standart SIZE_MAX could be as small as 65535.

So to prevent overflow of 'firmware_offset' we must limit maximum FW
size that could be processed by bluemoon.

Found by Linux Verification Center (linuxtesting.org) with the SVACE
static analysis tool.
---
tools/bluemoon.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/tools/bluemoon.c b/tools/bluemoon.c
index f50107a2a..729da36f6 100644
--- a/tools/bluemoon.c
+++ b/tools/bluemoon.c
@@ -492,6 +492,13 @@ static void request_firmware(const char *path)
return;
}

+ if (st.st_size > (SIZE_MAX - 4)) {
+ fprintf(stderr, "Firmware size is too big\n");
+ close(fd);
+ shutdown_device();
+ return;
+ }
+
firmware_data = malloc(st.st_size);
if (!firmware_data) {
fprintf(stderr, "Failed to allocate firmware buffer\n");
@@ -874,6 +881,12 @@ static void analyze_firmware(const char *path)
return;
}

+ if (st.st_size > (SIZE_MAX - 3)) {
+ fprintf(stderr, "Firmware size is too big\n");
+ close(fd);
+ return;
+ }
+
firmware_data = malloc(st.st_size);
if (!firmware_data) {
fprintf(stderr, "Failed to allocate firmware buffer\n");
--
2.34.0