Skip to content

Commit

Permalink
tracing/user_events: Ensure write index cannot be negative
Browse files Browse the repository at this point in the history
[ Upstream commit cd98c93 ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Beau Belgrave authored and gregkh committed May 11, 2023
1 parent a6a8b11 commit fa7f2f5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kernel/trace/trace_events_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,9 @@ static ssize_t user_events_write_core(struct file *file, struct iov_iter *i)
if (unlikely(copy_from_iter(&idx, sizeof(idx), i) != sizeof(idx)))
return -EFAULT;

if (idx < 0)
return -EINVAL;

rcu_read_lock_sched();

refs = rcu_dereference_sched(info->refs);
Expand Down
5 changes: 5 additions & 0 deletions tools/testing/selftests/user_events/ftrace_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ TEST_F(user, write_events) {
ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 3));
after = trace_bytes();
ASSERT_GT(after, before);

/* Negative index should fail with EINVAL */
reg.write_index = -1;
ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
ASSERT_EQ(EINVAL, errno);
}

TEST_F(user, write_fault) {
Expand Down

0 comments on commit fa7f2f5

Please sign in to comment.