Skip to contentSkip to conversation

tty: nozomi: avoid sprintf buffer overflow

PATCH Superseded Arnd Bergmann 2 participants 3 messages Nov 28, 2016 – Nov 29, 2016 TTY layer and serial drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: nozomi: avoid sprintf buffer overflow

Testing with a gcc-7 snapshot produced an internal compiler error for this file:

drivers/tty/nozomi.c: In function 'receive_flow_control':
drivers/tty/nozomi.c:919:12: internal compiler error: in get_substring_ranges_for_loc, at input.c:1388
static int receive_flow_control(struct nozomi *dc)

I've reported this at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78569
but also noticed that the code line contains a stack overflow, as it prints a string into a slightly shorter fixed-length 'tmp' variable.

I don't see any point in the temporary variable, we can simply use pr_debug() to print the output directly. This change should not change any of the output but avoids both the stack overflow and the gcc crash.

The stack overflow will not happen unless a module load parameter is also set to enable the debug messages.

---
 drivers/tty/nozomi.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/tty/nozomi.c +2 -7
index e2020a691058..17a182b28d52 100644
@@ -69,13 +69,8 @@
 #define NOZOMI_DEBUG_LEVEL 0x00
 
 #define P_BUF_SIZE 128
-#define NFO(_err_flag_, args...)				\
-do {								\
-	char tmp[P_BUF_SIZE];					\
-	snprintf(tmp, sizeof(tmp), ##args);			\
-	printk(_err_flag_ "[%d] %s(): %s\n", __LINE__,		\
-		__func__, tmp);				\
-} while (0)
+#define NFO(_err_flag_, fmt, args...)					\
+	pr_debug("[%d] %s(): " fmt "\n", __LINE__, __func__,  ##args);
 
 #define DBG1(args...) D_(0x01, ##args)
 #define DBG2(args...) D_(0x02, ##args)
Signature · 1 line
2.9.0
#1 lore raw
Quoted 31 lines · Arnd Bergmann

Testing with a gcc-7 snapshot produced an internal compiler error for this file:

drivers/tty/nozomi.c: In function 'receive_flow_control':
drivers/tty/nozomi.c:919:12: internal compiler error: in get_substring_ranges_for_loc, at input.c:1388
static int receive_flow_control(struct nozomi *dc)

I've reported this at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78569
but also noticed that the code line contains a stack overflow, as it prints a string into a slightly shorter fixed-length 'tmp' variable.

I don't see any point in the temporary variable, we can simply use pr_debug() to print the output directly. This change should not change any of the output but avoids both the stack overflow and the gcc crash.

The stack overflow will not happen unless a module load parameter is also set to enable the debug messages.

---
 drivers/tty/nozomi.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/tty/nozomi.c +0 -0
index e2020a691058..17a182b28d52 100644
@@ -69,13 +69,8 @@
 #define NOZOMI_DEBUG_LEVEL 0x00
 

drivers/tty/nozomi.c line 73

Quoted 10 lines
drivers/tty/nozomi.c +2 -7
 #define P_BUF_SIZE 128
-#define NFO(_err_flag_, args...)				\
-do {								\
-	char tmp[P_BUF_SIZE];					\
-	snprintf(tmp, sizeof(tmp), ##args);			\
-	printk(_err_flag_ "[%d] %s(): %s\n", __LINE__,		\
-		__func__, tmp);				\
-} while (0)
+#define NFO(_err_flag_, fmt, args...)					\
+	pr_debug("[%d] %s(): " fmt "\n", __LINE__, __func__,  ##args);

Could you also kill the now unused _err_flag_ parameter (including the only user of NFO)?

(Or inline pr_debug directly into the only user and kill NFO completely?)

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 3 lines · CET Jiri Slaby

Could you also kill the now unused _err_flag_ parameter (including the only user of NFO)?

Right. I was trying to avoid taking the cleanup too far, lacking a way to properly test it, but that would have been an obvious change.

Quoted 1 line

(Or inline pr_debug directly into the only user and kill NFO completely?)

I'll send both variants and let you pick one or the other.

Arnd