Skip to content

Commit 33e22b6

Browse files
rostedtgregkh
authored andcommitted
tracing: Add ustring operation to filtering string pointers
[ Upstream commit f37c3bb ] Since referencing user space pointers is special, if the user wants to filter on a field that is a pointer to user space, then they need to specify it. Add a ".ustring" attribute to the field name for filters to state that the field is pointing to user space such that the kernel can take the appropriate action to read that pointer. Link: https://lore.kernel.org/all/yt9d8rvmt2jq.fsf@linux.ibm.com/ Fixes: 77360f9 ("tracing: Add test for user space strings when filtering on string pointers") Tested-by: Sven Schnelle <svens@linux.ibm.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a9c6e02 commit 33e22b6

2 files changed

Lines changed: 66 additions & 24 deletions

File tree

‎Documentation/trace/events.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ The glob (~) accepts a wild card character (\*,?) and character classes
198198
prev_comm ~ "*sh*"
199199
prev_comm ~ "ba*sh"
200200

201+
If the field is a pointer that points into user space (for example
202+
"filename" from sys_enter_openat), then you have to append ".ustring" to the
203+
field name::
204+
205+
filename.ustring ~ "password"
206+
207+
As the kernel will have to know how to retrieve the memory that the pointer
208+
is at from user space.
209+
201210
5.2 Setting filters
202211
-------------------
203212

‎kernel/trace/trace_events_filter.c‎

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,23 @@ struct ustring_buffer {
665665
static __percpu struct ustring_buffer *ustring_per_cpu;
666666

667667
static __always_inline char *test_string(char *str)
668+
{
669+
struct ustring_buffer *ubuf;
670+
char *kstr;
671+
672+
if (!ustring_per_cpu)
673+
return NULL;
674+
675+
ubuf = this_cpu_ptr(ustring_per_cpu);
676+
kstr = ubuf->buffer;
677+
678+
/* For safety, do not trust the string pointer */
679+
if (!strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE))
680+
return NULL;
681+
return kstr;
682+
}
683+
684+
static __always_inline char *test_ustring(char *str)
668685
{
669686
struct ustring_buffer *ubuf;
670687
char __user *ustr;
@@ -676,23 +693,11 @@ static __always_inline char *test_string(char *str)
676693
ubuf = this_cpu_ptr(ustring_per_cpu);
677694
kstr = ubuf->buffer;
678695

679-
/*
680-
* We use TASK_SIZE to denote user or kernel space, but this will
681-
* not work for all architectures. If it picks the wrong one, it may
682-
* just fail the filter (but will not bug).
683-
*
684-
* TODO: Have a way to properly denote which one this is for.
685-
*/
686-
if (likely((unsigned long)str >= TASK_SIZE)) {
687-
/* For safety, do not trust the string pointer */
688-
if (!strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE))
689-
return NULL;
690-
} else {
691-
/* user space address? */
692-
ustr = (char __user *)str;
693-
if (!strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE))
694-
return NULL;
695-
}
696+
/* user space address? */
697+
ustr = (char __user *)str;
698+
if (!strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE))
699+
return NULL;
700+
696701
return kstr;
697702
}
698703

@@ -709,24 +714,42 @@ static int filter_pred_string(struct filter_pred *pred, void *event)
709714
return match;
710715
}
711716

717+
static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
718+
{
719+
int cmp, match;
720+
int len;
721+
722+
len = strlen(str) + 1; /* including tailing '\0' */
723+
cmp = pred->regex.match(str, &pred->regex, len);
724+
725+
match = cmp ^ pred->not;
726+
727+
return match;
728+
}
712729
/* Filter predicate for char * pointers */
713730
static int filter_pred_pchar(struct filter_pred *pred, void *event)
714731
{
715732
char **addr = (char **)(event + pred->offset);
716733
char *str;
717-
int cmp, match;
718-
int len;
719734

720735
str = test_string(*addr);
721736
if (!str)
722737
return 0;
723738

724-
len = strlen(str) + 1; /* including tailing '\0' */
725-
cmp = pred->regex.match(str, &pred->regex, len);
739+
return filter_pchar(pred, str);
740+
}
726741

727-
match = cmp ^ pred->not;
742+
/* Filter predicate for char * pointers in user space*/
743+
static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
744+
{
745+
char **addr = (char **)(event + pred->offset);
746+
char *str;
728747

729-
return match;
748+
str = test_ustring(*addr);
749+
if (!str)
750+
return 0;
751+
752+
return filter_pchar(pred, str);
730753
}
731754

732755
/*
@@ -1206,6 +1229,7 @@ static int parse_pred(const char *str, void *data,
12061229
struct filter_pred *pred = NULL;
12071230
char num_buf[24]; /* Big enough to hold an address */
12081231
char *field_name;
1232+
bool ustring = false;
12091233
char q;
12101234
u64 val;
12111235
int len;
@@ -1240,6 +1264,12 @@ static int parse_pred(const char *str, void *data,
12401264
return -EINVAL;
12411265
}
12421266

1267+
/* See if the field is a user space string */
1268+
if ((len = str_has_prefix(str + i, ".ustring"))) {
1269+
ustring = true;
1270+
i += len;
1271+
}
1272+
12431273
while (isspace(str[i]))
12441274
i++;
12451275

@@ -1377,7 +1407,10 @@ static int parse_pred(const char *str, void *data,
13771407
goto err_mem;
13781408
}
13791409

1380-
pred->fn = filter_pred_pchar;
1410+
if (ustring)
1411+
pred->fn = filter_pred_pchar_user;
1412+
else
1413+
pred->fn = filter_pred_pchar;
13811414
}
13821415
/* go past the last quote */
13831416
i++;

0 commit comments

Comments
 (0)