From: Zwb <ethercflow@gmail.com>
To: netdev@vger.kernel.org
Cc: yhs@fb.com, Zwb <ethercflow@gmail.com>
Subject: [PATCH bpf-next] bpf: add new helper fd2path for mapping a file descriptor to a pathname
Date: Thu, 17 Oct 2019 05:26:31 -0400 [thread overview]
Message-ID: <20191017092631.3739-1-ethercflow@gmail.com> (raw)
When people want to identify which file system files are being opened,
read, and written to, they can use this helper with file descriptor as
input to achieve this goal. Other pseudo filesystems are also supported.
Signed-off-by: Zwb <ethercflow@gmail.com>
---
include/linux/bpf.h | 1 +
include/uapi/linux/bpf.h | 1 +
kernel/bpf/core.c | 1 +
kernel/bpf/helpers.c | 39 ++++++++++++++++++++++++++++++++++
kernel/trace/bpf_trace.c | 2 ++
tools/include/uapi/linux/bpf.h | 1 +
6 files changed, 45 insertions(+)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 282e28bf41ec..c0a710cf2c88 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_fd2path_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 a65c3b0c6935..a4a5d432e572 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -2769,6 +2769,7 @@ union bpf_attr {
FN(get_current_pid_tgid), \
FN(get_current_uid_gid), \
FN(get_current_comm), \
+ FN(fd2path), \
FN(get_cgroup_classid), \
FN(skb_vlan_push), \
FN(skb_vlan_pop), \
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 66088a9e9b9e..349a8b1be232 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2042,6 +2042,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_fd2path_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 5e28718928ca..0832536c7ddb 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -487,3 +487,42 @@ const struct bpf_func_proto bpf_strtoul_proto = {
.arg4_type = ARG_PTR_TO_LONG,
};
#endif
+
+BPF_CALL_3(bpf_fd2path, char *, dst, u32, size, int, fd)
+{
+ struct fd f;
+ int ret;
+ char *p;
+
+ ret = security_locked_down(LOCKDOWN_BPF_READ);
+ if (ret < 0)
+ goto out;
+
+ f = fdget_raw(fd);
+ if (!f.file)
+ goto out;
+
+ p = d_path(&f.file->f_path, dst, size);
+ if (IS_ERR_OR_NULL(p))
+ ret = PTR_ERR(p);
+ else {
+ ret = strlen(p);
+ memmove(dst, p, ret);
+ dst[ret] = 0;
+ }
+
+ if (unlikely(ret < 0))
+out:
+ memset(dst, '0', size);
+
+ return ret;
+}
+
+const struct bpf_func_proto bpf_fd2path_proto = {
+ .func = bpf_fd2path,
+ .gpl_only = true,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_UNINIT_MEM,
+ .arg2_type = ARG_CONST_SIZE,
+ .arg3_type = ARG_ANYTHING,
+};
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 44bd08f2443b..0ca7fdefb8e5 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -735,6 +735,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_fd2path:
+ return &bpf_fd2path_proto;
default:
return NULL;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index a65c3b0c6935..a4a5d432e572 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -2769,6 +2769,7 @@ union bpf_attr {
FN(get_current_pid_tgid), \
FN(get_current_uid_gid), \
FN(get_current_comm), \
+ FN(fd2path), \
FN(get_cgroup_classid), \
FN(skb_vlan_push), \
FN(skb_vlan_pop), \
--
2.17.1
next reply other threads:[~2019-10-17 9:26 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-17 9:26 Zwb [this message]
2019-10-17 14:49 ` [PATCH bpf-next] bpf: add new helper fd2path for mapping a file descriptor to a pathname Daniel Borkmann
2019-10-18 16:07 ` Yonghong Song
-- strict thread matches above, loose matches on Subject: below --
2019-10-18 10:47 Wenbo Zhang
2019-10-18 18:20 ` Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191017092631.3739-1-ethercflow@gmail.com \
--to=ethercflow@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).