ilo-safer: now use termios for terminal settings, additional error checking. Prevent further unveil() operation as well.

This commit is contained in:
crc 2024-05-29 17:41:28 +02:00
parent e92150f1f2
commit 021c617339

View file

@ -6,10 +6,12 @@
|_|_|\___/ ilo.c (c) charles childers |_|_|\___/ ilo.c (c) charles childers
**************************************************************/ **************************************************************/
#include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#define T ds[sp] /* Top of Data Stack */ #define T ds[sp] /* Top of Data Stack */
#define N ds[sp-1] /* Next on Data Stack */ #define N ds[sp-1] /* Next on Data Stack */
@ -34,6 +36,17 @@ C *blocks, /* name of block file (ilo.blocks) */
I a, b, f, s, d, l; I a, b, f, s, d, l;
C i[1]; C i[1];
struct termios ot, nt;
V it() {
tcgetattr(STDIN_FILENO, &ot);
nt = ot;
nt.c_lflag &=(~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &nt);
}
V rt() { tcsetattr(STDIN_FILENO, TCSANOW, &ot); }
V p(char *s) { while(*s) putchar(*s++); } V p(char *s) { while(*s) putchar(*s++); }
V e(char *s) { p("E:"); p(s); p("\n"); exit(1); } V e(char *s) { p("E:"); p(s); p("\n"); exit(1); }
V dso() { if ((sp + 1) > 32) { e("DSO"); } } V dso() { if ((sp + 1) > 32) { e("DSO"); } }
@ -46,13 +59,14 @@ V uli() { if (f == -1) { e("ULI"); } }
V usi() { if (f == -1) { e("URI"); } } V usi() { if (f == -1) { e("URI"); } }
V urb() { if (f == -1) { e("URB"); } } V urb() { if (f == -1) { e("URB"); } }
V uwb() { if (f == -1) { e("UWB"); } } V uwb() { if (f == -1) { e("UWB"); } }
V push(I v) { dso(); ds[sp + 1] = v; sp += 1; } V push(I v) { dso(); ds[sp + 1] = v; sp += 1; }
I pop() { dsu(); sp -= 1; return ds[sp + 1]; } I pop() { dsu(); sp -= 1; return ds[sp + 1]; }
V load_image() { V load_image() {
f = open(rom, O_RDONLY, 0666); f = open(rom, O_RDONLY, 0666);
uli(); uli();
read(f, &m, 65536 * 4); if (read(f, &m, 65536 * 4) < 0) { uli(); }
close(f); close(f);
ip = sp = rp = 0; ip = sp = rp = 0;
} }
@ -60,21 +74,21 @@ V load_image() {
V save_image() { V save_image() {
f = open(rom, O_WRONLY, 0666); f = open(rom, O_WRONLY, 0666);
usi(); usi();
write(f, &m, 65536 * 4); if (write(f, &m, 65536 * 4) < 0) { usi(); }
close(f); close(f);
} }
V block_common() { V block_common() {
b = pop(); /* block buffer */ b = pop(); /* block buffer */
a = pop(); /* block number */ a = pop(); /* block number */
lseek(f, 4096 * a, SEEK_SET); if (lseek(f, 4096 * a, SEEK_SET) < 0) { e("LSEEK"); };
} }
V read_block() { V read_block() {
f = open(blocks, O_RDONLY, 0666); f = open(blocks, O_RDONLY, 0666);
urb(); urb();
block_common(); block_common();
read(f, m + b, 4096); if (read(f, m + b, 4096) < 0) { urb(); }
close(f); close(f);
} }
@ -82,12 +96,16 @@ V write_block() {
f = open(blocks, O_WRONLY, 0666); f = open(blocks, O_WRONLY, 0666);
uwb(); uwb();
block_common(); block_common();
write(f, m + b, 4096); if (write(f, m + b, 4096) < 0) { uwb(); }
close(f); close(f);
} }
V save_ip() { rso(); rp += 1; R = ip; } V save_ip() { rso(); rp += 1; R = ip; }
V symmetric() { if (b >= 0 && N < 0) { T += 1; N -= b; } } V symmetric() { if (b >= 0 && N < 0) { T += 1; N -= b; } }
V emit() { if (write(1, &i, 1) < 0) { exit(1); }; }
V bs() { i[0] = '\b'; emit();
i[0] = ' '; emit();
i[0] = '\b'; emit(); }
V li() { ip += 1; push(m[ip]); } V li() { ip += 1; push(m[ip]); }
V du() { push(T); } V du() { push(T); }
@ -116,13 +134,19 @@ V or() { N = T | N; sp -= 1; }
V xo() { N = T ^ N; sp -= 1; } V xo() { N = T ^ N; sp -= 1; }
V sl() { N = N << T; sp -= 1; } V sl() { N = N << T; sp -= 1; }
V sr() { N = N >> T; sp -= 1; } V sr() { N = N >> T; sp -= 1; }
V cp() { l = pop(); d = pop(); s = T; T = -1; V cp() { size_t l = pop() * 4;
while (l) { if (m[d] != m[s]) { T = 0; } void *d = m + pop();
l -= 1; s += 1; d += 1; } } void *s = m + pop();
V cy() { l = pop(); d = pop(); s = pop(); push((memcmp(s, d, l) == 0) ? -1 : 0);
while (l) { m[d] = m[s]; l -= 1; s += 1; d += 1; } } }
V ioa() { i[0] = (char)pop(); write(1, &i, 1); } V cy() { size_t l = pop() * 4;
V iob() { read(0, &i, 1); push(i[0]); } void *d = m + pop();
void *s = m + pop();
memmove(d, s, l);
}
V ioa() { i[0] = (char)pop(); emit(); }
V iob() { if (read(0, &i, 1) < 0) { rt(); exit(1); }; push(i[0]);
(i[0] == 8 || i[0] == 127) ? bs() : emit(); }
V ioc() { read_block(); } V ioc() { read_block(); }
V iod() { write_block(); } V iod() { write_block(); }
V ioe() { save_image(); } V ioe() { save_image(); }
@ -183,7 +207,8 @@ V add_restrictions() {
#ifdef __OpenBSD__ #ifdef __OpenBSD__
unveil(blocks, "rw"); unveil(blocks, "rw");
unveil(rom, "rw"); unveil(rom, "rw");
pledge("stdio rpath wpath", NULL); unveil(NULL, NULL);
pledge("stdio rpath wpath tty", NULL);
#endif #endif
} }
@ -191,7 +216,6 @@ I main(I argc, C **argv) {
blocks = (argc > 1) ? argv[1] : "ilo.blocks"; blocks = (argc > 1) ? argv[1] : "ilo.blocks";
rom = (argc > 2) ? argv[2] : "ilo.rom"; rom = (argc > 2) ? argv[2] : "ilo.rom";
add_restrictions(); add_restrictions();
load_image(); it(); load_image(); execute(); rt();
execute();
return 0; return 0;
} }