Skip to content

Commit 72ff2c8

Browse files
Tao ChenKernel Patches Daemon
authored andcommitted
bpf: Add bpf_get_task_cmdline kfunc
Add the bpf_get_task_cmdline kfunc. One use case is as follows: In production environments, there are often short-lived script tasks executed, and sometimes these tasks may cause stability issues. It is desirable to detect these script tasks via eBPF. The common approach is to check the process name, but it can be difficult to distinguish specific tasks in some cases. Take the shell as an example: some tasks are started via bash xxx.sh – their process name is bash, but the script name of the task can be obtained through the cmdline. Additionally, myabe this is helpful for security auditing purposes. Signed-off-by: Tao Chen <[email protected]>
1 parent bfb0726 commit 72ff2c8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

kernel/bpf/helpers.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,27 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
26882688
return p;
26892689
}
26902690

2691+
/*
2692+
* bpf_get_task_cmdline - Get the cmdline to a buffer
2693+
*
2694+
* @task: The task whose cmdline to get.
2695+
* @buffer: The buffer to save cmdline info.
2696+
* @len: The length of the buffer.
2697+
*
2698+
* Return: the size of the cmdline field copied. Note that the copy does
2699+
* not guarantee an ending NULL byte. A negative error code on failure.
2700+
*/
2701+
__bpf_kfunc int bpf_get_task_cmdline(struct task_struct *task, char *buffer, size_t len)
2702+
{
2703+
int ret;
2704+
2705+
ret = get_cmdline(task, buffer, len);
2706+
if (ret < 0)
2707+
memset(buffer, 0, len);
2708+
2709+
return ret;
2710+
}
2711+
26912712
/**
26922713
* bpf_task_from_vpid - Find a struct task_struct from its vpid by looking it up
26932714
* in the pid namespace of the current task. If a task is returned, it must
@@ -4428,6 +4449,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
44284449
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
44294450
BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)
44304451
BTF_ID_FLAGS(func, bpf_throw)
4452+
BTF_ID_FLAGS(func, bpf_get_task_cmdline, KF_SLEEPABLE | KF_TRUSTED_ARGS)
44314453
#ifdef CONFIG_BPF_EVENTS
44324454
BTF_ID_FLAGS(func, bpf_send_signal_task, KF_TRUSTED_ARGS)
44334455
#endif

0 commit comments

Comments
 (0)