From patchwork Fri Sep 6 15:09: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: 1159066 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="i5HXTJX/"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46Q1Fq5sccz9s00 for ; Sat, 7 Sep 2019 01:10:23 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725945AbfIFPKX (ORCPT ); Fri, 6 Sep 2019 11:10:23 -0400 Received: from mail-ua1-f65.google.com ([209.85.222.65]:42946 "EHLO mail-ua1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726940AbfIFPKX (ORCPT ); Fri, 6 Sep 2019 11:10:23 -0400 Received: by mail-ua1-f65.google.com with SMTP id w16so2147980uap.9; Fri, 06 Sep 2019 08:10:22 -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; bh=S3FbC4FTkQ+kbO4J/kQIM6G9ud3dS3/G95LWwoCOKoU=; b=i5HXTJX/4U4H8q53pLIw63ONb+Mnq9AAn8LwSebP5dq6/M3FQgbn8tB4lAX1Y4k24a rcz8mMcjk5VnKew7wwaA4mPTm3oVMvo3Cy6LitJWyB/YkGsEQNfug2DJFsjJr1QYo+St dTZjy3LE6xw8BBGCMX2PNgC5CsfT1mYq5uL9CwQi3DI1/IL80AnqSdG5NDS43Ssdscud 3wg0SxSGYZnAq1PLf7KRbeLrDcAcsjwlNONE45bRMphuQfsFzzop4jwRICU8jysVNKlq Uckik3e0Q9gRmWW7b6fvx2i3HP8ELPem796CEhG5SicJBoCLi+vt3QEJo818ArEOGXr2 NQtQ== 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; bh=S3FbC4FTkQ+kbO4J/kQIM6G9ud3dS3/G95LWwoCOKoU=; b=TD7LHlA1BzyAf2mqtzC0ZTOXWKB4va+5aNPoP0Oi2+Nfz/kgXdG84DNkvnu4fIssG6 4Gkq+h1qH8qa1Mu7spF4k1zbQwbZakKirGmnAot9rcVF0KBqW6GS1p7yQ9yXRJtc5ye/ kaNcw4rR2LwWZ71imSF7v552XQXFNJ216dM6aCOb6jy8cduVYJubvvgUd2ES6onY7VjM xiZc8zPveAmTSm95+6IhXmFBCReObN3StpkVRwaR1D0M8IhMGLHG/cQ6djarRAHPMO7U rCx2/GSK9CvwSANl9p32Nn9xnM1aNBaSOEhiKa5HahMKVxzXv9lDs8LK2QiisLhm9/AY SZyw== X-Gm-Message-State: APjAAAUxzw3kuiq3604DKbhCHhMmwXE6rdz+AHoDc5b+ZVTKXxkHSFD1 FnIWVeIIbT29U9Lf9S0GMXKGmAYdpEY= X-Google-Smtp-Source: APXvYqzNvM+e0QOpH0Ztt5wx5kzGK/0ufcKqgzQr0rGJkRFqWC+WqK4j7YRBafzIlLmn7VK9RoFz0w== X-Received: by 2002:ab0:649a:: with SMTP id p26mr4301691uam.11.1567782621473; Fri, 06 Sep 2019 08:10:21 -0700 (PDT) Received: from localhost.localdomain ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id o15sm4833822vkc.38.2019.09.06.08.10.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Sep 2019 08:10:20 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, cneirabustos@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next v10 1/4] fs/namei.c: make available filename_lookup() for bpf helpers. Date: Fri, 6 Sep 2019 11:09:49 -0400 Message-Id: <20190906150952.23066-2-cneirabustos@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190906150952.23066-1-cneirabustos@gmail.com> References: <20190906150952.23066-1-cneirabustos@gmail.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Signed-off-by: Carlos Neira --- fs/internal.h | 2 -- fs/namei.c | 1 - include/linux/namei.h | 4 ++++ 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/internal.h b/fs/internal.h index 315fcd8d237c..6647e15dd419 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -59,8 +59,6 @@ extern int finish_clean_context(struct fs_context *fc); /* * namei.c */ -extern int filename_lookup(int dfd, struct filename *name, unsigned flags, - struct path *path, struct path *root); extern int user_path_mountpoint_at(int, const char __user *, unsigned int, struct path *); extern int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *, unsigned int, struct path *); diff --git a/fs/namei.c b/fs/namei.c index 209c51a5226c..a89fc72a4a10 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include diff --git a/include/linux/namei.h b/include/linux/namei.h index 9138b4471dbf..b45c8b6f7cb4 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -6,6 +6,7 @@ #include #include #include +#include enum { MAX_NESTED_LINKS = 8 }; @@ -97,6 +98,9 @@ extern void unlock_rename(struct dentry *, struct dentry *); extern void nd_jump_link(struct path *path); +extern int filename_lookup(int dfd, struct filename *name, unsigned flags, + struct path *path, struct path *root); + static inline void nd_terminate_link(void *name, size_t len, size_t maxlen) { ((char *) name)[min(len, maxlen)] = '\0'; From patchwork Fri Sep 6 15:09: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: 1159068 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="oXrqX6di"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46Q1Ft1Gwtz9s00 for ; Sat, 7 Sep 2019 01:10:26 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729134AbfIFPKZ (ORCPT ); Fri, 6 Sep 2019 11:10:25 -0400 Received: from mail-ua1-f65.google.com ([209.85.222.65]:44465 "EHLO mail-ua1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726940AbfIFPKZ (ORCPT ); Fri, 6 Sep 2019 11:10:25 -0400 Received: by mail-ua1-f65.google.com with SMTP id z8so2139992uaq.11; Fri, 06 Sep 2019 08:10:24 -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; bh=n314PeD9rJUUH0ln6INZ73thO3OSZWeNNOdQWNTDRHs=; b=oXrqX6diObqthzEuEuVfNSpy9U5FfSqxQ7kCgw5JxpL+Gc19eru92cgdITU1okFMRg TCcIi/3fDI9xUzZkYtAUy+R61TlJ1/f9Mbtib1gVdbUVzXrMACPw6Yqg5f6MhUvE7f3y bp5xq5iU0w1pvrwPLcJFyurxSVd5KLemYsaCm8gqOwPIwW+HSaXS8sZywX72vwa+6/D2 f6ApHIoxpVJ22TEsXQHG4asoBLXcLDNihe34mzXBZ/dP5j16tCS8R+HEfy4Eo9kePU9r iOL33Uc9hcsk3F4CMCJOrvbOKSKWOrhP+u/JjyyYBe9i08yuK6bA3acPEftlnyDLP/zL 714A== 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; bh=n314PeD9rJUUH0ln6INZ73thO3OSZWeNNOdQWNTDRHs=; b=bFeMjmY9dKe+Kp03T32pIZMijHuYY4zykXO+6OLtmFyVrEKOtNMNY99B8tkr8tXNa2 fvST1uZ69ZatYx+s0etFErrnZjnRjPEwLVfizMOpOXoEd9rZup8TKqc8MKNNmpRfKXuW TDSNJY/cCVfXJ71NIwI7N5EYKURBwMIln/OcjhAYUsQmIEf+iyP8sDQlRRLENgNjnCKS D/oMBeIvhQLUeizEoeQt6R4ET65X+6OIrl04ep7pC717URMqc0Zl+Q/GXFyCfSp5IOIm 7nkBomTHsnwjZAAnwq5/scA07dueDaSBpEGSNGPrdwfoYA5VrsQfVYU0ezJ3Kd+SjCs9 KooA== X-Gm-Message-State: APjAAAV/veArt2Zbw5sx0EhJsqv/cZg2CT8BOA+nKiHP2caOcSkJmpZI 6O1ik9vnohWvXq25h2/0snhgswaHQ1Y= X-Google-Smtp-Source: APXvYqyMC7LshSz0/GZETxrgMPRhSwcKRup5EIn8NuC4mH5dOMyBla4rKgPOLMxgwFWqhCZneyKAcw== X-Received: by 2002:ab0:1446:: with SMTP id c6mr4602681uae.120.1567782623802; Fri, 06 Sep 2019 08:10:23 -0700 (PDT) Received: from localhost.localdomain ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id o15sm4833822vkc.38.2019.09.06.08.10.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Sep 2019 08:10:23 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, cneirabustos@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next v10 2/4] bpf: new helper to obtain namespace data from current task New bpf helper bpf_get_current_pidns_info. Date: Fri, 6 Sep 2019 11:09:50 -0400 Message-Id: <20190906150952.23066-3-cneirabustos@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190906150952.23066-1-cneirabustos@gmail.com> References: <20190906150952.23066-1-cneirabustos@gmail.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This helper(bpf_get_current_pidns_info) 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. Signed-off-by: Carlos Neira --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 35 +++++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 124 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5b9d22338606..819cb1c84be0 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_current_pidns_info_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 b5889257cc33..3ec9aa1438b7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2747,6 +2747,32 @@ union bpf_attr { * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 + * + * int bpf_get_current_pidns_info(struct bpf_pidns_info *pidns, u32 size_of_pidns) + * Description + * Get tgid, pid and namespace id as seen by the current namespace, + * and device major/minor numbers from /proc/self/ns/pid. Such + * information is stored in *pidns* of size *size*. + * + * This helper is used when pid filtering is needed inside a + * container as bpf_get_current_tgid() helper always returns the + * pid id as seen by the root namespace. + * Return + * 0 on success + * + * On failure, the returned value is one of the following: + * + * **-EINVAL** if *size_of_pidns* is not valid or unable to get ns, pid + * or tgid of the current task. + * + * **-ENOENT** if /proc/self/ns/pid does not exists. + * + * **-ENOENT** if /proc/self/ns does not exists. + * + * **-ENOMEM** if helper internal allocation fails. + * + * **-EPERM** if not able to call helper. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2859,7 +2885,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_current_pidns_info), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3610,4 +3637,10 @@ struct bpf_sockopt { __s32 retval; }; +struct bpf_pidns_info { + __u32 dev; /* dev_t from /proc/self/ns/pid inode */ + __u32 nsid; + __u32 tgid; + __u32 pid; +}; #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 8191a7db2777..3159f2a0188c 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2038,6 +2038,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_current_pidns_info __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..8dbe6347893c 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -11,6 +11,11 @@ #include #include #include +#include +#include +#include +#include +#include #include "../../lib/kstrtox.h" @@ -312,6 +317,87 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src, preempt_enable(); } +BPF_CALL_2(bpf_get_current_pidns_info, struct bpf_pidns_info *, pidns_info, u32, + size) +{ + const char *pidns_path = "/proc/self/ns/pid"; + struct pid_namespace *pidns = NULL; + struct filename *fname = NULL; + struct inode *inode; + struct path kp; + pid_t tgid = 0; + pid_t pid = 0; + int ret = -EINVAL; + int len; + + if (unlikely(in_interrupt() || + current->flags & (PF_KTHREAD | PF_EXITING))) + return -EPERM; + + if (unlikely(size != sizeof(struct bpf_pidns_info))) + return -EINVAL; + + pidns = task_active_pid_ns(current); + if (unlikely(!pidns)) + return -ENOENT; + + pidns_info->nsid = pidns->ns.inum; + pid = task_pid_nr_ns(current, pidns); + if (unlikely(!pid)) + goto clear; + + tgid = task_tgid_nr_ns(current, pidns); + if (unlikely(!tgid)) + goto clear; + + pidns_info->tgid = (u32) tgid; + pidns_info->pid = (u32) pid; + + fname = kmem_cache_alloc(names_cachep, GFP_ATOMIC); + if (unlikely(!fname)) { + ret = -ENOMEM; + goto clear; + } + const size_t fnamesize = offsetof(struct filename, iname[1]); + struct filename *tmp; + + tmp = kmalloc(fnamesize, GFP_ATOMIC); + if (unlikely(!tmp)) { + __putname(fname); + ret = -ENOMEM; + goto clear; + } + + tmp->name = (char *)fname; + fname = tmp; + len = strlen(pidns_path) + 1; + memcpy((char *)fname->name, pidns_path, len); + fname->uptr = NULL; + fname->aname = NULL; + fname->refcnt = 1; + + ret = filename_lookup(AT_FDCWD, fname, 0, &kp, NULL); + if (ret) + goto clear; + + inode = d_backing_inode(kp.dentry); + pidns_info->dev = (u32)inode->i_rdev; + + return 0; + +clear: + memset((void *)pidns_info, 0, (size_t) size); + return ret; +} + +const struct bpf_func_proto bpf_get_current_pidns_info_proto = { + .func = bpf_get_current_pidns_info, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_UNINIT_MEM, + .arg2_type = ARG_CONST_SIZE, +}; + #ifdef CONFIG_CGROUPS BPF_CALL_0(bpf_get_current_cgroup_id) { diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index ca1255d14576..5e1dc22765a5 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_current_pidns_info: + return &bpf_get_current_pidns_info_proto; default: return NULL; } From patchwork Fri Sep 6 15:09: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: 1159070 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="mm0pSIMU"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46Q1Fw2G7Cz9s00 for ; Sat, 7 Sep 2019 01:10:28 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728298AbfIFPK2 (ORCPT ); Fri, 6 Sep 2019 11:10:28 -0400 Received: from mail-vs1-f68.google.com ([209.85.217.68]:43785 "EHLO mail-vs1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726940AbfIFPK1 (ORCPT ); Fri, 6 Sep 2019 11:10:27 -0400 Received: by mail-vs1-f68.google.com with SMTP id u21so4257008vsl.10; Fri, 06 Sep 2019 08:10:27 -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; bh=gmvl5Peybo+fqx0T9NAhNXdtHozg73IhzrDtWDlyMss=; b=mm0pSIMUYKrAlu9sJ6a829/6Q3tqpk1RdBams6qxgypTtR95+dstZXcC2RETU9z4Ih 3U7Sb7TK1ehcOgqLdjK3KWfyFlKFEPzk+MvbRjsfRjI1yWCcphqs2WfKw3cpwIt8PxIB kGhjuvoqXOXlR2yKEqupe3PKTmj1pgHRFhzFZVscPXZEs4BoaodahtuE/aFjl358YmVz rsACigT4VCHePuHp4GHXDBMw7yPi0oH+PbSA3yhX0SJWT8MbIAWa4zfaS/5CKgb0keCH r5DtYADYtGO7k2YKxPRxKPWufQKfR2Gk7HaImg/a9THo1f32zIDCUmx6313shIR4jy00 T6Aw== 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; bh=gmvl5Peybo+fqx0T9NAhNXdtHozg73IhzrDtWDlyMss=; b=DtRe9poHQcviSmO8Ydh4wntL7Kb75ji6pWBicnQdd+BD0N/ZYaWGwWl3RROFKZExeB 5agJ1rMVLKcdqsLNN+XzQNuI30Y6Uwjgg+gpVrOLL58s843CoahI4Ozi9w1B6kMqpvgk Fk7SDXJ9wuoiw597NI6BNEb8gj0mGdaCdTFUAoxm14NYzZg/gfXUJDE3nAVV3byHTPyF LvWcOoxwoG+h2KXt5faaI488FdmX7Bb6FZrf8BKCHzKb+9FdEGl+HRpJctWcRcYll+d6 Reg6r1KcZIYbCFJAuCV0kd1JnRPclpiM/L97VRgfyyTPA40j7U3a6BOqcw+y+UQgikCT vY2A== X-Gm-Message-State: APjAAAWbwYLfX/UGZ6CqIs7yJN0ZG0rF0Wq4rYIVlY2SQXtTvmySl6De nNOjPuhTltRgc/3EN7KmW40pb1biqGE= X-Google-Smtp-Source: APXvYqysj/dAfDlmIWMHuD2J+9ioIaV4Y5iKbj9UM93abMSevD0UnIdza3aZX87dLb3TiIx2nK0PjQ== X-Received: by 2002:a67:f584:: with SMTP id i4mr5180833vso.134.1567782625959; Fri, 06 Sep 2019 08:10:25 -0700 (PDT) Received: from localhost.localdomain ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id o15sm4833822vkc.38.2019.09.06.08.10.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Sep 2019 08:10:25 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, cneirabustos@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next v10 3/4] tools: Added bpf_get_current_pidns_info helper. Date: Fri, 6 Sep 2019 11:09:51 -0400 Message-Id: <20190906150952.23066-4-cneirabustos@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190906150952.23066-1-cneirabustos@gmail.com> References: <20190906150952.23066-1-cneirabustos@gmail.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Signed-off-by: Carlos Neira --- tools/include/uapi/linux/bpf.h | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index b5889257cc33..3ec9aa1438b7 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2747,6 +2747,32 @@ union bpf_attr { * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 + * + * int bpf_get_current_pidns_info(struct bpf_pidns_info *pidns, u32 size_of_pidns) + * Description + * Get tgid, pid and namespace id as seen by the current namespace, + * and device major/minor numbers from /proc/self/ns/pid. Such + * information is stored in *pidns* of size *size*. + * + * This helper is used when pid filtering is needed inside a + * container as bpf_get_current_tgid() helper always returns the + * pid id as seen by the root namespace. + * Return + * 0 on success + * + * On failure, the returned value is one of the following: + * + * **-EINVAL** if *size_of_pidns* is not valid or unable to get ns, pid + * or tgid of the current task. + * + * **-ENOENT** if /proc/self/ns/pid does not exists. + * + * **-ENOENT** if /proc/self/ns does not exists. + * + * **-ENOMEM** if helper internal allocation fails. + * + * **-EPERM** if not able to call helper. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2859,7 +2885,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_current_pidns_info), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3610,4 +3637,10 @@ struct bpf_sockopt { __s32 retval; }; +struct bpf_pidns_info { + __u32 dev; /* dev_t from /proc/self/ns/pid inode */ + __u32 nsid; + __u32 tgid; + __u32 pid; +}; #endif /* _UAPI__LINUX_BPF_H__ */ From patchwork Fri Sep 6 15:09:52 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: 1159072 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="Jm0MZRQ1"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46Q1Fz2HH1z9s00 for ; Sat, 7 Sep 2019 01:10:31 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732513AbfIFPKa (ORCPT ); Fri, 6 Sep 2019 11:10:30 -0400 Received: from mail-vs1-f43.google.com ([209.85.217.43]:37884 "EHLO mail-vs1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726097AbfIFPKa (ORCPT ); Fri, 6 Sep 2019 11:10:30 -0400 Received: by mail-vs1-f43.google.com with SMTP id q9so4277383vsl.4; Fri, 06 Sep 2019 08:10:29 -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; bh=tbLjd0GWnCrkVzbQZwQbrNlcK+9otrffbif6oMZlZqA=; b=Jm0MZRQ1w80kn2G1lk0c3Zwo9Zr2ZxhnEbunLzvKiA4rE/CJGkNinM+gcBz1lqtUck TpsnUreGtP9FXY738CABEpZoQE42UFHLj+i+pvyGOFMw8Q8vLG/jtnptuXW3yEotTf8f FEB59Kr+O50p+N5LL180GCzBuEVy0saN3DbHCfuTWosUzNf/FeMNhqt3eF6ioqfNIh62 AyRmtZfyBzNuhWsc3HgDLoZi6r7kduS9vKGbDCmwA+GIdOnZ6EHGrvYDAGEbJP0B8Cfw E18kCwp5pDw/p6vfQJGHh5cYoo4xQreZUauInn0eFd+w+iZaYfSzXWi/IEZDHFlrWhto 9DKw== 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; bh=tbLjd0GWnCrkVzbQZwQbrNlcK+9otrffbif6oMZlZqA=; b=WmQBHHqCYWi8F/K85JGpL/ABfNLcPHX6O2JWU2jY5zQHgMmkIO7UNu3iSnnt4ZBSIw eNV6P4Ozvz6mT7uiOr34fFlA5hTg3QKOo0i8zmzHBHbKlSwDA0TUW9hQAiMWxXLGWIK5 uO1R0+m4mo6iC2yThrKehj5rjU26IhcvoYkJp0rd8+/qBYnJ0vqFVJLWYAdk27pbMquD On3l0AiepEfZJIiTdYWVArKEafU25unq5Xvag9PHKnumjA6qUKxlY0cdGKKein9pMbvx WeWKjZYCtlvuNVsbVf5bhl3VREtCCoicFicgXWfwWk14vJ7q/nGGLV/IbqLKbQLXb7q6 Lh4w== X-Gm-Message-State: APjAAAXgTo2zHAZSDNKohH8fksb3BBmAHO7zLwu89llHeDJHd62eCvj7 yIUe2zdiDRf//CqJjiE5VB/lIrr+vmI= X-Google-Smtp-Source: APXvYqxPnXyOBXSce2IMbXLF+3jllMrlnqDTCINWIghugf3XZB9anfkjYsP4+lTmMjXIAulAFmC+hA== X-Received: by 2002:a05:6102:86:: with SMTP id t6mr1018127vsp.170.1567782628173; Fri, 06 Sep 2019 08:10:28 -0700 (PDT) Received: from localhost.localdomain ([190.162.109.190]) by smtp.googlemail.com with ESMTPSA id o15sm4833822vkc.38.2019.09.06.08.10.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Sep 2019 08:10:27 -0700 (PDT) From: Carlos Neira To: netdev@vger.kernel.org Cc: yhs@fb.com, ebiederm@xmission.com, brouer@redhat.com, cneirabustos@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next v10 4/4] tools/testing/selftests/bpf: Add self-tests for helper bpf_get_pidns_info. Date: Fri, 6 Sep 2019 11:09:52 -0400 Message-Id: <20190906150952.23066-5-cneirabustos@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190906150952.23066-1-cneirabustos@gmail.com> References: <20190906150952.23066-1-cneirabustos@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Added 2 selftest: bpf_get_current_pidns_info helper is called in an interrupt context and also in a non interrupt context. Signed-off-by: Carlos Neira --- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../testing/selftests/bpf/progs/test_pidns_kern.c | 52 ++++++++ .../selftests/bpf/progs/test_pidns_nmi_kern.c | 52 ++++++++ tools/testing/selftests/bpf/test_pidns.c | 146 +++++++++++++++++++++ tools/testing/selftests/bpf/test_pidns_nmi.c | 139 ++++++++++++++++++++ 6 files changed, 393 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_pidns_kern.c create mode 100644 tools/testing/selftests/bpf/progs/test_pidns_nmi_kern.c create mode 100644 tools/testing/selftests/bpf/test_pidns.c create mode 100644 tools/testing/selftests/bpf/test_pidns_nmi.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 1faad0c3c3c9..8507c89141f5 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -29,7 +29,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test 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_sockopt test_sockopt_sk \ - test_sockopt_multi test_sockopt_inherit test_tcp_rtt + test_sockopt_multi test_sockopt_inherit test_tcp_rtt test_pidns test_pidns_nmi 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..30a70ebe583a 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -313,6 +313,9 @@ static unsigned int (*bpf_set_hash)(void *ctx, __u32 hash) = static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, unsigned long long flags) = (void *) BPF_FUNC_skb_adjust_room; +static int (*bpf_get_current_pidns_info)(struct bpf_pidns_info *buf, + unsigned int buf_size) = + (void *) BPF_FUNC_get_current_pidns_info; /* Scan the ARCH passed in from ARCH env variable (see Makefile) */ #if defined(__TARGET_ARCH_x86) 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..a4c0bde1608b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_pidns_kern.c @@ -0,0 +1,52 @@ +// 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 "bpf_helpers.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, struct bpf_pidns_info); +} nsidmap SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} pidmap SEC(".maps"); + +SEC("tracepoint/syscalls/sys_enter_nanosleep") +int trace(void *ctx) +{ + struct bpf_pidns_info nsinfo; + __u32 key = 0, *expected_pid; + struct bpf_pidns_info *val; + + if (bpf_get_current_pidns_info(&nsinfo, sizeof(nsinfo))) + return -EINVAL; + + expected_pid = bpf_map_lookup_elem(&pidmap, &key); + __u64 tgid = bpf_get_current_pid_tgid(); + + if (!expected_pid || *expected_pid != nsinfo.pid || + nsinfo.tgid != (__u32)tgid) + return 0; + + val = bpf_map_lookup_elem(&nsidmap, &key); + if (val) + *val = nsinfo; + + return 0; +} + +char _license[] SEC("license") = "GPL"; +__u32 _version SEC("version") = 1; diff --git a/tools/testing/selftests/bpf/progs/test_pidns_nmi_kern.c b/tools/testing/selftests/bpf/progs/test_pidns_nmi_kern.c new file mode 100644 index 000000000000..7f02e4e29021 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_pidns_nmi_kern.c @@ -0,0 +1,52 @@ +// 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 "bpf_helpers.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, struct bpf_pidns_info); +} nsidmap SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} pidmap SEC(".maps"); + +SEC("tracepoint/net/netif_receive_skb") +int trace(void *ctx) +{ + struct bpf_pidns_info nsinfo; + __u32 key = 0, *expected_pid; + struct bpf_pidns_info *val; + + if (bpf_get_current_pidns_info(&nsinfo, sizeof(nsinfo))) + return -EINVAL; + + expected_pid = bpf_map_lookup_elem(&pidmap, &key); + __u64 tgid = bpf_get_current_pid_tgid(); + + if (!expected_pid || *expected_pid != nsinfo.pid || + nsinfo.tgid != (__u32)tgid) + return 0; + + val = bpf_map_lookup_elem(&nsidmap, &key); + if (val) + *val = nsinfo; + + 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..0c579305da53 --- /dev/null +++ b/tools/testing/selftests/bpf/test_pidns.c @@ -0,0 +1,146 @@ +// 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) +{ + const char *probe_name = "syscalls/sys_enter_nanosleep"; + const char *file = "test_pidns_kern.o"; + int err, bytes, efd, prog_fd, pmu_fd; + struct bpf_pidns_info knsid = {}; + int pidmap_fd, nsidmap_fd; + struct perf_event_attr attr = {}; + struct bpf_object *obj; + __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; + + nsidmap_fd = bpf_find_map(__func__, obj, "nsidmap"); + if (CHECK(nsidmap_fd < 0, "bpf_find_map", "err %d errno %d\n", + nsidmap_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); + + 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(nsidmap_fd, &key, &knsid); + if (CHECK(err, "bpf_map_lookup_elem", "err %d errno %d\n", err, errno)) + goto close_pmu; + + if (stat("/proc/self/ns/pid", &st)) + goto close_pmu; + + if (CHECK(knsid.nsid != (__u32) st.st_ino, "namespace id", + "bpf %u user %u\n", knsid.nsid, (__u32) st.st_ino)) + goto close_pmu; + + if (CHECK(major(knsid.dev) != major(st.st_rdev), "dev major", + "bpf %u user %u\n", major(knsid.dev), major(st.st_rdev))) + goto close_pmu; + + if (CHECK(minor(knsid.dev) != minor(st.st_rdev), "dev minor", + "bpf %u user %u\n", minor(knsid.dev), minor(st.st_rdev))) + 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; +} diff --git a/tools/testing/selftests/bpf/test_pidns_nmi.c b/tools/testing/selftests/bpf/test_pidns_nmi.c new file mode 100644 index 000000000000..bb8075bbe7d8 --- /dev/null +++ b/tools/testing/selftests/bpf/test_pidns_nmi.c @@ -0,0 +1,139 @@ +// 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) +{ + const char *probe_name = "net/netif_receive_skb"; + const char *file = "test_pidns_kern.o"; + int err, bytes, efd, prog_fd, pmu_fd; + struct bpf_pidns_info knsid = {}; + int pidmap_fd, nsidmap_fd; + struct perf_event_attr attr = {}; + struct bpf_object *obj; + __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; + + nsidmap_fd = bpf_find_map(__func__, obj, "nsidmap"); + if (CHECK(nsidmap_fd < 0, "bpf_find_map", "err %d errno %d\n", + nsidmap_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); + + 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(nsidmap_fd, &key, &knsid); + if (CHECK(err, "bpf_map_lookup_elem", "err %d errno %d\n", err, errno)) + goto close_pmu; + + if (stat("/proc/self/ns/pid", &st)) + goto close_pmu; + + if (CHECK(knsid.nsid != (__u32) st.st_ino, "namespace_id", + "Called in interrupt context bpf %u user %u\n", + knsid.nsid, (__u32) st.st_ino)) + 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; +}