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

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
58705870 */
58715871 off = ptr_reg->off + ptr_reg->var_off.value;
58725872 if (mask_to_left)
5873− *ptr_limit = MAX_BPF_STACK + off + 1;
5873+ *ptr_limit = MAX_BPF_STACK + off;
58745874 else
5875− *ptr_limit = -off;
5875+ *ptr_limit = -off - 1;
58765876 return 0;
58775877 case PTR_TO_MAP_VALUE:
58785878 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;
58805880 } else {
58815881 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;
58835883 }
58845884 return 0;
58855885 default:
@@ -11668,7 +11668,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) · 5782 unmodified lines
1166811668 off_reg = issrc ? insn->src_reg : insn->dst_reg;
1166911669 if (isneg)
1167011670 *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);
1167211672 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
1167311673 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
1167411674 *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