Commit 764b4e0
committed
CBMC: Document and enforce 4 GiB limit of forall/exists quantifiers
Acknowledgements: Thanks to @nmouha for identifying this issue.
The forall/exists macros in cbmc.h declared the quantified variable as
`unsigned`, which on 64-bit platforms is 32 bits wide. When applied to
a size_t bound, this silently truncated the upper bound: the quantifier
only ranged over indices up to UINT32_MAX rather than the full size_t
range, leaving the predicate unchecked for any larger indices.
This was demonstrated by @nmouha in #1690
with a contrived bug in mlk_ct_cmov_zero that corrupts the buffer at
i == 2^32 yet still passes CBMC under the old macros. mlkem-native
itself never operates on buffers that large, so no real bug is masked,
but the quantifier definition was misleading and unsound for any future
use beyond UINT32_MAX.
Make the limitation explicit rather than widening the quantifier (which
prior experiments showed blows up SMT proof times for size_t-quantified
formulas):
- Declare the quantified variable as `uint32_t` (was `unsigned`).
- Explicitly cast the lower and upper bounds to `uint32_t`, so
passing a wider bound triggers CBMC's conversion check rather than
silently truncating.
- Tighten mlk_ct_cmov_zero's precondition from MLK_MAX_BUFFER_SIZE to
UINT32_MAX (the only size_t-bounded buffer in mlkem-native that
participates in a quantified contract).
- Change the loop counters in mlk_ct_memcmp and mlk_ct_cmov_zero to
`uint32_t` to make the index width explicit at the use site.
Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>1 parent 57d18f4 commit 764b4e0
2 files changed
Lines changed: 31 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
83 | 95 | | |
84 | 96 | | |
85 | 97 | | |
86 | 98 | | |
87 | 99 | | |
88 | 100 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
94 | 106 | | |
95 | 107 | | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
101 | 113 | | |
102 | 114 | | |
103 | 115 | | |
| |||
126 | 138 | | |
127 | 139 | | |
128 | 140 | | |
129 | | - | |
130 | | - | |
| 141 | + | |
| 142 | + | |
131 | 143 | | |
132 | 144 | | |
133 | 145 | | |
| |||
139 | 151 | | |
140 | 152 | | |
141 | 153 | | |
142 | | - | |
143 | | - | |
| 154 | + | |
| 155 | + | |
144 | 156 | | |
145 | 157 | | |
146 | 158 | | |
| |||
150 | 162 | | |
151 | 163 | | |
152 | 164 | | |
153 | | - | |
154 | | - | |
| 165 | + | |
| 166 | + | |
155 | 167 | | |
156 | 168 | | |
157 | 169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
336 | | - | |
| 336 | + | |
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
379 | | - | |
| 379 | + | |
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
385 | | - | |
| 385 | + | |
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
| |||
0 commit comments