interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / spark / public-read.om
| 1 | om2 |
| 2 | note Bounded public-file reader for the trusted supervisor. No writes to source paths. |
| 3 | note Walk an absolute path one component at a time, opening directories NOFOLLOW |
| 4 | note and using fchdir on the held descriptor. A renamed ancestor cannot redirect us. |
| 5 | note Linux ARM64 flags from asm/fcntl.h; stat mode from asm-generic/stat.h. |
| 6 | note stdout is empty on refusal. Exit 2 path, 3 open/type, 4 size/read, 5 output. |
| 7 | note Cap is 1048576 bytes. One extra byte detects growth beyond the cap. |
| 8 | global path uchar 4096 |
| 9 | global meta uchar 128 |
| 10 | global body uchar 1048577 |
| 11 | text root "/" |
| 12 | fn descend a ulong n ulong to bool |
| 13 | fd = fopen path a n or or int 16384 32768 524288 0 |
| 14 | if lt fd 0 |
| 15 | ret false |
| 16 | end |
| 17 | r = fchdir int fd |
| 18 | c = fclose int fd |
| 19 | ret and eq r 0 eq c 0 |
| 20 | end |
| 21 | fn parent n ulong to long |
| 22 | if ne get path 0 47 |
| 23 | ret -1 |
| 24 | end |
| 25 | r = chdir root 0 1 |
| 26 | if ne r 0 |
| 27 | ret -1 |
| 28 | end |
| 29 | a = ulong 1 |
| 30 | for i ulong n bound 4096 |
| 31 | if lt i a |
| 32 | continue |
| 33 | end |
| 34 | if eq get path i 0 |
| 35 | ret -1 |
| 36 | end |
| 37 | if eq get path i 47 |
| 38 | if eq i a |
| 39 | ret -1 |
| 40 | end |
| 41 | if eq get path a 46 |
| 42 | if eq sub i a 1 |
| 43 | ret -1 |
| 44 | end |
| 45 | if and eq sub i a 2 eq get path add a 1 46 |
| 46 | ret -1 |
| 47 | end |
| 48 | end |
| 49 | if not descend a sub i a |
| 50 | ret -1 |
| 51 | end |
| 52 | a = add i 1 |
| 53 | end |
| 54 | end |
| 55 | if ge a n |
| 56 | ret -1 |
| 57 | end |
| 58 | ret long a |
| 59 | end |
| 60 | fn capture fd int to long |
| 61 | if ne fstat fd meta 0 128 0 |
| 62 | ret -1 |
| 63 | end |
| 64 | mode = or uint get meta 16 shl uint get meta 17 8 |
| 65 | if ne and mode 61440 32768 |
| 66 | ret -1 |
| 67 | end |
| 68 | n = ulong 0 |
| 69 | while lt n len body bound 1048577 |
| 70 | r = read fd body n sub len body n |
| 71 | if lt r 0 |
| 72 | if eq r -4 |
| 73 | continue |
| 74 | end |
| 75 | ret -1 |
| 76 | end |
| 77 | if eq r 0 |
| 78 | ret long n |
| 79 | end |
| 80 | n = add n ulong r |
| 81 | end |
| 82 | ret -1 |
| 83 | end |
| 84 | fn output n ulong to int |
| 85 | p = ulong 0 |
| 86 | while lt p n bound 1048577 |
| 87 | r = write 1 body p sub n p |
| 88 | if le r 0 |
| 89 | if eq r -4 |
| 90 | continue |
| 91 | end |
| 92 | ret 5 |
| 93 | end |
| 94 | p = add p ulong r |
| 95 | end |
| 96 | ret 0 |
| 97 | end |
| 98 | fn main to int |
| 99 | if ne argcnt 2 |
| 100 | ret 2 |
| 101 | end |
| 102 | sz = argread 1 path 0 len path |
| 103 | if or le sz 1 ge sz long len path |
| 104 | ret 2 |
| 105 | end |
| 106 | a = parent ulong sz |
| 107 | if lt a 0 |
| 108 | ret 2 |
| 109 | end |
| 110 | fd = fopen path ulong a sub ulong sz ulong a 559104 0 |
| 111 | if lt fd 0 |
| 112 | ret 3 |
| 113 | end |
| 114 | n = capture int fd |
| 115 | r = fclose int fd |
| 116 | if or lt n 0 ne r 0 |
| 117 | ret 4 |
| 118 | end |
| 119 | ret output ulong n |
| 120 | end |