Artifact 648320139668cf4eef1011fcfa43f69559df2a79664413e00bdd562fadf87b45:
- File
src/platform.h
— part of check-in
[27d847f693]
at
2026-11-18 16:19:19
on branch trunk
— suit syncs and clones over https
suit sync, suit clone and suit tend now work with https:// remotes. http:// and file:// keep libfossil's built-in channel.
libfossil is the private byot build 2f20f44ba4 in place of trunk f2983dcd78, approved while suit is being proven; only libfossil.c and libfossil.h differ from trunk's files. Its stream channel does all of the HTTP: request headers, Content-Length, chunked and close-delimited replies (the last only after a clean close), the reply-size limit and the timeout. suit supplies the transport: src/tls.c, over non-blocking TCP and BearSSL 0.6, now vendored.
- TLS 1.2 only, ECDHE key exchange, AES-GCM or ChaCha20-Poly1305. A server offering only TLS 1.3, RSA key exchange or CBC is refused. - The chain is verified against trust anchors and the host name against the certificate. Anchors come from the system's PEM bundle read at run time (SSL_CERT_FILE, then the usual macOS and Linux paths), or from a per-wardrobe CA file set with --ca, which replaces the bundle. No roots are compiled in and there is no way to trust anything. - A certificate file must be a regular file owned by the user or root, writable by no one else, in a folder that passes the same check; --ca refuses a symlink, and a FIFO is refused at once. - Redirects are refused, never followed, and the message names the new address. No proxy support. - Nothing is sent before the handshake has verified. The login goes out only as libfossil's login card. Commands that read the sync secret turn core files off first. - Every connect, read, write and name lookup waits in poll with a 30 s idle limit, and a whole command stops after 10 minutes. getaddrinfo has no deadline, so it runs in a forked child that is killed and reaped. - BearSSL makes no system call of its own: it is built without its own randomness or clock, and suit seeds it and gives it the time through its fault wrappers. - Exit 18 now means the secure connection failed (handshake, certificate, host name). 17 stays the network and 19 a refused login.
A per-wardrobe CA file is kept in cafiles in suit's home folder, one NAME PATH line each. suit-run is built without the network code and is unchanged at 35496 bytes; the speed budget passes. Release suit went from 2291288 to 2490024 bytes.
An independent security review found no critical or high issues. Its one medium finding, a CA file trusted without checking its owner, mode or type, is fixed here, as are its low ones: separate storage for each set of anchors, core files off, and the release check now covering every test-only switch.
Checked against a real server: an anonymous clone of this repository over https from its forge, 8.4 s, verified with stock fossil.
A new suite, test/https.sh, with 170 cases, runs local servers with test certificates made at test time, including servers that misbehave on purpose. It prints skip lines where openssl or nc is missing. A new fuzz target covers the bundle reader.
3788 unit checks (was 3669), 625 shell cases, 1153 store cases, 985 placement cases, 378 project cases, 236 usage cases, 288 curate cases, 376 curate-apply cases and 170 https cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (76 minutes, on a loaded machine). 100% coverage in all 34 source files. Three store cases that expected "cannot use https yet" now expect the refusal for a missing certificate bundle. (user: dmestas, size: 7322) [annotate] [blame] [check-ins using]
#ifndef SUIT_PLATFORM_H #define SUIT_PLATFORM_H // The only file that knows which operating system this is, and the only one // that calls the operating system. Every call that can fail goes through a // wrapper here, so a test build can make the Nth call fail (docs/quality.md). #include <stddef.h> #include <stdint.h> #include <netdb.h> #include <sys/resource.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/types.h> #define PLATFORM_TRUE_PATH "/usr/bin/true" // ensure(cond): an invariant that cannot fail. Reports file and line and // aborts. On in every build except the coverage build, where it compiles to // nothing so the impossible side is not counted as a branch (the static // analyzer still sees the real one, which it needs to know the code after it). // COND must have no side effects. #if defined(SUIT_COVER) && !defined(__clang_analyzer__) #define ensure(cond) ((void)0) #else [[noreturn]] void ensure_fail(const char *cond, const char *file, int line); #define ensure(cond) ((cond) ? (void)0 : ensure_fail(#cond, __FILE__, __LINE__)) #endif // ---- calls that can fail -------------------------------------------------- // Each returns what the libc call returns and sets errno the same way. int sys_open(const char *path, int flags, mode_t mode); ssize_t sys_read(int fd, void *buf, size_t n); ssize_t sys_write(int fd, const void *buf, size_t n); int sys_close(int fd); int sys_fsync(int fd); int sys_fchmod(int fd, mode_t mode); int sys_stat(const char *path, struct stat *st); int sys_fstatat(int dirfd, const char *name, struct stat *st); int sys_rename(const char *from, const char *to); int sys_mkdir(const char *path, mode_t mode); int sys_unlink(const char *path); int sys_lstat(const char *path, struct stat *st); // The target of the symlink PATH, not NUL-terminated: its length, or -1. ssize_t sys_readlink(const char *path, char *buf, size_t cap); int sys_symlink(const char *target, const char *path); int sys_rmdir(const char *path); // Call CB(STATE, NAME) for each entry of the folder PATH except . and .. // CB returns 0 to go on or a positive number to stop, which is returned. // -1, with errno set, if the folder cannot be read. int sys_listdir(const char *path, int (*cb)(void *state, const char *name), void *state); // Whether process PID exists: 0, or -1 with errno ESRCH if it does not. int sys_kill0(pid_t pid); int sys_fstat(int fd, struct stat *st); off_t sys_lseek(int fd, off_t off, int whence); int sys_dup2(int from, int to); // setenv(NAME, VALUE, 1). int sys_setenv(const char *name, const char *value); char *sys_getcwd(char *buf, size_t cap); const char *sys_getenv(const char *name); // Spawn PATH with the current environment. Returns 0 or an error number. int sys_spawn(pid_t *pid, const char *path, char *const argv[]); // Replace this process with PATH. Returns only on failure. int sys_execv(const char *path, char *const argv[]); pid_t sys_wait4(pid_t pid, int *status, struct rusage *ru); // The network, for src/tls.c. Each returns what the libc call returns and // sets errno the same way. int sys_socket(int family, int type, int protocol); int sys_connect(int fd, const struct sockaddr *addr, socklen_t len); // poll on the one descriptor FD. int sys_poll(int fd, short events, int ms); // getsockopt SO_ERROR: the outcome of a non-blocking connect, in ERR. int sys_socket_error(int fd, int *err); // Set O_NONBLOCK on FD. int sys_set_nonblock(int fd); ssize_t sys_recv(int fd, void *buf, size_t n); // recv with MSG_WAITALL: until N bytes, the end, or an error. The fault name is // recv's. ssize_t sys_recv_all(int fd, void *buf, size_t n); // send without SIGPIPE. ssize_t sys_send(int fd, const void *buf, size_t n); // A connected pair of AF_UNIX stream sockets. int sys_socketpair(int fds[2]); pid_t sys_fork(void); // kill(PID, SIG). int sys_signal(pid_t pid, int sig); // getaddrinfo: 0 or an EAI_ code (EAI_FAIL for an injected fault). int sys_getaddrinfo(const char *host, const char *port, const struct addrinfo *hints, struct addrinfo **res); // N (at most 256) bytes from the system's random source: 0 or -1. int sys_entropy(void *buf, size_t n); // setrlimit(RLIMIT_CORE, 0): no core file, which could hold a secret. int sys_no_core_dumps(void); // End a forked child at once, without the parent's exit handlers. [[noreturn]] void sys_child_exit(int status); // CLOCK_MONOTONIC in nanoseconds. False on failure. bool sys_now_ns(uint64_t *ns); // The wall clock in seconds since 1970. False on failure. The test build // takes SUIT_NOW in its place. bool sys_wall_sec(int64_t *sec); // Write N bytes to PATH by way of a temporary file beside it: written, synced, // renamed over PATH. An executable PATH stays executable. Returns 0 or an // errno value; a failure leaves PATH as it was and no temporary file. int sys_write_atomic(const char *path, const void *src, size_t n); // The same, but a PATH that is not there yet is made with NEW_MODE. int sys_write_atomic_mode(const char *path, const void *src, size_t n, mode_t new_mode); // The same, and PATH has exactly MODE whatever the umask and whatever it had. int sys_write_atomic_exact(const char *path, const void *src, size_t n, mode_t mode); // The prefix of the temporary names written beside a file. #define SUIT_TMP_PREFIX ".suit-tmp." // Wait MS milliseconds. void platform_pause_ms(unsigned ms); // Write all of BUF, retrying after interruption and short writes. bool sys_write_all(int fd, const char *buf, size_t len); #ifdef SUIT_TEST // Forget the parsed SUIT_FAULT and the call counts, so the next wrapper call // reads the variable afresh. For tests that change it in a child process. void fault_reset(void); #endif // Absolute path of the running program. False if it fails or does not fit. bool platform_self_path(char *buf, size_t cap); // Which service manager holds the timers (src/install.c). The test build can // be told which with SUIT_PLATFORM=linux|macos. typedef enum { MANAGER_LAUNCHD, MANAGER_SYSTEMD } Manager; Manager platform_manager(void); // The program that loads and unloads timers. The test build takes // SUIT_LOADER in its place, so a test never runs the real one. const char *platform_loader(void); // ---- no failure to report ------------------------------------------------- // The user id the timers run as. unsigned platform_uid(void); // This process's id. unsigned platform_pid(void); // Whether FD is a terminal. bool platform_isatty(int fd); int64_t platform_mtime_sec(const struct stat *st); int64_t platform_mtime_nsec(const struct stat *st); // Peak resident size from a wait4 result, in bytes. uint64_t platform_peak_resident(const struct rusage *ru); // Format and write one message to FD in one go. A failure to write is not // reported: there is nowhere left to report it. Messages over SAY_MAX bytes // are cut and end in a newline. constexpr size_t SAY_MAX = 8192; __attribute__((format(printf, 2, 3))) void say(int fd, const char *fmt, ...); // Buffered output to a descriptor. Write errors are kept until out_finish. typedef struct { int fd; int err; bool failed; size_t len; char buf[8192]; } Out; void out_init(Out *o, int fd); void out_str(Out *o, const char *s); // Flush. False if any write failed; errno is set to that error. bool out_finish(Out *o); #endif