diff options
Diffstat (limited to 'src/user')
-rw-r--r-- | src/user/app/drawmouse/drawmouse.c | 3 | ||||
-rw-r--r-- | src/user/app/httpd/httpd.c | 4 | ||||
-rw-r--r-- | src/user/app/iochk/iochk.c | 3 | ||||
-rw-r--r-- | src/user/app/login/login.c | 4 | ||||
-rw-r--r-- | src/user/app/netdog/nd.c | 3 | ||||
-rw-r--r-- | src/user/app/netstack/fs.c | 3 | ||||
-rw-r--r-- | src/user/app/netstack/netstack.c | 3 | ||||
-rw-r--r-- | src/user/app/shell/builtins.c | 3 | ||||
-rw-r--r-- | src/user/app/tests/kernel/misc.c | 2 | ||||
-rw-r--r-- | src/user/app/tests/kernel/miscsyscall.c | 6 | ||||
-rw-r--r-- | src/user/lib/draw/draw.c | 3 | ||||
-rw-r--r-- | src/user/lib/fs/misc.c | 2 | ||||
-rw-r--r-- | src/user/lib/fs/whitelist.c | 9 | ||||
-rw-r--r-- | src/user/lib/stdio/file.c | 12 | ||||
-rw-r--r-- | src/user/lib/stdlib.c | 2 | ||||
-rw-r--r-- | src/user/lib/unistd.c | 9 |
16 files changed, 51 insertions, 20 deletions
diff --git a/src/user/app/drawmouse/drawmouse.c b/src/user/app/drawmouse/drawmouse.c index 47e8e8a..6ddfc79 100644 --- a/src/user/app/drawmouse/drawmouse.c +++ b/src/user/app/drawmouse/drawmouse.c @@ -1,3 +1,4 @@ +#include <camellia/flags.h> #include <camellia/syscalls.h> #include <shared/container/ring.h> #include <stdbool.h> @@ -49,7 +50,7 @@ struct packet { int main(void) { char buf[64]; - handle_t fd = _syscall_open("/kdev/ps2/mouse", 15, 0); + handle_t fd = _syscall_open("/kdev/ps2/mouse", 15, OPEN_READ); if (fd < 0) { eprintf("couldn't open mouse"); return 1; diff --git a/src/user/app/httpd/httpd.c b/src/user/app/httpd/httpd.c index 9141a39..2ffc2ba 100644 --- a/src/user/app/httpd/httpd.c +++ b/src/user/app/httpd/httpd.c @@ -21,7 +21,7 @@ static void handle(FILE *c) { char *end = strchr(path, ' '); if (end) *end = '\0'; - handle_t h = _syscall_open(path, strlen(path), OPEN_RO); + handle_t h = _syscall_open(path, strlen(path), OPEN_READ); if (h < 0) { fprintf(c, "HTTP/1.1 404 Not Found\r\n\r\n"); return; @@ -68,7 +68,7 @@ int main(int argc, char **argv) { const char *path = (argc > 1) ? argv[1] : "/net/listen/0.0.0.0/tcp/80"; handle_t conn; for (;;) { - conn = _syscall_open(path, strlen(path), 0); + conn = _syscall_open(path, strlen(path), OPEN_RW); if (conn < 0) { eprintf("open('%s') failed, %d", path, conn); return 1; diff --git a/src/user/app/iochk/iochk.c b/src/user/app/iochk/iochk.c index 38e0473..92975be 100644 --- a/src/user/app/iochk/iochk.c +++ b/src/user/app/iochk/iochk.c @@ -1,4 +1,5 @@ #include <assert.h> +#include <camellia/flags.h> #include <camellia/syscalls.h> #include <stdbool.h> #include <stdio.h> @@ -84,7 +85,7 @@ int main(int argc, char **argv) { for (; optind < argc; optind++) { const char *path = argv[optind]; verbosef("checking %s...\n", path); - handle_t h = _syscall_open(path, strlen(path), 0); + handle_t h = _syscall_open(path, strlen(path), OPEN_READ); if (h < 0) { eprintf("couldn't open %s", path); continue; diff --git a/src/user/app/login/login.c b/src/user/app/login/login.c index 5d9eb36..af6e19c 100644 --- a/src/user/app/login/login.c +++ b/src/user/app/login/login.c @@ -44,8 +44,10 @@ static void drv(const char *user) { forward_open(reqh, buf, req.len, req.flags); } else if (segcmp(buf, 1, "Users") && segcmp(buf, 3, "private")) { // /Users/*/private/** _syscall_fs_respond(reqh, NULL, -EACCES, 0); + } else if (!OPEN_WRITEABLE(req.flags)) { + forward_open(reqh, buf, req.len, req.flags); } else { - forward_open(reqh, buf, req.len, req.flags | OPEN_RO); + _syscall_fs_respond(reqh, NULL, -EACCES, 0); } break; diff --git a/src/user/app/netdog/nd.c b/src/user/app/netdog/nd.c index 363c6c6..8c16bb0 100644 --- a/src/user/app/netdog/nd.c +++ b/src/user/app/netdog/nd.c @@ -1,3 +1,4 @@ +#include <camellia/flags.h> #include <camellia/syscalls.h> #include <stdio.h> #include <string.h> @@ -34,7 +35,7 @@ int main(int argc, char **argv) { return 1; } - conn = _syscall_open(argv[1], strlen(argv[1]), 0); + conn = _syscall_open(argv[1], strlen(argv[1]), OPEN_RW); if (conn < 0) { eprintf("couldn't open '%s', err %u", argv[1], -conn); return -conn; diff --git a/src/user/app/netstack/fs.c b/src/user/app/netstack/fs.c index d8faadf..ad6c23c 100644 --- a/src/user/app/netstack/fs.c +++ b/src/user/app/netstack/fs.c @@ -144,7 +144,8 @@ static void fs_open(handle_t reqh, char *path, int flags) { } /* everything below ends up sending packets */ - if (flags & OPEN_RO) respond(NULL, -EACCES); + if (!OPEN_WRITEABLE(flags)) + respond(NULL, -EACCES); char *save; const char *verb, *proto, *port_s; diff --git a/src/user/app/netstack/netstack.c b/src/user/app/netstack/netstack.c index cc5247b..c0934fd 100644 --- a/src/user/app/netstack/netstack.c +++ b/src/user/app/netstack/netstack.c @@ -1,5 +1,6 @@ #include "proto.h" #include "util.h" +#include <camellia/flags.h> #include <camellia/syscalls.h> #include <stdbool.h> #include <stddef.h> @@ -33,7 +34,7 @@ int main(int argc, char **argv) { eprintf("usage: netstack iface ip gateway"); return 1; } - state.raw_h = _syscall_open(argv[1], strlen(argv[1]), 0); + state.raw_h = _syscall_open(argv[1], strlen(argv[1]), OPEN_RW); if (state.raw_h < 0) { eprintf("couldn't open %s", argv[1]); return 1; diff --git a/src/user/app/shell/builtins.c b/src/user/app/shell/builtins.c index bc7731c..a23e501 100644 --- a/src/user/app/shell/builtins.c +++ b/src/user/app/shell/builtins.c @@ -1,5 +1,6 @@ #include "builtins.h" #include "shell.h" +#include <camellia/flags.h> #include <camellia/path.h> #include <stdbool.h> #include <stdio.h> @@ -60,7 +61,7 @@ void cmd_getsize(int argc, char **argv) { } for (int i = 1; i < argc; i++) { - handle_t h = _syscall_open(argv[i], strlen(argv[i]), 0); + handle_t h = _syscall_open(argv[i], strlen(argv[i]), OPEN_READ); if (h < 0) { eprintf("error opening %s", argv[i]); continue; diff --git a/src/user/app/tests/kernel/misc.c b/src/user/app/tests/kernel/misc.c index c0bb6b3..9ee88dd 100644 --- a/src/user/app/tests/kernel/misc.c +++ b/src/user/app/tests/kernel/misc.c @@ -28,7 +28,7 @@ static void test_efault(void) { memcpy(str2, str, 16); - test((h = _syscall_open(TMPFILEPATH, strlen(TMPFILEPATH), OPEN_CREATE))); + test((h = _syscall_open(TMPFILEPATH, strlen(TMPFILEPATH), OPEN_CREATE | OPEN_WRITE)) >= 0); test(_syscall_write(h, str, 16, 0, 0) == 16); test(_syscall_write(h, str2, 16, 0, 0) == 16); diff --git a/src/user/app/tests/kernel/miscsyscall.c b/src/user/app/tests/kernel/miscsyscall.c index a2eed24..66899b1 100644 --- a/src/user/app/tests/kernel/miscsyscall.c +++ b/src/user/app/tests/kernel/miscsyscall.c @@ -270,6 +270,11 @@ static void test_sleep(void) { } } +static void test_badopen(void) { + test(_syscall_open(TMPFILEPATH, strlen(TMPFILEPATH), OPEN_CREATE | OPEN_WRITE) >= 0); + test(_syscall_open(TMPFILEPATH, strlen(TMPFILEPATH), OPEN_CREATE) == -EINVAL); +} + void r_k_miscsyscall(void) { run_test(test_await); run_test(test_pipe); @@ -277,4 +282,5 @@ void r_k_miscsyscall(void) { run_test(test_dup); run_test(test_execbuf); run_test(test_sleep); + run_test(test_badopen); } diff --git a/src/user/lib/draw/draw.c b/src/user/lib/draw/draw.c index 27c1d33..943b8ba 100644 --- a/src/user/lib/draw/draw.c +++ b/src/user/lib/draw/draw.c @@ -1,3 +1,4 @@ +#include <camellia/flags.h> #include <camellia/syscalls.h> #include <errno.h> #include <stdio.h> @@ -32,7 +33,7 @@ int fb_setup(struct framebuf *fb, const char *base) { /* assumes the read went correctly */ fclose(f); - fb->fd = _syscall_open(path, strlen(path), 0); + fb->fd = _syscall_open(path, strlen(path), OPEN_RW); if (fb->fd < 0) return fb->fd; fb->width = strtol(spec, &spec, 0); diff --git a/src/user/lib/fs/misc.c b/src/user/lib/fs/misc.c index f660f6f..860b312 100644 --- a/src/user/lib/fs/misc.c +++ b/src/user/lib/fs/misc.c @@ -135,7 +135,7 @@ void fs_union(const char **list) { size_t prefixlen = strlen(prefix); // TODO only open the directories once // TODO ensure trailing slash - handle_t h = _syscall_open(prefix, prefixlen, 0); + handle_t h = _syscall_open(prefix, prefixlen, OPEN_READ); if (h < 0) continue; end = end || dir_append_from(&db, h); _syscall_close(h); diff --git a/src/user/lib/fs/whitelist.c b/src/user/lib/fs/whitelist.c index 676b36f..571ebfb 100644 --- a/src/user/lib/fs/whitelist.c +++ b/src/user/lib/fs/whitelist.c @@ -1,5 +1,6 @@ #include <camellia/flags.h> #include <camellia/syscalls.h> +#include <errno.h> #include <stdlib.h> #include <string.h> #include <user/lib/fs/dir.h> @@ -49,6 +50,7 @@ void fs_whitelist(const char **whitelist) { switch (res.op) { case VFSOP_OPEN: { + bool error = false; bool passthru = false; bool inject = false; @@ -57,8 +59,9 @@ void fs_whitelist(const char **whitelist) { size_t entry_len = suffix_parse(*entry, strlen(*entry), &ro); /* If *entry is a prefix of the opened path, pass the open() through. */ if (prefix_match(*entry, entry_len, buf, res.len)) { - if (ro) res.flags |= OPEN_RO; passthru = true; + if (ro && OPEN_WRITEABLE(res.flags)) + error = true; break; } /* If the path is a prefix of *entry, we might need to inject a directory. */ @@ -66,7 +69,9 @@ void fs_whitelist(const char **whitelist) { inject = true; } } - if (passthru) { + if (error) { + _syscall_fs_respond(reqh, NULL, -EACCES, 0); + } else if (passthru) { forward_open(reqh, buf, res.len, res.flags); } else if (inject) { // TODO all the inject points could be precomputed diff --git a/src/user/lib/stdio/file.c b/src/user/lib/stdio/file.c index 8c0fc57..f3120d7 100644 --- a/src/user/lib/stdio/file.c +++ b/src/user/lib/stdio/file.c @@ -42,8 +42,16 @@ FILE *fopen(const char *path, const char *mode) { path = tmppath; } - if (mode[0] == 'w' || mode[0] == 'a') - flags |= OPEN_CREATE; + if (strchr(mode, 'e')) { + /* camellia extension: open as executable */ + flags |= OPEN_EXEC; + } else if (strchr(mode, 'r')) { + flags |= OPEN_READ; + if (strchr(mode, '+')) + flags |= OPEN_WRITE; + } else { + flags |= OPEN_WRITE | OPEN_CREATE; + } h = _syscall_open(path, strlen(path), flags); if (tmppath) free(tmppath); diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c index 38e87ad..c1ee217 100644 --- a/src/user/lib/stdlib.c +++ b/src/user/lib/stdlib.c @@ -10,7 +10,7 @@ _Noreturn void abort(void) { int mkstemp(char *template) { // TODO randomize template - handle_t h = _syscall_open(template, strlen(template), OPEN_CREATE); + handle_t h = _syscall_open(template, strlen(template), OPEN_CREATE | OPEN_RW); if (h < 0) { errno = -h; return -1; diff --git a/src/user/lib/unistd.c b/src/user/lib/unistd.c index ed8d77f..04a060d 100644 --- a/src/user/lib/unistd.c +++ b/src/user/lib/unistd.c @@ -1,3 +1,4 @@ +#include <camellia/flags.h> #include <camellia/path.h> #include <camellia/syscalls.h> #include <errno.h> @@ -30,7 +31,8 @@ int unlink(const char *path) { size_t abslen = absolutepath(abspath, path, len); if (abslen == 0) { errno = EINVAL; goto err; } - handle_t h = _syscall_open(abspath, abslen - 1, 0); + // TODO take cwd into account + handle_t h = _syscall_open(abspath, abslen - 1, OPEN_WRITE); if (h < 0) { errno = -h; goto err; } long ret = _syscall_remove(h); @@ -49,7 +51,7 @@ int isatty(int fd) { int execv(const char *path, char *const argv[]) { - FILE *file = fopen(path, "r"); + FILE *file = fopen(path, "e"); char hdr[4] = {0}; if (!file) return -1; @@ -114,7 +116,8 @@ int chdir(const char *path) { cwd2[len + 1] = '\0'; } - h = _syscall_open(cwd2, strlen(cwd2), 0); + /* check if exists */ + h = _syscall_open(cwd2, strlen(cwd2), OPEN_READ); if (h < 0) { errno = ENOENT; return -1; |