History

bpf: Add a ARG_PTR_TO_CONST_STR argument type

This type provides the guarantee that an argument is going to be a const pointer to somewhere in a read-only map value. It also checks that this pointer is followed by a zero character before the end of the map value.

Florent Revest committed 5 years ago

Commit fff13c41 parent d9c9e4d2 files · +42 −0Browse files

Showing the changes to kernel (1 of 2 files). Show all

Unified · Split

kernel/bpf/verifier.c+41−0
@@ -4787,6 +4787,7 @@ static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALU · 4786 unmodified lines
47874787 static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } };
47884788 static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } };
47894789 static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } };
4790+static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } };
47904791
47914792 static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
47924793 [ARG_PTR_TO_MAP_KEY] = &map_key_value_types,
@@ -4817,6 +4818,7 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { · 24 unmodified lines
48174818 [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types,
48184819 [ARG_PTR_TO_FUNC] = &func_ptr_types,
48194820 [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types,
4821+ [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types,
48204822 };
48214823
48224824 static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
@@ -5067,6 +5069,45 @@ skip_type_check: · 244 unmodified lines
50675069 if (err)
50685070 return err;
50695071 err = check_ptr_alignment(env, reg, 0, size, true);
5072+ } else if (arg_type == ARG_PTR_TO_CONST_STR) {
5073+ struct bpf_map *map = reg->map_ptr;
5074+ int map_off;
5075+ u64 map_addr;
5076+ char *str_ptr;
5077+
5078+ if (reg->type != PTR_TO_MAP_VALUE || !map ||
5079+ !bpf_map_is_rdonly(map)) {
5080+ verbose(env, "R%d does not point to a readonly map'\n", regno);
5081+ return -EACCES;
5082+ }
5083+
5084+ if (!tnum_is_const(reg->var_off)) {
5085+ verbose(env, "R%d is not a constant address'\n", regno);
5086+ return -EACCES;
5087+ }
5088+
5089+ if (!map->ops->map_direct_value_addr) {
5090+ verbose(env, "no direct value access support for this map type\n");
5091+ return -EACCES;
5092+ }
5093+
5094+ err = check_map_access(env, regno, reg->off,
5095+ map->value_size - reg->off, false);
5096+ if (err)
5097+ return err;
5098+
5099+ map_off = reg->off + reg->var_off.value;
5100+ err = map->ops->map_direct_value_addr(map, &map_addr, map_off);
5101+ if (err) {
5102+ verbose(env, "direct value access on string failed\n");
5103+ return err;
5104+ }
5105+
5106+ str_ptr = (char *)(long)(map_addr);
5107+ if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) {
5108+ verbose(env, "string is not zero-terminated\n");
5109+ return -EINVAL;
5110+ }
50705111 }
50715112
50725113 return err;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t