|
12 | 12 | #include <linux/filter.h> |
13 | 13 | #include <linux/ctype.h> |
14 | 14 | #include <linux/jiffies.h> |
| 15 | +#include <linux/pid_namespace.h> |
| 16 | +#include <linux/proc_ns.h> |
15 | 17 |
|
16 | 18 | #include "../../lib/kstrtox.h" |
17 | 19 |
|
@@ -499,3 +501,46 @@ const struct bpf_func_proto bpf_strtoul_proto = { |
499 | 501 | .arg4_type = ARG_PTR_TO_LONG, |
500 | 502 | }; |
501 | 503 | #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 | +}; |
0 commit comments