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
4787static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } };4787static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } };
4788static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } };4788static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } };
4789static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } };4789static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } };
4790static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } };
4790 4791
4791static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {4792static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
4792 [ARG_PTR_TO_MAP_KEY] = &map_key_value_types,4793 [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
4817 [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types,4818 [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types,
4818 [ARG_PTR_TO_FUNC] = &func_ptr_types,4819 [ARG_PTR_TO_FUNC] = &func_ptr_types,
4819 [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types,4820 [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types,
4821 [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types,
4820};4822};
4821 4823
4822static int check_reg_type(struct bpf_verifier_env *env, u32 regno,4824static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
@@ -5067,6 +5069,45 @@ skip_type_check: · 244 unmodified lines
5067 if (err)5069 if (err)
5068 return err;5070 return err;
5069 err = check_ptr_alignment(env, reg, 0, size, true);5071 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 }
5070 }5111 }
5071 5112
5072 return err;5113 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