History

bpf: Simplify alu_limit masking for pointer arithmetic

Instead of having the mov32 with aux->alu_limit - 1 immediate, move this operation to retrieve_ptr_limit() instead to simplify the logic and to allow for subsequent sanity boundary checks inside retrieve_ptr_limit(). This avoids in future that at the time of the verifier masking rewrite we'd run into an underflow which would not sign extend due to the nature of mov32 instruction.

Piotr Krysiuk committed 5 years ago

Commit b5871dc1 parent 10d2bb21 file · +5 −5Browse files

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

Unified · Split

kernel/bpf/verifier.c+5−5
@@ -5870,16 +5870,16 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, · 5869 unmodified lines
5870 */5870 */
5871 off = ptr_reg->off + ptr_reg->var_off.value;5871 off = ptr_reg->off + ptr_reg->var_off.value;
5872 if (mask_to_left)5872 if (mask_to_left)
5873 *ptr_limit = MAX_BPF_STACK + off + 1;5873 *ptr_limit = MAX_BPF_STACK + off;
5874 else5874 else
5875 *ptr_limit = -off;5875 *ptr_limit = -off - 1;
5876 return 0;5876 return 0;
5877 case PTR_TO_MAP_VALUE:5877 case PTR_TO_MAP_VALUE:
5878 if (mask_to_left) {5878 if (mask_to_left) {
5879 *ptr_limit = ptr_reg->umax_value + ptr_reg->off + 1;5879 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
5880 } else {5880 } else {
5881 off = ptr_reg->smin_value + ptr_reg->off;5881 off = ptr_reg->smin_value + ptr_reg->off;
5882 *ptr_limit = ptr_reg->map_ptr->value_size - off;5882 *ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
5883 }5883 }
5884 return 0;5884 return 0;
5885 default:5885 default:
@@ -11668,7 +11668,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) · 5782 unmodified lines
11668 off_reg = issrc ? insn->src_reg : insn->dst_reg;11668 off_reg = issrc ? insn->src_reg : insn->dst_reg;
11669 if (isneg)11669 if (isneg)
11670 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);11670 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
11671 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);11671 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit);
11672 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);11672 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
11673 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);11673 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
11674 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);11674 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);

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