From 22233765b926c2dfa9435aa10162424942ce093b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:04:39 +0100 Subject: [PATCH] Sys: mapRead and protect; open, openMode, openRead, create and openDir close on exec mapRead maps a file open only to read, protect sets the access of pages. The files open gives are now closed in a program exec starts, as pipe's are, and keepOnExec(fd, true) keeps one; spawn is unchanged since it closes all but its fds and more. No caller in lib, src, tools, examples or test relied on it. In both backends; tests in Sys.main, ExecOpened and ExecOpenedC. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index bf2c478..a7c3b02 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -16,7 +16,7 @@ | Module | What | |---|---| | `Rt` | ending the program, `exit`, `exitStatus` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `findEnv`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others, which takes what each C library fixes from `CAbi` | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `mapRead`, `protect`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `findEnv`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others, which takes what each C library fixes from `CAbi` | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | @@ -256,7 +256,7 @@ proc write*(fd: INT; s: []U8): INT; (* the bytes written *) proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) -proc open*(path: []U8; how: Accesses): INT; (* its fd *) +proc open*(path: []U8; how: Accesses): INT; (* its fd, closed on exec *) proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) proc openRead*(path: []U8): INT; (* open(path, {reading}) *) proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) @@ -272,8 +272,10 @@ proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *) -proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *) +proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not (as at first for a file made by open or pipe), in the program exec runs *) proc isTerminal*(fd: INT): BOOL; +proc mapRead*(fd, size: INT): ^U8; (* the first size bytes of fd, which may be open only to read, mapped read only; or nil *) +proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; (* the pages at p readable and writable as asked; neither, and touching them stops the program *) proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) proc munmap*(p: ^U8; size: INT); proc yield*; (* the processor given to another thread *) @@ -501,8 +503,10 @@ shell's `exec cmd` does, found and started as `spawn` would start it, `p.tty`, `p.group`, `p.foreground`, `p.fds` and `p.dir` included, except that it keeps this program's other open files: each stays open -in the program unless `keepOnExec(fd, false)` was said of it, as it is -of `pipe`'s, and those of `p.more` are kept. It returns only if the +in the program unless it is closed on exec, as the files `open`, +`pipe`, a socket or `memFile` gives are, unless `keepOnExec(fd, true)` +was said of them; `dup` and `dupTo` give files that stay open; and +those of `p.more` are kept. It returns only if the program can't run: -2 (ENOENT) or -13 (EACCES) if there is none to run, with nothing done; or, if the program is found but won't start, minus the errno, with what `p` asks of the terminal, the group, the @@ -533,11 +537,16 @@ `access` says whether this user, by its real ids, may read, write or execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` alone can't say; an empty `how` asks only whether it is there. -`open` refuses `executing`, with -22 (EINVAL). `nonblocking` opens a file without waiting: a fifo opened to write -waits for a reader and a fifo opened to read waits for a writer, but -with `nonblocking` the open returns at once (-6, ENXIO, for a write -with no reader), and a read or write that would wait is -11 later, as -`setBlocking(fd, false)` makes it. `umask` sets the bits +`open` refuses `executing`, with -22 (EINVAL). `nonblocking` opens a +file without waiting: a fifo opened to write waits for a reader and a +fifo opened to read waits for a writer, but with `nonblocking` the open +returns at once (-6, ENXIO, for a write with no reader), and a read or +write that would wait is -11 later, as `setBlocking(fd, false)` makes +it. A file `open`, `openMode`, `openRead` or `create` gives is closed in +a program `exec` or `execve` starts, as `pipe`'s are, so that a fifo or +a log one program holds doesn't reach every program it runs; +`keepOnExec(fd, true)` keeps one open, and `spawn` is unchanged, since +it passes its `fds` and `more` as before. `umask` sets the bits taken away from the mode of every file this program, and those it starts, make, and says what they were. `envAt` gives the environment one variable at a time, from 0, for a shell to list it and to pass it @@ -653,6 +662,15 @@ what one program writes there another sees at once: so a program hands an X server or a Wayland compositor its pixels, by `sendFds` of a `memFile` both map, rather than by copying them down a socket. +`mapFile` needs a file open to read and write; `mapRead` maps one that +is open only to read, a dictionary that isn't the user's to write, for +reading only: a write there stops the program. `protect` changes what +may be done with pages `mmap` gave, from a page boundary: read, write, +both, or neither, so that touching them stops the program, as a guard +page after a buffer does, or a reserve mapped in full and opened as it +fills. It takes the address as the system does, so with a C library it +is that library's whether an address not on a page boundary is -22 +(EINVAL) or rounded down. `readyOf` waits on several file descriptors at once, as a program does that listens to a server and a terminal both; a descriptor at its end, diff --git a/lib/C.raz b/lib/C.raz index 5ad2cdf..26f1d79 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -10,6 +10,7 @@ extern proc close*(fd: I32): I32; extern proc mmap*(addr: ^U8; n: UINT; prot, flags, fd: I32; off: I64): ^U8; extern proc munmap*(addr: ^U8; n: UINT): I32; +extern proc mprotect*(addr: ^U8; n: UINT; prot: I32): I32; extern proc isatty*(fd: I32): I32; extern proc exit*(status: I32); extern proc _exit*(status: I32); diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index f56711e..d9fa843 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -193,11 +193,11 @@ return true; end; -(* open's flags for how; Linux's numbers *) +(* open's flags for how, with O_CLOEXEC; Linux's numbers *) proc flags(how: Accesses): I32; var f: I32; begin - f := 0; (* O_RDONLY *) + f := 80000H; (* O_CLOEXEC, with O_RDONLY *) if writing in how then f := 1; (* O_WRONLY *) if reading in how then f := 2; end; (* O_RDWR *) @@ -210,9 +210,9 @@ return f; end; -(* The file at path, opened as how asks: its file descriptor, or minus - an errno. A file it creates has the permission bits mode, less - those the umask takes away. *) +(* The file at path, opened as how asks: its file descriptor, closed in + a program exec starts, or minus an errno. A file it creates has the + permission bits mode, less those the umask takes away. *) proc openMode*(path: []U8; how: Accesses; mode: INT): INT; var buf: [4096]U8; begin @@ -228,13 +228,15 @@ return openMode(path, how, 1A4H); (* 644 *) end; -(* The file at path, opened for reading. *) +(* The file at path, opened for reading, closed in a program exec + starts. *) proc openRead*(path: []U8): INT; begin return open(path, {reading}); end; -(* The file at path, made empty or new, opened for writing. *) +(* The file at path, made empty or new, opened for writing, closed in a + program exec starts. *) proc create*(path: []U8): INT; begin return open(path, {writing, creating, truncating}); @@ -337,7 +339,8 @@ end; (* Whether fd stays open in the program exec runs in this one's place: - yes, or no, closed then, as pipe's fds are at first. 0, or minus an + yes, or no, closed then, as the fds open, pipe and socket give are at + first. 0, or minus an errno. *) proc keepOnExec*(fd: INT; yes: BOOL): INT; var f: INT; @@ -388,6 +391,29 @@ return ^U8(p); end; +(* The first size bytes of the file fd, which may be open only to + read, mapped to be read only: writing there stops the program. nil + if it can't be; munmap gives them back. *) +proc mapRead*(fd, size: INT): ^U8; +var p: INT; +begin + p := SYSCALL(9, 0, size, 1, 2, fd, 0); (* mmap: read; MAP_PRIVATE *) + if p < 0 then return nil; end; + return ^U8(p); +end; + +(* Lets the pages from p, size bytes from a page boundary, be read and + written as read and write say; neither, and touching them stops the + program. 0, or minus an errno. *) +proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; +var prot: INT; +begin + prot := 0; (* PROT_NONE *) + if read then prot := 1; end; + if write then prot := prot | 2; end; + return SYSCALL(10, p, size, prot); (* mprotect *) +end; + (* Whether the file descriptor fd is a terminal: whether it has a terminal's settings to read (ioctl TCGETS). *) proc isTerminal*(fd: INT): BOOL; @@ -593,7 +619,7 @@ d.pos := 0; d.len := 0; if not cstr(path, a) then return -36; end; - d.fd := SYSCALL(2, ADR(a), 10000H); (* open, O_RDONLY, O_DIRECTORY *) + d.fd := SYSCALL(2, ADR(a), 90000H); (* open, O_RDONLY, O_DIRECTORY, O_CLOEXEC *) if d.fd < 0 then return d.fd; end; return 0; end; @@ -1770,7 +1796,11 @@ if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *) end; for i := 0 to 2 do - if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *) + if p.fds[i] = i then + _ := keepOnExec(i, true); (* dup2 of a file onto itself would leave it closed on exec *) + elsif p.fds[i] # 0 then + _ := SYSCALL(33, p.fds[i], i); (* dup2 *) + end; end; if LEN(p.dir) > 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *) return 0; @@ -1836,7 +1866,8 @@ (* Runs the program p describes in this one's place, as spawn would start it, but keeping this program's other fds as they are, open - unless keepOnExec said no; the fds of p.more are kept. It returns + unless closed on exec (as open and pipe give) and not kept by + keepOnExec; the fds of p.more are kept. It returns only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no program to run, before anything is done; or, once the program's terminal, group, fds and working directory are as p asks, which @@ -2398,6 +2429,33 @@ _ := remove("/tmp/raz-sys-fifo"); end; +(* The test of memory and files mapped: a file open only to read, mapped + to be read, and not to be written; and pages that change what may be + done with them. *) +proc testMaps; +var fd: INT; p, q: ^U8; +begin + fd := create("/tmp/raz-sys-map"); + if (fd < 0) or (write(fd, "hello") # 5) then exit(2); end; + close(fd); + fd := openRead("/tmp/raz-sys-map"); + if (fd < 0) or (mapFile(fd, 5) # nil) then exit(2); end; (* shared and writable: not for a file open to read *) + p := mapRead(fd, 5); + if (p = nil) or (p[0..5] # "hello") then exit(2); end; + munmap(p, 5); + close(fd); + _ := remove("/tmp/raz-sys-map"); + p := mmap(8192); + if p = nil then exit(2); end; + p[0] := 1; + q := p + 4096; + if (protect(q, 4096, true, false) # 0) or (q[0] # 0) then exit(2); end; + if protect(q, 4096, true, true) # 0 then exit(2); end; + q[0] := 2; + if (protect(p, 4096, false, false) # 0) or (protect(p, 4096, true, true) # 0) or (p[0] # 1) then exit(2); end; (* its bytes stay *) + munmap(p, 8192); +end; + (* The module's test: a file written, read back and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) @@ -2626,6 +2684,7 @@ testNet; testProcess; testServer; + testMaps; end; end Sys. diff --git a/lib/Sys.raz b/lib/Sys.raz index a8417b2..ef47ab3 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -234,11 +234,11 @@ return true; end; -(* open's flags for how *) +(* open's flags for how, with O_CLOEXEC *) proc flags(how: Accesses): I32; var f: I32; begin - f := CAbi.oRdonly; + f := CAbi.oRdonly | CAbi.oCloexec; if writing in how then f := CAbi.oWronly; if reading in how then f := CAbi.oRdwr; end; @@ -251,9 +251,9 @@ return f; end; -(* The file at path, opened as how asks: its file descriptor, or minus - an errno. A file it creates has the permission bits mode, less - those the umask takes away. *) +(* The file at path, opened as how asks: its file descriptor, closed in + a program exec starts, or minus an errno. A file it creates has the + permission bits mode, less those the umask takes away. *) proc openMode*(path: []U8; how: Accesses; mode: INT): INT; var buf: [4096]U8; r: INT; begin @@ -271,13 +271,15 @@ return openMode(path, how, 1A4H); (* 644 *) end; -(* The file at path, opened for reading. *) +(* The file at path, opened for reading, closed in a program exec + starts. *) proc openRead*(path: []U8): INT; begin return open(path, {reading}); end; -(* The file at path, made empty or new, opened for writing. *) +(* The file at path, made empty or new, opened for writing, closed in a + program exec starts. *) proc create*(path: []U8): INT; begin return open(path, {writing, creating, truncating}); @@ -408,7 +410,8 @@ end; (* Whether fd stays open in the program exec runs in this one's place: - yes, or no, closed then, as pipe's fds are at first. 0, or minus an + yes, or no, closed then, as the fds open, pipe and socket give are at + first. 0, or minus an errno. *) proc keepOnExec*(fd: INT; yes: BOOL): INT; var f: I32; @@ -463,6 +466,30 @@ return p; end; +(* The first size bytes of the file fd, which may be open only to + read, mapped to be read only: writing there stops the program. nil + if it can't be; munmap gives them back. *) +proc mapRead*(fd, size: INT): ^U8; +var p: ^U8; +begin + p := C.mmap(nil, UINT(size), CAbi.protRead, CAbi.mapPrivate, I32(fd), 0); + if INT(p) = -1 then return nil; end; + return p; +end; + +(* Lets the pages from p, size bytes from a page boundary, be read and + written as read and write say; neither, and touching them stops the + program. 0, or minus an errno. *) +proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; +var prot: I32; +begin + prot := 0; (* PROT_NONE *) + if read then prot := CAbi.protRead; end; + if write then prot := prot | CAbi.protWrite; end; + if C.mprotect(p, UINT(size), prot) # 0 then return failed(); end; + return 0; +end; + (* Renames old to new, replacing what new names: 0, or minus an errno. *) proc rename*(old, new: []U8): INT; var a, b: [4096]U8; @@ -1659,7 +1686,11 @@ if p.foreground then _ := C.tcsetpgrp(0, C.getpgrp()); end; end; for i := 0 to 2 do - if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end; + if p.fds[i] = i then + _ := keepOnExec(i, true); (* dup2 of a file onto itself would leave it closed on exec *) + elsif p.fds[i] # 0 then + _ := C.dup2(I32(p.fds[i]), I32(i)); + end; end; if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end; return 0; @@ -1738,7 +1769,8 @@ (* Runs the program p describes in this one's place, as spawn would start it, but keeping this program's other fds as they are, open - unless keepOnExec said no; the fds of p.more are kept. It returns + unless closed on exec (as open and pipe give) and not kept by + keepOnExec; the fds of p.more are kept. It returns only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no program to run, before anything is done; or, once the program's terminal, group, fds and working directory are as p asks, which @@ -2343,6 +2375,33 @@ _ := remove("/tmp/raz-sys-fifo"); end; +(* The test of memory and files mapped: a file open only to read, mapped + to be read, and not to be written; and pages that change what may be + done with them. *) +proc testMaps; +var fd: INT; p, q: ^U8; +begin + fd := create("/tmp/raz-sys-map"); + if (fd < 0) or (write(fd, "hello") # 5) then exit(2); end; + close(fd); + fd := openRead("/tmp/raz-sys-map"); + if (fd < 0) or (mapFile(fd, 5) # nil) then exit(2); end; (* shared and writable: not for a file open to read *) + p := mapRead(fd, 5); + if (p = nil) or (p[0..5] # "hello") then exit(2); end; + munmap(p, 5); + close(fd); + _ := remove("/tmp/raz-sys-map"); + p := mmap(8192); + if p = nil then exit(2); end; + p[0] := 1; + q := p + 4096; + if (protect(q, 4096, true, false) # 0) or (q[0] # 0) then exit(2); end; + if protect(q, 4096, true, true) # 0 then exit(2); end; + q[0] := 2; + if (protect(p, 4096, false, false) # 0) or (protect(p, 4096, true, true) # 0) or (p[0] # 1) then exit(2); end; (* its bytes stay *) + munmap(p, 8192); +end; + (* The module's test, as Linux's Sys has it: a file written, read back and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) @@ -2570,6 +2629,7 @@ testNet; testProcess; testServer; + testMaps; end; end Sys. diff --git a/test/libc/ExecOpenedC.expect b/test/libc/ExecOpenedC.expect new file mode 100644 index 0000000..02fce3d --- /dev/null +++ b/test/libc/ExecOpenedC.expect @@ -0,0 +1,2 @@ +closed 3 +open 4 diff --git a/test/libc/ExecOpenedC.raz b/test/libc/ExecOpenedC.raz new file mode 100644 index 0000000..6e2b496 --- /dev/null +++ b/test/libc/ExecOpenedC.raz @@ -0,0 +1,17 @@ +(* Over the C library: a file open gives is closed in the program exec + runs, unless keepOnExec says to keep it: of two opened files, sh sees + the second. *) +module ExecOpenedC; +import Sys; + +proc main*: INT; +var a, b: INT; +begin + a := Sys.openRead("/dev/null"); + b := Sys.create("/dev/null"); + if (a # 3) or (b # 4) or (Sys.keepOnExec(b, true) # 0) then return 1; end; + return Sys.exec({path: "sh", + args: ["sh", "-c", "for n in 3 4; do if [ -e /proc/self/fd/$n ]; then echo open $n; else echo closed $n; fi; done"]}); +end; + +end ExecOpenedC. diff --git a/test/run/ExecOpened.expect b/test/run/ExecOpened.expect new file mode 100644 index 0000000..02fce3d --- /dev/null +++ b/test/run/ExecOpened.expect @@ -0,0 +1,2 @@ +closed 3 +open 4 diff --git a/test/run/ExecOpened.raz b/test/run/ExecOpened.raz new file mode 100644 index 0000000..a59267e --- /dev/null +++ b/test/run/ExecOpened.raz @@ -0,0 +1,16 @@ +(* A file open gives is closed in the program exec runs, unless + keepOnExec says to keep it: of two opened files, sh sees the second. *) +module ExecOpened; +import Sys; + +proc main*: INT; +var a, b: INT; +begin + a := Sys.openRead("/dev/null"); + b := Sys.create("/dev/null"); + if (a # 3) or (b # 4) or (Sys.keepOnExec(b, true) # 0) then return 1; end; + return Sys.exec({path: "sh", + args: ["sh", "-c", "for n in 3 4; do if [ -e /proc/self/fd/$n ]; then echo open $n; else echo closed $n; fi; done"]}); +end; + +end ExecOpened.