Skip to content

Commit b123480

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
docs/bpf: Document some special sdiv/smod operations
Patch [1] fixed possible kernel crash due to specific sdiv/smod operations in bpf program. The following are related operations and the expected results of those operations: - LLONG_MIN/-1 = LLONG_MIN - INT_MIN/-1 = INT_MIN - LLONG_MIN%-1 = 0 - INT_MIN%-1 = 0 Those operations are replaced with codes which won't cause kernel crash. This patch documents what operations may cause exception and what replacement operations are. [1] https://lore.kernel.org/all/[email protected]/ Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9138048 commit b123480

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Documentation/bpf/standardization/instruction-set.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,34 +324,42 @@ register.
324324

325325
.. table:: Arithmetic instructions
326326

327-
===== ===== ======= ==========================================================
327+
===== ===== ======= ===================================================================================
328328
name code offset description
329-
===== ===== ======= ==========================================================
329+
===== ===== ======= ===================================================================================
330330
ADD 0x0 0 dst += src
331331
SUB 0x1 0 dst -= src
332332
MUL 0x2 0 dst \*= src
333333
DIV 0x3 0 dst = (src != 0) ? (dst / src) : 0
334-
SDIV 0x3 1 dst = (src != 0) ? (dst s/ src) : 0
334+
SDIV 0x3 1 dst = (src == 0) ? 0 : ((src == -1 && dst == LLONG_MIN) ? LLONG_MIN : (dst s/ src))
335335
OR 0x4 0 dst \|= src
336336
AND 0x5 0 dst &= src
337337
LSH 0x6 0 dst <<= (src & mask)
338338
RSH 0x7 0 dst >>= (src & mask)
339339
NEG 0x8 0 dst = -dst
340340
MOD 0x9 0 dst = (src != 0) ? (dst % src) : dst
341-
SMOD 0x9 1 dst = (src != 0) ? (dst s% src) : dst
341+
SMOD 0x9 1 dst = (src == 0) ? dst : ((src == -1 && dst == LLONG_MIN) ? 0: (dst s% src))
342342
XOR 0xa 0 dst ^= src
343343
MOV 0xb 0 dst = src
344344
MOVSX 0xb 8/16/32 dst = (s8,s16,s32)src
345345
ARSH 0xc 0 :term:`sign extending<Sign Extend>` dst >>= (src & mask)
346346
END 0xd 0 byte swap operations (see `Byte swap instructions`_ below)
347-
===== ===== ======= ==========================================================
347+
===== ===== ======= ===================================================================================
348348

349349
Underflow and overflow are allowed during arithmetic operations, meaning
350350
the 64-bit or 32-bit value will wrap. If BPF program execution would
351351
result in division by zero, the destination register is instead set to zero.
352+
Otherwise, for ``ALU64``, if execution would result in ``LLONG_MIN``
353+
dividing -1, the desination register is instead set to ``LLONG_MIN``. For
354+
``ALU``, if execution would result in ``INT_MIN`` dividing -1, the
355+
desination register is instead set to ``INT_MIN``.
356+
352357
If execution would result in modulo by zero, for ``ALU64`` the value of
353358
the destination register is unchanged whereas for ``ALU`` the upper
354-
32 bits of the destination register are zeroed.
359+
32 bits of the destination register are zeroed. Otherwise, for ``ALU64``,
360+
if execution would resuslt in ``LLONG_MIN`` modulo -1, the destination
361+
register is instead set to 0. For ``ALU``, if execution would result in
362+
``INT_MIN`` modulo -1, the destination register is instead set to 0.
355363

356364
``{ADD, X, ALU}``, where 'code' = ``ADD``, 'source' = ``X``, and 'class' = ``ALU``, means::
357365

0 commit comments

Comments
 (0)