瀏覽代碼

perf record: Prevent reading invalid data in record__mmap_read

When record__mmap_read() requires data more than the size of ring
buffer, drop those data to avoid accessing invalid memory.

This can happen when reading from overwritable ring buffer, which
should be avoided. However, check this for robustness.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1463987628-163563-3-git-send-email-wangnan0@huawei.com
Signed-off-by: He Kuang <hekuang@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Wang Nan 9 年之前
父節點
當前提交
2d11c65071
共有 1 個文件被更改,包括 8 次插入0 次删除
  1. 8 0
      tools/perf/builtin-record.c

+ 8 - 0
tools/perf/builtin-record.c

@@ -40,6 +40,7 @@
 #include <unistd.h>
 #include <unistd.h>
 #include <sched.h>
 #include <sched.h>
 #include <sys/mman.h>
 #include <sys/mman.h>
+#include <asm/bug.h>
 
 
 
 
 struct record {
 struct record {
@@ -98,6 +99,13 @@ static int record__mmap_read(struct record *rec, int idx)
 	rec->samples++;
 	rec->samples++;
 
 
 	size = head - old;
 	size = head - old;
+	if (size > (unsigned long)(md->mask) + 1) {
+		WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
+
+		md->prev = head;
+		perf_evlist__mmap_consume(rec->evlist, idx);
+		return 0;
+	}
 
 
 	if ((old & md->mask) + size != (head & md->mask)) {
 	if ((old & md->mask) + size != (head & md->mask)) {
 		buf = &data[old & md->mask];
 		buf = &data[old & md->mask];