History

tracing: Show real address for trace event arguments

To help debugging kernel, show real address for trace event arguments in tracefs/trace{,pipe} instead of hashed pointer value. Since ftrace human-readable format uses vsprintf(), all %p are translated to hash values instead of pointer address. However, when debugging the kernel, raw address value gives a hint when comparing with the memory mapping in the kernel. (Those are sometimes used with crash log, which is not hashed too) So converting %p with %px when calling trace_seq_printf(). Moreover, this is not improving the security because the tracefs can be used only by root user and the raw address values are readable from tracefs/percpu/cpu*/trace_pipe_raw file. Link: https://lkml.kernel.org/r/160277370703.29307.5134475491761971203.stgit@devnote2

Masami Hiramatsu committed 6 years ago

Commit efbbdaa1 parent 7d536755 files · +88 −3Browse files

Unified · Split

5 files
  1. include/linux/trace_events.h +4 −0
  2. include/trace/trace_events.h +1 −1
  3. kernel/trace/trace.c +70 −1
  4. kernel/trace/trace.h +2 −0
  5. kernel/trace/trace_output.c +11 −1
include/linux/trace_events.h+4−0
@@ -55,6 +55,8 @@ struct trace_event; · 54 unmodified lines
5555
5656 int trace_raw_output_prep(struct trace_iterator *iter,
5757 struct trace_event *event);
58+extern __printf(2, 3)
59+void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...);
5860
5961 /*
6062 * The trace entry - the most basic unit of tracing. This is what
@@ -87,6 +89,8 @@ struct trace_iterator { · 26 unmodified lines
8789 unsigned long iter_flags;
8890 void *temp; /* temp holder */
8991 unsigned int temp_size;
92+ char *fmt; /* modified format holder */
93+ unsigned int fmt_size;
9094
9195 /* trace_seq for __print_flags() and __print_symbolic() etc. */
9296 struct trace_seq tmp_seq;
include/trace/trace_events.h+1−1
@@ -364,7 +364,7 @@ trace_raw_output_##call(struct trace_iterator *iter, int flags, \ · 363 unmodified lines
364364 if (ret != TRACE_TYPE_HANDLED) \
365365 return ret; \
366366 \
367− trace_seq_printf(s, print); \
367+ trace_event_printf(iter, print); \
368368 \
369369 return trace_handle_return(s); \
370370 } \
kernel/trace/trace.c+70−1
@@ -3530,6 +3530,62 @@ __find_next_entry(struct trace_iterator *iter, int *ent_cpu, · 3529 unmodified lines
35303530 return next;
35313531 }
35323532
3533+#define STATIC_FMT_BUF_SIZE 128
3534+static char static_fmt_buf[STATIC_FMT_BUF_SIZE];
3535+
3536+static char *trace_iter_expand_format(struct trace_iterator *iter)
3537+{
3538+ char *tmp;
3539+
3540+ if (iter->fmt == static_fmt_buf)
3541+ return NULL;
3542+
3543+ tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE,
3544+ GFP_KERNEL);
3545+ if (tmp) {
3546+ iter->fmt_size += STATIC_FMT_BUF_SIZE;
3547+ iter->fmt = tmp;
3548+ }
3549+
3550+ return tmp;
3551+}
3552+
3553+const char *trace_event_format(struct trace_iterator *iter, const char *fmt)
3554+{
3555+ const char *p, *new_fmt;
3556+ char *q;
3557+
3558+ if (WARN_ON_ONCE(!fmt))
3559+ return fmt;
3560+
3561+ p = fmt;
3562+ new_fmt = q = iter->fmt;
3563+ while (*p) {
3564+ if (unlikely(q - new_fmt + 3 > iter->fmt_size)) {
3565+ if (!trace_iter_expand_format(iter))
3566+ return fmt;
3567+
3568+ q += iter->fmt - new_fmt;
3569+ new_fmt = iter->fmt;
3570+ }
3571+
3572+ *q++ = *p++;
3573+
3574+ /* Replace %p with %px */
3575+ if (p[-1] == '%') {
3576+ if (p[0] == '%') {
3577+ *q++ = *p++;
3578+ } else if (p[0] == 'p' && !isalnum(p[1])) {
3579+ *q++ = *p++;
3580+ *q++ = 'x';
3581+ }
3582+ }
3583+ }
3584+ *q = '\0';
3585+
3586+ return new_fmt;
3587+}
3588+
35333589 #define STATIC_TEMP_BUF_SIZE 128
35343590 static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
35353591
@@ -4322,6 +4378,16 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot) · 786 unmodified lines
43224378 if (iter->temp)
43234379 iter->temp_size = 128;
43244380
4381+ /*
4382+ * trace_event_printf() may need to modify given format
4383+ * string to replace %p with %px so that it shows real address
4384+ * instead of hash value. However, that is only for the event
4385+ * tracing, other tracer may not need. Defer the allocation
4386+ * until it is needed.
4387+ */
4388+ iter->fmt = NULL;
4389+ iter->fmt_size = 0;
4390+
43254391 /*
43264392 * We make a copy of the current tracer to avoid concurrent
43274393 * changes on it while we are reading.
@@ -4473,6 +4539,7 @@ static int tracing_release(struct inode *inode, struct file *file) · 145 unmodified lines
44734539
44744540 mutex_destroy(&iter->mutex);
44754541 free_cpumask_var(iter->started);
4542+ kfree(iter->fmt);
44764543 kfree(iter->temp);
44774544 kfree(iter->trace);
44784545 kfree(iter->buffer_iter);
@@ -9331,9 +9398,11 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) · 4852 unmodified lines
93319398
93329399 /* Simulate the iterator */
93339400 trace_init_global_iter(&iter);
9334− /* Can not use kmalloc for iter.temp */
9401+ /* Can not use kmalloc for iter.temp and iter.fmt */
93359402 iter.temp = static_temp_buf;
93369403 iter.temp_size = STATIC_TEMP_BUF_SIZE;
9404+ iter.fmt = static_fmt_buf;
9405+ iter.fmt_size = STATIC_FMT_BUF_SIZE;
93379406
93389407 for_each_tracing_cpu(cpu) {
93399408 atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
kernel/trace/trace.h+2−0
@@ -581,6 +581,8 @@ struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, · 580 unmodified lines
581581 void trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
582582 struct ring_buffer_event *event);
583583
584+const char *trace_event_format(struct trace_iterator *iter, const char *fmt);
585+
584586 int trace_empty(struct trace_iterator *iter);
585587
586588 void *trace_find_next_entry_inc(struct trace_iterator *iter);
kernel/trace/trace_output.c+11−1
@@ -312,13 +312,23 @@ int trace_raw_output_prep(struct trace_iterator *iter, · 311 unmodified lines
312312 }
313313 EXPORT_SYMBOL(trace_raw_output_prep);
314314
315+void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...)
316+{
317+ va_list ap;
318+
319+ va_start(ap, fmt);
320+ trace_seq_vprintf(&iter->seq, trace_event_format(iter, fmt), ap);
321+ va_end(ap);
322+}
323+EXPORT_SYMBOL(trace_event_printf);
324+
315325 static int trace_output_raw(struct trace_iterator *iter, char *name,
316326 char *fmt, va_list ap)
317327 {
318328 struct trace_seq *s = &iter->seq;
319329
320330 trace_seq_printf(s, "%s: ", name);
321− trace_seq_vprintf(s, fmt, ap);
331+ trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
322332
323333 return trace_handle_return(s);
324334 }