Fix bugs and add security features to redbean

- Fix a regression with the previous change that broke redbean
- Add chroot(), resource limit, seccomp, and other stuff to redbean
- Write lots and lots of documentation
- Iron out more system call issues
This commit is contained in:
Justine Tunney 2022-04-18 00:01:26 -07:00
parent f1dfa4bdfa
commit 7166679620
182 changed files with 1855 additions and 918 deletions

View file

@ -16,28 +16,31 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/sysv/consts/nr.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/intrin/lockxadd.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/descriptor.h"
#include "libc/thread/exit.h"
#include "libc/thread/self.h"
/**
* Exits cosmopolitan thread.
*
* @param rc is exit code
* @see _Exit1() for the raw system call
* @threadsafe
* @noreturn
*/
wontreturn void cthread_exit(int rc) {
cthread_t td = cthread_self();
cthread_t td;
STRACE("cthread_exit(%d)", rc);
td = cthread_self();
td->rc = rc;
size_t size = (intptr_t)(td->alloc.top) - (intptr_t)(td->alloc.bottom);
int state;
asm volatile("lock xadd\t%1, %0\n\t" // mark thread as finished
"test\t%2, %b1\n\t" // test if thread was detached
"jz .L.cthread_exit.%=\n\t" // skip unmap if not detached
"syscall\n" // unmap thread
".L.cthread_exit.%=:\n\t"
"mov\t%%rbx, %%rdi\n\t" // rc
"mov\t$60, %%rax\n\t"
"syscall" // thread exit
: "+m"(td->state), "=&r"(state)
: "I"(cthread_detached), "1"(cthread_finished), "a"(__NR_munmap),
"b"(rc), "D"(td->alloc.bottom), "S"(size)
: "rcx", "r11", "cc", "memory");
unreachable;
_lockxadd(&td->state, cthread_finished);
if (~td->state & cthread_detached) {
sys_munmap(td->alloc.bottom,
(intptr_t)td->alloc.top - (intptr_t)td->alloc.bottom);
}
_Exit1(rc);
}