diff --git a/README.md b/README.md index 02d55a8..f774d16 100644 --- a/README.md +++ b/README.md @@ -1011,12 +1011,14 @@ the prompt. - `search` (read): literal string over a directory tree, capped at 100 hits. - `list` (read): a directory. - `write` (mutate): writes a file, up to 4 MiB. The new text goes to a sibling - `.tmp` and is renamed over the target, so a full disk or a crash can never - leave the old file truncated; a symlink is followed and stays a symlink, and - the mode of an existing file is kept. + `.tmp`, is flushed and renamed over the target, so a full disk or a crash can + never leave the old file truncated. A symlink is followed and stays a link; + the mode of an existing file is kept, and a file the operator made read-only + is still refused (`Permission denied`) instead of being replaced. - `edit` (mutate): replaces exactly one occurrence; a non-unique `old` is an error, because an ambiguous edit is a wrong edit waiting to happen. Written - the same way as `write`, atomically. + the same way as `write`, atomically. A read error is reported instead of + being taken for the end of the file. - `run` (destructive): a shell command; the exit status is an observation. Its own process group, stdin `/dev/null`, an environment without splinter's API keys and tokens, so `run env` cannot hand them to the model. It is killed diff --git a/src/backend/builtin.c b/src/backend/builtin.c index 1957d3f..5c1fa72 100644 --- a/src/backend/builtin.c +++ b/src/backend/builtin.c @@ -381,6 +381,15 @@ write_atomic(const char *path, const char *text, size_t n, struct sp_buf *err) return sp_buf_puts(err, "error: path is too long"); snprintf(tmp, sizeof(tmp), "%s.tmp", real); have = stat(real, &st) == 0; + /* + * rename(2) only needs write permission on the directory, so a 0444 file + * would be replaced without a word - fopen(path, "w") refused it. The + * tools must not gain a way around a file's own mode. + */ + if (have && access(real, W_OK) != 0) { + sp_buf_printf(err, "error: %s: %s", real, strerror(errno)); + return -1; + } fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, have ? 0600 : 0666); if (fd < 0) { sp_buf_printf(err, "error: %s: %s", tmp, strerror(errno)); @@ -630,6 +639,10 @@ tool_read(const char *args, struct sp_buf *out, void *ctx) total += strlen(line); printed++; } + /* a short read must say so, or it looks like the end of the file */ + if (ferror(f) && !(printed >= limit || total >= TOOL_MAX_READ)) + sp_buf_printf(out, "[%s: read error after line %d: %s]\n", + path, no, strerror(errno)); fclose(f); if (printed == 0 && no < offset) sp_buf_printf(out, "[%s: no lines at offset %d (file has %d)]\n", @@ -725,6 +738,17 @@ slurp(const char *path, size_t *lenp, struct sp_buf *err) return NULL; } } + /* + * A read error is not the end of the file. Without this, edit() would + * take the truncated prefix for the whole file, find its `old` there + * and write the prefix back - a silent data loss on the way out. + */ + if (ferror(f)) { + sp_buf_printf(err, "error: %s: %s", path, strerror(errno)); + sp_buf_free(&buf); + fclose(f); + return NULL; + } fclose(f); if (buf.p == NULL) { sp_buf_free(&buf); diff --git a/tests/test_builtin.c b/tests/test_builtin.c index bb6fbb6..930401f 100644 --- a/tests/test_builtin.c +++ b/tests/test_builtin.c @@ -203,6 +203,80 @@ main(void) } } + /* + * ---- a read error is reported, not mistaken for the end ---- + * + * A directory opens fine on Linux and fails on the first read. read() + * used to print nothing at all, which is indistinguishable from an + * empty file; it now names the error. slurp() (behind edit/write) gets + * the same guard: an error there would hand edit() a truncated prefix + * to write back over the file. + */ + { + char big2[1600]; + + sp_buf_clear(&out); + snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", dir); + CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0); + CHECK(strstr(out.p, "read error") != NULL); + + /* the same failure mid-read, not at the very first byte */ + sp_buf_clear(&out); + CHECK(tool(&r, "read")->run("{\"path\":\"/proc/self/mem\"}", + &out, NULL) == 0); + CHECK(strstr(out.p, "error") != NULL || + strstr(out.p, "read error") != NULL); + + sp_buf_clear(&out); + snprintf(big2, sizeof(big2), + "{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", dir); + CHECK(tool(&r, "edit")->run(big2, &out, NULL) == 0); + CHECK(strstr(out.p, "error:") != NULL); + CHECK(strstr(out.p, "edited") == NULL); + + /* slurp() behind edit(): the EIO must not read as "empty" */ + sp_buf_clear(&out); + CHECK(tool(&r, "edit")->run( + "{\"path\":\"/proc/self/mem\",\"old\":\"x\",\"new\":\"y\"}", + &out, NULL) == 0); + CHECK(strstr(out.p, "Input/output error") != NULL); + CHECK(strstr(out.p, "is empty") == NULL); + } + + /* + * ---- the file's own mode still decides ---- + * + * A temp file plus rename() only needs write permission on the + * directory, so without a check a 0444 file would be replaced while + * fopen(path, "w") had always refused it. And a 0600 file must not + * become 0644 just because a tool wrote it. + */ + { + char ro[700], big2[1600]; + struct stat st; + + snprintf(ro, sizeof(ro), "%s/ro.txt", dir); + write_file(ro, "alt\n"); + CHECK(chmod(ro, 0444) == 0); + sp_buf_clear(&out); + snprintf(big2, sizeof(big2), + "{\"path\":\"%s\",\"content\":\"neu\\n\"}", ro); + CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0); + CHECK(strstr(out.p, "Permission denied") != NULL); + sp_buf_clear(&out); + snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", ro); + CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0); + CHECK(strstr(out.p, "alt") != NULL); + + CHECK(chmod(ro, 0600) == 0); + sp_buf_clear(&out); + snprintf(big2, sizeof(big2), + "{\"path\":\"%s\",\"content\":\"neu\\n\"}", ro); + CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0); + CHECK(stat(ro, &st) == 0); + CHECK((st.st_mode & 07777) == 0600); /* mode kept */ + } + /* ---- read refuses a binary file ---- */ { FILE *f;