Skip to contentSkip to conversation

trace-cmd: Fix segmentation fault in tracecmd_read_at() in specific use case

PATCH Superseded Tzvetomir Stoyanov (VMware) 2 participants 2 messages Oct 11, 2019 – Oct 14, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace-cmd: Fix segmentation fault in tracecmd_read_at() in specific use case

There is a segmentation fault in update_page_info() when the requested page is not loaded, handle->cpu_data[cpu].page is NULL. The problematic flow starts from tracecmd_read_at() API, when reading offset in the first page (less than 4K), and this page is still not loaded yet. The problem can be observed randomly - there is a sporadic KernelShark crash when loading a file, browsing and zooming events.

https://bugzilla.kernel.org/show_bug.cgi?id=205165

---
 lib/trace-cmd/trace-input.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +2 -1
index 6102eb3..da77418 100644
@@ -1278,7 +1278,8 @@ tracecmd_read_at(struct tracecmd_input *handle, unsigned long long offset,
 	/* check to see if we have this page already */
 	for (cpu = 0; cpu < handle->cpus; cpu++) {
 		if (handle->cpu_data[cpu].offset == page_offset &&
-		    handle->cpu_data[cpu].file_size)
+		    handle->cpu_data[cpu].file_size &&
+		    handle->cpu_data[cpu].page)
 			break;
 	}
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 18 lines · On Fri, 11 Oct 2019 14:33:53 +0300 "Tzvetomir Stoyanov (VMware)"…

There is a segmentation fault in update_page_info() when the requested page is not loaded, handle->cpu_data[cpu].page is NULL. The problematic flow starts from tracecmd_read_at() API, when reading offset in the first page (less than 4K), and this page is still not loaded yet. The problem can be observed randomly - there is a sporadic KernelShark crash when loading a file, browsing and zooming events.

https://bugzilla.kernel.org/show_bug.cgi?id=205165

---
 lib/trace-cmd/trace-input.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +0 -0
index 6102eb3..da77418 100644

lib/trace-cmd/trace-input.c tracecmd_read_at(struct tracecmd_input *handle, unsigned long long offset, · line 1285

Quoted 9 lines
lib/trace-cmd/trace-input.c +2 -1
@@ -1278,7 +1278,8 @@ tracecmd_read_at(struct tracecmd_input *handle, unsigned long long offset,
 	/* check to see if we have this page already */
 	for (cpu = 0; cpu < handle->cpus; cpu++) {
 		if (handle->cpu_data[cpu].offset == page_offset &&
-		    handle->cpu_data[cpu].file_size)
+		    handle->cpu_data[cpu].file_size &&
+		    handle->cpu_data[cpu].page)
 			break;
 	}
 

This does indeed look like a legit bug. But instead of checking here for page not existing, since it's not part of the criteria for finding the page (if the offset matches, we still want to break), lets do the check below:

	if (cpu < handle->cpus && handle->cpu_data[cpu].page) {
		if (pcpu)
			*pcpu = cpu;
		return read_event(handle, offset, cpu);
	} else
		return find_and_read_event(handle, offset, pcpu);

-- Steve