Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making Sense of Tristate Numbers (tnum)

Making Sense of Tristate Numbers (tnum)

Presented at Linux Plumbers Conference (LPC) 2024 in Vienna

shunghsiyu

May 26, 2025

Transcript

199 slides
  1. 1 7

  2. BPF Verifier & Safety Out-of-bound read? Address leakage? Infinite loop?

    Termination? Division-by-0? Out-of-bound write? Is pointer aligned? Invalid return value? Uninit stack? 17
  3. What’s the values being used? Out-of-bound read? Invalid return value?

    Infinite loop? Termination? Division-by-0? Uninit stack? Out-of-bound write? Is pointer aligned? 18
  4. /* i is some random number */ int i =

    bpf_get_prandom_u32(); /* mask must be 3 */ int mask = 3; /* i & mask can be 0, 1, 2, or 3 */ return i & mask; 19
  5. /* random number given as the * return value (register

    r0) */ call bpf_get_prandom_u32; /* mask stored in register r1*/ r1 = 3; r0 &= r1; /* (i & mask) kept in r0 */ ret; 21
  6. What’s the values being used? Out-of-bound read? Invalid return value?

    Infinite loop? Termination? Division-by-0? Uninit stack? Out-of-bound write? Is pointer aligned? 22
  7. What’s the value within registers? Out-of-bound read? Invalid return value?

    Infinite loop? Termination? Division-by-0? Uninit stack? Out-of-bound write? Is pointer aligned? 23
  8. Value Tracking Out-of-bound read? Invalid return value? Infinite loop? Termination?

    Division-by-0? Uninit stack? Out-of-bound write? Is pointer aligned? 24
  9. Attempt #1 - Naïve Approach /* Takes 2^31 GiB just

    to track a * single register */ struct values { char possibly_0 :1; char possibly_1 :1; char possibly_2 :1; ... } 26
  10. Attempt #1 - Naïve Approach /* Takes 2^31 GiB just

    to track a * single register */ struct values { char possibly_0 :1; char possibly_1 :1; char possibly_2 :1; ... } 27
  11. Attempt #1 - Naïve Approach struct values add_values(struct values *a,

    struct values *b) { if (a->possibly_0 && b->possibly_0) ret->possibly_0 = 1; if (a->possibly_0 && b->possibly_1) ret->possibly_1 = 1; if (a->possibly_1 && b->possibly_0) ret->possibly_1 = 1; /* Some bit-tricks would help, but ... */ 28
  12. struct values add_values(struct values *a, struct values *b) { if

    (a->possibly_0 && b->possibly_0) ret->possibly_0 = 1; if (a->possibly_0 && b->possibly_1) ret->possibly_1 = 1; if (a->possibly_1 && b->possibly_0) ret->possibly_1 = 1; /* Some bit-tricks would help, but ... */ Attempt #1 - Naïve Approach 29
  13. Attempt #2 - Ranges 32 struct values add_values(struct values *a,

    struct values *b) { /* Ignoring overflow for now */ ret->min = a->min + b->min; ret->max = a->max + b->max; }
  14. Attempt #2 - Ranges struct bpf_reg_state { struct tnum var_off;

    s64 smin_value; /* minimum possible (s64)value */ s64 smax_value; /* maximum possible (s64)value */ u64 umin_value; /* minimum possible (u64)value */ u64 umax_value; /* maximum possible (u64)value */ s32 s32_min_value; /* minimum possible (s32)value */ s32 s32_max_value; /* maximum possible (s32)value */ u32 u32_min_value; /* minimum possible (u32)value */ u32 u32_max_value; /* maximum possible (u32)value */ 34
  15. Attempt #2 - Ranges struct values xor_values(struct values *a, struct

    values *b) { } ain't nobody got time for that1 1: Okay, slightly mis-quoting here as the original actually refers to calculating signed-bounds in mul/and/or 37
  16. 39 Each bit in the register can have three possible

    states: - Unknown 🠂 x - Known to be set 🠂 1 - Known to be unset 🠂 0 Attempt #3 - Bitwise Pattern
  17. 40 Each bit in the register can have three possible

    states: - Unknown 🠂 x 🠂 {0, 1} - Known to be set 🠂 1 - Known to be unset 🠂 0 Attempt #3 - Bitwise Pattern
  18. 49 Abstract (how we represent such set of values) Concrete

    (actual values used in register) { 0b01, 0b11 } 0tx1
  19. 75 Abstract Concrete (ideal) { 1, 2 } 0txx {

    1, 2, 3, 4 } Concrete (actual)
  20. 76 Abstract Concrete (ideal) { 1, 2 } 0txx {

    1, 2, 3, 4 } Concrete (actual)
  21. 77 Abstract Concrete (ideal) { 1, 2 } 0txx {

    1, 2, 3, 4 } Concrete (actual) Fine
  22. 78 Abstract Concrete (ideal) { 1, 2 } 0txx {

    1, 2, 3, 4 } Concrete (actual)
  23. 79 Abstract Concrete (ideal) { 1, 2 } 0txx {

    1, 2, 3, 4 } Concrete (actual) Over-approximation (Static Analysis)
  24. 80 Abstract Concrete (ideal) { 1, 2 } 0txx {

    1, 2, 3, 4 } Concrete (actual) Fine
  25. Attempt #2 - Ranges struct bpf_reg_state { struct tnum var_off;

    s64 smin_value; /* minimum possible (s64)value */ s64 smax_value; /* maximum possible (s64)value */ u64 umin_value; /* minimum possible (u64)value */ u64 umax_value; /* maximum possible (u64)value */ s32 s32_min_value; /* minimum possible (s32)value */ s32 s32_max_value; /* maximum possible (s32)value */ u32 u32_min_value; /* minimum possible (u32)value */ u32 u32_max_value; /* maximum possible (u32)value */ 81
  26. 84 Abstract Concrete (ideal) { -1, 0 } 0tx…xx {

    0, 1, 2 .. 264-1 } Concrete (actual)
  27. 85 Abstract Concrete (ideal) { -1, 0 } 0tx…xx {

    0, 1, 2 .. 264-1 } Concrete (actual) Fine
  28. Attempt #2 - Ranges struct bpf_reg_state { struct tnum var_off;

    s64 smin_value; /* minimum possible (s64)value */ s64 smax_value; /* maximum possible (s64)value */ u64 umin_value; /* minimum possible (u64)value */ u64 umax_value; /* maximum possible (u64)value */ s32 s32_min_value; /* minimum possible (s32)value */ s32 s32_max_value; /* maximum possible (s32)value */ u32 u32_min_value; /* minimum possible (u32)value */ u32 u32_max_value; /* maximum possible (u32)value */ 86
  29. Can’t track nothing1 87 ∅ 1: We could make a

    currently unused and invalid representation of tnum (e.g. val = -1 && mask = -1) to mean an empty set, but might not be a good idea.
  30. /* assume this isn’t optimized out */ if (i <

    0 && i > 0) { /* never ever */ } 88
  31. /* assume this isn’t optimized out */ if (i <

    0 && i > 0) { /* */ } IMPOSSIBLE(*) to represent i 89
  32. /* compute branch direction of the expression "if * (<reg1>

    opcode <reg2>) goto target;" and return: * 1 - branch will be taken * 0 - branch will not be taken * -1 - unknown. Example: "if (reg1 < 5)" is unknown * when register value range [0,10] */ static int is_branch_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *reg2, u8 opcode, bool is_jmp32); 91
  33. static int is_scalar_branch_taken(...) { switch (opcode) { case BPF_JEQ: if

    (tnum_is_const(t1) && tnum_is_const(t2)) return t1.value == t2.value; ... return -1; ... 92
  34. int j = i - 1; /* int i is

    unknown */ if (i < 1 || i > 3) return; /* From here on 1 ≤ i ≤ 3 /* with j == i - 1 we know 0 ≤ j ≤ 2 */ if (j == 4) /* never ever */ 94
  35. struct bpf_reg_state { /* Upper bit of ID is used

    to remember relationship * between "linked" registers, e.g.: * r1 = r2; both will have r1->id == r2->id == N * r1 += 10; r1->id == N | BPF_ADD_CONST and * r1->off == 10 */ #define BPF_ADD_CONST (1U << 31) u32 id; ... 96
  36. 100 struct tnum { u64 value; /* whether bits are

    * set/unset, if known */ u64 mask; /* which bits are * unknown */ };
  37. 101 Each bit in the register can have three possible

    states: - Unknown 🠂 x - Known to be set 🠂 1 - Known to be unset 🠂 0
  38. 102 Each bit in the register can have three possible

    states: - Unknown 🠂 x - Known to be set 🠂 1 (mask[] = 0, value[] = 1) - Known to be unset 🠂 0
  39. 103 Each bit in the register can have three possible

    states: - Unknown 🠂 x - Known to be set 🠂 1 - Known to be unset 🠂 0 (mask[] = 0, value[] = 0)
  40. 104 Each bit in the register can have three possible

    states: - Unknown 🠂 x (mask[] = 1) - Known to be set 🠂 1 - Known to be unset 🠂 0
  41. 105 Each bit in the register can have three possible

    states: - Unknown 🠂 x (mask[] = 1, value[] = 0) - Known to be set 🠂 1 - Known to be unset 🠂 0
  42. 106 Each bit in the register can have three possible

    states: - Unknown 🠂 x - Known to be set 🠂 1 - Known to be unset 🠂 0 - Invalid 🠂 (mask[] = 1, value[] = 1)
  43. 127 & 0 1 0 0 & 0 0 &

    1 1 1 & 0 1 & 1
  44. 134 & 0 1 x 0 0 0 1 0

    1 x 0 {0, 1}
  45. 137 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x
  46. 138 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x ?
  47. 139 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x
  48. 140 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x {0, 1}
  49. 141 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x x
  50. 142 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x x
  51. 143 & 0 1 x 0 0 0 0 1

    0 1 x x 0 x x
  52. 144 & m=0 v=0 1 x m=0 v=0 m=0 v=0

    m=0 v=0 m=0 v=0 1 m=0 v=0 1 x x m=0 v=0 x x
  53. 145 & m=0 v=0 1 x m=0 v=0 m=0 v=0

    m=0 v=0 m=0 v=0 1 m=0 v=0 1 x x m=0 v=0 x x
  54. 146 & m=0 v=0 m=0 v=1 x m=0 v=0 m=0

    v=0 m=0 v=0 m=0 v=0 m=0 v=1 m=0 v=0 m=0 v=1 x x m=0 v=0 x x
  55. 147 & m=0 v=0 m=0 v=1 x m=0 v=0 m=0

    v=0 m=0 v=0 m=0 v=0 m=0 v=1 m=0 v=0 m=0 v=1 x x m=0 v=0 x x
  56. 148 & m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    m=0 v=0 m=0 v=0 m=0 v=0 m=0 v=1 m=0 v=0 m=0 v=1 m=1 v=0 m=1 v=0 m=0 v=0 m=1 v=0 m=1 v=0
  57. 149 & m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    v=0 v=0 v=0 m=0 v=1 v=0 v=1 v=0 m=1 v=0 v=0 v=0 v=0 & m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0 m=0 m=0 m=0 m=0 v=1 m=0 m=0 m=1 m=1 v=0 m=0 m=1 m=1
  58. 150 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0 0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  59. 151 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0 0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  60. 152 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0
  61. 153 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0
  62. 154 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0 value = a.value & b.value
  63. 155 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0 0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  64. 156 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  65. 157 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  66. 158 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  67. 159 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  68. 160 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1 mask = (a.value | a.mask) & (b.value | b.mask)
  69. 161 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1
  70. 162 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1 a.value & b.value
  71. 163 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1 mask = (a.value | a.mask) & (b.value | b.mask) & ~(a.value & b.value)
  72. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 164
  73. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 165
  74. 166 &.v m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 1 0 m=1 v=0 0 0 0 value = a.value & b.value
  75. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 167
  76. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 168
  77. 169 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1 mask = (a.value | a.mask) & (b.value | b.mask) & ~(a.value & b.value)
  78. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 170
  79. 171 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1 mask = (a.value | a.mask) & (b.value | b.mask) & ~(a.value & b.value)
  80. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 172
  81. 173 &.m m=0 v=0 m=0 v=1 m=1 v=0 m=0 v=0

    0 0 0 m=0 v=1 0 0 1 m=1 v=0 0 1 1 mask = (a.value | a.mask) & (b.value | b.mask) & ~(a.value & b.value)
  82. struct tnum tnum_and(struct tnum a, struct tnum b) { u64

    alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } 174
  83. /* Return @a with lowest @size bytes * retained, and

    all other bits set * to equal the sign bit (which might * be unknown). */ struct tnum tnum_scast(struct tnum a, u8 size) 176
  84. static void reg_bounds_sync(struct bpf_reg_state *reg) { /* tnum -> u64,

    s64, u32, s32 */ __update_reg_bounds(reg); /* u64 -> u32, s32; s64 -> u32, s32 * u64 -> s64; s64 -> u64 * u32 -> u64, s64; s32 -> u64, s64 */ __reg_deduce_bounds(reg); __reg_deduce_bounds(reg); /* 2nd time */ /* u64 -> tnum; u32 -> tnum */ __reg_bound_offset(reg); /* tnum -> u64, s64, u32, s32 */ __update_reg_bounds(reg); } 179
  85. static void __update_reg64_bounds(struct bpf_reg_state *reg) { /* min signed is

    max(sign bit) | min(other bits) */ reg->smin_value = max_t(s64, reg->smin_value, reg->var_off.value | (reg->var_off.mask & S64_MIN)); /* max signed is min(sign bit) | max(other bits) */ reg->smax_value = min_t(s64, reg->smax_value, reg->var_off.value | (reg->var_off.mask & S64_MAX)); reg->umin_value = max(reg->umin_value, reg->var_off.value); reg->umax_value = min(reg->umax_value, reg->var_off.value | reg->var_off.mask); } 180
  86. Sound, Precise, and Fast Abstract Interpretation with Tristate Numbers Harishankar

    Vishwanathan, Matan Shachnai, Srinivas Narayana, and Santosh Nagarakatte 188
  87. + + 189 Abstract Concrete { 1, 3 } 0t0x1

    { 3, 5 } 0txx1 { 4, 6, 8 } 0txx0 { 2, 4, 6, 8 }
  88. + + 190 Abstract Concrete { 1, 3 } 0t0x1

    { 3, 5 } 0txx1 { 4, 6, 8 } 0txx0 { 2, 4, 6, 8 }
  89. struct tnum dont_know(struct tnum a, struct tnum b) { /*

    Jon Snow knows nothing */ return tnum_unknown; } 192
  90. Tracks bit pattern - Simple (maybe not intuitively easy to

    understand) - Can’t track min/max/sign-crossing precisely Correct operation should - Not left any possible values out (i.e. sound) - Tries to exclude as much impossible values (i.e. precise) - without introducing unnecessary complexity 197
  91. • Sound, Precise, and Fast Abstract Interpretation with Tristate Numbers

    • Peeking into the BPF verifier • More than you want to know about BPF verifier • Value Tracking in BPF verifier • Model Checking (a very small part) of BPF Verifer 199

More Decks by shunghsiyu

Other Decks in Technology