Skip to content

Commit 792001f

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Add user-space variants of BPF_CORE_READ() family of macros
Add BPF_CORE_READ_USER(), BPF_CORE_READ_USER_STR() and their _INTO() variations to allow reading CO-RE-relocatable kernel data structures from the user-space. One of such cases is reading input arguments of syscalls, while reaping the benefits of CO-RE relocations w.r.t. handling 32/64 bit conversions and handling missing/new fields in UAPI data structs. Suggested-by: Gilad Reti <gilad.reti@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20201218235614.2284956-2-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 833d22f commit 792001f

File tree

1 file changed

+59
-39
lines changed

1 file changed

+59
-39
lines changed

tools/lib/bpf/bpf_core_read.h

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,20 @@ enum bpf_enum_value_kind {
195195
* (local) BTF, used to record relocation.
196196
*/
197197
#define bpf_core_read(dst, sz, src) \
198-
bpf_probe_read_kernel(dst, sz, \
199-
(const void *)__builtin_preserve_access_index(src))
198+
bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))
200199

200+
#define bpf_core_read_user(dst, sz, src) \
201+
bpf_probe_read_user(dst, sz, (const void *)__builtin_preserve_access_index(src))
201202
/*
202203
* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str()
203204
* additionally emitting BPF CO-RE field relocation for specified source
204205
* argument.
205206
*/
206207
#define bpf_core_read_str(dst, sz, src) \
207-
bpf_probe_read_kernel_str(dst, sz, \
208-
(const void *)__builtin_preserve_access_index(src))
208+
bpf_probe_read_kernel_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
209+
210+
#define bpf_core_read_user_str(dst, sz, src) \
211+
bpf_probe_read_user_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
209212

210213
#define ___concat(a, b) a ## b
211214
#define ___apply(fn, n) ___concat(fn, n)
@@ -264,51 +267,62 @@ enum bpf_enum_value_kind {
264267
read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)
265268

266269
/* "recursively" read a sequence of inner pointers using local __t var */
267-
#define ___rd_first(src, a) ___read(bpf_core_read, &__t, ___type(src), src, a);
268-
#define ___rd_last(...) \
269-
___read(bpf_core_read, &__t, \
270-
___type(___nolast(__VA_ARGS__)), __t, ___last(__VA_ARGS__));
271-
#define ___rd_p1(...) const void *__t; ___rd_first(__VA_ARGS__)
272-
#define ___rd_p2(...) ___rd_p1(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
273-
#define ___rd_p3(...) ___rd_p2(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
274-
#define ___rd_p4(...) ___rd_p3(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
275-
#define ___rd_p5(...) ___rd_p4(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
276-
#define ___rd_p6(...) ___rd_p5(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
277-
#define ___rd_p7(...) ___rd_p6(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
278-
#define ___rd_p8(...) ___rd_p7(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
279-
#define ___rd_p9(...) ___rd_p8(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__)
280-
#define ___read_ptrs(src, ...) \
281-
___apply(___rd_p, ___narg(__VA_ARGS__))(src, __VA_ARGS__)
282-
283-
#define ___core_read0(fn, dst, src, a) \
270+
#define ___rd_first(fn, src, a) ___read(fn, &__t, ___type(src), src, a);
271+
#define ___rd_last(fn, ...) \
272+
___read(fn, &__t, ___type(___nolast(__VA_ARGS__)), __t, ___last(__VA_ARGS__));
273+
#define ___rd_p1(fn, ...) const void *__t; ___rd_first(fn, __VA_ARGS__)
274+
#define ___rd_p2(fn, ...) ___rd_p1(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
275+
#define ___rd_p3(fn, ...) ___rd_p2(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
276+
#define ___rd_p4(fn, ...) ___rd_p3(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
277+
#define ___rd_p5(fn, ...) ___rd_p4(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
278+
#define ___rd_p6(fn, ...) ___rd_p5(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
279+
#define ___rd_p7(fn, ...) ___rd_p6(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
280+
#define ___rd_p8(fn, ...) ___rd_p7(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
281+
#define ___rd_p9(fn, ...) ___rd_p8(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
282+
#define ___read_ptrs(fn, src, ...) \
283+
___apply(___rd_p, ___narg(__VA_ARGS__))(fn, src, __VA_ARGS__)
284+
285+
#define ___core_read0(fn, fn_ptr, dst, src, a) \
284286
___read(fn, dst, ___type(src), src, a);
285-
#define ___core_readN(fn, dst, src, ...) \
286-
___read_ptrs(src, ___nolast(__VA_ARGS__)) \
287+
#define ___core_readN(fn, fn_ptr, dst, src, ...) \
288+
___read_ptrs(fn_ptr, src, ___nolast(__VA_ARGS__)) \
287289
___read(fn, dst, ___type(src, ___nolast(__VA_ARGS__)), __t, \
288290
___last(__VA_ARGS__));
289-
#define ___core_read(fn, dst, src, a, ...) \
290-
___apply(___core_read, ___empty(__VA_ARGS__))(fn, dst, \
291+
#define ___core_read(fn, fn_ptr, dst, src, a, ...) \
292+
___apply(___core_read, ___empty(__VA_ARGS__))(fn, fn_ptr, dst, \
291293
src, a, ##__VA_ARGS__)
292294

293295
/*
294296
* BPF_CORE_READ_INTO() is a more performance-conscious variant of
295297
* BPF_CORE_READ(), in which final field is read into user-provided storage.
296298
* See BPF_CORE_READ() below for more details on general usage.
297299
*/
298-
#define BPF_CORE_READ_INTO(dst, src, a, ...) \
299-
({ \
300-
___core_read(bpf_core_read, dst, (src), a, ##__VA_ARGS__) \
301-
})
300+
#define BPF_CORE_READ_INTO(dst, src, a, ...) ({ \
301+
___core_read(bpf_core_read, bpf_core_read, \
302+
dst, (src), a, ##__VA_ARGS__) \
303+
})
304+
305+
/* Variant of BPF_CORE_READ_INTO() for reading from user-space memory */
306+
#define BPF_CORE_READ_USER_INTO(dst, src, a, ...) ({ \
307+
___core_read(bpf_core_read_user, bpf_core_read_user, \
308+
dst, (src), a, ##__VA_ARGS__) \
309+
})
302310

303311
/*
304312
* BPF_CORE_READ_STR_INTO() does same "pointer chasing" as
305313
* BPF_CORE_READ() for intermediate pointers, but then executes (and returns
306314
* corresponding error code) bpf_core_read_str() for final string read.
307315
*/
308-
#define BPF_CORE_READ_STR_INTO(dst, src, a, ...) \
309-
({ \
310-
___core_read(bpf_core_read_str, dst, (src), a, ##__VA_ARGS__)\
311-
})
316+
#define BPF_CORE_READ_STR_INTO(dst, src, a, ...) ({ \
317+
___core_read(bpf_core_read_str, bpf_core_read, \
318+
dst, (src), a, ##__VA_ARGS__) \
319+
})
320+
321+
/* Variant of BPF_CORE_READ_STR_INTO() for reading from user-space memory */
322+
#define BPF_CORE_READ_USER_STR_INTO(dst, src, a, ...) ({ \
323+
___core_read(bpf_core_read_user_str, bpf_core_read_user, \
324+
dst, (src), a, ##__VA_ARGS__) \
325+
})
312326

313327
/*
314328
* BPF_CORE_READ() is used to simplify BPF CO-RE relocatable read, especially
@@ -334,12 +348,18 @@ enum bpf_enum_value_kind {
334348
* N.B. Only up to 9 "field accessors" are supported, which should be more
335349
* than enough for any practical purpose.
336350
*/
337-
#define BPF_CORE_READ(src, a, ...) \
338-
({ \
339-
___type((src), a, ##__VA_ARGS__) __r; \
340-
BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__); \
341-
__r; \
342-
})
351+
#define BPF_CORE_READ(src, a, ...) ({ \
352+
___type((src), a, ##__VA_ARGS__) __r; \
353+
BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__); \
354+
__r; \
355+
})
356+
357+
/* Variant of BPF_CORE_READ() for reading from user-space memory */
358+
#define BPF_CORE_READ_USER(src, a, ...) ({ \
359+
___type((src), a, ##__VA_ARGS__) __r; \
360+
BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \
361+
__r; \
362+
})
343363

344364
#endif
345365

0 commit comments

Comments
 (0)