Skip to content

Commit 753521e

Browse files
committed
bpf: Introduce composable reg, ret and arg types.
jira VULN-136 cve-pre CVE-2022-0500 commit-author Hao Luo <[email protected]> commit d639b9d upstream-diff bpf_free_kfunc_btf_tab prototype doesn't exist in this kernel version so a minor conflict was fixed up while cherry picking There are some common properties shared between bpf reg, ret and arg values. For instance, a value may be a NULL pointer, or a pointer to a read-only memory. Previously, to express these properties, enumeration was used. For example, in order to test whether a reg value can be NULL, reg_type_may_be_null() simply enumerates all types that are possibly NULL. The problem of this approach is that it's not scalable and causes a lot of duplication. These properties can be combined, for example, a type could be either MAYBE_NULL or RDONLY, or both. This patch series rewrites the layout of reg_type, arg_type and ret_type, so that common properties can be extracted and represented as composable flag. For example, one can write ARG_PTR_TO_MEM | PTR_MAYBE_NULL which is equivalent to the previous ARG_PTR_TO_MEM_OR_NULL The type ARG_PTR_TO_MEM are called "base type" in this patch. Base types can be extended with flags. A flag occupies the higher bits while base types sits in the lower bits. This patch in particular sets up a set of macro for this purpose. The following patches will rewrite arg_types, ret_types and reg_types respectively. Signed-off-by: Hao Luo <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] (cherry picked from commit d639b9d) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent 4675fc2 commit 753521e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

include/linux/bpf.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,29 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,
279279

280280
extern const struct bpf_map_ops bpf_map_offload_ops;
281281

282+
/* bpf_type_flag contains a set of flags that are applicable to the values of
283+
* arg_type, ret_type and reg_type. For example, a pointer value may be null,
284+
* or a memory is read-only. We classify types into two categories: base types
285+
* and extended types. Extended types are base types combined with a type flag.
286+
*
287+
* Currently there are no more than 32 base types in arg_type, ret_type and
288+
* reg_types.
289+
*/
290+
#define BPF_BASE_TYPE_BITS 8
291+
292+
enum bpf_type_flag {
293+
/* PTR may be NULL. */
294+
PTR_MAYBE_NULL = BIT(0 + BPF_BASE_TYPE_BITS),
295+
296+
__BPF_TYPE_LAST_FLAG = PTR_MAYBE_NULL,
297+
};
298+
299+
/* Max number of base types. */
300+
#define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
301+
302+
/* Max number of all types. */
303+
#define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
304+
282305
/* function argument constraints */
283306
enum bpf_arg_type {
284307
ARG_DONTCARE = 0, /* unused argument in helper function */
@@ -324,7 +347,13 @@ enum bpf_arg_type {
324347
ARG_PTR_TO_STACK_OR_NULL, /* pointer to stack or NULL */
325348
ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
326349
__BPF_ARG_TYPE_MAX,
350+
351+
/* This must be the last entry. Its purpose is to ensure the enum is
352+
* wide enough to hold the higher bits reserved for bpf_type_flag.
353+
*/
354+
__BPF_ARG_TYPE_LIMIT = BPF_TYPE_LIMIT,
327355
};
356+
static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
328357

329358
/* type of values returned from helper functions */
330359
enum bpf_return_type {
@@ -340,7 +369,14 @@ enum bpf_return_type {
340369
RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
341370
RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
342371
RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
372+
__BPF_RET_TYPE_MAX,
373+
374+
/* This must be the last entry. Its purpose is to ensure the enum is
375+
* wide enough to hold the higher bits reserved for bpf_type_flag.
376+
*/
377+
__BPF_RET_TYPE_LIMIT = BPF_TYPE_LIMIT,
343378
};
379+
static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
344380

345381
/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
346382
* to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
@@ -442,7 +478,13 @@ enum bpf_reg_type {
442478
PTR_TO_FUNC, /* reg points to a bpf program function */
443479
PTR_TO_MAP_KEY, /* reg points to a map element key */
444480
__BPF_REG_TYPE_MAX,
481+
482+
/* This must be the last entry. Its purpose is to ensure the enum is
483+
* wide enough to hold the higher bits reserved for bpf_type_flag.
484+
*/
485+
__BPF_REG_TYPE_LIMIT = BPF_TYPE_LIMIT,
445486
};
487+
static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
446488

447489
/* The information passed from prog-specific *_is_valid_access
448490
* back to the verifier.

include/linux/bpf_verifier.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,18 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
513513
u32 btf_id,
514514
struct bpf_attach_target_info *tgt_info);
515515

516+
#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS - 1, 0)
517+
518+
/* extract base type from bpf_{arg, return, reg}_type. */
519+
static inline u32 base_type(u32 type)
520+
{
521+
return type & BPF_BASE_TYPE_MASK;
522+
}
523+
524+
/* extract flags from an extended type. See bpf_type_flag in bpf.h. */
525+
static inline u32 type_flag(u32 type)
526+
{
527+
return type & ~BPF_BASE_TYPE_MASK;
528+
}
529+
516530
#endif /* _LINUX_BPF_VERIFIER_H */

0 commit comments

Comments
 (0)