Skip to content

Commit aee194b

Browse files
lukenelsAlexei Starovoitov
authored andcommitted
bpf, x86: Fix encoding for lower 8-bit registers in BPF_STX BPF_B
This patch fixes an encoding bug in emit_stx for BPF_B when the source register is BPF_REG_FP. The current implementation for BPF_STX BPF_B in emit_stx saves one REX byte when the operands can be encoded using Mod-R/M alone. The lower 8 bits of registers %rax, %rbx, %rcx, and %rdx can be accessed without using a REX prefix via %al, %bl, %cl, and %dl, respectively. Other registers, (e.g., %rsi, %rdi, %rbp, %rsp) require a REX prefix to use their 8-bit equivalents (%sil, %dil, %bpl, %spl). The current code checks if the source for BPF_STX BPF_B is BPF_REG_1 or BPF_REG_2 (which map to %rdi and %rsi), in which case it emits the required REX prefix. However, it misses the case when the source is BPF_REG_FP (mapped to %rbp). The result is that BPF_STX BPF_B with BPF_REG_FP as the source operand will read from register %ch instead of the correct %bpl. This patch fixes the problem by fixing and refactoring the check on which registers need the extra REX byte. Since no BPF registers map to %rsp, there is no need to handle %spl. Fixes: 6225827 ("net: filter: x86: internal BPF JIT") Signed-off-by: Xi Wang <[email protected]> Signed-off-by: Luke Nelson <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8ff3571 commit aee194b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ static bool is_ereg(u32 reg)
158158
BIT(BPF_REG_AX));
159159
}
160160

161+
/*
162+
* is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
163+
* lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
164+
* of encoding. al,cl,dl,bl have simpler encoding.
165+
*/
166+
static bool is_ereg_8l(u32 reg)
167+
{
168+
return is_ereg(reg) ||
169+
(1 << reg) & (BIT(BPF_REG_1) |
170+
BIT(BPF_REG_2) |
171+
BIT(BPF_REG_FP));
172+
}
173+
161174
static bool is_axreg(u32 reg)
162175
{
163176
return reg == BPF_REG_0;
@@ -598,9 +611,8 @@ static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
598611
switch (size) {
599612
case BPF_B:
600613
/* Emit 'mov byte ptr [rax + off], al' */
601-
if (is_ereg(dst_reg) || is_ereg(src_reg) ||
602-
/* We have to add extra byte for x86 SIL, DIL regs */
603-
src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
614+
if (is_ereg(dst_reg) || is_ereg_8l(src_reg))
615+
/* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
604616
EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
605617
else
606618
EMIT1(0x88);

0 commit comments

Comments
 (0)