Skip to contentSkip to conversation

tracing: avoid string overflow

PATCH In review Arnd Bergmann 3 participants 3 messages Mar 28, 2018 – Apr 2, 2018 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: avoid string overflow

'err' is used as a NUL-terminated string, but using strncpy() with the length equal to the buffer size may result in lack of the termination:

kernel/trace/trace_events_hist.c: In function 'hist_err_event':
kernel/trace/trace_events_hist.c:396:3: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(err, var, MAX_FILTER_STR_VAL);

This changes it to use the safer strscpy() instead.

Fixes f404da6e1d46 ("tracing: Add 'last error' error facility for hist triggers")
---
 kernel/trace/trace_events_hist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +1 -1
index 4f027642ceef..8357f36d7a1e 100644
@@ -393,7 +393,7 @@ static void hist_err_event(char *str, char *system, char *event, char *var)
 	else if (system)
 		snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
 	else
-		strncpy(err, var, MAX_FILTER_STR_VAL);
+		strscpy(err, var, MAX_FILTER_STR_VAL);
 
 	hist_err(str, err);
 }
Signature · 1 line
2.9.0
Acked-by on v1 1/1
#1 lore raw

Hi Arnd,

Quoted 20 lines · Arnd Bergmann

'err' is used as a NUL-terminated string, but using strncpy() with the length equal to the buffer size may result in lack of the termination:

kernel/trace/trace_events_hist.c: In function 'hist_err_event':
kernel/trace/trace_events_hist.c:396:3: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(err, var, MAX_FILTER_STR_VAL);

This changes it to use the safer strscpy() instead.

Fixes f404da6e1d46 ("tracing: Add 'last error' error facility for hist triggers")
---
 kernel/trace/trace_events_hist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +0 -0
index 4f027642ceef..8357f36d7a1e 100644

kernel/trace/trace_events_hist.c static void hist_err_event(char *str, char *system, char *event, char *var) · line 399

Quoted 8 lines
kernel/trace/trace_events_hist.c +1 -1
@@ -393,7 +393,7 @@ static void hist_err_event(char *str, char *system, char *event, char *var)
 	else if (system)
 		snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
 	else
-		strncpy(err, var, MAX_FILTER_STR_VAL);
+		strscpy(err, var, MAX_FILTER_STR_VAL);
 
 	hist_err(str, err);
 }

Yes, thanks for finding this, and for the patch!

maintainer
#2 lore raw
In reply to Arnd Bergmann
Quoted 11 lines · On Wed, 28 Mar 2018 16:09:10 +0200 Arnd Bergmann <[email protected]> wrote:

'err' is used as a NUL-terminated string, but using strncpy() with the length equal to the buffer size may result in lack of the termination:

kernel/trace/trace_events_hist.c: In function 'hist_err_event':
kernel/trace/trace_events_hist.c:396:3: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(err, var, MAX_FILTER_STR_VAL);

This changes it to use the safer strscpy() instead.

Fixes f404da6e1d46 ("tracing: Add 'last error' error facility for hist triggers")

Thanks! I pulled this in and will push it to git after testing has succeeded.

-- Steve

Quoted 17 lines
---
 kernel/trace/trace_events_hist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +1 -1
index 4f027642ceef..8357f36d7a1e 100644
@@ -393,7 +393,7 @@ static void hist_err_event(char *str, char *system, char *event, char *var)
 	else if (system)
 		snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
 	else
-		strncpy(err, var, MAX_FILTER_STR_VAL);
+		strscpy(err, var, MAX_FILTER_STR_VAL);
 
 	hist_err(str, err);
 }