mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] trace-cmd: Add check for truncated files
@ 2011-06-21  0:19 Vaibhav Nagarnaik
  2011-06-21 19:27 ` Steven Rostedt
  0 siblings, 1 reply; 2+ messages in thread
From: Vaibhav Nagarnaik @ 2011-06-21  0:19 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Michael Rubin, David Sharp, linux-kernel, Vaibhav Nagarnaik

If the trace data file that is being read is truncated, mmap'ing its
non-existent page does not give any error. When the page is read, the
process dies with a SIGBUS.

This patch adds a file size check while initializing the trace data and
makes sure that the file size matches the expected size; otherwise an
error is returned.

Signed-off-by: Vaibhav Nagarnaik <vnagarnaik@google.com>
---
 trace-input.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/trace-input.c b/trace-input.c
index f537ef5..723d47f 100644
--- a/trace-input.c
+++ b/trace-input.c
@@ -92,6 +92,7 @@ struct tracecmd_input {
 	size_t			header_files_start;
 	size_t			ftrace_files_start;
 	size_t			event_files_start;
+	size_t			total_file_size;
 };
 
 __thread struct tracecmd_input *tracecmd_curr_thread_handle;
@@ -2085,6 +2086,15 @@ int tracecmd_init_data(struct tracecmd_input *handle)
 		handle->cpu_data[cpu].file_offset = offset;
 		handle->cpu_data[cpu].file_size = size;
 
+		if (offset + size > handle->total_file_size) {
+			/* this happens if the file got truncated */
+			printf("File possibly truncated. "
+				"Need at least %llu, but file size is %lu.\n",
+				offset + size, handle->total_file_size);
+			errno = EINVAL;
+			return -1;
+		}
+
 		if (init_cpu(handle, cpu))
 			return -1;
 	}
@@ -2189,6 +2199,12 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd)
 	handle->header_files_start =
 		lseek64(handle->fd, 0, SEEK_CUR);
 
+	handle->total_file_size =
+		lseek64(handle->fd, 0, SEEK_END);
+
+	handle->header_files_start =
+		lseek64(handle->fd, handle->header_files_start, SEEK_SET);
+
 	return handle;
 
  failed_read:
-- 
1.7.3.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] trace-cmd: Add check for truncated files
  2011-06-21  0:19 [PATCH] trace-cmd: Add check for truncated files Vaibhav Nagarnaik
@ 2011-06-21 19:27 ` Steven Rostedt
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2011-06-21 19:27 UTC (permalink / raw)
  To: Vaibhav Nagarnaik; +Cc: Michael Rubin, David Sharp, linux-kernel

On Mon, 2011-06-20 at 17:19 -0700, Vaibhav Nagarnaik wrote:
> If the trace data file that is being read is truncated, mmap'ing its
> non-existent page does not give any error. When the page is read, the
> process dies with a SIGBUS.
> 
> This patch adds a file size check while initializing the trace data and
> makes sure that the file size matches the expected size; otherwise an
> error is returned.

Thanks Vaibhav,

I'll queue this up.

-- Steve



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-06-21 19:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-21  0:19 [PATCH] trace-cmd: Add check for truncated files Vaibhav Nagarnaik
2011-06-21 19:27 ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®