From patchwork Tue Oct 22 19:17:47 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: 1181617 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 (no SPF record) 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="VUZQposs"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46yNZS5d46z9sNx for ; Wed, 23 Oct 2019 06:18:08 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732746AbfJVTSI (ORCPT ); Tue, 22 Oct 2019 15:18:08 -0400 Received: from mail-qt1-f193.google.com ([209.85.160.193]:45065 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727851AbfJVTSI (ORCPT ); Tue, 22 Oct 2019 15:18:08 -0400 Received: by mail-qt1-f193.google.com with SMTP id c21so28444654qtj.12; Tue, 22 Oct 2019 12:18:06 -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=AhKfE6tOCfM1Lz5a/uUPR4TRFtdWhoskXzdYNzZtSYA=; b=VUZQpossR5/DSU3TtkLRvDciKo/SEJ+BTyo2LJuV+tFZAIZeJIV6KOaIAQLA4OOtpg ux9j6WF8w+wYWBa6P+I9bo7bZIFeKP9e+hzaMRkbN+Hb4LhADQ0CwtEKogZ8RAROrVxI wznb0FmBUhhEP2qa9b+Mm0njPe/AtASN2YyFBNOkHeZRho0ETzZ7ALVHvM9lS8qUELk/ IYTEMmrmuMEJoaacIImOOzbblah0SyAN7P5LkD7UBd0ECkiQDPqEVlgo/wO6u2gvT1m9 Ub13QvFtQK9TRoPdLrElXcdA7s4thTDq6bQyW7UMJrJTTFtEg0D9QTar66gvmGhE2Ui/ 7oGw== 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=AhKfE6tOCfM1Lz5a/uUPR4TRFtdWhoskXzdYNzZtSYA=; b=Gt4kFHB3/hnQEJgRC+/buZftwd6bf6MNtI5WnimHgtpNFdoggQpJfOGsCPZYdx/7v8 jyYJQwcHNXAsWWKZZfU5Pm4xx64x+C4KnQPQlFKnd6j3oH4AdSjq1kN+ZZMhp5X+cqFP 3xyQRBGNevR5TbXBYxVzncWH90x8OmLyJD/phZoCV8zjXxO0kpP0RkjlD5bMuDSbTR4y e3/DxFuBN3nUVWuVGb0uWBJuH6O0lWQh7CgNaIIdfNFjYAySLWiWb6tYBmtGULTDNZs1 iC2oK96V6AscATmhMoW6usik4ob7tQTZTmxuqSMRjmI8yQbAVq3GM0CLBUDxJUCKQFEh ejgA== X-Gm-Message-State: APjAAAWVBQLcE5xLUDRdEY39d2xahAiTZ9Cq7nrpUEXqoDykA0cr9bka zwsXE2bkTZTJk7NG4ZD92jPbyGPls1WoSsnh X-Google-Smtp-Source: APXvYqybe/SbRxnnsrSqqjNtPeQFgjh3jRvTIO9mKKkRShBJajm9Ks24DGOdrZ8M7tlTwXl5lUtfIA== X-Received: by 2002:a0c:d0e1:: with SMTP id b30mr4874313qvh.197.1571771885223; Tue, 22 Oct 2019 12:18:05 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id r36sm8015969qta.27.2019.10.22.12.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2019 12:18:04 -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 v15 1/5] fs/nsfs.c: added ns_match Date: Tue, 22 Oct 2019 16:17:47 -0300 Message-Id: <20191022191751.3780-2-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191022191751.3780-1-cneirabustos@gmail.com> References: <20191022191751.3780-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 | 14 ++++++++++++++ include/linux/proc_ns.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index a0431642c6b5..ef59cf347285 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -245,6 +245,20 @@ struct file *proc_ns_fget(int fd) return ERR_PTR(-EINVAL); } +/** + * ns_match() - Returns true if current namespace matches dev/ino provided. + * @ns_common: current ns + * @dev: dev_t from nsfs that will be matched against current nsfs + * @ino: ino_t from nsfs that will be matched against current nsfs + * + * Return: true if dev and ino matches the current nsfs. + */ +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 Oct 22 19:17:48 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: 1181619 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 (no SPF record) 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="DMUTy1tc"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46yNZW4pSJz9sNx for ; Wed, 23 Oct 2019 06:18:11 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732823AbfJVTSL (ORCPT ); Tue, 22 Oct 2019 15:18:11 -0400 Received: from mail-qk1-f193.google.com ([209.85.222.193]:47066 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727851AbfJVTSK (ORCPT ); Tue, 22 Oct 2019 15:18:10 -0400 Received: by mail-qk1-f193.google.com with SMTP id e66so17350801qkf.13; Tue, 22 Oct 2019 12:18:08 -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=0ZO7S6LM3jSCHbQEfmNEYf2yD/qvK5cSl3UDAKIr2A8=; b=DMUTy1tc4a9vMQdVfwcNa+3qX8Q9wvoJCK+yI08TU4OxjtAyzRR4I7en21kdxAvYzG w0IonBCbGZWnSNU4ognXWsOKef2w9g7w276IQN+lHpbGwOgq2UmAUJMRoR0ynDiTvmN6 A7hewQG2oSzr9B3L9Pn/0GRLXg1HdpBkszsqYPRY4w1i3TLmYU0dqklxn0o+1hq2NJb0 8kJK/whmbJun03yYlMeKj8CF9F4Lq+gOjPOQnSgi8o+mlDvN5bXMPv6f6uh4eMgptI8V XYAZTUIoeJVzR+pDKaM7IZ9tWj+CONXHa/8AeJGxCmH6UPvc4aDeVc81H4XiAzEG5LFy A8Pg== 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=0ZO7S6LM3jSCHbQEfmNEYf2yD/qvK5cSl3UDAKIr2A8=; b=Um1iAWAD7RPv74QwjJSg18i02g1P6xSG7ykDRBNmp1sPmAokezCEW7FC6GqIGDx7nr 5AtCjws19ZlTkk8xDVcbc9Y2HKoi3r5KyM8DvoPxP75wJCSmwpGjika7JdewPXaeCf8V vLqQkiFObRMeCVu+3TNFhjMr7OyJG5im7p/JM9hgwtsU5w8mc33ZbJ103Bxd1aTqFUI+ 3xKT5kF/W9hmYiSCTVyazQQnFVsxvPQg+75oUEukqubgkJzIAT1tl7SDIBqEe+yKeKH2 ZN8shZCQb/V8WfbQP670JIJdrej9jKYSrXeiCZEUB260N9MLa5+7QpYWBhwrmRZA3hEu TlSA== X-Gm-Message-State: APjAAAUcfyWC5vsrTjBaYrVmen/lnnu/1CXdDt6xi3XVTws2wh/iCk8F 9vWAfqHztyC/cFGDNuPXaVmtiPiVWwLf6983 X-Google-Smtp-Source: APXvYqxyW8PViZh3AycUqVq5NYcF0ynS+YcsUQIwSexVHtCW7Lqy/V0C2SyouvDjwG/eyCbwA+d+dQ== X-Received: by 2002:a37:8547:: with SMTP id h68mr4636852qkd.162.1571771887521; Tue, 22 Oct 2019 12:18:07 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id r36sm8015969qta.27.2019.10.22.12.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2019 12:18:06 -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 v15 2/5] bpf: added new helper bpf_get_ns_current_pid_tgid Date: Tue, 22 Oct 2019 16:17:48 -0300 Message-Id: <20191022191751.3780-3-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191022191751.3780-1-cneirabustos@gmail.com> References: <20191022191751.3780-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 Acked-by: Yonghong Song --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 20 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 45 ++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 2c2c29b49845..1d7c86019113 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1082,6 +1082,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 4af8b0819a32..4c3e0b0952e6 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2775,6 +2775,19 @@ union bpf_attr { * restricted to raw_tracepoint bpf programs. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size) + * Description + * Returns 0 on success, values for *pid* and *tgid* as seen from the current + * *namespace* will be returned in *nsdata*. + * + * 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, or if dev conversion to dev_t lost high bits. + * + * **-ENOENT** if pidns does not exists for the current task. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2888,7 +2901,8 @@ union bpf_attr { FN(sk_storage_delete), \ FN(send_signal), \ FN(tcp_gen_syncookie), \ - FN(skb_output), + FN(skb_output), \ + FN(get_ns_current_pid_tgid), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3639,4 +3653,8 @@ struct bpf_sockopt { __s32 retval; }; +struct bpf_pidns_info { + __u32 pid; + __u32 tgid; +}; #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 673f5d40a93e..04083942a13a 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2079,6 +2079,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..5477ad984d7c 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,46 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, }; #endif + +BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino, + struct bpf_pidns_info *, nsdata, u32, size) +{ + struct task_struct *task = current; + struct pid_namespace *pidns; + int err = -EINVAL; + + if (unlikely(size != sizeof(struct bpf_pidns_info))) + goto clear; + + if (unlikely((u64)(dev_t)dev != dev)) + goto clear; + + if (unlikely(!task)) + goto clear; + + pidns = task_active_pid_ns(task); + if (unlikely(!pidns)) { + err = -ENOENT; + goto clear; + } + + if (!ns_match(&pidns->ns, (dev_t)dev, ino)) + goto clear; + + nsdata->pid = task_pid_nr_ns(task, pidns); + nsdata->tgid = task_tgid_nr_ns(task, pidns); + return 0; +clear: + memset((void *)nsdata, 0, (size_t) size); + return err; +} + +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, + .arg3_type = ARG_PTR_TO_UNINIT_MEM, + .arg4_type = ARG_CONST_SIZE, +}; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index c3240898cc44..07f6fa354f15 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -735,6 +735,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 Oct 22 19:17:49 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: 1181620 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 (no SPF record) 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="cSaurt/J"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46yNZX6LZ4z9sP6 for ; Wed, 23 Oct 2019 06:18:12 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732863AbfJVTSL (ORCPT ); Tue, 22 Oct 2019 15:18:11 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:33114 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732810AbfJVTSL (ORCPT ); Tue, 22 Oct 2019 15:18:11 -0400 Received: by mail-qk1-f194.google.com with SMTP id 71so13509069qkl.0; Tue, 22 Oct 2019 12:18:10 -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=S56WLPMIQyH40sEaPP4IlYDbb/ty7fLg8KIWGLpaAaI=; b=cSaurt/JFUzQZyQVB6FAVqQLFyRcyH0a0rGsXSH6MQjvXkCY4rNqbUYzFJTjKRa6RI 7AqKJ402huOzMcSSYPF7hhPWB/RBpz94DzPMEIcJQlF+UgjaytJtqQAPZ3q68oB/wuKo HvpVpXLUCkIhHwr5hYoA5P37fyZEImC/LC51a8IIfiHKLUeZag1OGzV+NirlFKtK7PSm BAmkmhh7XvZJepns4n2IAmVL4p3yeewV8yRHrWISSZfeP+ufk1IwL9xphsW9xyAf5vfz LnkK3Ulz3CKD9MsB5pt3C+CepZJUua6VG/VrMThLrdmEaGsbmt1Zhuyq516Xvl/eUSkg JjBw== 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=S56WLPMIQyH40sEaPP4IlYDbb/ty7fLg8KIWGLpaAaI=; b=pmEDGb63nkUp6gGpFc0+R9ti8a7JNT8psA+yXdLqafC7APJTyqzQgFSdnreMxU/uhE o5iCnOp3/VpothUXlafVirCYPY6uu/wjp5axAs4VmX2YKpmzJhyUX0MqXnLI3GRBMb/l Sqdz4oh1R8rxJWKvSoqD1QEZxoI9v9defcoNX+hmGZdxC/F9puWx8u/9PfC3jz5FR2ww WWqZyJ7Yt+qMCM639RYRNDj/NyinO0U1BqgB32IwaiqfdjbPNquHH96Fx8zES1hsLMFZ wp7b7XT9JDIBI3/AZmJe+2YCguIeaE27cFdRy+URycMtvSv4uNmiDWMzkSctdHGKaJoI nZkw== X-Gm-Message-State: APjAAAU4lxsqGxTh3ec7H3MSGAqPPYLztDlWd6l8K9C2jLX/Ceph3Yr2 ajCOmhgHC4LMxpxaKdPTcYvbzbSIfESxMil7 X-Google-Smtp-Source: APXvYqyikDPze1zpmUC2oGKpvrH1b+xjhi5Ll+Z95w69nWNZJwGRjIjol+XWN6nLIjsWIrZmIGzDuw== X-Received: by 2002:ae9:d8c2:: with SMTP id u185mr4895114qkf.120.1571771889597; Tue, 22 Oct 2019 12:18:09 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id r36sm8015969qta.27.2019.10.22.12.18.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2019 12:18:09 -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 v15 3/5] tools: Added bpf_get_ns_current_pid_tgid helper Date: Tue, 22 Oct 2019 16:17:49 -0300 Message-Id: <20191022191751.3780-4-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191022191751.3780-1-cneirabustos@gmail.com> References: <20191022191751.3780-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 sync tools/include/uapi/linux/bpf.h to include new helper. Signed-off-by: Carlos Neira Acked-by: Yonghong Song --- tools/include/uapi/linux/bpf.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4af8b0819a32..4c3e0b0952e6 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2775,6 +2775,19 @@ union bpf_attr { * restricted to raw_tracepoint bpf programs. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size) + * Description + * Returns 0 on success, values for *pid* and *tgid* as seen from the current + * *namespace* will be returned in *nsdata*. + * + * 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, or if dev conversion to dev_t lost high bits. + * + * **-ENOENT** if pidns does not exists for the current task. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2888,7 +2901,8 @@ union bpf_attr { FN(sk_storage_delete), \ FN(send_signal), \ FN(tcp_gen_syncookie), \ - FN(skb_output), + FN(skb_output), \ + FN(get_ns_current_pid_tgid), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3639,4 +3653,8 @@ struct bpf_sockopt { __s32 retval; }; +struct bpf_pidns_info { + __u32 pid; + __u32 tgid; +}; #endif /* _UAPI__LINUX_BPF_H__ */ From patchwork Tue Oct 22 19:17:50 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: 1181623 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 (no SPF record) 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="IszFyBHa"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46yNZb0bN0z9sPf for ; Wed, 23 Oct 2019 06:18:15 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732891AbfJVTSO (ORCPT ); Tue, 22 Oct 2019 15:18:14 -0400 Received: from mail-qt1-f170.google.com ([209.85.160.170]:35785 "EHLO mail-qt1-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732810AbfJVTSN (ORCPT ); Tue, 22 Oct 2019 15:18:13 -0400 Received: by mail-qt1-f170.google.com with SMTP id m15so28564190qtq.2; Tue, 22 Oct 2019 12:18:12 -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=MulQUD6rGJeM5sHo2oz8MH20hWywb8PTM9mTAlClRgQ=; b=IszFyBHam6KaaN6+58bXUAW4AjgncJy7nPwyNBMRYkzLkIzt3n3otgFTTVXTvNBZTW 55PL/5irZGMPoxzWViKAmNZu/gqKwo2qa7zIeEx80E+Wj/ZzCSPxdHVmCYAJJ7zScrC9 T475vqMMAF0Bqk6nc/ZeiJYfOqBYufHkZnFwaFinPiAWkhe+aGNjuO51QGuGse7HKZig FCrDyKPL0pkA6FJOsDAQYYTTtTvQD1oAaupb0liSVeXpqT1r4bgYab/kNXcT2O8PbWvr gVyd5Ziz+1GKW4L/BE3iueZgFwNJ1eEIfUyU86ekyQy44mwiscaB9HZ+qV24yCobQmMj fH7g== 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=MulQUD6rGJeM5sHo2oz8MH20hWywb8PTM9mTAlClRgQ=; b=Z9/mMmkaGa+5O+ArEgRgGwD+vWpScUJbmI5qrFpVVao1iBnyvdXbivQGHKgnRZmPp8 B+yVgN9oRpJgX+bUM3rHR8iy0clsyPxyDpIe1DtQ2Pb/PDBhk9/T/blM1oCjyQe5XhtQ zr/1TGaXHCuvFLn8SSyqgs3JE1KdGCKd53tzINWxN+MTcyLLOy5UrTuKYa27rQGV8JGR OP1FB/P3dqk+4sLrbj1O3wgU3itMjSGOKzZZPoBCNJx9w2Izt1o/kWIMzQrzoo/MaXiH Pu1MLNGi6xA/Hzf0eKian//Y4CTGgfFQsFZulmlkObG3UnAIkLKOK+/bk11lgOhBvgaZ m8SA== X-Gm-Message-State: APjAAAWMhRxILAifSWagaeNMVwmi7vCOvd7LWm2uNLwTtyDyjLX+55sR t70lKQRrUtoZw6Iu3XyatCFCvTlop9Kf0Q51 X-Google-Smtp-Source: APXvYqxm1dFy04+qvI75wHgG6GhNLXCv5pn88h/fNR8DqRcQgB3slFRnBW9AF7IwmAMbkXIv7202ng== X-Received: by 2002:a0c:b918:: with SMTP id u24mr4954192qvf.212.1571771891662; Tue, 22 Oct 2019 12:18:11 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id r36sm8015969qta.27.2019.10.22.12.18.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2019 12:18:11 -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 v15 4/5] tools/testing/selftests/bpf: Add self-tests for new helper. Date: Tue, 22 Oct 2019 16:17:50 -0300 Message-Id: <20191022191751.3780-5-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191022191751.3780-1-cneirabustos@gmail.com> References: <20191022191751.3780-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 Acked-by: Yonghong Song --- .../bpf/prog_tests/ns_current_pid_tgid.c | 87 +++++++++++++++++++ .../bpf/progs/test_ns_current_pid_tgid.c | 37 ++++++++ 2 files changed, 124 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c create mode 100644 tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c diff --git a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c new file mode 100644 index 000000000000..257f18999bb6 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Carlos Neira cneirabustos@gmail.com */ +#include +#include +#include +#include +#include + +struct bss { + __u64 dev; + __u64 ino; + __u64 pid_tgid; + __u64 user_pid_tgid; +}; + +void test_ns_current_pid_tgid(void) +{ + const char *probe_name = "raw_tracepoint/sys_enter"; + const char *file = "test_ns_current_pid_tgid.o"; + int err, key = 0, duration = 0; + struct bpf_link *link = NULL; + struct bpf_program *prog; + struct bpf_map *bss_map; + struct bpf_object *obj; + struct bss bss; + struct stat st; + __u64 id; + + obj = bpf_object__open_file(file, NULL); + if (CHECK(IS_ERR(obj), "obj_open", "err %ld\n", PTR_ERR(obj))) + return; + + err = bpf_object__load(obj); + if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno)) + goto cleanup; + + bss_map = bpf_object__find_map_by_name(obj, "test_ns_.bss"); + if (CHECK(!bss_map, "find_bss_map", "failed\n")) + goto cleanup; + + prog = bpf_object__find_program_by_title(obj, probe_name); + if (CHECK(!prog, "find_prog", "prog '%s' not found\n", + probe_name)) + goto cleanup; + + memset(&bss, 0, sizeof(bss)); + pid_t tid = syscall(SYS_gettid); + pid_t pid = getpid(); + + id = (__u64) tid << 32 | pid; + bss.user_pid_tgid = id; + + if (CHECK_FAIL(stat("/proc/self/ns/pid", &st))) { + perror("Failed to stat /proc/self/ns/pid"); + goto cleanup; + } + + bss.dev = st.st_dev; + bss.ino = st.st_ino; + + err = bpf_map_update_elem(bpf_map__fd(bss_map), &key, &bss, 0); + if (CHECK(err, "setting_bss", "failed to set bss : %d\n", err)) + goto cleanup; + + link = bpf_program__attach_raw_tracepoint(prog, "sys_enter"); + if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", + PTR_ERR(link))) + goto cleanup; + + /* trigger some syscalls */ + usleep(1); + + err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &key, &bss); + if (CHECK(err, "set_bss", "failed to get bss : %d\n", err)) + goto cleanup; + + if (CHECK(id != bss.pid_tgid, "Compare user pid/tgid vs. bpf pid/tgid", + "User pid/tgid %llu EBPF pid/tgid %llu\n", id, bss.pid_tgid)) + goto cleanup; +cleanup: + + if (!IS_ERR_OR_NULL(link)) { + bpf_link__destroy(link); + link = NULL; + } + bpf_object__close(obj); +} diff --git a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c new file mode 100644 index 000000000000..cdb77eb1a4fb --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Carlos Neira cneirabustos@gmail.com */ + +#include +#include +#include "bpf_helpers.h" + +static volatile struct { + __u64 dev; + __u64 ino; + __u64 pid_tgid; + __u64 user_pid_tgid; +} res; + +SEC("raw_tracepoint/sys_enter") +int trace(void *ctx) +{ + __u64 ns_pid_tgid, expected_pid; + struct bpf_pidns_info nsdata; + __u32 key = 0; + + if (bpf_get_ns_current_pid_tgid(res.dev, res.ino, &nsdata, + sizeof(struct bpf_pidns_info))) + return 0; + + ns_pid_tgid = (__u64)nsdata.tgid << 32 | nsdata.pid; + expected_pid = res.user_pid_tgid; + + if (expected_pid != ns_pid_tgid) + return 0; + + res.pid_tgid = ns_pid_tgid; + + return 0; +} + +char _license[] SEC("license") = "GPL"; From patchwork Tue Oct 22 19:17:51 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: 1181625 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 (no SPF record) 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="a7ljsWsC"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46yNZd1YKkz9sP6 for ; Wed, 23 Oct 2019 06:18:17 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732894AbfJVTSP (ORCPT ); Tue, 22 Oct 2019 15:18:15 -0400 Received: from mail-qk1-f180.google.com ([209.85.222.180]:37451 "EHLO mail-qk1-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732810AbfJVTSP (ORCPT ); Tue, 22 Oct 2019 15:18:15 -0400 Received: by mail-qk1-f180.google.com with SMTP id u184so17390015qkd.4; Tue, 22 Oct 2019 12:18:14 -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=dWKTTugTGOTAxmBT2CipEcIGsfmcg1KJPSrEI8Zo/tg=; b=a7ljsWsCc/4Bghe/SCSDdYexeZZP33WgyGqgJzUtJ4mhoXtnyVld7wXJZ5hSFrPXU3 6/EHSJMJc7xseT/MOnFvW42zIuBlVQApB1LMI9WX/ITVxqx48c75JRTCtslzmiCITLXl e/v8bCrsx2B/2MybdoPvaoeCq1oXmOj5AUCpSX5nt1gUOmy49D4j2g1ozjtmq3t9aKL4 2kzi7ZqBtAdlAJXcS92Dd/cqyAsT36cl/we6V+c5N3wpBZTJzUWhTj/mhWtJlmx7MtCO fAixR1REoGf/X/eH3uz+PW6cLLvZRK0jXeRu1hLYDYj7YxSLtcGPFXUSWdOBewXgIfMO //iQ== 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=dWKTTugTGOTAxmBT2CipEcIGsfmcg1KJPSrEI8Zo/tg=; b=JEVmMjCTbaHFxrv4Coz+ivfL3n90II46FYcawO1K74SaLcRxWBG8L61jo5mhcJeIT/ 6TNRBavA5M5bJeB5D6agLCeF8eE9cWSUNQ+RO46dIYIBDj187ClC5QDhNx0f5BbBmP9f q2Cd9ZPoX4anhX01fTnzuyXXU5QU+bE2Hb0V2KCI0KQgphsPXiKzv3R0noIMhMMsRGb8 gwtn1ipwLrxY+hmvY/3gjk5cprGX9nzWC4tZtgvK02GhYpO0lzYmDlKaK1womwuwE+9O pt002SSQyCCLP97EhaSDQt3SOQ3zb2DPyKc4IY6eDWgnadpQooWTq6WrtcrttKJ2zU6m TUzg== X-Gm-Message-State: APjAAAX8k29w86icvRkynVDKkRppzRgBqcMQPwIj9Ddg/O0eGVYVNYEr 67OE4kAmfeSAkZo4Ubkzmfy6LhszufdvVB7E X-Google-Smtp-Source: APXvYqzXFVeITHx3x74ps8yYB7QAvlyWCveljANotGc2wyAdd9eVCog83fmqZoAOK1JvN/BbXNUDVg== X-Received: by 2002:a37:4b4f:: with SMTP id y76mr4491493qka.488.1571771893770; Tue, 22 Oct 2019 12:18:13 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id r36sm8015969qta.27.2019.10.22.12.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2019 12:18:13 -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 v15 5/5] bpf_helpers_doc.py: Add struct bpf_pidns_info to known types Date: Tue, 22 Oct 2019 16:17:51 -0300 Message-Id: <20191022191751.3780-6-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191022191751.3780-1-cneirabustos@gmail.com> References: <20191022191751.3780-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 Add struct bpf_pidns_info to known types Signed-off-by: Carlos Neira Acked-by: Yonghong Song --- scripts/bpf_helpers_doc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/bpf_helpers_doc.py b/scripts/bpf_helpers_doc.py index 7548569e8076..021cc387d414 100755 --- a/scripts/bpf_helpers_doc.py +++ b/scripts/bpf_helpers_doc.py @@ -437,6 +437,7 @@ class PrinterHelpers(Printer): 'struct bpf_fib_lookup', 'struct bpf_perf_event_data', 'struct bpf_perf_event_value', + 'struct bpf_pidns_info', 'struct bpf_sock', 'struct bpf_sock_addr', 'struct bpf_sock_ops',