Skip to content

Commit cd98c93

Browse files
beaubelgraverostedt
authored andcommitted
tracing/user_events: Ensure write index cannot be negative
The write index indicates which event the data is for and accesses a per-file array. The index is passed by user processes during write() calls as the first 4 bytes. Ensure that it cannot be negative by returning -EINVAL to prevent out of bounds accesses. Update ftrace self-test to ensure this occurs properly. Link: https://lkml.kernel.org/r/20230425225107.8525-2-beaub@linux.microsoft.com Fixes: 7f5a08c ("user_events: Add minimal support for trace_event into ftrace") Reported-by: Doug Cook <dcook@linux.microsoft.com> Signed-off-by: Beau Belgrave <beaub@linux.microsoft.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 96928d9 commit cd98c93

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

kernel/trace/trace_events_user.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,9 @@ static ssize_t user_events_write_core(struct file *file, struct iov_iter *i)
18241824
if (unlikely(copy_from_iter(&idx, sizeof(idx), i) != sizeof(idx)))
18251825
return -EFAULT;
18261826

1827+
if (idx < 0)
1828+
return -EINVAL;
1829+
18271830
rcu_read_lock_sched();
18281831

18291832
refs = rcu_dereference_sched(info->refs);

tools/testing/selftests/user_events/ftrace_test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ TEST_F(user, write_events) {
296296
ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 3));
297297
after = trace_bytes();
298298
ASSERT_GT(after, before);
299+
300+
/* Negative index should fail with EINVAL */
301+
reg.write_index = -1;
302+
ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
303+
ASSERT_EQ(EINVAL, errno);
299304
}
300305

301306
TEST_F(user, write_fault) {

0 commit comments

Comments
 (0)