Skip to content

Commit b4490c5

Browse files
cneiraAlexei Starovoitov
authored andcommitted
bpf: Added new helper bpf_get_ns_current_pid_tgid
New bpf helper bpf_get_ns_current_pid_tgid, This helper will return pid and tgid from current task which namespace matches dev_t and inode number provided, this will allows us to instrument a process inside a container. Signed-off-by: Carlos Neira <cneirabustos@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200304204157.58695-3-cneirabustos@gmail.com
1 parent 1e2328e commit b4490c5

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-2
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,7 @@ extern const struct bpf_func_proto bpf_strtol_proto;
14971497
extern const struct bpf_func_proto bpf_strtoul_proto;
14981498
extern const struct bpf_func_proto bpf_tcp_sock_proto;
14991499
extern const struct bpf_func_proto bpf_jiffies64_proto;
1500+
extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
15001501

15011502
/* Shared helpers among cBPF and eBPF. */
15021503
void bpf_user_rnd_init_once(void);

include/uapi/linux/bpf.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,19 @@ union bpf_attr {
29142914
* of sizeof(struct perf_branch_entry).
29152915
*
29162916
* **-ENOENT** if architecture does not support branch records.
2917+
*
2918+
* int bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size)
2919+
* Description
2920+
* Returns 0 on success, values for *pid* and *tgid* as seen from the current
2921+
* *namespace* will be returned in *nsdata*.
2922+
*
2923+
* On failure, the returned value is one of the following:
2924+
*
2925+
* **-EINVAL** if dev and inum supplied don't match dev_t and inode number
2926+
* with nsfs of current task, or if dev conversion to dev_t lost high bits.
2927+
*
2928+
* **-ENOENT** if pidns does not exists for the current task.
2929+
*
29172930
*/
29182931
#define __BPF_FUNC_MAPPER(FN) \
29192932
FN(unspec), \
@@ -3035,7 +3048,8 @@ union bpf_attr {
30353048
FN(tcp_send_ack), \
30363049
FN(send_signal_thread), \
30373050
FN(jiffies64), \
3038-
FN(read_branch_records),
3051+
FN(read_branch_records), \
3052+
FN(get_ns_current_pid_tgid),
30393053

30403054
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
30413055
* function eBPF program intends to call
@@ -3829,4 +3843,8 @@ struct bpf_sockopt {
38293843
__s32 retval;
38303844
};
38313845

3846+
struct bpf_pidns_info {
3847+
__u32 pid;
3848+
__u32 tgid;
3849+
};
38323850
#endif /* _UAPI__LINUX_BPF_H__ */

kernel/bpf/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
21492149
const struct bpf_func_proto bpf_get_current_comm_proto __weak;
21502150
const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
21512151
const struct bpf_func_proto bpf_get_local_storage_proto __weak;
2152+
const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak;
21522153

21532154
const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
21542155
{

kernel/bpf/helpers.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <linux/filter.h>
1313
#include <linux/ctype.h>
1414
#include <linux/jiffies.h>
15+
#include <linux/pid_namespace.h>
16+
#include <linux/proc_ns.h>
1517

1618
#include "../../lib/kstrtox.h"
1719

@@ -499,3 +501,46 @@ const struct bpf_func_proto bpf_strtoul_proto = {
499501
.arg4_type = ARG_PTR_TO_LONG,
500502
};
501503
#endif
504+
505+
BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
506+
struct bpf_pidns_info *, nsdata, u32, size)
507+
{
508+
struct task_struct *task = current;
509+
struct pid_namespace *pidns;
510+
int err = -EINVAL;
511+
512+
if (unlikely(size != sizeof(struct bpf_pidns_info)))
513+
goto clear;
514+
515+
if (unlikely((u64)(dev_t)dev != dev))
516+
goto clear;
517+
518+
if (unlikely(!task))
519+
goto clear;
520+
521+
pidns = task_active_pid_ns(task);
522+
if (unlikely(!pidns)) {
523+
err = -ENOENT;
524+
goto clear;
525+
}
526+
527+
if (!ns_match(&pidns->ns, (dev_t)dev, ino))
528+
goto clear;
529+
530+
nsdata->pid = task_pid_nr_ns(task, pidns);
531+
nsdata->tgid = task_tgid_nr_ns(task, pidns);
532+
return 0;
533+
clear:
534+
memset((void *)nsdata, 0, (size_t) size);
535+
return err;
536+
}
537+
538+
const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = {
539+
.func = bpf_get_ns_current_pid_tgid,
540+
.gpl_only = false,
541+
.ret_type = RET_INTEGER,
542+
.arg1_type = ARG_ANYTHING,
543+
.arg2_type = ARG_ANYTHING,
544+
.arg3_type = ARG_PTR_TO_UNINIT_MEM,
545+
.arg4_type = ARG_CONST_SIZE,
546+
};

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
843843
return &bpf_send_signal_thread_proto;
844844
case BPF_FUNC_perf_event_read_value:
845845
return &bpf_perf_event_read_value_proto;
846+
case BPF_FUNC_get_ns_current_pid_tgid:
847+
return &bpf_get_ns_current_pid_tgid_proto;
846848
default:
847849
return NULL;
848850
}

scripts/bpf_helpers_doc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ class PrinterHelpers(Printer):
435435
'struct bpf_fib_lookup',
436436
'struct bpf_perf_event_data',
437437
'struct bpf_perf_event_value',
438+
'struct bpf_pidns_info',
438439
'struct bpf_sock',
439440
'struct bpf_sock_addr',
440441
'struct bpf_sock_ops',

tools/include/uapi/linux/bpf.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,19 @@ union bpf_attr {
29142914
* of sizeof(struct perf_branch_entry).
29152915
*
29162916
* **-ENOENT** if architecture does not support branch records.
2917+
*
2918+
* int bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size)
2919+
* Description
2920+
* Returns 0 on success, values for *pid* and *tgid* as seen from the current
2921+
* *namespace* will be returned in *nsdata*.
2922+
*
2923+
* On failure, the returned value is one of the following:
2924+
*
2925+
* **-EINVAL** if dev and inum supplied don't match dev_t and inode number
2926+
* with nsfs of current task, or if dev conversion to dev_t lost high bits.
2927+
*
2928+
* **-ENOENT** if pidns does not exists for the current task.
2929+
*
29172930
*/
29182931
#define __BPF_FUNC_MAPPER(FN) \
29192932
FN(unspec), \
@@ -3035,7 +3048,8 @@ union bpf_attr {
30353048
FN(tcp_send_ack), \
30363049
FN(send_signal_thread), \
30373050
FN(jiffies64), \
3038-
FN(read_branch_records),
3051+
FN(read_branch_records), \
3052+
FN(get_ns_current_pid_tgid),
30393053

30403054
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
30413055
* function eBPF program intends to call
@@ -3829,4 +3843,8 @@ struct bpf_sockopt {
38293843
__s32 retval;
38303844
};
38313845

3846+
struct bpf_pidns_info {
3847+
__u32 pid;
3848+
__u32 tgid;
3849+
};
38323850
#endif /* _UAPI__LINUX_BPF_H__ */

0 commit comments

Comments
 (0)