Skip to content

Commit 2c42cfb

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf: Change zero-padding of strings in perf_event_mmap_event()
Oleg complained about the excessive 0-ing in perf_event_mmap_event(), so try and be smarter about it while keeping it fairly fool proof and avoid leaking random bits out to userspace. Suggested-by: Oleg Nesterov <[email protected]> Signed-off-by: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 3ea2f2b commit 2c42cfb

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

kernel/events/core.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5106,15 +5106,13 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
51065106
unsigned int size;
51075107
char tmp[16];
51085108
char *buf = NULL;
5109-
const char *name;
5110-
5111-
memset(tmp, 0, sizeof(tmp));
5109+
char *name;
51125110

51135111
if (file) {
51145112
struct inode *inode;
51155113
dev_t dev;
51165114

5117-
buf = kzalloc(PATH_MAX, GFP_KERNEL);
5115+
buf = kmalloc(PATH_MAX, GFP_KERNEL);
51185116
if (!buf) {
51195117
name = strncpy(tmp, "//enomem", sizeof(tmp));
51205118
goto got_name;
@@ -5137,7 +5135,7 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
51375135
min = MINOR(dev);
51385136

51395137
} else {
5140-
name = arch_vma_name(vma);
5138+
name = (char *)arch_vma_name(vma);
51415139
if (name) {
51425140
name = strncpy(tmp, name, sizeof(tmp) - 1);
51435141
tmp[sizeof(tmp) - 1] = '\0';
@@ -5160,7 +5158,14 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
51605158
}
51615159

51625160
got_name:
5163-
size = ALIGN(strlen(name)+1, sizeof(u64));
5161+
/*
5162+
* Since our buffer works in 8 byte units we need to align our string
5163+
* size to a multiple of 8. However, we must guarantee the tail end is
5164+
* zero'd out to avoid leaking random bits to userspace.
5165+
*/
5166+
size = strlen(name)+1;
5167+
while (!IS_ALIGNED(size, sizeof(u64)))
5168+
name[size++] = '\0';
51645169

51655170
mmap_event->file_name = name;
51665171
mmap_event->file_size = size;

0 commit comments

Comments
 (0)