Skip to contentSkip to conversation

bpf: fix crash in bpf_path_d_path() for exiting tasks

PATCH New Matt Bobrowski 1 participant 1 message Oct 20 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 bpf: fix crash in bpf_path_d_path() for exiting tasks

bpf_path_d_path() passes its path to d_path(), which resolves it against current->fs through get_fs_root_rcu() without checking that the calling task still has one. An exiting task does not. do_exit() calls exit_fs(), which clears tsk->fs, before exit_task_work(), and a file whose last reference the task drops in exit_files() has its final __fput() deferred to that task work. So a BPF LSM program attached to file_release, which may pass file->f_path to bpf_path_d_path() as a trusted argument, dereferences a NULL fs whenever a task exits with a file still open:

  Call trace:
   lock_acquire+0x8c/0x218
   d_path+0x270/0x378
   bpf_path_d_path+0x28/0x80
   bpf_prog_9801cf2b982f05b2_file_release+0x1d0/0xc60
   bpf_trampoline_6442515074+0x7c/0x100
   bpf_lsm_file_release+0x8/0x20
   security_file_release+0x58/0x90
   __fput+0xcc/0x320
   ____fput+0x1c/0x30
   task_work_run+0x88/0x100
   do_exit+0x210/0x3e8
   do_group_exit+0x3c/0xa0
   __arm64_sys_exit_group+0x20/0x30

With lockdep enabled the fault is taken in lock_acquire(), from the seqcount read in get_fs_root_rcu(); without it, on the read of fs->seq itself.

Fix this by returning -EINVAL from bpf_path_d_path() when current->fs is NULL. Only the task itself clears its fs, in exit_fs(), so the check cannot race with that.

Fixes d08e2045ebf0 ("bpf: introduce new VFS based BPF kfuncs")
---
 fs/bpf_fs_kfuncs.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/bpf_fs_kfuncs.c +8 -0
index 08f0847c4970..6665a78218e1 100644
@@ -87,6 +87,14 @@ __bpf_kfunc int bpf_path_d_path(const struct path *path, char *buf, size_t buf__
 	if (!buf__sz)
 		return -EINVAL;
 
+	/*
+	 * An exiting task releases its last files from exit_task_work(), after
+	 * exit_fs() has already dropped its ->fs, which d_path() uses to
+	 * resolve against.
+	 */
+	if (!current->fs)
+		return -EINVAL;
+
 	ret = d_path(path, buf, buf__sz);
 	if (IS_ERR(ret))
 		return PTR_ERR(ret);
Signature · 1 line
2.47.3