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 48121C43381 for ; Wed, 20 Mar 2019 05:51:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ED1DD2184E for ; Wed, 20 Mar 2019 05:51:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EcWI0QVE" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726529AbfCTFvF (ORCPT ); Wed, 20 Mar 2019 01:51:05 -0400 Received: from mail-qt1-f174.google.com ([209.85.160.174]:42352 "EHLO mail-qt1-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725988AbfCTFvF (ORCPT ); Wed, 20 Mar 2019 01:51:05 -0400 Received: by mail-qt1-f174.google.com with SMTP id u7so1166584qtg.9 for ; Tue, 19 Mar 2019 22:51:04 -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=NW8hKveiZKsGpxVU4sqiiFCZZxiUjTL9K5O8QZdrh68=; b=EcWI0QVE6YZt3nkQBKsuIsoQ4WSAQTX1bRTkncrep/xwAkeQR1RlFEVGT28OX/bwZX ETTfAEWd3oVhy/9V+JiEQ2TcxH+3cX5gXy8x9g897HatNfuEP9ZXua1NeDCOfPSQIlVX e2R30MOTna3IwSpa/oeDJg23n1AHWDQwdFga3mRZzcMwHNMTrKjcOEl1Szx96gG63EJ6 Eh3zuj0OdeTdLpz+Id/wq7Qagg6xPXUrTAf3uMKyB2lYO9OjF/lKj4WlwqNVcMgNYCKR EQFYTao1s8mZ0Vj3MFytYRP6GTyASoN/UrhoeJL70jyB54Eako0JHDthi8G3PjrdJ6BY G3CQ== 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=NW8hKveiZKsGpxVU4sqiiFCZZxiUjTL9K5O8QZdrh68=; b=WkqrHWQateQsKIXb51nmoX88gfE8jm0i+cAYZ3O0lUyYcPoQcdNH8YkiT51xD3gqwH zu40AJhBARtvqA+d4zJwv4Rp3ZMnLONJ3Gi3ztxph98wJPp7Uj3I4tvGwu6dt0dmyVJU iIFVDXVRcCdLoq7fQKZAhrlk+q95f0e/tQ2NaQ0OzukGuhMceDbotQa0tQukx2/C/BaI 6RHf8boQxydOZKfejGo9zrgKLXfaiYHaf9l/JCd+Re13yH2lOWA4ns0TMYOff28VmVbX g5XHNjPqEfpVSXVoPFmBSJwMl+mA6WiXO7U0mAKN8Z7d0sP1wPMp1sjfSTI6rL3BTK9f BUNg== X-Gm-Message-State: APjAAAWyy+DSh/agz5wXzc8flOvy616RNqyzFXQIzKrVBeA/n15/X89O 6FV+xqRf4RJZLeVuL7SVP4PDNwnLYHU= X-Google-Smtp-Source: APXvYqxjPa6OH/7XY9eGtWMpgKlu4pOzS5tXnFGgX+zwunOdMlCIRql7CR35be/1njmki3usTVqIUQ== X-Received: by 2002:a0c:b597:: with SMTP id g23mr5027786qve.142.1553061063846; Tue, 19 Mar 2019 22:51:03 -0700 (PDT) Received: from dev00 ([190.162.109.53]) by smtp.gmail.com with ESMTPSA id e15sm543790qkl.80.2019.03.19.22.51.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Mar 2019 22:51:02 -0700 (PDT) Date: Wed, 20 Mar 2019 02:50:58 -0300 From: Carlos Neira 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: <20190320055057.n7bly4gs3kqhr7n5@dev00> References: <20190320053313.amhaibzsx5d2r65w@dev00> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="rrz4lzyizrtr6wuk" Content-Disposition: inline In-Reply-To: <20190320053313.amhaibzsx5d2r65w@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 --rrz4lzyizrtr6wuk Content-Type: text/plain; charset=us-ascii Content-Disposition: inline --rrz4lzyizrtr6wuk Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-PATCH-bpf-next-2-3-BPF-New-helper-to-obtain-namespac.patch" >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 --rrz4lzyizrtr6wuk--