Files
splinter/src/curl.c
T
lotherkandClaude Opus 5.5 6fe30f4787 security: the authorization model holds where the model writes the input
A review found the tool classes - the core of the harness - bypassable in
several places where model output was pasted into a shell or trusted as an id.
Each fix below has a test that reproduces the original failure.

No shell for model data
- webfetch put the URL between '...' and ran it through popen: a quote in the
  URL ran any command, from a tool that needed no consent. curl is now exec'd
  with an argv, http(s) only, HTML stripped in C, stderr off the curses screen.
- webfetch is mutate: with read free, "read a key, fetch evil/?k=..." left the
  machine without anybody being asked. websearch stays read (fixed host).
- kids: the parent-approval command got the model's arguments inside '...'
  via system(); they are now "$1". The domain check reads the URL with the
  same parser as the tool (duplicate keys) and refuses user@host URLs.
- delegate: the two copies of the child argv are one (exec_delegate). The ssh
  path exec'd splinter with "ssh" as argv[0]; it now runs ssh and quotes every
  word for the remote shell. A single delegate to a host now really runs there.

Approvals (ack)
- The id was the pid: a late yes stayed on disk and approved the next request
  unseen. Ids are random, requests 0600 with O_EXCL, deleted when answered or
  expired; an answer is refused after expiry; the first answer wins (link(2)).
- An ack dir owned by someone else or writable by others is refused.
- --ack accepted every word but "no" as yes ("nein" approved). Now strict.
- The terminal thread blocked in fgets after a phone answer; it is stoppable.

Secrets
- API key and Telegram token were in curl's argv, readable via ps. src/curl.c
  passes URL and auth headers as a -K config on stdin (transport, telegram,
  tg-ask.sh; its dead eval fallback is gone).
- 'run' and task_start no longer inherit splinter's keys ("run env"). 'run' got
  its own process group, stdin /dev/null and SPLINTER_RUN_TIMEOUT (300 s).

MCP server
- Beyond 127.x it refuses to start without SPLINTER_MCP_TOKEN; connections
  must begin with "AUTH <token>". SIGPIPE ignored, idle connections closed.

Crashes
- task_kill sent kill(-0) - SIGTERM to splinter's own process group - because
  popen gave no pid. Tasks now have a pid and a process group; integer ids work.
- ui: a 3- or 4-byte UTF-8 character overflowed char[2] on every redraw
  (now sp_ui_ascii_char, tested); the scroll offset was taken from one buffer
  and applied to another, reading past its end.

Repo
- The test binaries are no longer tracked (.gitignore already said so).

make check: 23 test binaries, green, also under ASan/UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:19:01 +00:00

164 lines
3.7 KiB
C

/*
* splinter -- curl(1) without secrets on its command line.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Every argument of a process is readable by every user of the machine
* (/proc/<pid>/cmdline, ps -ef). An API key in "-H 'Authorization: Bearer ...'"
* or a Telegram token in the URL was therefore public for as long as the request
* ran. curl reads a config from stdin with "-K -"; the URL and the secret
* headers go there, and argv keeps only what may be seen.
*
* It is also the end of building curl command lines as shell strings: no quote
* in a URL can change what runs.
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "curl.h"
#define CURL_MAX_ARGS 32
int
sp_curl_config_line(struct sp_buf *b, const char *key, const char *value)
{
const char *s;
if (sp_buf_printf(b, "%s = \"", key) != 0)
return -1;
for (s = value; *s != '\0'; s++) {
const char *esc = NULL;
switch (*s) {
case '"': esc = "\\\""; break;
case '\\': esc = "\\\\"; break;
case '\n': esc = "\\n"; break;
case '\r': esc = "\\r"; break;
case '\t': esc = "\\t"; break;
}
if (esc != NULL ? sp_buf_puts(b, esc) : sp_buf_add(b, s, 1))
return -1;
}
return sp_buf_puts(b, "\"\n");
}
static int
write_all(int fd, const char *p, size_t n)
{
while (n > 0) {
ssize_t w = write(fd, p, n);
if (w < 0) {
if (errno == EINTR)
continue;
return -1;
}
p += w;
n -= (size_t)w;
}
return 0;
}
int
sp_curl_start(const char *const *args, const char *url,
const char *const *headers, int quiet, pid_t *pid, struct sp_buf *err)
{
struct sp_buf cfg;
char *argv[CURL_MAX_ARGS + 4];
int in[2], out[2], n = 0, i, failed;
pid_t child;
if (sp_buf_init(&cfg) != 0)
return -1;
failed = sp_curl_config_line(&cfg, "url", url != NULL ? url : "");
for (i = 0; !failed && headers != NULL && headers[i] != NULL; i++)
failed = sp_curl_config_line(&cfg, "header", headers[i]);
if (failed) {
sp_buf_free(&cfg);
sp_buf_puts(err, "curl: out of memory");
return -1;
}
argv[n++] = "curl";
for (i = 0; args != NULL && args[i] != NULL; i++) {
if (i >= CURL_MAX_ARGS) {
sp_buf_free(&cfg);
sp_buf_puts(err, "curl: too many arguments");
return -1;
}
argv[n++] = (char *)args[i];
}
argv[n++] = "-K";
argv[n++] = "-";
argv[n] = NULL;
if (pipe(in) != 0) {
sp_buf_free(&cfg);
sp_buf_printf(err, "curl: pipe: %s", strerror(errno));
return -1;
}
if (pipe(out) != 0) {
close(in[0]);
close(in[1]);
sp_buf_free(&cfg);
sp_buf_printf(err, "curl: pipe: %s", strerror(errno));
return -1;
}
child = fork();
if (child < 0) {
close(in[0]);
close(in[1]);
close(out[0]);
close(out[1]);
sp_buf_free(&cfg);
sp_buf_printf(err, "curl: fork: %s", strerror(errno));
return -1;
}
if (child == 0) {
(void)dup2(in[0], STDIN_FILENO);
(void)dup2(out[1], STDOUT_FILENO);
if (quiet) {
int dn = open("/dev/null", O_WRONLY);
if (dn >= 0) {
(void)dup2(dn, STDERR_FILENO);
close(dn);
}
}
close(in[0]);
close(in[1]);
close(out[0]);
close(out[1]);
execvp("curl", argv);
_exit(127);
}
close(in[0]);
close(out[1]);
/*
* The config is a few hundred bytes, far below a pipe's buffer, and curl
* reads all of it before it starts: writing first cannot deadlock.
*/
if (write_all(in[1], cfg.p, cfg.len) != 0) {
sp_buf_printf(err, "curl: writing the config: %s",
strerror(errno));
close(in[1]);
close(out[0]);
(void)kill(child, SIGKILL);
(void)waitpid(child, NULL, 0);
sp_buf_free(&cfg);
return -1;
}
close(in[1]);
/* the copy in memory holds the key too: do not leave it lying around */
memset(cfg.p, 0, cfg.cap);
sp_buf_free(&cfg);
*pid = child;
return out[0];
}