mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-26 22:38:30 +00:00
Add seccomp bpf sandboxing to redbean
It's now possible to pass the `-S` or `-SS` flags to sandbox redbean worker proecsses after they've been forked. The first `-S` flag is intended to be a permissive builtin policy that limits system calls to only that which the various parts of redbean serving need. The second `-SS` flag is intended to be more restrictive, preventing things like the Lua extensions you download off the web from using the HTTP client or sockets APIs. In upcoming changes you'll be able to implement your own Berkeley Packet Filter sandbox programs and load them via Lua.
This commit is contained in:
parent
7166679620
commit
5a132f9652
79 changed files with 2271 additions and 651 deletions
|
@ -1,35 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/consts/seccomp.h"
|
||||
|
||||
const struct DescribeFlags kSeccompOperationFlags[] = {
|
||||
{SECCOMP_GET_NOTIF_SIZES, "GET_NOTIF_SIZES"}, // order matters
|
||||
{SECCOMP_GET_ACTION_AVAIL, "GET_ACTION_AVAIL"}, //
|
||||
{SECCOMP_SET_MODE_FILTER, "SET_MODE_FILTER"}, //
|
||||
{SECCOMP_SET_MODE_STRICT, "SET_MODE_STRICT"}, //
|
||||
};
|
||||
|
||||
const char *DescribeSeccompOperationFlags(int x) {
|
||||
static char seccompflags[128];
|
||||
return DescribeFlags(seccompflags, sizeof(seccompflags),
|
||||
kSeccompOperationFlags, ARRAYLEN(kSeccompOperationFlags),
|
||||
"SECCOMP_", x);
|
||||
}
|
|
@ -16,7 +16,6 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/issandboxed.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
|
@ -39,7 +38,7 @@ noasan noubsan int IsDebuggerPresent(bool force) {
|
|||
if (!force && IsGenuineCosmo()) return 0;
|
||||
if (!force && getenv("HEISENDEBUG")) return 0;
|
||||
if (IsWindows()) return NtGetPeb()->BeingDebugged; /* needs noasan */
|
||||
if (__issandboxed) return false;
|
||||
if (__isworker) return false;
|
||||
res = 0;
|
||||
if ((fd = __sysv_open("/proc/self/status", O_RDONLY, 0)) >= 0) {
|
||||
if ((got = __sysv_read(fd, buf, sizeof(buf) - 1)) > 0) {
|
||||
|
|
|
@ -18,5 +18,11 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
|
||||
// SECCOMP_SET_MODE_STRICT
|
||||
bool __issandboxed;
|
||||
/**
|
||||
* Indicates if current execution context is a worker task.
|
||||
*
|
||||
* Setting this to true on things like the forked process of a web
|
||||
* server is a good idea since it'll ask the C runtime to not pull
|
||||
* magical stunts like attaching GDB to the process on crash.
|
||||
*/
|
||||
bool __isworker;
|
|
@ -246,7 +246,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
const char *s, *f;
|
||||
unsigned long long x;
|
||||
unsigned i, j, m, rem, sign, hash, cols, prec;
|
||||
char c, *p, *e, pdot, zero, flip, dang, base, quot, z[128];
|
||||
char c, *p, *e, pdot, zero, flip, dang, base, quot, uppr, z[128];
|
||||
if (kistextpointer(b) || kisdangerous(b)) n = 0;
|
||||
if (!kistextpointer(fmt)) fmt = "!!WONTFMT";
|
||||
p = b;
|
||||
|
@ -270,6 +270,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
type = 0;
|
||||
cols = 0;
|
||||
zero = 0;
|
||||
uppr = 0;
|
||||
abet = "0123456789abcdef";
|
||||
for (;;) {
|
||||
switch ((c = *f++)) {
|
||||
|
@ -302,6 +303,10 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
sign = c;
|
||||
continue;
|
||||
|
||||
case '^':
|
||||
uppr = c;
|
||||
continue;
|
||||
|
||||
case 'h':
|
||||
--type;
|
||||
continue;
|
||||
|
@ -507,6 +512,12 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
type = 0;
|
||||
goto FormatString;
|
||||
} else {
|
||||
if (p + 4 <= e) {
|
||||
*p++ = 'e';
|
||||
*p++ = 'r';
|
||||
*p++ = 'r';
|
||||
*p++ = '=';
|
||||
}
|
||||
type = 0;
|
||||
x = unixerr;
|
||||
goto FormatDecimal;
|
||||
|
@ -558,10 +569,6 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
goto FormatString;
|
||||
}
|
||||
|
||||
case 'S':
|
||||
c = 's';
|
||||
type = 1;
|
||||
// fallthrough
|
||||
case 's':
|
||||
if (!(s = va_arg(va, const void *))) {
|
||||
s = sign != ' ' ? "NULL" : "";
|
||||
|
@ -598,6 +605,9 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
if ((t & 0300) == 0200) goto ActuallyEmitByte;
|
||||
++i;
|
||||
EmitByte:
|
||||
if (uppr && 'a' <= t && t <= 'z') {
|
||||
t -= 'a' - 'A';
|
||||
}
|
||||
if (UNLIKELY(quot) && (t == '\\' || ((t == '"' && c == 's') ||
|
||||
(t == '\'' && c == 'c')))) {
|
||||
if (p + 2 <= e) {
|
||||
|
@ -671,9 +681,15 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
if (!t) break;
|
||||
++i;
|
||||
EmitChar:
|
||||
if (t <= 0x7f) {
|
||||
goto EmitByte;
|
||||
} else if (t <= 0x7ff) {
|
||||
if (t <= 0x7f) goto EmitByte;
|
||||
if (uppr) {
|
||||
if (weaken(towupper)) {
|
||||
t = weaken(towupper)(t);
|
||||
} else if (uppr && 'a' <= t && t <= 'z') {
|
||||
t -= 'a' - 'A';
|
||||
}
|
||||
}
|
||||
if (t <= 0x7ff) {
|
||||
if (p + 2 <= e) {
|
||||
p[0] = 0300 | (t >> 6);
|
||||
p[1] = 0200 | (t & 077);
|
||||
|
@ -886,6 +902,7 @@ privileged void kvprintf(const char *fmt, va_list v) {
|
|||
* - `+` plus leftpad if positive (aligns w/ negatives)
|
||||
* - ` ` space leftpad if positive (aligns w/ negatives)
|
||||
* - `#` represent value with literal syntax, e.g. 0x, 0b, quotes
|
||||
* - `^` uppercasing w/ towupper() if linked, otherwise toupper()
|
||||
*
|
||||
* Error numbers:
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue