Skip to contentSkip to conversation

bpf: Drop unprotected find_vpid() in favour of find_get_pid()

PATCH In review Lee Jones 1 participant 3 messages Aug 9, 2022 – Sep 8, 2022

Subsystems (2)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

PATCH v3 1/1 bpf: Drop unprotected find_vpid() in favour of find_get_pid()

The documentation for find_vpid() clearly states:

"Must be called with the tasklist_lock or rcu_read_lock() held."

Presently we do neither.

Let's use find_get_pid() which searches for the vpid, then takes a reference to it preventing early free, all within the safety of rcu_read_lock(). Once we have our reference we can safely make use of it up until the point it is put.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Fixes 41bdc4b40ed6f ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY")

---

v1 => v2:
* Commit log update - description - no code differences

v2 => v3:
* Commit log update - spelling of find_vpid() - no code differences

 kernel/bpf/syscall.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/bpf/syscall.c +4 -1
index 83c7136c5788d..c20cff30581c4 100644
@@ -4385,6 +4385,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
 	const struct perf_event *event;
 	struct task_struct *task;
 	struct file *file;
+	struct pid *ppid;
 	int err;
 
 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
@@ -4396,7 +4397,9 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
 	if (attr->task_fd_query.flags != 0)
 		return -EINVAL;
 
-	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
+	ppid = find_get_pid(pid);
+	task = get_pid_task(ppid, PIDTYPE_PID);
+	put_pid(ppid);
 	if (!task)
 		return -ENOENT;
 
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
author
#1 lore raw
Quoted 25 lines · Lee Jones

The documentation for find_vpid() clearly states:

"Must be called with the tasklist_lock or rcu_read_lock() held."

Presently we do neither.

Let's use find_get_pid() which searches for the vpid, then takes a reference to it preventing early free, all within the safety of rcu_read_lock(). Once we have our reference we can safely make use of it up until the point it is put.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Fixes 41bdc4b40ed6f ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY")
Quoted 7 lines

---

v1 => v2:
* Commit log update - description - no code differences

v2 => v3:
* Commit log update - spelling of find_vpid() - no code differences

Did anyone get a chance to look at this please?

Would you like a [RESEND]?

Quoted 26 lines
 kernel/bpf/syscall.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/bpf/syscall.c +4 -1
index 83c7136c5788d..c20cff30581c4 100644
@@ -4385,6 +4385,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
 	const struct perf_event *event;
 	struct task_struct *task;
 	struct file *file;
+	struct pid *ppid;
 	int err;
 
 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
@@ -4396,7 +4397,9 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
 	if (attr->task_fd_query.flags != 0)
 		return -EINVAL;
 
-	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
+	ppid = find_get_pid(pid);
+	task = get_pid_task(ppid, PIDTYPE_PID);
+	put_pid(ppid);
 	if (!task)
 		return -ENOENT;
 
Signature · 1 line
Lee Jones [李琼斯]
author
#2 lore raw
Quoted 32 lines · Lee Jones
Quoted 30 lines · Lee Jones

The documentation for find_vpid() clearly states:

"Must be called with the tasklist_lock or rcu_read_lock() held."

Presently we do neither.

Let's use find_get_pid() which searches for the vpid, then takes a reference to it preventing early free, all within the safety of rcu_read_lock(). Once we have our reference we can safely make use of it up until the point it is put.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Fixes 41bdc4b40ed6f ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY")

---

v1 => v2:
* Commit log update - description - no code differences

Quoted 6 lines
Quoted 2 lines

v2 => v3:
* Commit log update - spelling of find_vpid() - no code differences

Did anyone get a chance to look at this please?

Would you like a [RESEND]?

Scrap that. I've just seen the last replies to v2.

Leave it with me.

Signature · 1 line
Lee Jones [李琼斯]