From patchwork Tue Sep 24 15:20:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Antonio Neira Bustos X-Patchwork-Id: 1166682 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="IQBOi4N4"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46d4d907sWz9sP3 for ; Wed, 25 Sep 2019 01:20:29 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732233AbfIXPU2 (ORCPT ); Tue, 24 Sep 2019 11:20:28 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:41698 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730625AbfIXPU2 (ORCPT ); Tue, 24 Sep 2019 11:20:28 -0400 Received: by mail-qt1-f195.google.com with SMTP id x4so2587568qtq.8; Tue, 24 Sep 2019 08:20:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=p5dmhqgXzU0PAo+f6c2oMinYRJIOzYqFfyKVcX2FFw4=; b=IQBOi4N4ItZRhQrHkWMrfJLQhpYrEu4dLY7tSIb4CKITJ1SMLe7SzFV0WRgfTnghx1 kTx2+5N8smtyn9Hhvo2Gh6Nwq5c3vH1jZn1ticd228bgbbn3WgPuliF2YTJVfN8QYa6P c/ePcCz5CTRgkqYaw5Slep3s1lrOUitY5Vzcj8mRkVM47sIsFSzJe9frDMjktnpoyzR4 BfZ4RDNfIilPSyrTSAVRbtU4+4BGvMnvMwhE9g008KhfCfBk0NqWKoG2czoeUlRjwi4H cA/Ww3FFitCf7Qw4vSKQ3WLZvYzXyxj1Cz+g2ETRkfkNI6OZQ3z/CwdBF/3j+KCanP5s 6AlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=p5dmhqgXzU0PAo+f6c2oMinYRJIOzYqFfyKVcX2FFw4=; b=gbqkgNYycnYYv6n80bpGDyWGFl4aD1mwn0pu0OsnCNF9KFkFJ9i9oJ7gS44vJZL/2C a8wfOY/S77ex+5wg2Fb/u3qQfC5S9VWtf99Dw2LO0yhBnxt/WwUgAJBxGakd+qfMmxUM f6GWsKYXY49NTDpq884YPsL3GAs//Pf4IYhBZ6ynVVeEw9yDuqoPDhPoL4WqjUQUWEkD uNczl/1XQ7qO6CuFGFk0NjNtJ7nSg9Z79n+sPAHzqhi641JymJgwYZXKX7DoquE3+/80 ksJvpaJ+8B9p3cSVOVJbb4zlsOKrcx37MmygGXd5bUbseXir7etNw7Afze7lJF9/ocyV MwPw== X-Gm-Message-State: APjAAAUeHD1rjb8jcjJ1lQjLeLc5nje7weVdZx7Vi5GGRCkdecSFJWWC ekYAlWzI8dJXdw7wVGBM0LJjuYY5KBY= X-Google-Smtp-Source: APXvYqw9PY/lhJ73EzCXWsikiVQZ/uCl7VBROMZ9OezrOAMobg4OnZLhSNl9+6nrwsoM42hDlywdqQ== X-Received: by 2002:aed:2259:: with SMTP id o25mr3496173qtc.55.1569338425505; Tue, 24 Sep 2019 08:20:25 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id h68sm1073533qkd.35.2019.09.24.08.20.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2019 08:20:24 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, bpf@vger.kernel.org, cneirabustos@gmail.com Subject: [PATCH bpf-next v11 1/4] fs/nsfs.c: added ns_match Date: Tue, 24 Sep 2019 12:20:02 -0300 Message-Id: <20190924152005.4659-2-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190924152005.4659-1-cneirabustos@gmail.com> References: <20190924152005.4659-1-cneirabustos@gmail.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org ns_match returns true if the namespace inode and dev_t matches the ones provided by the caller. Signed-off-by: Carlos Neira --- fs/nsfs.c | 8 ++++++++ include/linux/proc_ns.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index a0431642c6b5..256f6295d33d 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -245,6 +245,14 @@ struct file *proc_ns_fget(int fd) return ERR_PTR(-EINVAL); } +/* Returns true if current namespace matches dev/ino. + */ +bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino) +{ + return ((ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev)); +} + + static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry) { struct inode *inode = d_inode(dentry); diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h index d31cb6215905..1da9f33489f3 100644 --- a/include/linux/proc_ns.h +++ b/include/linux/proc_ns.h @@ -82,6 +82,8 @@ typedef struct ns_common *ns_get_path_helper_t(void *); extern void *ns_get_path_cb(struct path *path, ns_get_path_helper_t ns_get_cb, void *private_data); +extern bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino); + extern int ns_get_name(char *buf, size_t size, struct task_struct *task, const struct proc_ns_operations *ns_ops); extern void nsfs_init(void); From patchwork Tue Sep 24 15:20:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Antonio Neira Bustos X-Patchwork-Id: 1166684 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="kL3hAbl/"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46d4dB50DMz9sP3 for ; Wed, 25 Sep 2019 01:20:30 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732279AbfIXPUa (ORCPT ); Tue, 24 Sep 2019 11:20:30 -0400 Received: from mail-qt1-f196.google.com ([209.85.160.196]:44093 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732277AbfIXPUa (ORCPT ); Tue, 24 Sep 2019 11:20:30 -0400 Received: by mail-qt1-f196.google.com with SMTP id u40so2574125qth.11; Tue, 24 Sep 2019 08:20:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4FCdKcUe9HTHpEsqAWhKQaxQOBpuFRezzfrMNuqYlEg=; b=kL3hAbl/bPbOgf+icxwlQSOvIguHi1EvAzVjZg8l1zETfYHfajUex/KcObvsc2rAWP AbAOlJEvyeQPm/AAXS5Yi4ymViY9CgYHKbN6okQfORfmzAvVL3l4XzEQ7/kBGleeDA3s M6LGZR8jdYdVGCtUtGNPPi7Na4TnRFXHgW9Mw/SXAi/p/C8wOn8HMtQIT6rK8u6OQ4cj XqNy2f6AuHmsfBM1AEXQ4g5KcGpopmkBSXAVLHZGFW93cSx97bPOTdzSpC9PqwVa4zjm dD8FMl/dohuuieikBbcx7sV4TnC972vRaTTs+s51kecNqZayzYHqorOWi61C5eR6AN8U i38w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4FCdKcUe9HTHpEsqAWhKQaxQOBpuFRezzfrMNuqYlEg=; b=i9V9PXMxDS2s/l8nfRAD6ZknW/zTkq6Dgt6rJSki4a6AMXoJyTYyRn0IkVriSKJecb bedTfQXYntrM+ntz7/QJYftRJcTCTGbQf91tSKl5AUfeovK2LOn694IZSLXw4LEJ5lm2 6sY/V6BvRdGiG9KWAayrIzpOR2LJiCncHojgza0St6VDrUP/sg/sYN9f232YfNrfK4wa B6grf2TxuDw3gT5+ZWbp+7YOq7lBt7j3P3nCxjerzNmPdzFxoUfyaD3SU4GF/0aH2PK+ UUwxClFsE0c2kw6v9U8ezp6ONl4hQswVBAXmTA7RMK/STfjhab1EJlcFHAS3YNch0MvQ LnOg== X-Gm-Message-State: APjAAAVSuILLIaPVq5RkcVwthV8Lkx9/VCy+CiEFAfgr0lV4HAESJCg1 NT/UBfgjEOY8fHeL/gUFPpuxkQ2YTQM= X-Google-Smtp-Source: APXvYqwfRayK2HPCUlm9vFFc9YxYZlrKUX7b+pUogfY0bo4/+pOt9EWeUnbeh6DyHTA/5HioWo3HCQ== X-Received: by 2002:ac8:71cb:: with SMTP id i11mr3415297qtp.208.1569338427711; Tue, 24 Sep 2019 08:20:27 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id h68sm1073533qkd.35.2019.09.24.08.20.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2019 08:20:27 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, bpf@vger.kernel.org, cneirabustos@gmail.com Subject: [PATCH bpf-next v11 2/4] bpf: added new helper bpf_get_ns_current_pid_tgid Date: Tue, 24 Sep 2019 12:20:03 -0300 Message-Id: <20190924152005.4659-3-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190924152005.4659-1-cneirabustos@gmail.com> References: <20190924152005.4659-1-cneirabustos@gmail.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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 --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 32 ++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5b9d22338606..231001475504 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1055,6 +1055,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto; extern const struct bpf_func_proto bpf_strtol_proto; extern const struct bpf_func_proto bpf_strtoul_proto; extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto; /* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once(void); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 77c6be96d676..9272dc8fb08c 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2750,6 +2750,21 @@ union bpf_attr { * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 + * + * int bpf_get_ns_current_pid_tgid(u32 dev, u64 inum) + * Return + * A 64-bit integer containing the current tgid and pid from current task + * which namespace inode and dev_t matches , and is create as such: + * *current_task*\ **->tgid << 32 \|** + * *current_task*\ **->pid**. + * + * On failure, the returned value is one of the following: + * + * **-EINVAL** if dev and inum supplied don't match dev_t and inode number + * with nsfs of current task. + * + * **-ENOENT** if /proc/self/ns does not exists. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2862,7 +2877,8 @@ union bpf_attr { FN(sk_storage_get), \ FN(sk_storage_delete), \ FN(send_signal), \ - FN(tcp_gen_syncookie), + FN(tcp_gen_syncookie), \ + FN(get_ns_current_pid_tgid), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 66088a9e9b9e..b2fd5358f472 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2042,6 +2042,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; const struct bpf_func_proto bpf_get_current_comm_proto __weak; const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak; const struct bpf_func_proto bpf_get_local_storage_proto __weak; +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak; const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) { diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 5e28718928ca..81a716eae7ed 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include "../../lib/kstrtox.h" @@ -487,3 +489,33 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, }; #endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u32, dev, u64, inum) +{ + struct task_struct *task = current; + struct pid_namespace *pidns; + pid_t pid, tgid; + + if (unlikely(!task)) + return -EINVAL; + + pidns = task_active_pid_ns(task); + if (unlikely(!pidns)) + return -ENOENT; + + if (!ns_match(&pidns->ns, (dev_t)dev, inum)) + return -EINVAL; + + pid = task_pid_nr_ns(task, pidns); + tgid = task_tgid_nr_ns(task, pidns); + + return (u64) tgid << 32 | pid; +} + +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { + .func = bpf_get_ns_current_pid_tgid, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_ANYTHING, + .arg2_type = ARG_ANYTHING, +}; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index ca1255d14576..1d34f1013e78 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -709,6 +709,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) #endif case BPF_FUNC_send_signal: return &bpf_send_signal_proto; + case BPF_FUNC_get_ns_current_pid_tgid: + return &bpf_get_ns_current_pid_tgid_proto; default: return NULL; } From patchwork Tue Sep 24 15:20:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Antonio Neira Bustos X-Patchwork-Id: 1166686 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="S+wgdPrL"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46d4dF4hsdz9sP3 for ; Wed, 25 Sep 2019 01:20:33 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732357AbfIXPUc (ORCPT ); Tue, 24 Sep 2019 11:20:32 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:39820 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732277AbfIXPUb (ORCPT ); Tue, 24 Sep 2019 11:20:31 -0400 Received: by mail-qk1-f195.google.com with SMTP id 4so2165034qki.6; Tue, 24 Sep 2019 08:20:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6isa7u+Ujr1ueMUpo8eCINPWXfwol52ywCtO8hQl9tg=; b=S+wgdPrLyHrm/MmKiGYshhE0f/hjtoYVUg/3NfIemWa5eGM3rbea8o1Q25H/elEoM6 5Z2wnPbeITWcngOPRqh9D0rrMkg+cbAlWEXbS3BOfj4kuzppbHThIbW6R9ZPxGv0BKcK v0v0G/SlBQoLU7ZPONLDwUBWSPdqIkRqUnRndHkHR0uU/ksXHF3JYcsirDs0XpAB6omd nkczLG9I7+iQ5hauwd9LVfQqhd2/Y1vTqXSYzHnnrLN6CP42CeA8RCTeXbN+bhcJvvq3 jwM/DnfSR6RC2ZlvIN0Io6DTW7yQVYg2VMjJ0LuIdFAf5vQLAZPZgOvo7cB8VmdPUxni vR9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6isa7u+Ujr1ueMUpo8eCINPWXfwol52ywCtO8hQl9tg=; b=lGy9UTSM48VH6k14q1f4vhQQ1algjsu9oFKm0dvBlXvI1eF7qmr6tSOmWKiIcgk6Xc BGQMi4NjPnHTx2HKfa86j9Yst/4tOgBljtwxOvKAYJb8QBEB8rmQebTq1URSAE2UQ8n8 ovhRepEvnIxIaWCdOcvnsSfMOUdRTsaLj6ZYkIkj8Lzi2tZA384DIfdfC/fvx86zU+Sn 9ql2g27k2U+QMV+MF1iA51RoMCDySgqFDQ063vCVR8ypSvkoj/TASy9Fbr29FzPCPNHk z2JG5oT8pyKXgb6g41C6MqjIUZ1rgTjalQK7cDGb9y0lkP3Tma73VL1mZioGy/TzlECn FvGg== X-Gm-Message-State: APjAAAXQbZ9clhA2GfjhwOIoDeacF2M+fh8Jxy8WFBUTdGVl1nvVJxbd P/JiH2k9wp/nenupYqCwC3JZNF5v1tk= X-Google-Smtp-Source: APXvYqy+w9U5G0Nu3h0TbjbAs7l5PeGJ/bycRExWT6gcPHwMcW381xiF+bcb+k7W1+Nxu+Ez9UFOOQ== X-Received: by 2002:a37:af81:: with SMTP id y123mr2973871qke.145.1569338430087; Tue, 24 Sep 2019 08:20:30 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id h68sm1073533qkd.35.2019.09.24.08.20.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2019 08:20:29 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, bpf@vger.kernel.org, cneirabustos@gmail.com Subject: [PATCH bpf-next v11 3/4] tools: Added bpf_get_ns_current_pid_tgid helper Date: Tue, 24 Sep 2019 12:20:04 -0300 Message-Id: <20190924152005.4659-4-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190924152005.4659-1-cneirabustos@gmail.com> References: <20190924152005.4659-1-cneirabustos@gmail.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Signed-off-by: Carlos Neira --- tools/include/uapi/linux/bpf.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 77c6be96d676..9272dc8fb08c 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2750,6 +2750,21 @@ union bpf_attr { * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 + * + * int bpf_get_ns_current_pid_tgid(u32 dev, u64 inum) + * Return + * A 64-bit integer containing the current tgid and pid from current task + * which namespace inode and dev_t matches , and is create as such: + * *current_task*\ **->tgid << 32 \|** + * *current_task*\ **->pid**. + * + * On failure, the returned value is one of the following: + * + * **-EINVAL** if dev and inum supplied don't match dev_t and inode number + * with nsfs of current task. + * + * **-ENOENT** if /proc/self/ns does not exists. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2862,7 +2877,8 @@ union bpf_attr { FN(sk_storage_get), \ FN(sk_storage_delete), \ FN(send_signal), \ - FN(tcp_gen_syncookie), + FN(tcp_gen_syncookie), \ + FN(get_ns_current_pid_tgid), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call From patchwork Tue Sep 24 15:20:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Antonio Neira Bustos X-Patchwork-Id: 1166687 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JZuUEeD5"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46d4dJ0bn8z9sPY for ; Wed, 25 Sep 2019 01:20:35 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732460AbfIXPUe (ORCPT ); Tue, 24 Sep 2019 11:20:34 -0400 Received: from mail-qk1-f169.google.com ([209.85.222.169]:42003 "EHLO mail-qk1-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731643AbfIXPUe (ORCPT ); Tue, 24 Sep 2019 11:20:34 -0400 Received: by mail-qk1-f169.google.com with SMTP id f16so2151840qkl.9; Tue, 24 Sep 2019 08:20:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=39ft0VWg4fce//AGmM6MxERxPxbG5AIm3iEKhOWgxnk=; b=JZuUEeD5ov84QjGV0tmmE5WwBtm5sHvZpajTodCDXTKF1u2j8AnbdTO5RMFeD0Y+wS BtdoYfraJ+CkqEzn5+vUw/bN7J1eu62wsI+QpMywxp2m8JrZoaadMFUg6pbkgdv/2HaV tpqH1jVQjEpolCs43C9++WRCHImlaSgFR2/+rizLH5fOTW+jT85rOqgCk0xvBdlvWa2P de9sKHfk8m+y600MpNUCh0AZrrS5Eu0ILNnsyPUrQ24eZmzwOTtBPbE3HtX06oXh3ub9 VL3aok8dRinmQPm4Z6Qjyzv9zo47MTw5Lp4qzrGSlOWnt+xmcc9WhRLCrbtIMFCo0zZA RbOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=39ft0VWg4fce//AGmM6MxERxPxbG5AIm3iEKhOWgxnk=; b=UU+OmBAnxnLmBwDNfLCkRX8Da5UTD66m/a9B9gbrNtoP1KA+oqCWmyffkoyNtDTLbS 5ZCKsbfbm2TyJpwatIm5dbX99uQB0qNJi5ZbVp+/tieivgWrY55wp3nNtMEXLw/50QsX gy3dW7UU3eR64zOX+ZNTWAo9EAJtxrEu4ItC9yAI97kN582t3455T7ST97/mp1YMQsNa o+8DsvHgfd0BT09hLqJ1u8zrYWZf961AeiOZl0aBoMekLQ94nTwSmilg5J6XLktVmV/R NkQHE91DAdsC8sSV4nwIP1+4juODG/UB72Ra6kq5iBcazkrKDYfckv9xrkTCgFrfZA16 XwNQ== X-Gm-Message-State: APjAAAX8L8NVVQidxLEN/mkn4jm04HYuNeIq8ONqD00UomMplrlvEY5g NalwG8amUWKDjBX92GNDdmWPpdFPxaU= X-Google-Smtp-Source: APXvYqwqtz/sE7IXdJWR4aEyZzqqKkoITIFV7ru1pQONA74gv7P3vaUSL0wk8EBoFJpfGoIo1lRd2A== X-Received: by 2002:a37:5cc1:: with SMTP id q184mr2994415qkb.212.1569338432234; Tue, 24 Sep 2019 08:20:32 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id h68sm1073533qkd.35.2019.09.24.08.20.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2019 08:20:31 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, bpf@vger.kernel.org, cneirabustos@gmail.com Subject: [PATCH bpf-next v11 4/4] tools/testing/selftests/bpf: Add self-tests for new helper. Date: Tue, 24 Sep 2019 12:20:05 -0300 Message-Id: <20190924152005.4659-5-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190924152005.4659-1-cneirabustos@gmail.com> References: <20190924152005.4659-1-cneirabustos@gmail.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Self tests added for new helper Signed-off-by: Carlos Neira --- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../selftests/bpf/progs/test_pidns_kern.c | 71 ++++++++ tools/testing/selftests/bpf/test_pidns.c | 152 ++++++++++++++++++ 4 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_pidns_kern.c create mode 100644 tools/testing/selftests/bpf/test_pidns.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 7f3196af1ae4..d86b28aa8f44 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -28,7 +28,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage test_select_reuseport test_section_names \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ - test_btf_dump test_cgroup_attach xdping + test_btf_dump test_cgroup_attach xdping test_pidns BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) TEST_GEN_FILES = $(BPF_OBJ_FILES) diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index 6c4930bc6e2e..03d0e15ae29f 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -231,6 +231,9 @@ static int (*bpf_send_signal)(unsigned sig) = (void *)BPF_FUNC_send_signal; static long long (*bpf_tcp_gen_syncookie)(struct bpf_sock *sk, void *ip, int ip_len, void *tcp, int tcp_len) = (void *) BPF_FUNC_tcp_gen_syncookie; +static int (*bpf_get_ns_current_pid_tgid)(__u32 dev, __u64 inum) = + (void *) BPF_FUNC_get_ns_current_pid_tgid; + /* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions diff --git a/tools/testing/selftests/bpf/progs/test_pidns_kern.c b/tools/testing/selftests/bpf/progs/test_pidns_kern.c new file mode 100644 index 000000000000..96cb707db3ee --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_pidns_kern.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2018 Carlos Neira cneirabustos@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ + +#include +#include "bpf_helpers.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} ns_inum_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} ns_dev_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} pidmap SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} ns_pid_map SEC(".maps"); + + + +SEC("tracepoint/syscalls/sys_enter_nanosleep") +int trace(void *ctx) +{ + __u32 key = 0, *expected_pid, *dev; + __u64 *val, *inum, nspid; + __u32 pid; + + dev = bpf_map_lookup_elem(&ns_dev_map, &key); + if (!dev) + return 0; + + inum = bpf_map_lookup_elem(&ns_inum_map, &key); + if (!inum) + return 0; + + nspid = bpf_get_ns_current_pid_tgid(*dev, *inum); + expected_pid = bpf_map_lookup_elem(&pidmap, &key); + + if (!expected_pid || *expected_pid != nspid) + return 0; + + val = bpf_map_lookup_elem(&ns_pid_map, &key); + if (val) + *val = nspid; + + return 0; +} + +char _license[] SEC("license") = "GPL"; +__u32 _version SEC("version") = 1; diff --git a/tools/testing/selftests/bpf/test_pidns.c b/tools/testing/selftests/bpf/test_pidns.c new file mode 100644 index 000000000000..088f8025f2bf --- /dev/null +++ b/tools/testing/selftests/bpf/test_pidns.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2018 Carlos Neira cneirabustos@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "cgroup_helpers.h" +#include "bpf_rlimit.h" + +#define CHECK(condition, tag, format...) ({ \ + int __ret = !!(condition); \ + if (__ret) { \ + printf("%s:FAIL:%s ", __func__, tag); \ + printf(format); \ + } else { \ + printf("%s:PASS:%s\n", __func__, tag); \ + } \ + __ret; \ +}) + +static int bpf_find_map(const char *test, struct bpf_object *obj, + const char *name) +{ + struct bpf_map *map; + + map = bpf_object__find_map_by_name(obj, name); + if (!map) + return -1; + return bpf_map__fd(map); +} + + +int main(int argc, char **argv) +{ + int pidmap_fd, ns_inum_map_fd, ns_dev_map_fd, ns_pid_map_fd; + const char *probe_name = "syscalls/sys_enter_nanosleep"; + const char *file = "test_pidns_kern.o"; + int err, bytes, efd, prog_fd, pmu_fd; + struct perf_event_attr attr = {}; + struct bpf_object *obj; + __u32 nspid = 0; + __u32 key = 0, pid; + int exit_code = 1; + struct stat st; + char buf[256]; + + err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); + if (CHECK(err, "bpf_prog_load", "err %d errno %d\n", err, errno)) + goto cleanup_cgroup_env; + + ns_dev_map_fd = bpf_find_map(__func__, obj, "ns_dev_map"); + if (CHECK(ns_dev_map_fd < 0, "bpf_find_map", "err %d errno %d\n", + ns_dev_map_fd, errno)) + goto close_prog; + + ns_inum_map_fd = bpf_find_map(__func__, obj, "ns_inum_map"); + if (CHECK(ns_inum_map_fd < 0, "bpf_find_map", "err %d errno %d\n", + ns_inum_map_fd, errno)) + goto close_prog; + + ns_pid_map_fd = bpf_find_map(__func__, obj, "ns_pid_map"); + if (CHECK(ns_pid_map_fd < 0, "bpf_find_map", "err %d errno %d\n", + ns_pid_map_fd, errno)) + goto close_prog; + + pidmap_fd = bpf_find_map(__func__, obj, "pidmap"); + if (CHECK(pidmap_fd < 0, "bpf_find_map", "err %d errno %d\n", + pidmap_fd, errno)) + goto close_prog; + + pid = getpid(); + bpf_map_update_elem(pidmap_fd, &key, &pid, 0); + + if (stat("/proc/self/ns/pid", &st)) + goto close_prog; + + bpf_map_update_elem(ns_inum_map_fd, &key, &st.st_ino, 0); + bpf_map_update_elem(ns_dev_map_fd, &key, &st.st_dev, 0); + + + snprintf(buf, sizeof(buf), + "/sys/kernel/debug/tracing/events/%s/id", probe_name); + efd = open(buf, O_RDONLY, 0); + if (CHECK(efd < 0, "open", "err %d errno %d\n", efd, errno)) + goto close_prog; + bytes = read(efd, buf, sizeof(buf)); + close(efd); + if (CHECK(bytes <= 0 || bytes >= sizeof(buf), "read", + "bytes %d errno %d\n", bytes, errno)) + goto close_prog; + + attr.config = strtol(buf, NULL, 0); + attr.type = PERF_TYPE_TRACEPOINT; + attr.sample_type = PERF_SAMPLE_RAW; + attr.sample_period = 1; + attr.wakeup_events = 1; + + pmu_fd = syscall(__NR_perf_event_open, &attr, getpid(), -1, -1, 0); + if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n", pmu_fd, + errno)) + goto close_prog; + + err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); + if (CHECK(err, "perf_event_ioc_enable", "err %d errno %d\n", err, + errno)) + goto close_pmu; + + err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); + if (CHECK(err, "perf_event_ioc_set_bpf", "err %d errno %d\n", err, + errno)) + goto close_pmu; + + /* trigger some syscalls */ + sleep(1); + + err = bpf_map_lookup_elem(ns_pid_map_fd, &key, &nspid); + if (CHECK(err, "bpf_map_lookup_elem", "err %d errno %d\n", err, errno)) + goto close_pmu; + + if (CHECK(nspid != pid, "compare nspid vs pid", + "kern nspid %u user pid %u", nspid, pid)) + goto close_pmu; + + exit_code = 0; + printf("%s:PASS\n", argv[0]); + +close_pmu: + close(pmu_fd); +close_prog: + bpf_object__close(obj); +cleanup_cgroup_env: + return exit_code; +}