Skip to content

Commit 88903c4

Browse files
mhiramatrostedt
authored andcommitted
tracing/probe: Add ustring type for user-space string
Add "ustring" type for fetching user-space string from kprobe event. User can specify ustring type at uprobe event, and it is same as "string" for uprobe. Note that probe-event provides this option but it doesn't choose the correct type automatically since we have not way to decide the address is in user-space or not on some arch (and on some other arch, you can fetch the string by "string" type). So user must carefully check the target code (e.g. if you see __user on the target variable) and use this new type. Link: http://lkml.kernel.org/r/155789871009.26965.14167558859557329331.stgit@devnote2 Acked-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 3d70818 commit 88903c4

7 files changed

Lines changed: 84 additions & 10 deletions

File tree

‎Documentation/trace/kprobetrace.rst‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ Synopsis of kprobe_events
5555
NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
5656
FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
5757
(u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
58-
(x8/x16/x32/x64), "string" and bitfield are supported.
58+
(x8/x16/x32/x64), "string", "ustring" and bitfield
59+
are supported.
5960

6061
(\*1) only for the probe on function entry (offs == 0).
6162
(\*2) only for return probe.
@@ -77,7 +78,11 @@ apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
7778
wrong, but '+8($stack):x8[8]' is OK.)
7879
String type is a special type, which fetches a "null-terminated" string from
7980
kernel space. This means it will fail and store NULL if the string container
80-
has been paged out.
81+
has been paged out. "ustring" type is an alternative of string for user-space.
82+
Note that kprobe-event provides string/ustring types, but doesn't change it
83+
automatically. So user has to decide if the targe string in kernel or in user
84+
space carefully. On some arch, if you choose wrong one, it always fails to
85+
record string data.
8186
The string array type is a bit different from other types. For other base
8287
types, <base-type>[1] is equal to <base-type> (e.g. +0(%di):x32[1] is same
8388
as +0(%di):x32.) But string[1] is not equal to string. The string type itself

‎kernel/trace/trace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4847,7 +4847,7 @@ static const char readme_msg[] =
48474847
"\t $stack<index>, $stack, $retval, $comm\n"
48484848
#endif
48494849
"\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
4850-
"\t b<bit-width>@<bit-offset>/<container-size>,\n"
4850+
"\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
48514851
"\t <type>\\[<array-size>\\]\n"
48524852
#ifdef CONFIG_HIST_TRIGGERS
48534853
"\t field: <stype> <name>;\n"

‎kernel/trace/trace_kprobe.c‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,15 @@ fetch_store_strlen(unsigned long addr)
886886
return (ret < 0) ? ret : len;
887887
}
888888

889+
/* Return the length of string -- including null terminal byte */
890+
static nokprobe_inline int
891+
fetch_store_strlen_user(unsigned long addr)
892+
{
893+
const void __user *uaddr = (__force const void __user *)addr;
894+
895+
return strnlen_unsafe_user(uaddr, MAX_STRING_SIZE);
896+
}
897+
889898
/*
890899
* Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
891900
* length and relative data location.
@@ -894,19 +903,46 @@ static nokprobe_inline int
894903
fetch_store_string(unsigned long addr, void *dest, void *base)
895904
{
896905
int maxlen = get_loc_len(*(u32 *)dest);
897-
u8 *dst = get_loc_data(dest, base);
906+
void *__dest;
898907
long ret;
899908

900909
if (unlikely(!maxlen))
901910
return -ENOMEM;
911+
912+
__dest = get_loc_data(dest, base);
913+
902914
/*
903915
* Try to get string again, since the string can be changed while
904916
* probing.
905917
*/
906-
ret = strncpy_from_unsafe(dst, (void *)addr, maxlen);
918+
ret = strncpy_from_unsafe(__dest, (void *)addr, maxlen);
919+
if (ret >= 0)
920+
*(u32 *)dest = make_data_loc(ret, __dest - base);
921+
922+
return ret;
923+
}
907924

925+
/*
926+
* Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
927+
* with max length and relative data location.
928+
*/
929+
static nokprobe_inline int
930+
fetch_store_string_user(unsigned long addr, void *dest, void *base)
931+
{
932+
const void __user *uaddr = (__force const void __user *)addr;
933+
int maxlen = get_loc_len(*(u32 *)dest);
934+
void *__dest;
935+
long ret;
936+
937+
if (unlikely(!maxlen))
938+
return -ENOMEM;
939+
940+
__dest = get_loc_data(dest, base);
941+
942+
ret = strncpy_from_unsafe_user(__dest, uaddr, maxlen);
908943
if (ret >= 0)
909-
*(u32 *)dest = make_data_loc(ret, (void *)dst - base);
944+
*(u32 *)dest = make_data_loc(ret, __dest - base);
945+
910946
return ret;
911947
}
912948

‎kernel/trace/trace_probe.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static const struct fetch_type probe_fetch_types[] = {
7878
/* Special types */
7979
__ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
8080
"__data_loc char[]"),
81+
__ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1,
82+
"__data_loc char[]"),
8183
/* Basic types */
8284
ASSIGN_FETCH_TYPE(u8, u8, 0),
8385
ASSIGN_FETCH_TYPE(u16, u16, 0),
@@ -569,7 +571,8 @@ static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
569571
goto fail;
570572

571573
/* Store operation */
572-
if (!strcmp(parg->type->name, "string")) {
574+
if (!strcmp(parg->type->name, "string") ||
575+
!strcmp(parg->type->name, "ustring")) {
573576
if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
574577
code->op != FETCH_OP_COMM) {
575578
trace_probe_log_err(offset + (t ? (t - arg) : 0),
@@ -590,7 +593,11 @@ static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
590593
goto fail;
591594
}
592595
}
593-
code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
596+
/* If op == DEREF, replace it with STRING */
597+
if (!strcmp(parg->type->name, "ustring"))
598+
code->op = FETCH_OP_ST_USTRING;
599+
else
600+
code->op = FETCH_OP_ST_STRING;
594601
code->size = parg->type->size;
595602
parg->dynamic = true;
596603
} else if (code->op == FETCH_OP_DEREF) {
@@ -618,7 +625,8 @@ static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
618625
/* Loop(Array) operation */
619626
if (parg->count) {
620627
if (scode->op != FETCH_OP_ST_MEM &&
621-
scode->op != FETCH_OP_ST_STRING) {
628+
scode->op != FETCH_OP_ST_STRING &&
629+
scode->op != FETCH_OP_ST_USTRING) {
622630
trace_probe_log_err(offset + (t ? (t - arg) : 0),
623631
BAD_STRING);
624632
ret = -EINVAL;

‎kernel/trace/trace_probe.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ enum fetch_op {
9696
FETCH_OP_ST_RAW, /* Raw: .size */
9797
FETCH_OP_ST_MEM, /* Mem: .offset, .size */
9898
FETCH_OP_ST_STRING, /* String: .offset, .size */
99+
FETCH_OP_ST_USTRING, /* User String: .offset, .size */
99100
// Stage 4 (modify) op
100101
FETCH_OP_MOD_BF, /* Bitfield: .basesize, .lshift, .rshift */
101102
// Stage 5 (loop) op

‎kernel/trace/trace_probe_tmpl.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs,
5959
static nokprobe_inline int fetch_store_strlen(unsigned long addr);
6060
static nokprobe_inline int
6161
fetch_store_string(unsigned long addr, void *dest, void *base);
62+
static nokprobe_inline int fetch_store_strlen_user(unsigned long addr);
63+
static nokprobe_inline int
64+
fetch_store_string_user(unsigned long addr, void *dest, void *base);
6265
static nokprobe_inline int
6366
probe_mem_read(void *dest, void *src, size_t size);
6467

@@ -91,6 +94,10 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
9194
ret = fetch_store_strlen(val + code->offset);
9295
code++;
9396
goto array;
97+
} else if (code->op == FETCH_OP_ST_USTRING) {
98+
ret += fetch_store_strlen_user(val + code->offset);
99+
code++;
100+
goto array;
94101
} else
95102
return -EILSEQ;
96103
}
@@ -106,6 +113,10 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
106113
loc = *(u32 *)dest;
107114
ret = fetch_store_string(val + code->offset, dest, base);
108115
break;
116+
case FETCH_OP_ST_USTRING:
117+
loc = *(u32 *)dest;
118+
ret = fetch_store_string_user(val + code->offset, dest, base);
119+
break;
109120
default:
110121
return -EILSEQ;
111122
}
@@ -123,7 +134,8 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
123134
total += ret;
124135
if (++i < code->param) {
125136
code = s3;
126-
if (s3->op != FETCH_OP_ST_STRING) {
137+
if (s3->op != FETCH_OP_ST_STRING &&
138+
s3->op != FETCH_OP_ST_USTRING) {
127139
dest += s3->size;
128140
val += s3->size;
129141
goto stage3;

‎kernel/trace/trace_uprobe.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ fetch_store_string(unsigned long addr, void *dest, void *base)
176176
return ret;
177177
}
178178

179+
static nokprobe_inline int
180+
fetch_store_string_user(unsigned long addr, void *dest, void *base)
181+
{
182+
return fetch_store_string(addr, dest, base);
183+
}
184+
179185
/* Return the length of string -- including null terminal byte */
180186
static nokprobe_inline int
181187
fetch_store_strlen(unsigned long addr)
@@ -191,6 +197,12 @@ fetch_store_strlen(unsigned long addr)
191197
return (len > MAX_STRING_SIZE) ? 0 : len;
192198
}
193199

200+
static nokprobe_inline int
201+
fetch_store_strlen_user(unsigned long addr)
202+
{
203+
return fetch_store_strlen(addr);
204+
}
205+
194206
static unsigned long translate_user_vaddr(unsigned long file_offset)
195207
{
196208
unsigned long base_addr;

0 commit comments

Comments
 (0)