Skip to contentSkip to conversation

bpf: Add bpf_task_cwd_from_pid() kfunc

PATCH In review Rong Tao 4 participants 7 messages May 29, 2025 – May 30, 2025 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

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

Rong Tao

author
#1 lore raw

PATCH 1/2 bpf: Add bpf_task_cwd_from_pid() kfunc

From: Rong Tao <[email protected]>

It is a bit troublesome to get cwd based on pid in bpf program, such as bpftrace example [1].

This patch therefore adds a new bpf_task_cwd_from_pid() kfunc which allows BPF programs to get cwd from a pid.

[1] https://github.com/bpftrace/bpftrace/issues/3314

---
 kernel/bpf/helpers.c | 45 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
kernel/bpf/helpers.c +45 -0
index b71e428ad936..0f32fbc997bb 100644
@@ -24,6 +24,10 @@
 #include <linux/bpf_mem_alloc.h>
 #include <linux/kasan.h>
 #include <linux/bpf_verifier.h>
+#include <linux/fs.h>
+#include <linux/fs_struct.h>
+#include <linux/path.h>
+#include <linux/string.h>
 
 #include "../../lib/kstrtox.h"
 
@@ -2643,6 +2647,46 @@ __bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid)
 	return p;
 }
 
+/**
+ * bpf_task_cwd_from_pid - Get a task's absolute pathname of the current
+ * working directory from its pid.
+ * @pid: The pid of the task being looked up.
+ * @buf: The array pointed to by buf.
+ * @buf_len: buf length.
+ */
+__bpf_kfunc int bpf_task_cwd_from_pid(s32 pid, char *buf, u32 buf_len)
+{
+	struct path pwd;
+	char kpath[256], *path;
+	struct task_struct *task;
+
+	if (!buf || buf_len == 0)
+		return -EINVAL;
+
+	rcu_read_lock();
+	task = pid_task(find_vpid(pid), PIDTYPE_PID);
+	if (!task) {
+		rcu_read_unlock();
+		return -ESRCH;
+	}
+	task_lock(task);
+	if (!task->fs) {
+		task_unlock(task);
+		return -ENOENT;
+	}
+	get_fs_pwd(task->fs, &pwd);
+	task_unlock(task);
+	rcu_read_unlock();
+
+	path = d_path(&pwd, kpath, sizeof(kpath));
+	path_put(&pwd);
+	if (IS_ERR(path))
+		return PTR_ERR(path);
+
+	strncpy(buf, path, buf_len);
+	return 0;
+}
+
 /**
  * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data.
  * @p: The dynptr whose data slice to retrieve
@@ -3314,6 +3358,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
 #endif
 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_task_cwd_from_pid, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_throw)
 #ifdef CONFIG_BPF_EVENTS
 BTF_ID_FLAGS(func, bpf_send_signal_task, KF_TRUSTED_ARGS)
Signature · 1 line
2.49.0
#2 lore raw
Quoted 10 lines · Rong Tao

From: Rong Tao <[email protected]>

It is a bit troublesome to get cwd based on pid in bpf program, such as bpftrace example [1].

This patch therefore adds a new bpf_task_cwd_from_pid() kfunc which allows BPF programs to get cwd from a pid.

[1] https://github.com/bpftrace/bpftrace/issues/3314

Yes. This is cumbersome, but adding a very specific kfunc to the kernel is not a solution.
This is tracing, no need for precise cwd. probe_read_kernel can do the job. bpftrace needs to have better C interop. Once that happens any kind of tracing extraction will be easy to write in C. Like this bpf_task_cwd_from_pid() can already be written as C bpf program.

author
#3 lore raw
Quoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Rong Tao

From: Rong Tao <[email protected]>

It is a bit troublesome to get cwd based on pid in bpf program, such as bpftrace example [1].

This patch therefore adds a new bpf_task_cwd_from_pid() kfunc which allows BPF programs to get cwd from a pid.

Quoted 8 lines
Quoted 1 line

[1] https://github.com/bpftrace/bpftrace/issues/3314

Yes. This is cumbersome, but adding a very specific kfunc to the kernel is not a solution.
This is tracing, no need for precise cwd. probe_read_kernel can do the job. bpftrace needs to have better C interop. Once that happens any kind of tracing extraction will be easy to write in C. Like this bpf_task_cwd_from_pid() can already be written as C bpf program.

Thanks for your reply, Yesterday I tried many ways to implement the solution of getting cwd from pid/task, but all failed. The basic idea is to rewrite the d_path() code, but in the bpf program, there will be various memory security access problems, even if enough if (!ptr) are added, the program cannot be loaded successfully.

https://github.com/Rtoax/bcc/commit/2ba7a2389fc1183264e5195ff26561d93038886c

    bcc/tools$ sudo ./opensnoop.py -F

    ; if (dentry == vfsmnt->mnt_root || dentry == dentry->d_parent) { @ main.c:174 109: (79) r2 = *(u64 *)(r7 +0)
    R7 invalid mem access 'scalar'

At the same time, bpf_d_path cannot be used because it can only be applied to functions in btf_allowlist_d_path. Currently, it is impossible to get cwd from pid/task in user mode. Any suggestions?

In addition, I fully tested this patch yesterday and it performed well.

Rong Tao

reviewer
#4 lore raw
Quoted 28 lines · Rong Tao
Quoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Rong Tao

From: Rong Tao <[email protected]>

It is a bit troublesome to get cwd based on pid in bpf program, such as bpftrace example [1].

This patch therefore adds a new bpf_task_cwd_from_pid() kfunc which allows BPF programs to get cwd from a pid.

Quoted 8 lines
Quoted 1 line

[1] https://github.com/bpftrace/bpftrace/issues/3314

Yes. This is cumbersome, but adding a very specific kfunc to the kernel is not a solution.
This is tracing, no need for precise cwd. probe_read_kernel can do the job. bpftrace needs to have better C interop. Once that happens any kind of tracing extraction will be easy to write in C. Like this bpf_task_cwd_from_pid() can already be written as C bpf program.

Thanks for your reply, Yesterday I tried many ways to implement the solution of getting cwd from pid/task, but all failed. The basic idea is to rewrite the d_path() code, but in the bpf program, there will be various memory security access problems, even if enough if (!ptr) are added, the program cannot be loaded successfully.

https://github.com/Rtoax/bcc/commit/2ba7a2389fc1183264e5195ff26561d93038886c

Quoted 6 lines

    bcc/tools$ sudo ./opensnoop.py -F

    ; if (dentry == vfsmnt->mnt_root || dentry == dentry->d_parent) { @ main.c:174
    109: (79) r2 = *(u64 *)(r7 +0)
    R7 invalid mem access 'scalar'

I think you can use bpf_probe_read_kernel() helper to get r2?

Quoted 10 lines

At the same time, bpf_d_path cannot be used because it can only be applied to functions in btf_allowlist_d_path. Currently, it is impossible to get cwd from pid/task in user mode. Any suggestions?

In addition, I fully tested this patch yesterday and it performed well.

Rong Tao

author
#5 lore raw
Quoted 31 lines · Yonghong Song
Quoted 28 lines · Rong Tao
Quoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Rong Tao

From: Rong Tao <[email protected]>

It is a bit troublesome to get cwd based on pid in bpf program, such as bpftrace example [1].

This patch therefore adds a new bpf_task_cwd_from_pid() kfunc which allows BPF programs to get cwd from a pid.

Quoted 8 lines
Quoted 1 line

[1] https://github.com/bpftrace/bpftrace/issues/3314

Yes. This is cumbersome, but adding a very specific kfunc to the kernel is not a solution.
This is tracing, no need for precise cwd. probe_read_kernel can do the job. bpftrace needs to have better C interop. Once that happens any kind of tracing extraction will be easy to write in C. Like this bpf_task_cwd_from_pid() can already be written as C bpf program.

Thanks for your reply, Yesterday I tried many ways to implement the solution of getting cwd from pid/task, but all failed. The basic idea is to rewrite the d_path() code, but in the bpf program, there will be various memory security access problems, even if enough if (!ptr) are added, the program cannot be loaded successfully.

https://github.com/Rtoax/bcc/commit/2ba7a2389fc1183264e5195ff26561d93038886c

Quoted 8 lines
Quoted 6 lines

    bcc/tools$ sudo ./opensnoop.py -F

    ; if (dentry == vfsmnt->mnt_root || dentry == dentry->d_parent) { @ main.c:174
    109: (79) r2 = *(u64 *)(r7 +0)
    R7 invalid mem access 'scalar'

I think you can use bpf_probe_read_kernel() helper to get r2?

Thanks a lot, bpf_probe_read_kernel() works :)

Quoted 12 lines
Quoted 11 lines

At the same time, bpf_d_path cannot be used because it can only be applied to functions in btf_allowlist_d_path. Currently, it is impossible to get cwd from pid/task in user mode. Any suggestions?

In addition, I fully tested this patch yesterday and it performed well.

Rong Tao

build bot
#6 lore raw
In reply to Rong Tao
BCC: [email protected] CC: [email protected] In-Reply-To: <[email protected]> References: <[email protected]> TO: Rong Tao…

BCC: [email protected]
CC: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: Rong Tao <[email protected]>
TO: [email protected]
TO: [email protected]
CC: [email protected]
CC: [email protected]
CC: Andrii Nakryiko <[email protected]>
CC: Martin KaFai Lau <[email protected]>
CC: Eduard Zingerman <[email protected]>
CC: Song Liu <[email protected]>
CC: Yonghong Song <[email protected]>
CC: John Fastabend <[email protected]>
CC: KP Singh <[email protected]>
CC: Stanislav Fomichev <[email protected]>
CC: Hao Luo <[email protected]>
CC: Jiri Olsa <[email protected]>
CC: Mykola Lysenko <[email protected]>
CC: Shuah Khan <[email protected]>
CC: Juntong Deng <[email protected]>
CC: Amery Hung <[email protected]>
CC: Dave Marchevsky <[email protected]>
CC: Hou Tao <[email protected]>
CC: "(open list:BPF \(Safe Dynamic Programs and Tools\))" <[email protected]> (open list:BPF (Safe Dynamic Programs and Tools))
CC: [email protected]
CC: [email protected]

Hi Rong,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Rong-Tao/selftests-bpf-Add-selftests-for-bpf_task_cwd_from_pid/20250529-113933
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/tencent_97F8B56B340F51DB604B482FEBF012460505%40qq.com
patch subject: [PATCH bpf-next 1/2] bpf: Add bpf_task_cwd_from_pid() kfunc

:::::: branch date: 17 hours ago
:::::: commit date: 17 hours ago
config: x86_64-randconfig-161-20250529 (https://download.01.org/0day-ci/archive/20250530/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
kernel/bpf/helpers.c:2687 bpf_task_cwd_from_pid() warn: inconsistent returns 'rcu_read'.

vim +/rcu_read +2687 kernel/bpf/helpers.c

675c3596ff32c0 Juntong Deng 2024-10-14  2649  
b24383bde5a454 Rong Tao     2025-05-29  2650  /**
b24383bde5a454 Rong Tao     2025-05-29  2651   * bpf_task_cwd_from_pid - Get a task's absolute pathname of the current
b24383bde5a454 Rong Tao     2025-05-29  2652   * working directory from its pid.
b24383bde5a454 Rong Tao     2025-05-29  2653   * @pid: The pid of the task being looked up.
b24383bde5a454 Rong Tao     2025-05-29  2654   * @buf: The array pointed to by buf.
b24383bde5a454 Rong Tao     2025-05-29  2655   * @buf_len: buf length.
b24383bde5a454 Rong Tao     2025-05-29  2656   */
b24383bde5a454 Rong Tao     2025-05-29  2657  __bpf_kfunc int bpf_task_cwd_from_pid(s32 pid, char *buf, u32 buf_len)
b24383bde5a454 Rong Tao     2025-05-29  2658  {
b24383bde5a454 Rong Tao     2025-05-29  2659  	struct path pwd;
b24383bde5a454 Rong Tao     2025-05-29  2660  	char kpath[256], *path;
b24383bde5a454 Rong Tao     2025-05-29  2661  	struct task_struct *task;
b24383bde5a454 Rong Tao     2025-05-29  2662  
b24383bde5a454 Rong Tao     2025-05-29  2663  	if (!buf || buf_len == 0)
b24383bde5a454 Rong Tao     2025-05-29  2664  		return -EINVAL;
b24383bde5a454 Rong Tao     2025-05-29  2665  
b24383bde5a454 Rong Tao     2025-05-29  2666  	rcu_read_lock();
b24383bde5a454 Rong Tao     2025-05-29  2667  	task = pid_task(find_vpid(pid), PIDTYPE_PID);
b24383bde5a454 Rong Tao     2025-05-29  2668  	if (!task) {
b24383bde5a454 Rong Tao     2025-05-29  2669  		rcu_read_unlock();
b24383bde5a454 Rong Tao     2025-05-29  2670  		return -ESRCH;
b24383bde5a454 Rong Tao     2025-05-29  2671  	}
b24383bde5a454 Rong Tao     2025-05-29  2672  	task_lock(task);
b24383bde5a454 Rong Tao     2025-05-29  2673  	if (!task->fs) {
b24383bde5a454 Rong Tao     2025-05-29  2674  		task_unlock(task);
b24383bde5a454 Rong Tao     2025-05-29  2675  		return -ENOENT;
b24383bde5a454 Rong Tao     2025-05-29  2676  	}
b24383bde5a454 Rong Tao     2025-05-29  2677  	get_fs_pwd(task->fs, &pwd);
b24383bde5a454 Rong Tao     2025-05-29  2678  	task_unlock(task);
b24383bde5a454 Rong Tao     2025-05-29  2679  	rcu_read_unlock();
b24383bde5a454 Rong Tao     2025-05-29  2680  
b24383bde5a454 Rong Tao     2025-05-29  2681  	path = d_path(&pwd, kpath, sizeof(kpath));
b24383bde5a454 Rong Tao     2025-05-29  2682  	path_put(&pwd);
b24383bde5a454 Rong Tao     2025-05-29  2683  	if (IS_ERR(path))
b24383bde5a454 Rong Tao     2025-05-29  2684  		return PTR_ERR(path);
b24383bde5a454 Rong Tao     2025-05-29  2685  
b24383bde5a454 Rong Tao     2025-05-29  2686  	strncpy(buf, path, buf_len);
b24383bde5a454 Rong Tao     2025-05-29 @2687  	return 0;
b24383bde5a454 Rong Tao     2025-05-29  2688  }
b24383bde5a454 Rong Tao     2025-05-29  2689  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#7 lore raw

PATCH 2/2 selftests/bpf: Add selftests for bpf_task_cwd_from_pid()

In reply to a message not in the archive

From: Rong Tao <[email protected]>

Add some selftest testcases that validate the expected behavior of the bpf_task_cwd_from_pid() kfunc that was added in the prior patch.

---
 .../selftests/bpf/prog_tests/task_kfunc.c     |  3 ++
 .../selftests/bpf/progs/task_kfunc_common.h   |  1 +
 .../selftests/bpf/progs/task_kfunc_success.c  | 47 +++++++++++++++++++
 3 files changed, 51 insertions(+)
tools/testing/selftests/bpf/prog_tests/task_kfunc.c +3 -0
index 83b90335967a..c18a0cb67164 100644
@@ -149,6 +149,9 @@ static const char * const success_tests[] = {
 	"task_kfunc_acquire_trusted_walked",
 	"test_task_kfunc_flavor_relo",
 	"test_task_kfunc_flavor_relo_not_found",
+	"test_task_cwd_from_pid_arg",
+	"test_task_cwd_from_pid",
+	"test_task_cwd_from_pid_current",
 };
 
 static const char * const vpid_success_tests[] = {
tools/testing/selftests/bpf/progs/task_kfunc_common.h +1 -0
index e9c4fea7a4bb..b762054a1825 100644
@@ -24,6 +24,7 @@ struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
 void bpf_task_release(struct task_struct *p) __ksym;
 struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
 struct task_struct *bpf_task_from_vpid(s32 vpid) __ksym;
+int bpf_task_cwd_from_pid(s32 pid, char *buf, u32 buf_len) __ksym;
 void bpf_rcu_read_lock(void) __ksym;
 void bpf_rcu_read_unlock(void) __ksym;
 
tools/testing/selftests/bpf/progs/task_kfunc_success.c +47 -0

53 lines not on this page · raw message

Signature · 1 line
2.49.0