[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <tencent_0C1631C5C04C0820EBEE925625E195713405@qq.com>
Date: Fri, 30 May 2025 14:34:02 +0800
From: Rong Tao <rtoax@...mail.com>
To: Yonghong Song <yonghong.song@...ux.dev>,
Alexei Starovoitov <alexei.starovoitov@...il.com>
Cc: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>, rongtao@...tc.cn,
Andrii Nakryiko <andrii@...nel.org>, Martin KaFai Lau
<martin.lau@...ux.dev>, Eduard Zingerman <eddyz87@...il.com>,
Song Liu <song@...nel.org>, John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>,
Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
Mykola Lysenko <mykolal@...com>, Shuah Khan <shuah@...nel.org>,
Juntong Deng <juntong.deng@...look.com>,
Amery Hung <amery.hung@...edance.com>,
Dave Marchevsky <davemarchevsky@...com>, Hou Tao <houtao1@...wei.com>,
"open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)"
<bpf@...r.kernel.org>, open list <linux-kernel@...r.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK" <linux-kselftest@...r.kernel.org>
Subject: Re: [PATCH bpf-next 1/2] bpf: Add bpf_task_cwd_from_pid() kfunc
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 <rtoax@...mail.com> wrote:
>>>> From: Rong Tao <rongtao@...tc.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
>>
>>
>
Powered by blists - more mailing lists