From patchwork Wed Dec 18 17:38:23 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: 1212641 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="c72qmGJl"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dMgS35Rlz9sRC for ; Thu, 19 Dec 2019 04:38:44 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727421AbfLRRin (ORCPT ); Wed, 18 Dec 2019 12:38:43 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:32832 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbfLRRim (ORCPT ); Wed, 18 Dec 2019 12:38:42 -0500 Received: by mail-pg1-f195.google.com with SMTP id 6so1654066pgk.0; Wed, 18 Dec 2019 09:38:42 -0800 (PST) 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=c72qmGJltw78P+qY1SAll2shY2/dWvkPIu9YhRgzgO+nqz3mTpC3GHHRJmQxeMbAwG KDdvdRrdV2Re7lInYUrXYG2miSji1dQWRUdwmV2ToD5MaffDERWw7UUnFttQtpjL2TxT RO7kKIffQkntsNaPb24ZimA9RCl+4qzUXhEFzwEqW5ng7YhFAczPHNBIHqYee1ELx17b 4YR14KWxoVGNkt5h08wut6hxOrSVdQO3GxNW9apc+fVKbO8XVq+0+PGHqE+QF3+uOEfn IBBKgcdIjSJeaFUJsxGbTaL8HZorJYbJF9RUXYMX8Xu0Xypm6Fhn+lxhloTymst4zTNs DvuA== 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=XzUbiTGRXzXsllBRjW2BDxlOzANTObf7M8qh66ADJ7NaahhOrdaN5gaOPXVqekPESG 9F/12+mkMgzCNPjJ3IpQRTZW30TJVqa0tPqxUyB8k+8UtBlbjXxRjDV2XbZSlJiFao7z h6Y4c3fPZbQll1cWw81vAvOKwP3pamsgwAAUsJkaBzF2A3xChf9N9SvfV5Y8y+1ZG1wL 40qgqTgl7ahjwAdVJMBF4AZjUm2Z6nd9yhMvSR1T0CFDQcEkMQXKbD4k9wu1SW8/HJlo C700XtWED02PgGv36AhrEGYLLvyki7h4Yg7nwFqpRpQttjtcV0iTQ0kulxZJ91Znkn8o 6yGA== X-Gm-Message-State: APjAAAVs7+2EOGxkkjNxlCn6E7qXUwdvwikzMJ8J2gVz+gMdRfF71GBJ 0uA4JTPKSgMi+Y+EoOfnN4DZEubmAVE= X-Google-Smtp-Source: APXvYqxpIsgMw+l2pFtasN0rzHIiZy5Fn+lNPjmRsGtq6pNX7/NwO2TR90EdIcrSZzAtDzLq4RYYvw== X-Received: by 2002:a63:904c:: with SMTP id a73mr4339807pge.335.1576690721771; Wed, 18 Dec 2019 09:38:41 -0800 (PST) Received: from bpf-kern-dev.byteswizards.com (pc-184-104-160-190.cm.vtr.net. [190.160.104.184]) by smtp.googlemail.com with ESMTPSA id s15sm3991925pgq.4.2019.12.18.09.38.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2019 09:38:41 -0800 (PST) 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 v16 1/5] fs/nsfs.c: added ns_match Date: Wed, 18 Dec 2019 14:38:23 -0300 Message-Id: <20191218173827.20584-2-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191218173827.20584-1-cneirabustos@gmail.com> References: <20191218173827.20584-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 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 Wed Dec 18 17:38:24 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: 1212643 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="DWRc6O50"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dMgT5kcSz9sRK for ; Thu, 19 Dec 2019 04:38:45 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727425AbfLRRip (ORCPT ); Wed, 18 Dec 2019 12:38:45 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:36257 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbfLRRio (ORCPT ); Wed, 18 Dec 2019 12:38:44 -0500 Received: by mail-pj1-f68.google.com with SMTP id n59so1201895pjb.1; Wed, 18 Dec 2019 09:38:44 -0800 (PST) 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=VqrUOuKk2JGEFEQuYWcTKickDqlx6bClNhIOxM47HI4=; b=DWRc6O50gSs54r2ykKwMMFLq5Kl5SN8IAXH23IMr9Z6kVC714yNXv6+Z64++agh1wX 4fm1xk1wWNLijm0jOB5LAWC2Lr/4CF5bseTHZk5TkgUSHiKLIrERMGPn20fzTf2cP8GE 3NU9ExuLmsEz4wtLFpVb51lmh2dLzDS/lGOkR8byylO4T7NEojW9IUEIeSWEh8lxWRnw 6WWq4H6JGm9/ODPHznq52A4MstFiul0EXm+SYEHOa1WnTPgMXevabG86sKJ9oZKffcnB 5hmSEQTOEajqBgAiPwLmP1bHo7ygUznz9g8KkTuLHvSIOpQGcoG8JROIGRKkgJjov8Nb IWbw== 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=VqrUOuKk2JGEFEQuYWcTKickDqlx6bClNhIOxM47HI4=; b=T3RJUiEYoFJkn1YwqO3yqlnBBpqIZX6L5KRdMfONYPt2qdUmgGNs0iCxg22XwvlNU0 TdOpDRq03FxB3ShihK/d7EkUByeJGmik1xVernlBWKnrlbzOWh4eOClxR1mKjjGtiU+g XckJTDItWNngQyHkQ6HrQNZbNHyv8lJu1ERQ9y08t1OHp4rWnp3inFbzNTeWZS09QMPG Vm+TABoMV/fNCeNV6gRVxS84/VNvsBdWAeDyh5J+K5bGGOTX+YemA+j3JL2cVApFHW5f PIjc6fbcGqCtDT7N1Mb8PA5bjaTgTCg3yWLPXkae4zo/iAiiWPhsfIQSDrIRamfd0/8H 7iBQ== X-Gm-Message-State: APjAAAWfd3IH+PsH2NjazQaFpbd30zv73+28Q3IGWl05j1+68fYQITuL fCkhpvofMapaBemXa1jKeyiaBjE1qck= X-Google-Smtp-Source: APXvYqyBrooCAo6sGhgTemUSwTX4QJOojEGnum36ND7Wwt89gYlgnP1niB4Us76893uPQSoQcfgr2g== X-Received: by 2002:a17:90a:2486:: with SMTP id i6mr4398088pje.9.1576690723800; Wed, 18 Dec 2019 09:38:43 -0800 (PST) Received: from bpf-kern-dev.byteswizards.com (pc-184-104-160-190.cm.vtr.net. [190.160.104.184]) by smtp.googlemail.com with ESMTPSA id s15sm3991925pgq.4.2019.12.18.09.38.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2019 09:38:43 -0800 (PST) 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 v16 2/5] bpf: added new helper bpf_get_ns_current_pid_tgid Date: Wed, 18 Dec 2019 14:38:24 -0300 Message-Id: <20191218173827.20584-3-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191218173827.20584-1-cneirabustos@gmail.com> References: <20191218173827.20584-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 | 19 ++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 45 ++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 35903f148be5..a40b3e13cf98 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1224,6 +1224,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 dbbcf0b02970..75864cd91b50 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2821,6 +2821,18 @@ union bpf_attr { * Return * On success, the strictly positive length of the string, including * the trailing NUL character. On error, a negative value. + * 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), \ @@ -2938,7 +2950,8 @@ union bpf_attr { FN(probe_read_user), \ FN(probe_read_kernel), \ FN(probe_read_user_str), \ - FN(probe_read_kernel_str), + FN(probe_read_kernel_str), \ + FN(get_ns_current_pid_tgid), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3689,4 +3702,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 49e32acad7d8..59b892ab2acb 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2145,6 +2145,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 cada974c9f4e..4aea086c20e5 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 e5ef4ae9edb5..8c931cd1a768 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -822,6 +822,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 Wed Dec 18 17:38:25 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: 1212645 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="KiMaLC7+"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dMgW39GTz9sRG for ; Thu, 19 Dec 2019 04:38:47 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727444AbfLRRir (ORCPT ); Wed, 18 Dec 2019 12:38:47 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:34847 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbfLRRiq (ORCPT ); Wed, 18 Dec 2019 12:38:46 -0500 Received: by mail-pf1-f196.google.com with SMTP id b19so1606468pfo.2; Wed, 18 Dec 2019 09:38:46 -0800 (PST) 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=KjPwmFi9va1zvHRTrGSZQ7PTAqFjJMFz3DanPqDlHqM=; b=KiMaLC7+bfy6i8PEDSFOGGSF94BvefX+O8j6QzOkrXB+yJNXZ/onasYyaRW7zDdh8z f8zc5sWfT0XONQuKEo8GLStD1Qsg95JawAO5XVcXtgSDk4xJMmx3SI7PXJ0uoi7ooFFd cxWNDycqNHX7NvqWuLAZnmz6UxGPEuxi9m8W/mDUarKN1ui6eJIdnqtWdnkodfQYrbqf UnZdmZZ1DAuMS+a8YPUduZSbnQRBpkaQE5Yc2+xTAg5qLV7K7vzErzJCCjHlOV0Vq8pv cy9WTskh2m88axELcKl56y9jV1Lo10U6DBWw6NhCdhyHXn7NAnv8c3fU4zFRyp4lx7fq ItNg== 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=KjPwmFi9va1zvHRTrGSZQ7PTAqFjJMFz3DanPqDlHqM=; b=S4945AvLF+1xirfGTPrB1QAXm4eSzEWDkW8XcILf/e05r+2qmbxCzf8HC7635HOgQ/ BfvtOu+AYSHMXQ2XwbS4m7GPgn/VfIVIFEBTKZyVJ8ROcKclKmvcrh9CkEYzZ8iXJaBd UIycp5LPaHJHu2OgP+M58jb8pDe4rIGSxil9eu0iSKlaO8OlUS046zpMCdoRS3hM0ZgB zlhS3mDa3e02luKngia6QfOetA2T2QFwShIYIUln7KtFrcySOQrll3XFyFuYjgqzfHZh jOqTw6sixkndpnHVpesxA5HXRZeXXn7VgSz1GE3fhgC362DiIVJQFdY/iWGSCEPhTKC5 Vbwg== X-Gm-Message-State: APjAAAVB56bJl0jnw20Tsn5edc12Efmeq6Huyhvku7JLGfLcgVKhfZys SBXUs8tDTyCuJcYa66JK3AAjF3HDYLA= X-Google-Smtp-Source: APXvYqw3ERb4uqmSAyMuf7Qyskh0MdLo15P9bvmqQeecnGFr33Km6jfkVLXh3wb7/M32OR4PnhZ2ew== X-Received: by 2002:aa7:9556:: with SMTP id w22mr4364082pfq.198.1576690725874; Wed, 18 Dec 2019 09:38:45 -0800 (PST) Received: from bpf-kern-dev.byteswizards.com (pc-184-104-160-190.cm.vtr.net. [190.160.104.184]) by smtp.googlemail.com with ESMTPSA id s15sm3991925pgq.4.2019.12.18.09.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2019 09:38:45 -0800 (PST) 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 v16 3/5] tools: Added bpf_get_ns_current_pid_tgid helper Date: Wed, 18 Dec 2019 14:38:25 -0300 Message-Id: <20191218173827.20584-4-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191218173827.20584-1-cneirabustos@gmail.com> References: <20191218173827.20584-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 Acked-by: Yonghong Song --- tools/include/uapi/linux/bpf.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index dbbcf0b02970..75864cd91b50 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2821,6 +2821,18 @@ union bpf_attr { * Return * On success, the strictly positive length of the string, including * the trailing NUL character. On error, a negative value. + * 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), \ @@ -2938,7 +2950,8 @@ union bpf_attr { FN(probe_read_user), \ FN(probe_read_kernel), \ FN(probe_read_user_str), \ - FN(probe_read_kernel_str), + FN(probe_read_kernel_str), \ + FN(get_ns_current_pid_tgid), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3689,4 +3702,8 @@ struct bpf_sockopt { __s32 retval; }; +struct bpf_pidns_info { + __u32 pid; + __u32 tgid; +}; #endif /* _UAPI__LINUX_BPF_H__ */ From patchwork Wed Dec 18 17:38:26 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: 1212647 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="cQJeSggR"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dMgZ0Lrfz9sRC for ; Thu, 19 Dec 2019 04:38:50 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727063AbfLRRit (ORCPT ); Wed, 18 Dec 2019 12:38:49 -0500 Received: from mail-pf1-f194.google.com ([209.85.210.194]:45241 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbfLRRit (ORCPT ); Wed, 18 Dec 2019 12:38:49 -0500 Received: by mail-pf1-f194.google.com with SMTP id 2so1579894pfg.12; Wed, 18 Dec 2019 09:38:48 -0800 (PST) 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=ll2XUGPtS1DGsRu7y7Hq5A+fQ0XX0UH+KolA2vHqi/U=; b=cQJeSggR70rBCXPHqVoivz6FWxkUplAQr+SqV+arKhXzuKM4fGvn4B/lGHAQhcwudD /jEb+ujVMCXNPggqifD/0V9c28UuoZ2h9oDGOHH9N2IVQoJ0sBUax2DF7JTfT2WVrDjD SFY0JvV5ZoFUvTx4eUuMocrwoWF2Mk+RR0LPX4j8+w+E4c3XqxI0IbryVBTvyXJlNBNG 6zJRmq9kNgY4rCD/Qrl2K+lwTAZm9XBMt/dvb3uMMeovTwj9cQAxdFiEIqgnzt9hFi9f E6shDb7ZEeHcC8WKt6ru/n6MVQFH7x4hbqZPRr/7IhAu8J92d2CRnstTc4/0FYYKKMaI Jvxg== 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=ll2XUGPtS1DGsRu7y7Hq5A+fQ0XX0UH+KolA2vHqi/U=; b=Nfst4mKUg3lFqkgdH6C+ssEtzi1+oR4TYYXgZDPWFlgWNfN/hf4Yrfby3FqMWWSCpP 3GiPEhMpUvbxAOg0hUnttt2fs5p6VfY8Pp25cZMaxEElfGxVpB/j8x5ErtETKYu89nf9 i/MIEiKbPvEGHCK3VZZTGlLkOGWiuuQBTzie/+Sd4xnWsF4onYB8M7AsIrHWObINN/Gl i61JbUKDFTCFheyA+ENRCMUnumGXapeCZa8jluymFjEzXfseBbEer3SprMw2N0eJUfHD cJzFWb21ldbCwVZa3BrepcCoHqbe5ItCN5GhHcTZ8inj20jAseFu+yDpiTyeMG9TFMa8 h0bA== X-Gm-Message-State: APjAAAWT6y7RyfzKLPPCOeUL7iHDB5198oXu94KqDuStXzeon3W8/vQK E869PjJyZqta+mfFtZnxcFApJhAgsNc= X-Google-Smtp-Source: APXvYqz0F5JLI7Oy7llKdrzKPfe4F8ftkLar9M2Ik5iTZ1b1j+EWhOtydPE4o3FoaCLGwbBiuj7wEg== X-Received: by 2002:a62:aa09:: with SMTP id e9mr4342927pff.154.1576690727770; Wed, 18 Dec 2019 09:38:47 -0800 (PST) Received: from bpf-kern-dev.byteswizards.com (pc-184-104-160-190.cm.vtr.net. [190.160.104.184]) by smtp.googlemail.com with ESMTPSA id s15sm3991925pgq.4.2019.12.18.09.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2019 09:38:47 -0800 (PST) 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 v16 4/5] tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid. Date: Wed, 18 Dec 2019 14:38:26 -0300 Message-Id: <20191218173827.20584-5-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191218173827.20584-1-cneirabustos@gmail.com> References: <20191218173827.20584-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 bpf_get_ns_current_pid_tgid Signed-off-by: Carlos Neira Acked-by: Yonghong Song --- .../bpf/prog_tests/ns_current_pid_tgid.c | 88 +++++++++++++++++++ .../bpf/progs/test_ns_current_pid_tgid.c | 37 ++++++++ 2 files changed, 125 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..afd4a19dda14 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c @@ -0,0 +1,88 @@ +// 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))) { + link = NULL; + 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 BPF pid/tgid %llu\n", id, bss.pid_tgid)) + goto cleanup; +cleanup: + if (!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 Wed Dec 18 17:38:27 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: 1212649 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="D5NnhQbI"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dMgf1kv0z9sRC for ; Thu, 19 Dec 2019 04:38:54 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727473AbfLRRix (ORCPT ); Wed, 18 Dec 2019 12:38:53 -0500 Received: from mail-pj1-f65.google.com ([209.85.216.65]:38675 "EHLO mail-pj1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbfLRRiv (ORCPT ); Wed, 18 Dec 2019 12:38:51 -0500 Received: by mail-pj1-f65.google.com with SMTP id l35so1199929pje.3; Wed, 18 Dec 2019 09:38:50 -0800 (PST) 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=D5NnhQbI+mDaV1PQ4wBHjLBzJKH9ScxJC052WKvW4Z6uaUXUwQ0gBnyT72EnwqYCsv IQMG4CS1ihS79lMMdcZlf8adV04jocAb6ky054iehZffDFg5nwUVTLCDqclQgZ5ArOV3 HbwlMg9bcwmSRXTDoZ2QJQLEojYeA9wiZVbNWhJmsqi4Y93lPIOq+KJ1V9/dTky6r3P9 mUTSbGgGM4e5Gfqp1MDJ3TWcm0l+8exPIqWzIP0jf6738PXoijZIgemK6Qm6ybONW+oO SgcY4ld+zCEY7P2J0opCoPvmN3v57iShrQoIHHuvFSeQSPTmCaqgQpN0Ay+lKi5NMshU ZVAA== 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=naD329H2z77Pj/rys21dGBHGFLZ7bYokFO1DLZOUZ0Yc+xLROpkz9S0loFBoKaopnk 2yUcU9kSgo2IWQqyAbMCn8pvNcFKEXQLnj9tlnhpFW/woPY5/oBAJUM1mFAqEy0g4FH+ W9wB4gypLmM50ESNLG5FNEUyKUcvzO5r6W+xQbFwDKch5HxcDEdAHtGTD79x4il7OuiV t+8S/CaETRHXcBIRjYA6wGyRhzWblUjj594RfvkKnHp6hI8VMtkv885+6qC8QTX99RRa AYZIT2HGXaJqaglspCt6oPK8G0hpjhEDzEVKTcFbwN07STJ/FRPaPgun/a7zx7b/V4B1 yWpw== X-Gm-Message-State: APjAAAWGYHVWtUSIfe5nIVH+eBxZQ0HTM8RbkB0g5eJI7HF+7B0UgVl9 7qYZk4vZ2SJ27TrwyLUacI5PwvX485E= X-Google-Smtp-Source: APXvYqwJz146CAnZE2imfyh5skgR8nGQUP67rzwpH9rPhAjEbV67e/6su1iR3e9KEQz46PDr29taZQ== X-Received: by 2002:a17:90a:fa92:: with SMTP id cu18mr4280234pjb.114.1576690729976; Wed, 18 Dec 2019 09:38:49 -0800 (PST) Received: from bpf-kern-dev.byteswizards.com (pc-184-104-160-190.cm.vtr.net. [190.160.104.184]) by smtp.googlemail.com with ESMTPSA id s15sm3991925pgq.4.2019.12.18.09.38.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2019 09:38:49 -0800 (PST) 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 v16 5/5] bpf_helpers_doc.py: Add struct bpf_pidns_info to known types Date: Wed, 18 Dec 2019 14:38:27 -0300 Message-Id: <20191218173827.20584-6-cneirabustos@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191218173827.20584-1-cneirabustos@gmail.com> References: <20191218173827.20584-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',