{"id":819321,"url":"http://patchwork.ozlabs.org/api/patches/819321/?format=json","web_url":"http://patchwork.ozlabs.org/project/netdev/patch/20170927213756.1254938-2-kafai@fb.com/","project":{"id":7,"url":"http://patchwork.ozlabs.org/api/projects/7/?format=json","name":"Linux network development","link_name":"netdev","list_id":"netdev.vger.kernel.org","list_email":"netdev@vger.kernel.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170927213756.1254938-2-kafai@fb.com>","list_archive_url":null,"date":"2017-09-27T21:37:52","name":"[net-next,1/5] bpf: Add name, load_time, uid and map_ids to bpf_prog_info","commit_ref":null,"pull_url":null,"state":"accepted","archived":true,"hash":"ddd803f8c54f4014b600480d9175f4a15da3fee9","submitter":{"id":64907,"url":"http://patchwork.ozlabs.org/api/people/64907/?format=json","name":"Martin KaFai Lau","email":"kafai@fb.com"},"delegate":{"id":34,"url":"http://patchwork.ozlabs.org/api/users/34/?format=json","username":"davem","first_name":"David","last_name":"Miller","email":"davem@davemloft.net"},"mbox":"http://patchwork.ozlabs.org/project/netdev/patch/20170927213756.1254938-2-kafai@fb.com/mbox/","series":[{"id":5453,"url":"http://patchwork.ozlabs.org/api/series/5453/?format=json","web_url":"http://patchwork.ozlabs.org/project/netdev/list/?series=5453","date":"2017-09-27T21:37:55","name":"bpf: Extend bpf_{prog,map}_info","version":1,"mbox":"http://patchwork.ozlabs.org/series/5453/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/819321/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/819321/checks/","tags":{},"related":[],"headers":{"Return-Path":"<netdev-owner@vger.kernel.org>","X-Original-To":"patchwork-incoming@ozlabs.org","Delivered-To":"patchwork-incoming@ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (1024-bit key;\n\tunprotected) header.d=fb.com header.i=@fb.com header.b=\"mKLiSFgp\";\n\tdkim-atps=neutral"],"Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3y2WQr3vGjz9t67\n\tfor <patchwork-incoming@ozlabs.org>;\n\tThu, 28 Sep 2017 07:38:28 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1752102AbdI0Vh7 (ORCPT <rfc822;patchwork-incoming@ozlabs.org>);\n\tWed, 27 Sep 2017 17:37:59 -0400","from mx0a-00082601.pphosted.com ([67.231.145.42]:60038 \"EHLO\n\tmx0a-00082601.pphosted.com\" rhost-flags-OK-OK-OK-OK)\n\tby vger.kernel.org with ESMTP id S1751958AbdI0Vh6 (ORCPT\n\t<rfc822;netdev@vger.kernel.org>); Wed, 27 Sep 2017 17:37:58 -0400","from pps.filterd (m0044010.ppops.net [127.0.0.1])\n\tby mx0a-00082601.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id\n\tv8RLYShh015871\n\tfor <netdev@vger.kernel.org>; Wed, 27 Sep 2017 14:37:57 -0700","from mail.thefacebook.com ([199.201.64.23])\n\tby mx0a-00082601.pphosted.com with ESMTP id 2d8jaggmau-2\n\t(version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT)\n\tfor <netdev@vger.kernel.org>; Wed, 27 Sep 2017 14:37:57 -0700","from mx-out.facebook.com (192.168.52.123) by\n\tPRN-CHUB14.TheFacebook.com (192.168.16.24) with Microsoft SMTP Server\n\tid 14.3.319.2; Wed, 27 Sep 2017 14:37:57 -0700","by devbig738.prn1.facebook.com (Postfix, from userid 6611)    id\n\t7255D4500949; Wed, 27 Sep 2017 14:37:56 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com;\n\th=from : to : cc : subject\n\t: date : message-id : in-reply-to : references : mime-version :\n\tcontent-type; s=facebook;\n\tbh=XBYrVP7xWT+YSWVyf1rAyITCgX5iUOvO2jIa6QQvQYg=; \n\tb=mKLiSFgpjLDlet1Plu11qZHis2VyQEVXfWFKqcWRWcSSJht4oyNwcs1EQRdVHghLEh6H\n\tMVH776euGLPNJ6/qcTqU3WDesxo36zNMD+9FJz+NbvHcNfaRqXjPOJarzsypfOjc5xFL\n\trUo4heV8MT4EehbvQgFOt5B59igMH6NdOQk= ","Smtp-Origin-Hostprefix":"devbig","From":"Martin KaFai Lau <kafai@fb.com>","Smtp-Origin-Hostname":"devbig738.prn1.facebook.com","To":"<netdev@vger.kernel.org>","CC":"Alexei Starovoitov <ast@fb.com>,\n\tDaniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>","Smtp-Origin-Cluster":"prn1c29","Subject":"[PATCH net-next 1/5] bpf: Add name, load_time,\n\tuid and map_ids to bpf_prog_info","Date":"Wed, 27 Sep 2017 14:37:52 -0700","Message-ID":"<20170927213756.1254938-2-kafai@fb.com>","X-Mailer":"git-send-email 2.9.5","In-Reply-To":"<20170927213756.1254938-1-kafai@fb.com>","References":"<20170927213756.1254938-1-kafai@fb.com>","X-FB-Internal":["Safe","Safe"],"MIME-Version":"1.0","Content-Type":"text/plain","X-Proofpoint-Spam-Reason":"safe","X-Proofpoint-Virus-Version":"vendor=fsecure engine=2.50.10432:, ,\n\tdefinitions=2017-09-27_08:, , signatures=0","Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"},"content":"The patch adds name and load_time to struct bpf_prog_aux.  They\nare also exported to bpf_prog_info.\n\nThe bpf_prog's name is passed by userspace during BPF_PROG_LOAD.\nThe kernel only stores the first (BPF_PROG_NAME_LEN - 1) bytes\nand the name stored in the kernel is always \\0 terminated.\n\nThe kernel will reject name that contains characters other than\nisalnum() and '_'.  It will also reject name that is not null\nterminated.\n\nThe existing 'user->uid' of the bpf_prog_aux is also exported to\nthe bpf_prog_info as created_by_uid.\n\nThe existing 'used_maps' of the bpf_prog_aux is exported to\nthe newly added members 'nr_map_ids' and 'map_ids' of\nthe bpf_prog_info.  On the input, nr_map_ids tells how\nbig the userspace's map_ids buffer is.  On the output,\nnr_map_ids tells the exact user_map_cnt and it will only\ncopy up to the userspace's map_ids buffer is allowed.\n\nSigned-off-by: Martin KaFai Lau <kafai@fb.com>\nAcked-by: Alexei Starovoitov <ast@fb.com>\nAcked-by: Daniel Borkmann <daniel@iogearbox.net>\n---\n include/linux/bpf.h      |  2 ++\n include/uapi/linux/bpf.h |  8 ++++++++\n kernel/bpf/syscall.c     | 51 +++++++++++++++++++++++++++++++++++++++++++++++-\n 3 files changed, 60 insertions(+), 1 deletion(-)","diff":"diff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 2b672c50f160..33ccc474fb04 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -187,6 +187,8 @@ struct bpf_prog_aux {\n \tstruct bpf_map **used_maps;\n \tstruct bpf_prog *prog;\n \tstruct user_struct *user;\n+\tu64 load_time; /* ns since boottime */\n+\tu8 name[BPF_OBJ_NAME_LEN];\n \tunion {\n \t\tstruct work_struct work;\n \t\tstruct rcu_head\trcu;\ndiff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h\nindex e43491ac4823..bd6348269bf5 100644\n--- a/include/uapi/linux/bpf.h\n+++ b/include/uapi/linux/bpf.h\n@@ -175,6 +175,8 @@ enum bpf_attach_type {\n /* Specify numa node during map creation */\n #define BPF_F_NUMA_NODE\t\t(1U << 2)\n \n+#define BPF_OBJ_NAME_LEN 16U\n+\n union bpf_attr {\n \tstruct { /* anonymous struct used by BPF_MAP_CREATE command */\n \t\t__u32\tmap_type;\t/* one of enum bpf_map_type */\n@@ -210,6 +212,7 @@ union bpf_attr {\n \t\t__aligned_u64\tlog_buf;\t/* user supplied buffer */\n \t\t__u32\t\tkern_version;\t/* checked when prog_type=kprobe */\n \t\t__u32\t\tprog_flags;\n+\t\t__u8\t\tprog_name[BPF_OBJ_NAME_LEN];\n \t};\n \n \tstruct { /* anonymous struct used by BPF_OBJ_* commands */\n@@ -812,6 +815,11 @@ struct bpf_prog_info {\n \t__u32 xlated_prog_len;\n \t__aligned_u64 jited_prog_insns;\n \t__aligned_u64 xlated_prog_insns;\n+\t__u64 load_time;\t/* ns since boottime */\n+\t__u32 created_by_uid;\n+\t__u32 nr_map_ids;\n+\t__aligned_u64 map_ids;\n+\t__u8  name[BPF_OBJ_NAME_LEN];\n } __attribute__((aligned(8)));\n \n struct bpf_map_info {\ndiff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c\nindex 25d074920a00..45970df3f820 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -23,6 +23,9 @@\n #include <linux/version.h>\n #include <linux/kernel.h>\n #include <linux/idr.h>\n+#include <linux/cred.h>\n+#include <linux/timekeeping.h>\n+#include <linux/ctype.h>\n \n #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \\\n \t\t\t   (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \\\n@@ -312,6 +315,30 @@ int bpf_map_new_fd(struct bpf_map *map)\n \t\t   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \\\n \t\t   sizeof(attr->CMD##_LAST_FIELD)) != NULL\n \n+/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.\n+ * Return 0 on success and < 0 on error.\n+ */\n+static int bpf_obj_name_cpy(char *dst, const char *src)\n+{\n+\tconst char *end = src + BPF_OBJ_NAME_LEN;\n+\n+\t/* Copy all isalnum() and '_' char */\n+\twhile (src < end && *src) {\n+\t\tif (!isalnum(*src) && *src != '_')\n+\t\t\treturn -EINVAL;\n+\t\t*dst++ = *src++;\n+\t}\n+\n+\t/* No '\\0' found in BPF_OBJ_NAME_LEN number of bytes */\n+\tif (src == end)\n+\t\treturn -EINVAL;\n+\n+\t/* '\\0' terminates dst */\n+\t*dst = 0;\n+\n+\treturn 0;\n+}\n+\n #define BPF_MAP_CREATE_LAST_FIELD numa_node\n /* called via syscall */\n static int map_create(union bpf_attr *attr)\n@@ -973,7 +1000,7 @@ struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)\n EXPORT_SYMBOL_GPL(bpf_prog_get_type);\n \n /* last field in 'union bpf_attr' used by this command */\n-#define\tBPF_PROG_LOAD_LAST_FIELD prog_flags\n+#define\tBPF_PROG_LOAD_LAST_FIELD prog_name\n \n static int bpf_prog_load(union bpf_attr *attr)\n {\n@@ -1037,6 +1064,11 @@ static int bpf_prog_load(union bpf_attr *attr)\n \tif (err < 0)\n \t\tgoto free_prog;\n \n+\tprog->aux->load_time = ktime_get_boot_ns();\n+\terr = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);\n+\tif (err)\n+\t\tgoto free_prog;\n+\n \t/* run eBPF verifier */\n \terr = bpf_check(&prog, attr);\n \tif (err < 0)\n@@ -1358,8 +1390,25 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,\n \n \tinfo.type = prog->type;\n \tinfo.id = prog->aux->id;\n+\tinfo.load_time = prog->aux->load_time;\n+\tinfo.created_by_uid = from_kuid_munged(current_user_ns(),\n+\t\t\t\t\t       prog->aux->user->uid);\n \n \tmemcpy(info.tag, prog->tag, sizeof(prog->tag));\n+\tmemcpy(info.name, prog->aux->name, sizeof(prog->aux->name));\n+\n+\tulen = info.nr_map_ids;\n+\tinfo.nr_map_ids = prog->aux->used_map_cnt;\n+\tulen = min_t(u32, info.nr_map_ids, ulen);\n+\tif (ulen) {\n+\t\tu32 *user_map_ids = (u32 *)info.map_ids;\n+\t\tu32 i;\n+\n+\t\tfor (i = 0; i < ulen; i++)\n+\t\t\tif (put_user(prog->aux->used_maps[i]->id,\n+\t\t\t\t     &user_map_ids[i]))\n+\t\t\t\treturn -EFAULT;\n+\t}\n \n \tif (!capable(CAP_SYS_ADMIN)) {\n \t\tinfo.jited_prog_len = 0;\n","prefixes":["net-next","1/5"]}