omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / host / prlimit / prlimit.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/host/prlimit/prlimit.om: fixture for prlimit (keyword 235), program mode. Run by the OM-2 |
| 3 | note conductor through seed/host/prlimit/prlimit.cases from the repository root. Kernel truth is |
| 4 | note /proc/self/limits, read independently of the word under test. |
| 5 | note Checks (parent exit = number of the first failed check, 0 when all hold): |
| 6 | note 1 n 16 reads RLIMIT_NOFILE: 0, both u64 words written over a 0xAA poison, cur <= max. |
| 7 | note 2 the pair equals the "Max open files" soft/hard columns of /proc/self/limits. |
| 8 | note 3 n 32 sets soft 40 (hard kept): 0, and the old pair lands at A[off+16..off+32). |
| 9 | note 4 n 16 reads back {40, max}; /proc/self/limits shows soft 40. |
| 10 | note 5 the limit acts: opening files stops with -24 (EMFILE), every descriptor below 40. |
| 11 | note 6 n 32 restores the original pair; the old pair returned is {40, max}. |
| 12 | note 7 n 24, 8 and 0 return -22 without a kernel call: A[16..32) keeps its poison although A[0..16) |
| 13 | note holds a valid pair a set would accept. |
| 14 | note 8 res 99 returns -22 (kernel EINVAL) and the output slice keeps its poison. |
| 15 | note 9 pid 2147483647 returns -3 (ESRCH). |
| 16 | note 10 pid 1 returns -1 (EPERM) unless this process runs with euid 0. |
| 17 | note 11 n 16 reads RLIMIT_AS (9) equal to "Max address space" ("unlimited" is 2^64-1). |
| 18 | note 12 a slice past the array end traps 3 (child exit 103), for n 16 and for n 32. |
| 19 | note 16 a nonzero, unaligned off (9): n 16 writes the pair only to A[9..25), n 32 reads the new pair from |
| 20 | note A[9..25) and writes the old pair only to A[25..41); every other byte keeps its poison. |
| 21 | note Without an argument (AArch64 native) the RLIMIT_AS effect is checked across execprog; the x86linux |
| 22 | note row passes "noas" because qemu-user ignores RLIMIT_AS writes: |
| 23 | note 13 a child sets RLIMIT_AS soft 1 GiB and execprogs [argv0, "cap"]: the new image reads 1 GiB (exit 8). |
| 24 | note 14 a child sets RLIMIT_AS soft 64 MiB and execprogs [argv0, "big"]: the 128 MiB image cannot be |
| 25 | note mapped (execve ENOMEM after the point of no return), so the kernel kills it with SIGSEGV (signal 11). |
| 26 | note 15 control: [argv0, "big"] without a cap runs and exits 7. |
| 27 | om2 |
| 28 | text plim "/proc/self/limits" |
| 29 | text knof "Max open files" |
| 30 | text kas "Max address space" |
| 31 | text cmcap "cap" |
| 32 | text cmbig "big" |
| 33 | text cmnoas "noas" |
| 34 | global hugeimg ulong 16777216 |
| 35 | global a uchar 64 |
| 36 | global lim uchar 8192 |
| 37 | global pv ulong 2 |
| 38 | global argv uchar 4096 |
| 39 | global buf uchar 4096 |
| 40 | global status uchar 4 |
| 41 | global ofd ulong 64 |
| 42 | |
| 43 | fn rd64 off ulong to ulong |
| 44 | x = ulong 0 |
| 45 | for i ulong 8 |
| 46 | x = or x shl ulong get a add off i mul i 8 |
| 47 | end |
| 48 | ret x |
| 49 | end |
| 50 | |
| 51 | fn wr64 off ulong v ulong to void |
| 52 | for i ulong 8 |
| 53 | put a add off i uchar and shr v mul i 8 255 |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | fn poison to void |
| 58 | for i ulong len a |
| 59 | put a i 170 |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | note 1 when a[lo..hi) still holds the poison byte everywhere. |
| 64 | fn intact lo ulong hi ulong to ulong |
| 65 | for i ulong sub hi lo |
| 66 | if ne get a add lo i 170 |
| 67 | ret 0 |
| 68 | end |
| 69 | end |
| 70 | ret 1 |
| 71 | end |
| 72 | |
| 73 | note Read /proc/self/limits into lim; returns its length (0 on failure). |
| 74 | fn loadlim to ulong |
| 75 | fd = fopen plim 0 len plim 524288 0 |
| 76 | if lt fd 0 |
| 77 | ret 0 |
| 78 | end |
| 79 | n = read int fd lim 0 len lim |
| 80 | c = fclose int fd |
| 81 | if le n 0 |
| 82 | ret 0 |
| 83 | end |
| 84 | ret ulong n |
| 85 | end |
| 86 | |
| 87 | note Parse one column at p (skipping spaces): a decimal or "unlimited" (2^64-1) into pv[k]; returns |
| 88 | note the position after it, or 0 when neither is there. |
| 89 | fn col p ulong k ulong n ulong to ulong |
| 90 | q = p |
| 91 | while lt q n |
| 92 | if ne get lim q 32 |
| 93 | break |
| 94 | end |
| 95 | q = add q 1 |
| 96 | end |
| 97 | if ge q n |
| 98 | ret 0 |
| 99 | end |
| 100 | if eq get lim q 117 |
| 101 | put pv k not ulong 0 |
| 102 | ret add q 9 |
| 103 | end |
| 104 | v = ulong 0 |
| 105 | s = q |
| 106 | while lt q n |
| 107 | d = ulong get lim q |
| 108 | if or lt d 48 gt d 57 |
| 109 | break |
| 110 | end |
| 111 | v = add mul v 10 sub d 48 |
| 112 | q = add q 1 |
| 113 | end |
| 114 | if eq q s |
| 115 | ret 0 |
| 116 | end |
| 117 | put pv k v |
| 118 | ret q |
| 119 | end |
| 120 | |
| 121 | note Find the row named key and parse its soft/hard columns into pv; 1 on success. |
| 122 | fn row key view uchar * to ulong |
| 123 | n = loadlim |
| 124 | if eq n 0 |
| 125 | ret 0 |
| 126 | end |
| 127 | k = len key |
| 128 | i = ulong 0 |
| 129 | while le add i k n |
| 130 | hit = ulong 1 |
| 131 | for j ulong k |
| 132 | if ne get lim add i j get key j |
| 133 | hit = 0 |
| 134 | break |
| 135 | end |
| 136 | end |
| 137 | if eq hit 1 |
| 138 | p = col add i k 0 n |
| 139 | if eq p 0 |
| 140 | ret 0 |
| 141 | end |
| 142 | if eq col p 1 n 0 |
| 143 | ret 0 |
| 144 | end |
| 145 | ret 1 |
| 146 | end |
| 147 | i = add i 1 |
| 148 | end |
| 149 | ret 0 |
| 150 | end |
| 151 | |
| 152 | note 1 when argv[1] equals m. |
| 153 | fn argis m view uchar * to ulong |
| 154 | n = argread 1 buf 0 len buf |
| 155 | if ne n long len m |
| 156 | ret 0 |
| 157 | end |
| 158 | for i ulong len m |
| 159 | if ne get buf i get m i |
| 160 | ret 0 |
| 161 | end |
| 162 | end |
| 163 | ret 1 |
| 164 | end |
| 165 | |
| 166 | note argv packet [argv0, m]; returns its length. |
| 167 | fn mkargv m view uchar * to ulong |
| 168 | n = argread 0 buf 0 len buf |
| 169 | if le n 0 |
| 170 | exit 90 |
| 171 | end |
| 172 | k = ulong n |
| 173 | put argv 0 2 |
| 174 | put argv 1 0 |
| 175 | put argv 2 uchar and k 255 |
| 176 | put argv 3 uchar shr k 8 |
| 177 | for i ulong k |
| 178 | put argv add i 4 get buf i |
| 179 | end |
| 180 | pos = add k 4 |
| 181 | put argv pos uchar len m |
| 182 | put argv add pos 1 0 |
| 183 | for j ulong len m |
| 184 | put argv add add pos 2 j get m j |
| 185 | end |
| 186 | ret add add pos 2 len m |
| 187 | end |
| 188 | |
| 189 | fn reap pid long to ulong |
| 190 | r = waitpid int pid status 0 len status 0 |
| 191 | if ne r pid |
| 192 | ret 1000 |
| 193 | end |
| 194 | if ne get status 0 0 |
| 195 | ret add 2000 ulong and get status 0 127 |
| 196 | end |
| 197 | ret ulong get status 1 |
| 198 | end |
| 199 | |
| 200 | note A forked child sets RLIMIT_AS soft to cap (cap 0: no change) and execprogs [argv0, m]; returns |
| 201 | note the child's exit, or 2000 + signal. |
| 202 | fn capexec cap ulong m view uchar * to ulong |
| 203 | an = mkargv m |
| 204 | pid = fork |
| 205 | if lt pid 0 |
| 206 | ret 1001 |
| 207 | end |
| 208 | if eq pid 0 |
| 209 | if ne cap 0 |
| 210 | r = prlimit 0 9 a 0 16 |
| 211 | if ne r 0 |
| 212 | exit 96 |
| 213 | end |
| 214 | wr64 0 cap |
| 215 | r = prlimit 0 9 a 0 32 |
| 216 | if ne r 0 |
| 217 | exit 97 |
| 218 | end |
| 219 | end |
| 220 | r = execprog argv 0 an |
| 221 | exit 98 |
| 222 | end |
| 223 | ret reap pid |
| 224 | end |
| 225 | |
| 226 | note A forked child calls prlimit with an out-of-range slice of a (64 bytes); returns its exit. |
| 227 | fn trapkid off ulong n ulong to ulong |
| 228 | pid = fork |
| 229 | if lt pid 0 |
| 230 | ret 1001 |
| 231 | end |
| 232 | if eq pid 0 |
| 233 | r = prlimit 0 7 a off n |
| 234 | exit 98 |
| 235 | end |
| 236 | ret reap pid |
| 237 | end |
| 238 | |
| 239 | note Child modes: "cap" exits 8 when RLIMIT_AS soft reads 1 GiB (9 otherwise); "big" touches the |
| 240 | note last byte of the 128 MiB global and exits 7. |
| 241 | fn child to int |
| 242 | if eq argis cmcap 1 |
| 243 | r = prlimit 0 9 a 0 16 |
| 244 | if and eq r 0 eq rd64 0 1073741824 |
| 245 | ret 8 |
| 246 | end |
| 247 | ret 9 |
| 248 | end |
| 249 | put hugeimg 16777215 1 |
| 250 | ret int add 6 get hugeimg 16777215 |
| 251 | end |
| 252 | |
| 253 | note 1-6: read, compare, set, read back, effect, restore (RLIMIT_NOFILE). |
| 254 | fn nofile to int |
| 255 | poison |
| 256 | r = prlimit 0 7 a 0 16 |
| 257 | if or ne r 0 eq intact 0 16 1 |
| 258 | ret 1 |
| 259 | end |
| 260 | cur = rd64 0 |
| 261 | max = rd64 8 |
| 262 | if gt cur max |
| 263 | ret 1 |
| 264 | end |
| 265 | if eq row knof 0 |
| 266 | ret 2 |
| 267 | end |
| 268 | if or ne get pv 0 cur ne get pv 1 max |
| 269 | ret 2 |
| 270 | end |
| 271 | poison |
| 272 | wr64 0 40 |
| 273 | wr64 8 max |
| 274 | r = prlimit 0 7 a 0 32 |
| 275 | if or ne r 0 or ne rd64 16 cur ne rd64 24 max |
| 276 | ret 3 |
| 277 | end |
| 278 | poison |
| 279 | r = prlimit 0 7 a 0 16 |
| 280 | if or ne r 0 or ne rd64 0 40 ne rd64 8 max |
| 281 | ret 4 |
| 282 | end |
| 283 | if or eq row knof 0 ne get pv 0 40 |
| 284 | ret 4 |
| 285 | end |
| 286 | if ne fill 0 |
| 287 | ret 5 |
| 288 | end |
| 289 | poison |
| 290 | wr64 0 cur |
| 291 | wr64 8 max |
| 292 | r = prlimit 0 7 a 0 32 |
| 293 | if or ne r 0 or ne rd64 16 40 ne rd64 24 max |
| 294 | ret 6 |
| 295 | end |
| 296 | ret 0 |
| 297 | end |
| 298 | |
| 299 | note 5: open /proc/self/limits until refused; the refusal is -24 and every descriptor is below 40. |
| 300 | fn fill to int |
| 301 | got = ulong 0 |
| 302 | bad = ulong 1 |
| 303 | for i ulong 64 |
| 304 | fd = fopen plim 0 len plim 524288 0 |
| 305 | if lt fd 0 |
| 306 | if eq fd neg long 24 |
| 307 | bad = 0 |
| 308 | end |
| 309 | break |
| 310 | end |
| 311 | put ofd got ulong fd |
| 312 | got = add got 1 |
| 313 | if ge fd 40 |
| 314 | break |
| 315 | end |
| 316 | end |
| 317 | for j ulong got |
| 318 | if ge get ofd j 40 |
| 319 | bad = 1 |
| 320 | end |
| 321 | c = fclose int get ofd j |
| 322 | end |
| 323 | ret int bad |
| 324 | end |
| 325 | |
| 326 | note 7-12: refusals, errno and slice traps. |
| 327 | fn errs to int |
| 328 | for n ulong 3 |
| 329 | poison |
| 330 | r = prlimit 0 7 a 0 16 |
| 331 | w = ulong 24 |
| 332 | if eq n 1 |
| 333 | w = 8 |
| 334 | end |
| 335 | if eq n 2 |
| 336 | w = 0 |
| 337 | end |
| 338 | r = prlimit 0 7 a 0 w |
| 339 | if or ne r neg long 22 eq intact 16 32 0 |
| 340 | ret 7 |
| 341 | end |
| 342 | end |
| 343 | poison |
| 344 | r = prlimit 0 99 a 0 16 |
| 345 | if or ne r neg long 22 eq intact 0 16 0 |
| 346 | ret 8 |
| 347 | end |
| 348 | r = prlimit 2147483647 7 a 0 16 |
| 349 | if ne r neg long 3 |
| 350 | ret 9 |
| 351 | end |
| 352 | if ne geteuid 0 |
| 353 | r = prlimit 1 7 a 0 16 |
| 354 | if ne r neg long 1 |
| 355 | ret 10 |
| 356 | end |
| 357 | end |
| 358 | poison |
| 359 | r = prlimit 0 9 a 0 16 |
| 360 | if or ne r 0 eq row kas 0 |
| 361 | ret 11 |
| 362 | end |
| 363 | if or ne get pv 0 rd64 0 ne get pv 1 rd64 8 |
| 364 | ret 11 |
| 365 | end |
| 366 | if or ne trapkid 56 16 103 ne trapkid 40 32 103 |
| 367 | ret 12 |
| 368 | end |
| 369 | ret 0 |
| 370 | end |
| 371 | |
| 372 | note 16: the slice offset reaches the kernel (n 16 and n 32 at off 9, unaligned). |
| 373 | fn offs to int |
| 374 | poison |
| 375 | r = prlimit 0 7 a 0 16 |
| 376 | if ne r 0 |
| 377 | ret 16 |
| 378 | end |
| 379 | cur = rd64 0 |
| 380 | max = rd64 8 |
| 381 | poison |
| 382 | r = prlimit 0 7 a 9 16 |
| 383 | if or ne r 0 or ne rd64 9 cur ne rd64 17 max |
| 384 | ret 16 |
| 385 | end |
| 386 | if or eq intact 0 9 0 eq intact 25 64 0 |
| 387 | ret 16 |
| 388 | end |
| 389 | poison |
| 390 | wr64 9 cur |
| 391 | wr64 17 max |
| 392 | r = prlimit 0 7 a 9 32 |
| 393 | if or ne r 0 or ne rd64 25 cur ne rd64 33 max |
| 394 | ret 16 |
| 395 | end |
| 396 | if or eq intact 0 9 0 eq intact 41 64 0 |
| 397 | ret 16 |
| 398 | end |
| 399 | ret 0 |
| 400 | end |
| 401 | |
| 402 | fn main to int |
| 403 | if eq argcnt 2 |
| 404 | if eq argis cmnoas 0 |
| 405 | ret child |
| 406 | end |
| 407 | end |
| 408 | k = nofile |
| 409 | if ne k 0 |
| 410 | ret k |
| 411 | end |
| 412 | k = errs |
| 413 | if ne k 0 |
| 414 | ret k |
| 415 | end |
| 416 | k = offs |
| 417 | if ne k 0 |
| 418 | ret k |
| 419 | end |
| 420 | if eq argcnt 2 |
| 421 | ret 0 |
| 422 | end |
| 423 | if ne capexec 1073741824 cmcap 8 |
| 424 | ret 13 |
| 425 | end |
| 426 | if ne capexec 67108864 cmbig 2011 |
| 427 | ret 14 |
| 428 | end |
| 429 | if ne capexec 0 cmbig 7 |
| 430 | ret 15 |
| 431 | end |
| 432 | ret 0 |
| 433 | end |