Skip to contentSkip to conversation

staging: gpib: avoid buffer overflow

PATCH In review Kees Bakker 2 participants 3 messages Oct 17, 2024 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: gpib: avoid buffer overflow

The remaining buffer size for snprintf also depends on pos.

---
 drivers/staging/gpib/ni_usb/ni_usb_gpib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gpib/ni_usb/ni_usb_gpib.c +1 -1
index 1da263676f2a..4df1ceaa138f 100644
@@ -364,7 +364,7 @@ static void ni_usb_dump_raw_block(const u8 *raw_data, int length)
 			pr_info("%s\n", print_buf);
 			pos = 0;
 		}
-		pos += snprintf(&print_buf[pos], RAW_BUF_SIZE, " %02x", raw_data[i]);
+		pos += snprintf(&print_buf[pos], RAW_BUF_SIZE - pos, " %02x", raw_data[i]);
 	}
 	if (pos)
 		pr_info("%s\n", print_buf);
Signature · 1 line
2.47.0
maintainer
#1 lore raw
Quoted 12 lines · Kees Bakker

The remaining buffer size for snprintf also depends on pos.

---
 drivers/staging/gpib/ni_usb/ni_usb_gpib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gpib/ni_usb/ni_usb_gpib.c +0 -0
index 1da263676f2a..4df1ceaa138f 100644

drivers/staging/gpib/ni_usb/ni_usb_gpib.c static void ni_usb_dump_raw_block(const u8 *raw_data, int length) · line 370

Quoted 8 lines
drivers/staging/gpib/ni_usb/ni_usb_gpib.c +1 -1
@@ -364,7 +364,7 @@ static void ni_usb_dump_raw_block(const u8 *raw_data, int length)
 			pr_info("%s\n", print_buf);
 			pos = 0;
 		}
-		pos += snprintf(&print_buf[pos], RAW_BUF_SIZE, " %02x", raw_data[i]);
+		pos += snprintf(&print_buf[pos], RAW_BUF_SIZE - pos, " %02x", raw_data[i]);
 	}
 	if (pos)
 		pr_info("%s\n", print_buf);

The better thing to do would be to delete this whole function and just use the in-kernel hex_dump_to_buffer() function instead, which handles all of this logic properly.

Can you do that instead?

thanks,

greg k-h

maintainer
#2 lore raw
Quoted 26 lines · Greg KH
Quoted 12 lines · Kees Bakker

The remaining buffer size for snprintf also depends on pos.

---
 drivers/staging/gpib/ni_usb/ni_usb_gpib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gpib/ni_usb/ni_usb_gpib.c +0 -0
index 1da263676f2a..4df1ceaa138f 100644

drivers/staging/gpib/ni_usb/ni_usb_gpib.c static void ni_usb_dump_raw_block(const u8 *raw_data, int length) · line 370

Quoted 8 lines
drivers/staging/gpib/ni_usb/ni_usb_gpib.c +1 -1
@@ -364,7 +364,7 @@ static void ni_usb_dump_raw_block(const u8 *raw_data, int length)
 			pr_info("%s\n", print_buf);
 			pos = 0;
 		}
-		pos += snprintf(&print_buf[pos], RAW_BUF_SIZE, " %02x", raw_data[i]);
+		pos += snprintf(&print_buf[pos], RAW_BUF_SIZE - pos, " %02x", raw_data[i]);
 	}
 	if (pos)
 		pr_info("%s\n", print_buf);

The better thing to do would be to delete this whole function and just use the in-kernel hex_dump_to_buffer() function instead, which handles all of this logic properly.

Quoted 1 line

Can you do that instead?

Oops, I mean print_hex_dump().

thanks,

greg k-h