From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_NEOMUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03A07C43381 for ; Wed, 20 Mar 2019 16:52:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C5F9021841 for ; Wed, 20 Mar 2019 16:52:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PqsFcFE3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726828AbfCTQwf (ORCPT ); Wed, 20 Mar 2019 12:52:35 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:33671 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726123AbfCTQwf (ORCPT ); Wed, 20 Mar 2019 12:52:35 -0400 Received: by mail-qk1-f195.google.com with SMTP id k189so9294229qkc.0 for ; Wed, 20 Mar 2019 09:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=YzKAOz/o6gaOW924YzEXRJsnU94DizFRfaSLctphGTk=; b=PqsFcFE3iEmsKjpF4KKUzQPCUtBCeaUjimhWVE4BNF7OmPsOVqc0pWpkG7PckNXdy0 dy+76YHjkyJcR1JTTzAUFnkjA30nIEV86gLkl6p2oVDsdMpT2svVZaZq2klwDY3KlxyK cVTk8Fp7wnsKXHlNcMrPYQ9Ve+MdoSkZX9UERydEjeM1cnxYgkDHHcVjsbOrNaqHzC1z tBVVj7k+arJcxmfYL/HM+NcOSe1vFs9ZHQrQpRduTNxXy00iE9jqvhUqLFSF+njPOdbg cX3rluAOphxTbTRkbyP0IYisEfdDinUdBg2aIEZMnQFy9hHt8x3WaIS2+ggMlO9uKVPt e6ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=YzKAOz/o6gaOW924YzEXRJsnU94DizFRfaSLctphGTk=; b=ZapgHStrSu6mZx9gujWaOp3IuRVwsy54HUaW3Bk/WoLpKAKiPk7DYySiwAqW5Z88Ls STfR1BUtD02dPFgHvkbixdJtEwz0QdQGmbguNdZxYrouM8C9tMdbCz5+631PYm5VK2iy n0LJZUqmDNASLp789BtTw5qEdK2ET4b9B2EsOhvQuKQ4ddunOWA/tortN77K2UIGnoTz Tcf9H/2v3Fr16cpH0SzaRlAFTQxfdneffLdnT9zASgleh5gJogeOY5worelbOtvtVJ+B xyarW9kafPkcn+n4eq9JrnKDOLekXRkR+4LLmR/Fkgzt3JRU9S5p+PW40SDTfk7ljFFl EauQ== X-Gm-Message-State: APjAAAWkBKkt+sec1f8jyPAiGA+7de6X41zwI8mC8cJm3ZLCKEfwpllC pM3od5+sOUwmVJjsKEtdnUeb0JBg/Tk= X-Google-Smtp-Source: APXvYqw98UkcqA9APxcAJ6u9E64e//VQDBWg8sp6fdvpsTuVn3gzS/WVZ+hSmflF4hPKDyMp2A5emg== X-Received: by 2002:a37:5c05:: with SMTP id q5mr21541107qkb.20.1553100753947; Wed, 20 Mar 2019 09:52:33 -0700 (PDT) Received: from dev00 ([190.162.109.53]) by smtp.gmail.com with ESMTPSA id 17sm1718786qtt.18.2019.03.20.09.52.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 20 Mar 2019 09:52:33 -0700 (PDT) Date: Wed, 20 Mar 2019 13:52:28 -0300 From: Carlos Antonio Neira Bustos To: netdev@vger.kernel.org Cc: ebiederm@xmission.com, quentin.monnet@netronome.com, ys114321@gmail.com, brouer@redhat.com, cneirabustos@gmail.com Subject: Re: [PATCH v3 bpf-next 2/3] BPF: helpers: New helper to obtain namespace data from current task Message-ID: <20190320165227.krvz6mn3ymcqddqn@dev00> References: <20190320164920.ha5ne7nlnu7fbx5i@dev00> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190320164920.ha5ne7nlnu7fbx5i@dev00> User-Agent: NeoMutt/20170113 (1.7.2) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org >From b7f5af5c1513b6e89fc5b68ddb1ad65e4fbede02 Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 18 Mar 2019 13:09:35 -0300 Subject: [PATCH] [PATCH bpf-next 2/3] BPF: New helper to obtain namespace data from current task This helper obtains the active namespace from current and returns pid, tgid, device and namespace id as seen from that namespace, allowing to instrument a process inside a container. Device is read from /proc/self/ns/pid, as in the future it's possible that different pid_ns files may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 linux plumbers conference. Currently bpf_get_current_pid_tgid(), is used to do pid filtering in bcc's scripts but this helper returns the pid as seen by the root namespace which is fine when a bcc script is not executed inside a container. When the process of interest is inside a container, pid filtering will not work if bpf_get_current_pid_tgid() is used. This helper addresses this limitation returning the pid as it's seen by the current namespace where the script is executing. This helper has the same use cases as bpf_get_current_pid_tgid() as it can be used to do pid filtering even inside a container. For example a bcc script using bpf_get_current_pid_tgid() (tools/funccount.py): u32 pid = bpf_get_current_pid_tgid() >> 32; if (pid != ) return 0; Could be modified to use bpf_get_current_pidns_info() as follows: struct bpf_pidns pidns; bpf_get_current_pidns_info(&pidns, sizeof(struct bpf_pidns)); u32 pid = pidns.tgid; u32 nsid = pidns.nsid; if ((pid != ) && (nsid != )) return 0; To find out the name PID namespace id of a process, you could use this command: $ ps -h -o pidns -p Or this other command: $ ls -Li /proc//ns/pid Signed-off-by: Carlos Antonio Neira Bustos --- tools/include/uapi/linux/bpf.h | 25 ++++++++++++++++++++++++- tools/testing/selftests/bpf/bpf_helpers.h | 3 +++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 3c38ac9a92a7..5c7e517a59a6 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2366,6 +2366,18 @@ union bpf_attr { * current value is ect (ECN capable). Works with IPv6 and IPv4. * Return * 1 if set, 0 if not set. + * + * int bpf_get_current_pidns(struct bpf_pidns_info *pidns, u32 size_of_pidns) + * Description + * Copies into *pidns* pid, namespace id and tgid as seen by the + * current namespace and also device from /proc/self/ns/pid. + * *size_of_pidns* must be the size of *pidns* + * + * This helper is used when pid filtering is needed inside a + * container as bpf_get_current_tgid() helper returns always the + * pid id as seen by the root namespace. + * Return + * 0 on success -EINVAL on error. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2465,7 +2477,8 @@ union bpf_attr { FN(spin_unlock), \ FN(sk_fullsock), \ FN(tcp_sock), \ - FN(skb_ecn_set_ce), + FN(skb_ecn_set_ce), \ + FN(get_current_pidns_info), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3152,4 +3165,14 @@ struct bpf_line_info { struct bpf_spin_lock { __u32 val; }; + +/* helper bpf_get_current_pidns_info will store the following + * data, dev will contain major/minor from /proc/self/pid. + */ +struct bpf_pidns_info { + __u32 dev; + __u32 nsid; + __u32 tgid; + __u32 pid; +}; #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index c9433a496d54..8cd630d84c07 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -182,6 +182,9 @@ static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) = (void *) BPF_FUNC_tcp_sock; static int (*bpf_skb_ecn_set_ce)(void *ctx) = (void *) BPF_FUNC_skb_ecn_set_ce; +static int (*bpf_get_current_pidns_info)(struct bpf_pidns_info *buf, + unsigned int buf_size) = + (void *) BPF_FUNC_get_current_pidns_info; /* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions -- 2.11.0