Skip to contentSkip to conversation

kernel: events: strncpy, always be sure of NUL terminated.

PATCH New Chen Gang 2 participants 2 messages Apr 8, 2013 perf events

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 kernel: events: strncpy, always be sure of NUL terminated.

for NUL terminated string, always be sure of '\0' at the end.

in our case, need return value, so still use strncpy
(strlcpy return the size, not the pointer)

---
 kernel/events/core.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/events/core.c +2 -1
index 4757941..bdc0e25 100644
@@ -4752,7 +4752,8 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
 	} else {
 		if (arch_vma_name(mmap_event->vma)) {
 			name = strncpy(tmp, arch_vma_name(mmap_event->vma),
-				       sizeof(tmp));
+				       sizeof(tmp) - 1);
+			tmp[sizeof(tmp) - 1] = '\0';
 			goto got_name;
 		}
 
Signature · 1 line
1.7.7.6
#1 lore raw

[tip:perf/urgent] perf: Fix strncpy() use, always make sure it' s NUL terminated

perf: Fix strncpy() use, always make sure it's NUL terminated
Commit-ID:  c97847d2f0eb77c806e650e04d9bbcf79fa05730
Gitweb:     http://git.kernel.org/tip/c97847d2f0eb77c806e650e04d9bbcf79fa05730
Author:     Chen Gang <[email protected]>
AuthorDate: Mon, 8 Apr 2013 11:48:27 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 8 Apr 2013 13:26:55 +0200

perf: Fix strncpy() use, always make sure it's NUL terminated

For NUL terminated string, always make sure that there's '\0' at the end.

In our case we need a return value, so still use strncpy() and fix up the tail explicitly.

(strlcpy() returns the size, not the pointer)

---
 kernel/events/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/core.c +2 -1
index 59412d0..7f0d67e 100644
@@ -4737,7 +4737,8 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
 	} else {
 		if (arch_vma_name(mmap_event->vma)) {
 			name = strncpy(tmp, arch_vma_name(mmap_event->vma),
-				       sizeof(tmp));
+				       sizeof(tmp) - 1);
+			tmp[sizeof(tmp) - 1] = '\0';
 			goto got_name;
 		}