interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / spark / public-read.om

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