diff mbox series

bpf:add function

Message ID CAB8PBH+EVX3iTr7Nu-QFHAom+VnjPLEk0hpWSi0QiyD5u-bKag@mail.gmail.com (mailing list archive)
State Rejected
Delegated to: BPF
Headers show
Series bpf:add function | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply
bpf/vmtest-bpf-PR fail merge-conflict

Commit Message

山竹小 June 7, 2022, 1:28 p.m. UTC
Add the absolute path to get the executable corresponding tothe task

Signed-off-by: mangosteen728 < mangosteen728@gmail.com>
---
Hi
This is my first attempt to submit patch, there are shortcomings
please more but wait.

In security audit often need to get the absolute path to the
executable of the process so I tried to add bpf_get_task_exe_path in
the helpers function to get.

The code currently only submits the implementation of the function and
how is this patch merge possible if I then add the relevant places。

thanks
mangosteen728
kernel/bpf/helpers.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)

Comments

Greg Kroah-Hartman June 7, 2022, 3:41 p.m. UTC | #1
On Tue, Jun 07, 2022 at 09:28:58PM +0800, 山竹小 wrote:
> Add the absolute path to get the executable corresponding tothe task
> 
> Signed-off-by: mangosteen728 < mangosteen728@gmail.com>
> ---
> Hi
> This is my first attempt to submit patch, there are shortcomings
> please more but wait.
> 
> In security audit often need to get the absolute path to the
> executable of the process so I tried to add bpf_get_task_exe_path in
> the helpers function to get.
> 
> The code currently only submits the implementation of the function and
> how is this patch merge possible if I then add the relevant places。
> 
> thanks
> mangosteen728
> kernel/bpf/helpers.c | 37 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 225806a..797f 850 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -257,6 +257,43 @@
> .arg2_type = ARG_CONST_SIZE,
> };
> 
> +BPF_CALL_3(bpf_get_task_exe_path, struct task_struct *, task, char *,
> buf, u32, sz)
> +{
> + struct file *exe_file = NULL;
> + char *p = NULL;
> + long len = 0;
> +
> + if (!sz)
> + return 0;
> + exe_file = get_task_exe_file(tsk);
> + if (IS_ERR_OR_NULL(exe_file))
> + return 0;
> + p = d_path(&exe_file->f_path, buf, sz);
> + if (IS_ERR_OR_NULL(path)) {
> + len = PTR_ERR(p);
> + } else {
> + len = buf + sz - p;
> + memmove(buf, p, len);
> + }
> + fput(exe_file);
> + return len;
> +}
> +
> +static const struct bpf_func_proto bpf_get_task_exe_path_proto = {
> + .func       = bpf_get_task_exe_path,
> + .gpl_only   = false,
> + .ret_type   = RET_INTEGER,
> + .arg1_type  = ARG_PTR_TO_BTF_ID,
> + .arg2_type  = ARG_PTR_TO_MEM,
> + .arg3_type  = ARG_CONST_SIZE_OR_ZERO,
> +};
> +

Something went really wrong with your patch :(

But the larger issue is, there is no such thing as a "absolute path to a
file" within the kernel, sorry.  This just is not going to work, and it
has come up again and again and again with regards to other kernel
subsystems many times.

Step back and answer "why" you think you need a path to an executable?
What needs this that you can not do it in userspace?  What are you going
to do with this supposed information if you get it?

And then think about filesystem namespaces...

sorry, this isn't going to work.

greg k-h
diff mbox series

Patch

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 225806a..797f 850 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -257,6 +257,43 @@ 
.arg2_type = ARG_CONST_SIZE,
};

+BPF_CALL_3(bpf_get_task_exe_path, struct task_struct *, task, char *,
buf, u32, sz)
+{
+ struct file *exe_file = NULL;
+ char *p = NULL;
+ long len = 0;
+
+ if (!sz)
+ return 0;
+ exe_file = get_task_exe_file(tsk);
+ if (IS_ERR_OR_NULL(exe_file))
+ return 0;
+ p = d_path(&exe_file->f_path, buf, sz);
+ if (IS_ERR_OR_NULL(path)) {
+ len = PTR_ERR(p);
+ } else {
+ len = buf + sz - p;
+ memmove(buf, p, len);
+ }
+ fput(exe_file);
+ return len;
+}
+
+static const struct bpf_func_proto bpf_get_task_exe_path_proto = {
+ .func       = bpf_get_task_exe_path,
+ .gpl_only   = false,
+ .ret_type   = RET_INTEGER,
+ .arg1_type  = ARG_PTR_TO_BTF_ID,
+ .arg2_type  = ARG_PTR_TO_MEM,
+ .arg3_type  = ARG_CONST_SIZE_OR_ZERO,
+};
+
--