Skip to contentSkip to conversation

kprobes: Limit max data_size of the kretprobe instances

PATCH In review Masami Hiramatsu 2 participants 3 messages Dec 1, 2021 – Dec 2, 2021 kprobes

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

PATCH 1/1 kprobes: Limit max data_size of the kretprobe instances

The kretprobe::data_size is unsigned (size_t) but it is used as 'data_size + sizeof(struct kretprobe_instance)'. Thus, it can be smaller than sizeof(struct kretprobe_instance) while allocating memory for the kretprobe_instance.

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

Fixes f47cd9b553aa ("kprobes: kretprobe user entry-handler")
---
 include/linux/kprobes.h |    2 ++
 kernel/kprobes.c        |    3 +++
 2 files changed, 5 insertions(+)
include/linux/kprobes.h +2 -0
index e974caf39d3e..8c8f7a4d93af 100644
@@ -153,6 +153,8 @@ struct kretprobe {
 	struct kretprobe_holder *rph;
 };
 
+#define KRETPROBE_MAX_DATA_SIZE	4096
+
 struct kretprobe_instance {
 	union {
 		struct freelist_node freelist;
kernel/kprobes.c +3 -0
index e9db0c810554..21eccc961bba 100644
@@ -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;
 
#1 lore raw
Quoted 4 lines · On Wed, 1 Dec 2021 23:45:50 +0900 Masami Hiramatsu <[email protected]> wrote:

The kretprobe::data_size is unsigned (size_t) but it is used as 'data_size + sizeof(struct kretprobe_instance)'. Thus, it can be smaller than sizeof(struct kretprobe_instance) while allocating memory for the kretprobe_instance.

The above doesn't make sense.

data_size is unsigned but it is used as
'data_size + sizeof(struct kretprobe_instance)'.

What does that mean?

What can be smaller than sizeof(struct kretprobe_instance) and why does it matter?

-- Steve

Quoted 4 lines

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

author maintainer
#2 lore raw
Quoted 10 lines · On Wed, 1 Dec 2021 11:19:22 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Wed, 1 Dec 2021 23:45:50 +0900 Masami Hiramatsu <[email protected]> wrote:

The kretprobe::data_size is unsigned (size_t) but it is used as 'data_size + sizeof(struct kretprobe_instance)'. Thus, it can be smaller than sizeof(struct kretprobe_instance) while allocating memory for the kretprobe_instance.

The above doesn't make sense.

Quoted 7 lines

data_size is unsigned but it is used as
'data_size + sizeof(struct kretprobe_instance)'.

What does that mean?

What can be smaller than sizeof(struct kretprobe_instance) and why does it matter?

OK, what about this ?

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.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>