Skip to content

Commit

Permalink
kprobes: Limit max data_size of the kretprobe instances
Browse files Browse the repository at this point in the history
The 'kprobe::data_size' is unsigned, thus it can not be negative.  But if
user sets it enough big number (e.g. (size_t)-8), the result of 'data_size
+ sizeof(struct kretprobe_instance)' becomes smaller than sizeof(struct
kretprobe_instance) or zero. In result, the kretprobe_instance are
allocated without enough memory, and kretprobe accesses outside of
allocated memory.

To avoid this issue, introduce a max limitation of the
kretprobe::data_size. 4KB per instance should be OK.

Link: https://lkml.kernel.org/r/163836995040.432120.10322772773821182925.stgit@devnote2

Cc: stable@vger.kernel.org
Fixes: f47cd9b ("kprobes: kretprobe user entry-handler")
Reported-by: zhangyue <zhangyue1@kylinos.cn>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
  • Loading branch information
mhiramat authored and rostedt committed Dec 2, 2021
1 parent f25667e commit 6bbfa44
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/linux/kprobes.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ struct kretprobe {
struct kretprobe_holder *rph;
};

#define KRETPROBE_MAX_DATA_SIZE 4096

struct kretprobe_instance {
union {
struct freelist_node freelist;
Expand Down
3 changes: 3 additions & 0 deletions kernel/kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,9 @@ int register_kretprobe(struct kretprobe *rp)
}
}

if (rp->data_size > KRETPROBE_MAX_DATA_SIZE)
return -E2BIG;

rp->kp.pre_handler = pre_handler_kretprobe;
rp->kp.post_handler = NULL;

Expand Down

0 comments on commit 6bbfa44

Please sign in to comment.