Skip to content

Commit 558ddf8

Browse files
bpf: Introduce composable reg, ret and arg types.
jira VULN-140 pre-cve CVE-2022-23222 commit-author Hao Luo <[email protected]> commit d639b9d upstream-diff A merge confict arised because the function `bpf_free_kfunc_btf_tab` introduced in 2357672 ("bpf: Introduce BPF support for kernel module function calls") does not exist in our tree. 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: Pratham Patel <[email protected]>
1 parent f80970f commit 558ddf8

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
@@ -277,6 +277,29 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,
277277

278278
extern const struct bpf_map_ops bpf_map_offload_ops;
279279

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

327356
/* type of values returned from helper functions */
328357
enum bpf_return_type {
@@ -338,7 +367,14 @@ enum bpf_return_type {
338367
RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
339368
RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
340369
RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
370+
__BPF_RET_TYPE_MAX,
371+
372+
/* This must be the last entry. Its purpose is to ensure the enum is
373+
* wide enough to hold the higher bits reserved for bpf_type_flag.
374+
*/
375+
__BPF_RET_TYPE_LIMIT = BPF_TYPE_LIMIT,
341376
};
377+
static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
342378

343379
/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
344380
* to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
@@ -440,7 +476,13 @@ enum bpf_reg_type {
440476
PTR_TO_FUNC, /* reg points to a bpf program function */
441477
PTR_TO_MAP_KEY, /* reg points to a map element key */
442478
__BPF_REG_TYPE_MAX,
479+
480+
/* This must be the last entry. Its purpose is to ensure the enum is
481+
* wide enough to hold the higher bits reserved for bpf_type_flag.
482+
*/
483+
__BPF_REG_TYPE_LIMIT = BPF_TYPE_LIMIT,
443484
};
485+
static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
444486

445487
/* The information passed from prog-specific *_is_valid_access
446488
* back to the verifier.

include/linux/bpf_verifier.h

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

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

0 commit comments

Comments
 (0)