54
54
#define DST regs[insn->dst_reg]
55
55
#define SRC regs[insn->src_reg]
56
56
#define FP regs[BPF_REG_FP]
57
+ #define AX regs[BPF_REG_AX]
57
58
#define ARG1 regs[BPF_REG_ARG1]
58
59
#define CTX regs[BPF_REG_CTX]
59
60
#define IMM insn->imm
@@ -857,6 +858,26 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,
857
858
BUILD_BUG_ON (BPF_REG_AX + 1 != MAX_BPF_JIT_REG );
858
859
BUILD_BUG_ON (MAX_BPF_REG + 1 != MAX_BPF_JIT_REG );
859
860
861
+ /* Constraints on AX register:
862
+ *
863
+ * AX register is inaccessible from user space. It is mapped in
864
+ * all JITs, and used here for constant blinding rewrites. It is
865
+ * typically "stateless" meaning its contents are only valid within
866
+ * the executed instruction, but not across several instructions.
867
+ * There are a few exceptions however which are further detailed
868
+ * below.
869
+ *
870
+ * Constant blinding is only used by JITs, not in the interpreter.
871
+ * The interpreter uses AX in some occasions as a local temporary
872
+ * register e.g. in DIV or MOD instructions.
873
+ *
874
+ * In restricted circumstances, the verifier can also use the AX
875
+ * register for rewrites as long as they do not interfere with
876
+ * the above cases!
877
+ */
878
+ if (from -> dst_reg == BPF_REG_AX || from -> src_reg == BPF_REG_AX )
879
+ goto out ;
880
+
860
881
if (from -> imm == 0 &&
861
882
(from -> code == (BPF_ALU | BPF_MOV | BPF_K ) ||
862
883
from -> code == (BPF_ALU64 | BPF_MOV | BPF_K ))) {
@@ -1188,7 +1209,6 @@ bool bpf_opcode_in_insntable(u8 code)
1188
1209
*/
1189
1210
static u64 ___bpf_prog_run (u64 * regs , const struct bpf_insn * insn , u64 * stack )
1190
1211
{
1191
- u64 tmp ;
1192
1212
#define BPF_INSN_2_LBL (x , y ) [BPF_##x | BPF_##y] = &&x##_##y
1193
1213
#define BPF_INSN_3_LBL (x , y , z ) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
1194
1214
static const void * jumptable [256 ] = {
@@ -1268,36 +1288,36 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
1268
1288
(* (s64 * ) & DST ) >>= IMM ;
1269
1289
CONT ;
1270
1290
ALU64_MOD_X :
1271
- div64_u64_rem (DST , SRC , & tmp );
1272
- DST = tmp ;
1291
+ div64_u64_rem (DST , SRC , & AX );
1292
+ DST = AX ;
1273
1293
CONT ;
1274
1294
ALU_MOD_X :
1275
- tmp = (u32 ) DST ;
1276
- DST = do_div (tmp , (u32 ) SRC );
1295
+ AX = (u32 ) DST ;
1296
+ DST = do_div (AX , (u32 ) SRC );
1277
1297
CONT ;
1278
1298
ALU64_MOD_K :
1279
- div64_u64_rem (DST , IMM , & tmp );
1280
- DST = tmp ;
1299
+ div64_u64_rem (DST , IMM , & AX );
1300
+ DST = AX ;
1281
1301
CONT ;
1282
1302
ALU_MOD_K :
1283
- tmp = (u32 ) DST ;
1284
- DST = do_div (tmp , (u32 ) IMM );
1303
+ AX = (u32 ) DST ;
1304
+ DST = do_div (AX , (u32 ) IMM );
1285
1305
CONT ;
1286
1306
ALU64_DIV_X :
1287
1307
DST = div64_u64 (DST , SRC );
1288
1308
CONT ;
1289
1309
ALU_DIV_X :
1290
- tmp = (u32 ) DST ;
1291
- do_div (tmp , (u32 ) SRC );
1292
- DST = (u32 ) tmp ;
1310
+ AX = (u32 ) DST ;
1311
+ do_div (AX , (u32 ) SRC );
1312
+ DST = (u32 ) AX ;
1293
1313
CONT ;
1294
1314
ALU64_DIV_K :
1295
1315
DST = div64_u64 (DST , IMM );
1296
1316
CONT ;
1297
1317
ALU_DIV_K :
1298
- tmp = (u32 ) DST ;
1299
- do_div (tmp , (u32 ) IMM );
1300
- DST = (u32 ) tmp ;
1318
+ AX = (u32 ) DST ;
1319
+ do_div (AX , (u32 ) IMM );
1320
+ DST = (u32 ) AX ;
1301
1321
CONT ;
1302
1322
ALU_END_TO_BE :
1303
1323
switch (IMM ) {
@@ -1553,7 +1573,7 @@ STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1553
1573
static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1554
1574
{ \
1555
1575
u64 stack[stack_size / sizeof(u64)]; \
1556
- u64 regs[MAX_BPF_REG ]; \
1576
+ u64 regs[MAX_BPF_EXT_REG ]; \
1557
1577
\
1558
1578
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1559
1579
ARG1 = (u64) (unsigned long) ctx; \
@@ -1566,7 +1586,7 @@ static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1566
1586
const struct bpf_insn *insn) \
1567
1587
{ \
1568
1588
u64 stack[stack_size / sizeof(u64)]; \
1569
- u64 regs[MAX_BPF_REG ]; \
1589
+ u64 regs[MAX_BPF_EXT_REG ]; \
1570
1590
\
1571
1591
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1572
1592
BPF_R1 = r1; \
0 commit comments