From patchwork Tue Oct 1 21:41:38 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: 1170268 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="urHtHhIj"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46jXm744xxz9sPL for ; Wed, 2 Oct 2019 07:41:59 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727647AbfJAVl7 (ORCPT ); Tue, 1 Oct 2019 17:41:59 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:34742 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726326AbfJAVl6 (ORCPT ); Tue, 1 Oct 2019 17:41:58 -0400 Received: by mail-qt1-f195.google.com with SMTP id 3so23664845qta.1; Tue, 01 Oct 2019 14:41:58 -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=urHtHhIjIp0PzGCletNHTVGjx7Ujq1Vv7ZOiT7bH9fdnhQ6LBSutYuiv1687AFDEXY 9+IUjalGYdO1/keIVDKMdfjtj7qBdVcVCROeK8qUFfVWqlqi8E8S1XeY6Mwx5OHU7P35 Q7VxnKrjqCRg6d3zk5c8TUR/80vmbi/KE2Bz/dWQFk3SAaj4wZAMFQN0+eV8WIgWF7qf l0wCnuiLfuxREKXAK60Ho7gZYmyzqYugxwg+FU2mEJJd8rdxCRVSNCxpowIIC/1EfDKz jqn1mdgcJMMEujPuZ/J1ZezaMSAxGM3sIuWr97Nk620fLsDA4NzqOKdYJAiU5IGxT7rR wj1w== 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=UpiUl780PwNj0gc06684kS56O6FAGs5PRuP3TZN9gxP8y5sC7tGkBehgQo9JLdAkfa aiIZpvq4eylvWoQLJO5awW6gNwDDBh5zTRz2Ml0YAXcWfVCOXGriAfLKTaFzx9LckVYo 2pfAPPFqlbBCVBMjgIAkioGPE+0fWOZRSywI0y+GLdENx0+DDARhaKrA9Qoe7eTPUKbk iaSBMjDUAwYIF5lqdYToUHKhCvbSmF5ouSTM6mbGQASq8pZnfzQ/VOeS2rPCvXNGimZ8 vAsmxWMb/Nu+s1940Ip7mw13MVCLme/V+DzzBm5iy/Q1RYHrQGouNz4EWh1WCuX56fR1 9h3Q== X-Gm-Message-State: APjAAAVwNY/NucjidSKpkYFefEHp+0NN1S1xsxoQkW0VvSrxR215pGSX FM0SOfxxXyM7mWjkEovyxy7l+84Y7vo= X-Google-Smtp-Source: APXvYqyuCy80892fZx5otSPJdpWHyqSUBx34mODQB0K/TjlzBTPLP9f8uG+H84x+bzYkMUqQQm+bOg== X-Received: by 2002:ac8:1417:: with SMTP id k23mr502160qtj.93.1569966117271; Tue, 01 Oct 2019 14:41:57 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id v13sm8559352qtp.61.2019.10.01.14.41.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 14:41:56 -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 V12 1/4] fs/nsfs.c: added ns_match Date: Tue, 1 Oct 2019 18:41:38 -0300 Message-Id: <20191001214141.6294-2-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191001214141.6294-1-cneirabustos@gmail.com> References: <20191001214141.6294-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 Oct 1 21:41:39 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: 1170270 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="h80AYzek"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46jXmB0Tk9z9sPL for ; Wed, 2 Oct 2019 07:42:02 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727762AbfJAVmB (ORCPT ); Tue, 1 Oct 2019 17:42:01 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:37952 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726326AbfJAVmA (ORCPT ); Tue, 1 Oct 2019 17:42:00 -0400 Received: by mail-qk1-f194.google.com with SMTP id u186so12836523qkc.5; Tue, 01 Oct 2019 14:42:00 -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=mJEbpnnBACnjKqVJ7ryTqoKmyEa7W0hY6uGaayjFIYA=; b=h80AYzek/0mtKGTG/s6iFgvbhXKJGTvX6PBJ70/+iPeybULGYsNjwDQJM1/sKgG52L 0jh70gaSzstDlldMZCwpZHZDk6pYWLjM9ksE4+xRcNBvO7BtC1OiHplVTErVZ/Bpa/Tv is6mkB5u/RUm9L43cx+DnLF3wAr/7j370Nv5otRPoq5B9VkhCm8eZyrzQylG/5hj0l4E xfwKjXwklg/jWgf3sDGKE5toRQ4fagK7uDBuQfCnrlcJwjuZY4Hg45WLAoCksTpf56RS ul2t4WMwqLfutbcLf5WK3r/TKSRXfR7RiPflLzfRzMtnc7EMKRY+mzjfgOHJbyNFH9dK RJVQ== 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=mJEbpnnBACnjKqVJ7ryTqoKmyEa7W0hY6uGaayjFIYA=; b=WPWy9GcIPXiTN8XN9Am9tgFp483TsRxZrIVRdDLDDNDkBR0hvOFVf6jQnYyri7Hq63 x7qIQZD+09l2Iu2/VsXBBi1geGHhfT55hT93cWYsIt9nDsPnPINm/rcPHNJZFiAHGjZ0 4tQ6xspqyR52eGK4gMpgwg7izyjBrfPmdUrnMkH235Wy59o0WTy6az4rlVJNnHFROuo6 j4hlwiroeSqSSVU4Qf4v2imrQj0raGhIAlApfvfXlGf+Cb/vS2rVXbMqbTlqJ2hBC3Cp NiCnEaLPnmDN/ph49eUh4YbJ8cQM5mcAI9lAPTQZmbcJfTZsgBZXSLO0Xdmyx6d3vABO XTDQ== X-Gm-Message-State: APjAAAWbmI9OCnqiXhECufcBfFpuBL0eZ6bx9/D90WB0TqRdUbidVGXD iDtlMhKKKHHRkgl4+3PEXULBp4Omr80= X-Google-Smtp-Source: APXvYqyvtKcku2+wJzxA5Qewwa8T6dTbMdM6Ab3lnVp5UHSGw3lN2BYrMSdOd9jse2XtuN97Hk26CA== X-Received: by 2002:a05:620a:147:: with SMTP id e7mr285180qkn.227.1569966119391; Tue, 01 Oct 2019 14:41:59 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id v13sm8559352qtp.61.2019.10.01.14.41.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 14:41:58 -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 V12 2/4] bpf: added new helper bpf_get_ns_current_pid_tgid Date: Tue, 1 Oct 2019 18:41:39 -0300 Message-Id: <20191001214141.6294-3-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191001214141.6294-1-cneirabustos@gmail.com> References: <20191001214141.6294-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 | 36 ++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 57 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..ea8145d7f897 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 + * + * u64 bpf_get_ns_current_pid_tgid(u64 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, or if dev conversion to dev_t lost high bits. + * + * **-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..8777181d1717 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,37 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, }; #endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u64, dev, u64, inum) +{ + struct task_struct *task = current; + struct pid_namespace *pidns; + pid_t pid, tgid; + + if ((u64)(dev_t)dev != dev) + return -EINVAL; + + 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 44bd08f2443b..32331a1dcb6d 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 1 21:41:40 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: 1170272 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="uYJwGbJy"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46jXmD22crz9sPL for ; Wed, 2 Oct 2019 07:42:04 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727892AbfJAVmD (ORCPT ); Tue, 1 Oct 2019 17:42:03 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:33964 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726326AbfJAVmD (ORCPT ); Tue, 1 Oct 2019 17:42:03 -0400 Received: by mail-qk1-f194.google.com with SMTP id q203so12870297qke.1; Tue, 01 Oct 2019 14:42:02 -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=4q/41TxGZ/5B1Wg6FL4I+DCjlS5tHb+h5GEKZ9pO0qY=; b=uYJwGbJyBk00Ek3CiG6N4o0LsaJNJL1jT+v/MIdFPH8EeiLNXowreeFxZG5aoFs/y5 1het9RrdCB7m6pThHrDFQwaQANkinQ5X/bpqf5sUUPkEpDX5J67uI6cnkXa+TkMNeZK3 eLgtCFJldOlKgRqISjC4vgKSkj0e27JGtXmLxEjFWHSf6imEUG817YRF9AycnecFU0T2 cjdDEaUMb+vho4sp89ERRI7HBmIjxMTL7AyTAFIZvUJG1yqqJxz36W5pkCqBVskDVBnm Ypyt1wSyT1P2TkXZsvvQvt4Ib/QhTTtoRQ2IcqBh1qVUJvcJeeFCEl6hCSITUtXJD3yI ffKg== 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=4q/41TxGZ/5B1Wg6FL4I+DCjlS5tHb+h5GEKZ9pO0qY=; b=P+TDAN1EfMzdcmDyLOqzPAf5LcLbbrw4QKQKOYSfx7JiGFgEhGPCwlSV1S5CRHNkk0 NC3zWWsA1mXnxbPn0B9Hu7Ph/Z9j8T4wyJvwV9iZOoQTnGLDk7aqYlEPrSTRRhqJVoys 0vCZxXhvqt3QxvHJ7YFTOpIGYMlJNi3uLdkTM64vj/HCTBGK4EEinT/+PFrPlNF9p9xO EflHNcVvoOull52EWrUP3M/kHik6RPkwyJQSXbwlel99DD/1WbZLhIV0YNKL46TA3x74 +7wcaYde3u9YKU3Cgod+wMvfE8VV7ecH89PpsMju7XZM+RHkfCQATFNm7hGgGyQFSnn8 WWJQ== X-Gm-Message-State: APjAAAXJ9EToz4PiHZFNheRMMls8+fUAGFHJuVbvs4Jf/VssIaNv6M/E txaHSFM2TJ8Gx+7RgmRWj059jRPAh58= X-Google-Smtp-Source: APXvYqx0k1vKzqiG6jAH8O4ZK+Tqj1TE9v7CNwZsOntvAOK3hM9fZeWMqeR8lzdxOB3xeWhUIq6ELw== X-Received: by 2002:a37:62d8:: with SMTP id w207mr265870qkb.261.1569966121483; Tue, 01 Oct 2019 14:42:01 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id v13sm8559352qtp.61.2019.10.01.14.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 14:42:00 -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 V12 3/4] tools: Added bpf_get_ns_current_pid_tgid helper Date: Tue, 1 Oct 2019 18:41:40 -0300 Message-Id: <20191001214141.6294-4-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191001214141.6294-1-cneirabustos@gmail.com> References: <20191001214141.6294-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 sync tools/include/uapi/linux/bpf.h to include new helper. 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..ea8145d7f897 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 + * + * u64 bpf_get_ns_current_pid_tgid(u64 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, or if dev conversion to dev_t lost high bits. + * + * **-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 Oct 1 21:41:41 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: 1170274 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="PMp/14Gd"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46jXmH3V8Hz9sPL for ; Wed, 2 Oct 2019 07:42:07 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727942AbfJAVmG (ORCPT ); Tue, 1 Oct 2019 17:42:06 -0400 Received: from mail-qt1-f180.google.com ([209.85.160.180]:39549 "EHLO mail-qt1-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726326AbfJAVmF (ORCPT ); Tue, 1 Oct 2019 17:42:05 -0400 Received: by mail-qt1-f180.google.com with SMTP id n7so23637267qtb.6; Tue, 01 Oct 2019 14:42:04 -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=6ZCudQkJo34iD4d2cux5rc3LwCd+AdYRLKzvHuPbh88=; b=PMp/14GdI76R8wdVS8L+RqMs+q6+zKyNbffs41+D9hS/+2H1naDud5jaRCkD/FLeJj jDepTviTr7F7Pc0xyUjf0NQJQL81wtZ7YMtXcL8dofMaP2hk43XPj6xiVShLmANYHz7D XR+SBKNLqwG8iaPAn7Hc/AQ9J8C9rxIXlireHfPhuCZdOSBmjLXIm/ObUuJLCrvlOAzA 8/zf4remYzeogpGAzA25yCfOt6Qmktsym4rLgM/2iN7dTOxLMfXCHcdLgzSQllxZ0Lxt HwrexLW4boy+ap9ppqYIAJjQiE+MD00gJ1lvSFynmTGMIdaErnIoMDC2bNcb8x3Egtfu U8CA== 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=6ZCudQkJo34iD4d2cux5rc3LwCd+AdYRLKzvHuPbh88=; b=UKc2GtZ1ZxxiNpEMtYbOP0CTly4vnnhcNHF6qilnJPpX2/4J1Hn1TZwShJULheZ+hc Kf9khnSCn/RUIUAU1EVMq/Kpy9kJ1wnNiKI/UER0EDfMHAfvGg7Ryk/ipm9J1Y4rsocf ULsQolcBpyQygeABJ9U0gBqKV6O/iGKHUIhFE6HcsC+gNbHQSYtDpsUg7DKq4VPqIqax v1Mb7k3XRPlqGmtKcp/EoNH15GrMhc9TkZXDJGxlFCFub/6BOfK65KDehZX5HMjgxBxp Rv2Lki0iR91UvQXgDI+bHvIFP3w7qwNCltyxNjUrdIbFKXlaV8fSe0QOohwE4oF9JvOy ZTwA== X-Gm-Message-State: APjAAAWO3wXXAu/F6rEdgATaxfLybghBpAGOaPBg/pnJ26iOGvh7ODCe A+NxywQNfxq0FRJ+tNxQsKsbQkgWqO8= X-Google-Smtp-Source: APXvYqzmS6d54RBI9VhzsXGZFkQtGZpOJbYcwCjgko10gVDEgd5ZotpRe7i8wponao2Hf4Mm38i76g== X-Received: by 2002:ac8:2c8f:: with SMTP id 15mr538201qtw.3.1569966123665; Tue, 01 Oct 2019 14:42:03 -0700 (PDT) Received: from ebpf00.byteswizards.com ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id v13sm8559352qtp.61.2019.10.01.14.42.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 14:42:03 -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 V12 4/4] tools/testing/selftests/bpf: Add self-tests for new helper. Date: Tue, 1 Oct 2019 18:41:41 -0300 Message-Id: <20191001214141.6294-5-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191001214141.6294-1-cneirabustos@gmail.com> References: <20191001214141.6294-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 Self tests added for new helper Signed-off-by: Carlos Neira --- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../bpf/prog_tests/get_ns_current_pid_tgid.c | 85 +++++++++++++++++++ .../bpf/progs/get_ns_current_pid_tgid_kern.c | 47 ++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_ns_current_pid_tgid.c create mode 100644 tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index 54a50699bbfd..19c03ba066b1 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -233,6 +233,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 unsigned long long (*bpf_get_ns_current_pid_tgid)(__u64 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/prog_tests/get_ns_current_pid_tgid.c b/tools/testing/selftests/bpf/prog_tests/get_ns_current_pid_tgid.c new file mode 100644 index 000000000000..4d8ec524d373 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/get_ns_current_pid_tgid.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Carlos Neira cneirabustos@gmail.com */ +#include +#include +#include +#include +#include +#include + +void test_get_ns_current_pid_tgid(void) +{ + const char *probe_name = "syscalls/sys_enter_nanosleep"; + const char *file = "get_ns_current_pid_tgid_kern.o"; + int ns_data_map_fd, duration = 0; + struct perf_event_attr attr = {}; + int err, efd, prog_fd, pmu_fd; + __u64 ino, dev, id, nspid; + struct bpf_object *obj; + struct stat st; + __u32 key = 0; + char buf[256]; + + err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); + if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) + return; + + ns_data_map_fd = bpf_find_map(__func__, obj, "ns_data_map"); + if (CHECK_FAIL(ns_data_map_fd < 0)) + goto close_prog; + + pid_t tid = syscall(SYS_gettid); + pid_t pid = getpid(); + + id = (__u64) tid << 32 | pid; + bpf_map_update_elem(ns_data_map_fd, &key, &id, 0); + + if (stat("/proc/self/ns/pid", &st)) + goto close_prog; + + dev = st.st_dev; + ino = st.st_ino; + key = 1; + bpf_map_update_elem(ns_data_map_fd, &key, &dev, 0); + key = 2; + bpf_map_update_elem(ns_data_map_fd, &key, &ino, 0); + + snprintf(buf, sizeof(buf), + "/sys/kernel/debug/tracing/events/%s/id", probe_name); + efd = open(buf, O_RDONLY, 0); + read(efd, buf, sizeof(buf)); + close(efd); + 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_FAIL(pmu_fd < 0)) + goto cleanup; + + err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); + if (CHECK_FAIL(err)) + goto cleanup; + + err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); + if (CHECK_FAIL(err)) + goto cleanup; + + /* trigger some syscalls */ + sleep(1); + key = 3; + err = bpf_map_lookup_elem(ns_data_map_fd, &key, &nspid); + if (CHECK_FAIL(err)) + goto cleanup; + + if (CHECK(id != nspid, "Compare user pid/tgid %llu vs. bpf pid/tgid %llu", + "Userspace pid/tgid %llu EBPF pid/tgid %llu\n", id, nspid)) + goto cleanup; + +cleanup: + close(pmu_fd); +close_prog: + bpf_object__close(obj); +} diff --git a/tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c b/tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c new file mode 100644 index 000000000000..00a325e85e8c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Carlos Neira cneirabustos@gmail.com */ + +#include +#include "bpf_helpers.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 4); + __type(key, __u32); + __type(value, __u64); +} ns_data_map SEC(".maps"); + + +SEC("tracepoint/syscalls/sys_enter_nanosleep") +int trace(void *ctx) +{ + __u64 *val, *inum, *dev, nspid, *expected_pid; + __u32 key = 1; + + dev = bpf_map_lookup_elem(&ns_data_map, &key); + if (!dev) + return 0; + key = 2; + inum = bpf_map_lookup_elem(&ns_data_map, &key); + + if (!inum) + return 0; + + nspid = bpf_get_ns_current_pid_tgid(*dev, *inum); + key = 0; + expected_pid = bpf_map_lookup_elem(&ns_data_map, &key); + + if (!expected_pid || *expected_pid != nspid) + return 0; + + key = 3; + val = bpf_map_lookup_elem(&ns_data_map, &key); + + if (val) + *val = nspid; + + return 0; +} + +char _license[] SEC("license") = "GPL"; +__u32 _version SEC("version") = 1;