History

bpf: Rework ptr_limit into alu_limit and add common error path

Small refactor with no semantic changes in order to consolidate the max ptr_limit boundary check.

Daniel Borkmann committed 5 years ago

Commit b658bbb1 parent 24c109b1 file · +13 −8Browse files

Unified · Split

kernel/bpf/verifier.c+13−8
@@ -5858,12 +5858,12 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) · 5857 unmodified lines
58585858
58595859 static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
58605860 const struct bpf_reg_state *off_reg,
5861− u32 *ptr_limit, u8 opcode)
5861+ u32 *alu_limit, u8 opcode)
58625862 {
58635863 bool off_is_neg = off_reg->smin_value < 0;
58645864 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
58655865 (opcode == BPF_SUB && !off_is_neg);
5866− u32 off, max;
5866+ u32 off, max = 0, ptr_limit = 0;
58675867
58685868 if (!tnum_is_const(off_reg->var_off) &&
58695869 (off_reg->smin_value < 0) != (off_reg->smax_value < 0))
@@ -5880,22 +5880,27 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, · 10 unmodified lines
58805880 */
58815881 off = ptr_reg->off + ptr_reg->var_off.value;
58825882 if (mask_to_left)
5883− *ptr_limit = MAX_BPF_STACK + off;
5883+ ptr_limit = MAX_BPF_STACK + off;
58845884 else
5885− *ptr_limit = -off - 1;
5886− return *ptr_limit >= max ? -ERANGE : 0;
5885+ ptr_limit = -off - 1;
5886+ break;
58875887 case PTR_TO_MAP_VALUE:
58885888 max = ptr_reg->map_ptr->value_size;
58895889 if (mask_to_left) {
5890− *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
5890+ ptr_limit = ptr_reg->umax_value + ptr_reg->off;
58915891 } else {
58925892 off = ptr_reg->smin_value + ptr_reg->off;
5893− *ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
5893+ ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
58945894 }
5895− return *ptr_limit >= max ? -ERANGE : 0;
5895+ break;
58965896 default:
58975897 return -EINVAL;
58985898 }
5899+
5900+ if (ptr_limit >= max)
5901+ return -ERANGE;
5902+ *alu_limit = ptr_limit;
5903+ return 0;
58995904 }
59005905
59015906 static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,

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