On 5/30/25 09:55, Yonghong Song wrote:


On 5/29/25 6:28 PM, Rong Tao wrote:

On 5/29/25 13:44, Alexei Starovoitov wrote:
On Wed, May 28, 2025 at 8:37 PM Rong Tao <rt...@foxmail.com> wrote:
From: Rong Tao <rong...@cestc.cn>

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.
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'

I think you can use bpf_probe_read_kernel() helper to get r2?
Thanks a lot, bpf_probe_read_kernel() works :)


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





Reply via email to