lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240718115153.1967859-2-yikai.lin@vivo.com>
Date: Thu, 18 Jul 2024 19:51:43 +0800
From: Lin Yikai <yikai.lin@...o.com>
To: Alexei Starovoitov <ast@...nel.org>,
	Daniel Borkmann <daniel@...earbox.net>,
	Andrii Nakryiko <andrii@...nel.org>,
	Martin KaFai Lau <martin.lau@...ux.dev>,
	Eduard Zingerman <eddyz87@...il.com>,
	Song Liu <song@...nel.org>,
	Yonghong Song <yonghong.song@...ux.dev>,
	John Fastabend <john.fastabend@...il.com>,
	KP Singh <kpsingh@...nel.org>,
	Stanislav Fomichev <sdf@...ichev.me>,
	Hao Luo <haoluo@...gle.com>,
	Jiri Olsa <jolsa@...nel.org>,
	Matt Bobrowski <mattbobrowski@...gle.com>,
	Steven Rostedt <rostedt@...dmis.org>,
	Masami Hiramatsu <mhiramat@...nel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
	Mykola Lysenko <mykolal@...com>,
	Shuah Khan <shuah@...nel.org>,
	Lin Yikai <yikai.lin@...o.com>,
	linux-kernel@...r.kernel.org,
	bpf@...r.kernel.org,
	linux-trace-kernel@...r.kernel.org,
	linux-kselftest@...r.kernel.org
Cc: opensource.kernel@...o.com
Subject: [PATCH bpf-next v1 1/3] bpf: Add bpf_file_d_path helper

Add the "bpf_file_d_path" helper function
to retrieve the path from a struct file object.
But there is no need to include vmlinux.h
or reference the definition of struct file.

Additionally, update the bpf.h tools uapi header.

Signed-off-by: Lin Yikai <yikai.lin@...o.com>
---
 include/uapi/linux/bpf.h       | 20 ++++++++++++++++++++
 kernel/trace/bpf_trace.c       | 34 ++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++++
 3 files changed, 74 insertions(+)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 35bcf52dbc65..7e5cec61a877 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5792,6 +5792,25 @@ union bpf_attr {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * long bpf_file_d_path(void *file, char *dst, u32 size)
+ *	Description
+ *		Return full path for the given *file* object.
+ *
+ *		In order to solve issues where certain eBPF programs can not include
+ *		the definition of struct file or vmlinux.h
+ *		due to their complexity and conflicts on some operating system,
+ *		the variable *file* here is declared as type void*
+ *		instead of the traditional struct file*.
+ *		It will be forcibly converted into type struct file* later.
+ *
+ *		If the path is larger than *size*, then only *size*
+ *		bytes will be copied to *dst*
+ *
+ *	Return
+ *		On success, the strictly positive length of the string,
+ *		including the trailing NULL character. On error, a negative
+ *		value.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -6006,6 +6025,7 @@ union bpf_attr {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(file_d_path, 212, ##ctx)			\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index cd098846e251..70fde7f20e97 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1257,6 +1257,38 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
 	.arg1_type	= ARG_PTR_TO_CTX,
 };
 
+BPF_CALL_3(bpf_file_d_path, void *, file, char*, dst, u32, size)
+{
+	long len;
+	struct file copy;
+	char *ptr;
+
+	if (!size)
+		return 0;
+
+	len = copy_from_kernel_nofault(&copy, (struct file *)file, sizeof(struct file));
+	if (len < 0)
+		return len;
+
+	ptr = d_path(&(copy.f_path), dst, size);
+	if (IS_ERR(ptr)) {
+		len = PTR_ERR(ptr);
+	} else {
+		len = dst + size - ptr;
+		memmove(dst, ptr, len);
+	}
+	return len;
+}
+
+const struct bpf_func_proto bpf_file_d_path_proto = {
+	.func		= bpf_file_d_path,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_ANYTHING,
+	.arg2_type	= ARG_PTR_TO_MEM,
+	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
+};
+
 #ifdef CONFIG_KEYS
 __bpf_kfunc_start_defs();
 
@@ -1629,6 +1661,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_find_vma_proto;
 	case BPF_FUNC_trace_vprintk:
 		return bpf_get_trace_vprintk_proto();
+	case BPF_FUNC_file_d_path:
+		return &bpf_file_d_path_proto;
 	default:
 		return bpf_base_func_proto(func_id, prog);
 	}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 35bcf52dbc65..7e5cec61a877 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5792,6 +5792,25 @@ union bpf_attr {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * long bpf_file_d_path(void *file, char *dst, u32 size)
+ *	Description
+ *		Return full path for the given *file* object.
+ *
+ *		In order to solve issues where certain eBPF programs can not include
+ *		the definition of struct file or vmlinux.h
+ *		due to their complexity and conflicts on some operating system,
+ *		the variable *file* here is declared as type void*
+ *		instead of the traditional struct file*.
+ *		It will be forcibly converted into type struct file* later.
+ *
+ *		If the path is larger than *size*, then only *size*
+ *		bytes will be copied to *dst*
+ *
+ *	Return
+ *		On success, the strictly positive length of the string,
+ *		including the trailing NULL character. On error, a negative
+ *		value.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -6006,6 +6025,7 @@ union bpf_attr {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(file_d_path, 212, ##ctx)			\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
-- 
2.34.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ