From patchwork Wed Apr 15 19:27:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 221142 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=-9.8 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT 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 58993C3815B for ; Wed, 15 Apr 2020 19:32:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3410B20784 for ; Wed, 15 Apr 2020 19:32:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="IepUGivx" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2411897AbgDOTah (ORCPT ); Wed, 15 Apr 2020 15:30:37 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:37252 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2411817AbgDOT2X (ORCPT ); Wed, 15 Apr 2020 15:28:23 -0400 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 03FJSC9S007588 for ; Wed, 15 Apr 2020 12:28:20 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=yd6omChVWlKDAQ+4CsV2D1kT8tOgiYp8DJQFULum+mo=; b=IepUGivxnqarnUXSBc7KbX2nThQ1NC5XkZgb0MchcVSWvpJ6xiXHZZ0pi9HVH5Ub9nAP X7R7LuQF+Z6tLZLORpnY/es6mONDmnr3m8IxMv3ANwkFRWfgDshrSzjU+o3drOov85+o gaBG62Dt+3B8mPj3G06N1Y496SX1qA+6QBA= Received: from mail.thefacebook.com ([163.114.132.120]) by m0001303.ppops.net with ESMTP id 30dn7fymmh-14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 15 Apr 2020 12:28:20 -0700 Received: from intmgw001.03.ash8.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Wed, 15 Apr 2020 12:28:06 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 607433700AF5; Wed, 15 Apr 2020 12:27:54 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: Andrii Nakryiko , , Martin KaFai Lau , CC: Alexei Starovoitov , Daniel Borkmann , Smtp-Origin-Cluster: ftw2c04 Subject: [RFC PATCH bpf-next v2 12/17] bpf: implement query for target_proto and file dumper prog_id Date: Wed, 15 Apr 2020 12:27:54 -0700 Message-ID: <20200415192754.4083756-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200415192740.4082659-1-yhs@fb.com> References: <20200415192740.4082659-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-04-15_07:2020-04-14,2020-04-15 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 lowpriorityscore=0 suspectscore=4 phishscore=0 mlxscore=0 spamscore=0 adultscore=0 impostorscore=0 mlxlogscore=999 priorityscore=1501 bulkscore=0 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004150144 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Given a fd representing a bpfdump target, user can retrieve the target_proto name which represents the bpf program prototype. Given a fd representing a file dumper, user can retrieve the bpf_prog id associated with that dumper. Signed-off-by: Yonghong Song --- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 11 +++++- kernel/bpf/dump.c | 72 ++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 2 +- tools/include/uapi/linux/bpf.h | 11 +++++- 5 files changed, 95 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 401e5bf921a2..a1ae8509e735 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1138,6 +1138,8 @@ int bpf_prog_dump_create(struct bpf_prog *prog); struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size, u64 *session_id, u64 *seq_num, bool is_last); int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx); +int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr); + int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 75f3657d526c..856e3f8a63b8 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -533,7 +533,10 @@ union bpf_attr { }; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ - __u32 bpf_fd; + union { + __u32 bpf_fd; + __u32 dump_query_fd; + }; __u32 info_len; __aligned_u64 info; } info; @@ -3618,6 +3621,12 @@ struct bpf_btf_info { __u32 id; } __attribute__((aligned(8))); +struct bpf_dump_info { + __aligned_u64 prog_ctx_type_name; + __u32 type_name_buf_len; + __u32 prog_id; +} __attribute__((aligned(8))); + /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed * by user and intended to be used by socket (e.g. to bind to, depends on * attach attach type). diff --git a/kernel/bpf/dump.c b/kernel/bpf/dump.c index 789b35772a81..643591bf5aea 100644 --- a/kernel/bpf/dump.c +++ b/kernel/bpf/dump.c @@ -93,6 +93,78 @@ static void *get_extra_priv_dptr(void *old_ptr, u32 old_size) return old_ptr + roundup(old_size, 8); } +int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr) +{ + struct bpf_dump_info __user *ubpf_dinfo; + struct bpfdump_target_info *tinfo; + struct dumper_inode_info *i_info; + struct bpf_dump_info bpf_dinfo; + const char *prog_ctx_type_name; + void * __user tname_buf; + u32 tname_len, info_len; + struct file *filp; + struct fd qfd; + int err = 0; + + qfd = fdget(attr->info.dump_query_fd); + filp = qfd.file; + if (!filp) + return -EBADF; + + if (filp->f_op != &bpf_dumper_ops && + filp->f_inode->i_op != &bpfdump_dir_iops) { + err = -EINVAL; + goto done; + } + + info_len = attr->info.info_len; + ubpf_dinfo = u64_to_user_ptr(attr->info.info); + err = bpf_check_uarg_tail_zero(ubpf_dinfo, sizeof(bpf_dinfo), + info_len); + if (err) + goto done; + info_len = min_t(u32, sizeof(bpf_dinfo), info_len); + + memset(&bpf_dinfo, 0, sizeof(bpf_dinfo)); + if (copy_from_user(&bpf_dinfo, ubpf_dinfo, info_len)) { + err = -EFAULT; + goto done; + } + + /* copy prog_id for dumpers */ + if (filp->f_op == &bpf_dumper_ops) { + i_info = filp->f_inode->i_private; + bpf_dinfo.prog_id = i_info->prog->aux->id; + tinfo = i_info->tinfo; + } else { + tinfo = filp->f_inode->i_private; + } + + prog_ctx_type_name = tinfo->prog_ctx_type_name; + + tname_len = strlen(prog_ctx_type_name) + 1; + if (bpf_dinfo.type_name_buf_len < tname_len) { + err = -ENOSPC; + goto done; + } + + /* copy prog_ctx_type_name */ + tname_buf = u64_to_user_ptr(bpf_dinfo.prog_ctx_type_name); + if (copy_to_user(tname_buf, prog_ctx_type_name, tname_len)) { + err = -EFAULT; + goto done; + } + + /* copy potentially updated bpf_dinfo and info_len */ + if (copy_to_user(ubpf_dinfo, &bpf_dinfo, info_len) || + put_user(info_len, &uattr->info.info_len)) + return -EFAULT; + +done: + fdput(qfd); + return err; +} + #ifdef CONFIG_PROC_FS static void dumper_show_fdinfo(struct seq_file *m, struct file *filp) { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index e6a4514435c4..1cde78e53a17 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3358,7 +3358,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, else if (f.file->f_op == &btf_fops) err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); else - err = -EINVAL; + err = bpf_dump_query(attr, uattr); fdput(f); return err; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 75f3657d526c..856e3f8a63b8 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -533,7 +533,10 @@ union bpf_attr { }; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ - __u32 bpf_fd; + union { + __u32 bpf_fd; + __u32 dump_query_fd; + }; __u32 info_len; __aligned_u64 info; } info; @@ -3618,6 +3621,12 @@ struct bpf_btf_info { __u32 id; } __attribute__((aligned(8))); +struct bpf_dump_info { + __aligned_u64 prog_ctx_type_name; + __u32 type_name_buf_len; + __u32 prog_id; +} __attribute__((aligned(8))); + /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed * by user and intended to be used by socket (e.g. to bind to, depends on * attach attach type).