提交 7683e305 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

libbpf: Clarify kernel type use with USER variants of CORE reading macros

mainline inclusion
from mainline-5.12-rc1
commit e22d7f05
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e22d7f05e445165e58feddb4e40cc9c0f94453bc

-------------------------------------------------

Add comments clarifying that USER variants of CO-RE reading macro are still
only going to work with kernel types, defined in kernel or kernel module BTF.
This should help preventing invalid use of those macro to read user-defined
types (which doesn't work with CO-RE).
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210108194408.3468860-1-andrii@kernel.org
(cherry picked from commit e22d7f05)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 ac917ac9
...@@ -205,6 +205,7 @@ enum bpf_enum_value_kind { ...@@ -205,6 +205,7 @@ enum bpf_enum_value_kind {
#define bpf_core_read(dst, sz, src) \ #define bpf_core_read(dst, sz, src) \
bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src)) bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))
/* NOTE: see comments for BPF_CORE_READ_USER() about the proper types use. */
#define bpf_core_read_user(dst, sz, src) \ #define bpf_core_read_user(dst, sz, src) \
bpf_probe_read_user(dst, sz, (const void *)__builtin_preserve_access_index(src)) bpf_probe_read_user(dst, sz, (const void *)__builtin_preserve_access_index(src))
/* /*
...@@ -215,6 +216,7 @@ enum bpf_enum_value_kind { ...@@ -215,6 +216,7 @@ enum bpf_enum_value_kind {
#define bpf_core_read_str(dst, sz, src) \ #define bpf_core_read_str(dst, sz, src) \
bpf_probe_read_kernel_str(dst, sz, (const void *)__builtin_preserve_access_index(src)) bpf_probe_read_kernel_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
/* NOTE: see comments for BPF_CORE_READ_USER() about the proper types use. */
#define bpf_core_read_user_str(dst, sz, src) \ #define bpf_core_read_user_str(dst, sz, src) \
bpf_probe_read_user_str(dst, sz, (const void *)__builtin_preserve_access_index(src)) bpf_probe_read_user_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
...@@ -310,7 +312,11 @@ enum bpf_enum_value_kind { ...@@ -310,7 +312,11 @@ enum bpf_enum_value_kind {
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
}) })
/* Variant of BPF_CORE_READ_INTO() for reading from user-space memory */ /*
* Variant of BPF_CORE_READ_INTO() for reading from user-space memory.
*
* NOTE: see comments for BPF_CORE_READ_USER() about the proper types use.
*/
#define BPF_CORE_READ_USER_INTO(dst, src, a, ...) ({ \ #define BPF_CORE_READ_USER_INTO(dst, src, a, ...) ({ \
___core_read(bpf_core_read_user, bpf_core_read_user, \ ___core_read(bpf_core_read_user, bpf_core_read_user, \
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
...@@ -322,7 +328,11 @@ enum bpf_enum_value_kind { ...@@ -322,7 +328,11 @@ enum bpf_enum_value_kind {
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
}) })
/* Non-CO-RE variant of BPF_CORE_READ_USER_INTO() */ /* Non-CO-RE variant of BPF_CORE_READ_USER_INTO().
*
* As no CO-RE relocations are emitted, source types can be arbitrary and are
* not restricted to kernel types only.
*/
#define BPF_PROBE_READ_USER_INTO(dst, src, a, ...) ({ \ #define BPF_PROBE_READ_USER_INTO(dst, src, a, ...) ({ \
___core_read(bpf_probe_read_user, bpf_probe_read_user, \ ___core_read(bpf_probe_read_user, bpf_probe_read_user, \
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
...@@ -338,7 +348,11 @@ enum bpf_enum_value_kind { ...@@ -338,7 +348,11 @@ enum bpf_enum_value_kind {
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
}) })
/* Variant of BPF_CORE_READ_STR_INTO() for reading from user-space memory */ /*
* Variant of BPF_CORE_READ_STR_INTO() for reading from user-space memory.
*
* NOTE: see comments for BPF_CORE_READ_USER() about the proper types use.
*/
#define BPF_CORE_READ_USER_STR_INTO(dst, src, a, ...) ({ \ #define BPF_CORE_READ_USER_STR_INTO(dst, src, a, ...) ({ \
___core_read(bpf_core_read_user_str, bpf_core_read_user, \ ___core_read(bpf_core_read_user_str, bpf_core_read_user, \
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
...@@ -350,7 +364,12 @@ enum bpf_enum_value_kind { ...@@ -350,7 +364,12 @@ enum bpf_enum_value_kind {
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
}) })
/* Non-CO-RE variant of BPF_CORE_READ_USER_STR_INTO() */ /*
* Non-CO-RE variant of BPF_CORE_READ_USER_STR_INTO().
*
* As no CO-RE relocations are emitted, source types can be arbitrary and are
* not restricted to kernel types only.
*/
#define BPF_PROBE_READ_USER_STR_INTO(dst, src, a, ...) ({ \ #define BPF_PROBE_READ_USER_STR_INTO(dst, src, a, ...) ({ \
___core_read(bpf_probe_read_user_str, bpf_probe_read_user, \ ___core_read(bpf_probe_read_user_str, bpf_probe_read_user, \
dst, (src), a, ##__VA_ARGS__) \ dst, (src), a, ##__VA_ARGS__) \
...@@ -386,7 +405,16 @@ enum bpf_enum_value_kind { ...@@ -386,7 +405,16 @@ enum bpf_enum_value_kind {
__r; \ __r; \
}) })
/* Variant of BPF_CORE_READ() for reading from user-space memory */ /*
* Variant of BPF_CORE_READ() for reading from user-space memory.
*
* NOTE: all the source types involved are still *kernel types* and need to
* exist in kernel (or kernel module) BTF, otherwise CO-RE relocation will
* fail. Custom user types are not relocatable with CO-RE.
* The typical situation in which BPF_CORE_READ_USER() might be used is to
* read kernel UAPI types from the user-space memory passed in as a syscall
* input argument.
*/
#define BPF_CORE_READ_USER(src, a, ...) ({ \ #define BPF_CORE_READ_USER(src, a, ...) ({ \
___type((src), a, ##__VA_ARGS__) __r; \ ___type((src), a, ##__VA_ARGS__) __r; \
BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \ BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \
...@@ -400,7 +428,12 @@ enum bpf_enum_value_kind { ...@@ -400,7 +428,12 @@ enum bpf_enum_value_kind {
__r; \ __r; \
}) })
/* Non-CO-RE variant of BPF_CORE_READ_USER() */ /*
* Non-CO-RE variant of BPF_CORE_READ_USER().
*
* As no CO-RE relocations are emitted, source types can be arbitrary and are
* not restricted to kernel types only.
*/
#define BPF_PROBE_READ_USER(src, a, ...) ({ \ #define BPF_PROBE_READ_USER(src, a, ...) ({ \
___type((src), a, ##__VA_ARGS__) __r; \ ___type((src), a, ##__VA_ARGS__) __r; \
BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \ BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册