diff options
author | Marvin Borner | 2021-03-15 22:54:54 +0100 |
---|---|---|
committer | Marvin Borner | 2021-03-15 22:54:54 +0100 |
commit | dc9f9f55cb6b38b87d8c228ae9abb4b53ebfb25c (patch) | |
tree | e4a51d5e94f963bcabe834b3ba535a8da871c42f /libc | |
parent | 665b00e7f44e5088b134d111ba1d5226c11f0ae8 (diff) |
System hardening and errno impl
Diffstat (limited to 'libc')
-rw-r--r-- | libc/Makefile | 3 | ||||
-rw-r--r-- | libc/errno.c | 14 | ||||
-rw-r--r-- | libc/inc/errno.h | 49 | ||||
-rw-r--r-- | libc/inc/str.h | 2 | ||||
-rw-r--r-- | libc/print.c | 5 | ||||
-rw-r--r-- | libc/random.c | 10 | ||||
-rw-r--r-- | libc/str.c | 46 | ||||
-rw-r--r-- | libc/sys.c | 20 |
8 files changed, 140 insertions, 9 deletions
diff --git a/libc/Makefile b/libc/Makefile index 3bf4473..2dd2c5f 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -1,7 +1,8 @@ # MIT License, Copyright (c) 2020 Marvin Borner -# TODO: Remove serial and cpu from libc? +# TODO: Remove cpu from libc? COBJS = sanitize.o \ + errno.o \ str.o \ alloc.o \ mem.o \ diff --git a/libc/errno.c b/libc/errno.c new file mode 100644 index 0000000..7204f5b --- /dev/null +++ b/libc/errno.c @@ -0,0 +1,14 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#include <def.h> +#include <errno.h> +#ifdef userspace + +static u32 error = 0; + +u32 *__errno(void) +{ + return &error; +} + +#endif diff --git a/libc/inc/errno.h b/libc/inc/errno.h new file mode 100644 index 0000000..76b4b85 --- /dev/null +++ b/libc/inc/errno.h @@ -0,0 +1,49 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef ERRNO_H +#define ERRNO_H + +#include <def.h> + +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ +#define EDOM 33 /* Math argument out of domain of func */ +#define ERANGE 34 /* Math result not representable */ +#define EMAX 35 /* Max errno */ + +#ifdef userspace +#define errno (*__errno()) +extern u32 *__errno(void); +#endif + +#endif diff --git a/libc/inc/str.h b/libc/inc/str.h index 0ef49a6..d0a521f 100644 --- a/libc/inc/str.h +++ b/libc/inc/str.h @@ -17,4 +17,6 @@ int strncmp(const char *s1, const char *s2, u32 n); char *strinv(char *s); char *strdup(const char *s); +const char *strerror(u32 err); + #endif diff --git a/libc/print.c b/libc/print.c index 1c577e5..b687239 100644 --- a/libc/print.c +++ b/libc/print.c @@ -158,8 +158,10 @@ int print(const char *str) // The kernel prints everything into the serial console +#include <mm.h> #include <proc.h> #include <serial.h> + #define RED "\x1B[1;31m" #define GRN "\x1B[1;32m" #define YEL "\x1B[1;33m" @@ -223,6 +225,8 @@ void print_trace(u32 count) __asm__ volatile("movl %%ebp, %0;" : "=r"(stk)); print("EBP\tEIP\n"); for (u32 i = 0; stk && i < count; i++) { + /* u32 eip = memory_valid((void *)stk->eip) ? stk->eip : stk->eip + 64; */ + /* printf("0x%x\t0x%x\n", stk->ebp, eip); */ printf("0x%x\t0x%x\n", stk->ebp, stk->eip); stk = stk->ebp; } @@ -240,6 +244,7 @@ NORETURN void panic(const char *format, ...) #ifdef kernel print("--- DON'T PANIC! ---\n"); print(buf); + print_trace(5); loop(); #else err(1, buf); diff --git a/libc/random.c b/libc/random.c index cfd082d..2801029 100644 --- a/libc/random.c +++ b/libc/random.c @@ -19,7 +19,10 @@ u32 rdrand(void) return rand(); u32 rd; - __asm__ volatile("rdrand %%eax" : "=a"(rd)); + __asm__ volatile("1:\n" + "rdrand %0\n" + "jnc 1b\n" + : "=r"(rd)); return rd; #else return rand(); @@ -33,7 +36,10 @@ u32 rdseed(void) return rand(); u32 rd; - __asm__ volatile("rdseed %%eax" : "=a"(rd)); + __asm__ volatile("1:\n" + "rdseed %0\n" + "jnc 1b\n" + : "=r"(rd)); return rd; #else return rand(); @@ -1,6 +1,7 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include <def.h> +#include <errno.h> #include <mem.h> #include <str.h> @@ -124,3 +125,48 @@ char *strdup(const char *s) return d; } + +static const char *strerrors[EMAX] = { + "Success", + "Operation not permitted", + "No such file or directory", + "No such process", + "Interrupted system call", + "I/O error", + "No such device or address", + "Argument list too long", + "Exec format error", + "Bad file number", + "No child processes", + "Try again", + "Out of memory", + "Permission denied", + "Bad address", + "Block device required", + "Device or resource busy", + "File exists", + "Cross-device link", + "No such device", + "Not a directory", + "Is a directory", + "Invalid argument", + "File table overflow", + "Too many open files", + "Not a typewriter", + "Text file busy", + "File too large", + "No space left on device", + "Illegal seek", + "Read-only file system", + "Too many links", + "Broken pipe", + "Math argument out of domain of func", + "Math result not representable", +}; + +const char *strerror(u32 error) +{ + if (error <= EMAX) + return strerrors[error]; + return "Unknown error"; +} @@ -2,6 +2,7 @@ // Syscall implementation #include <arg.h> +#include <errno.h> #include <sys.h> #if defined(userspace) @@ -10,25 +11,32 @@ * Definitions */ +#define ERRIFY(ret) \ + if (ret < 0) { \ + errno = -ret; \ + return -1; \ + } \ + return ret + int sys0(enum sys num) { int a; __asm__ volatile("int $0x80" : "=a"(a) : "0"(num)); - return a; + ERRIFY(a); } int sys1(enum sys num, int d1) { int a; __asm__ volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)d1)); - return a; + ERRIFY(a); } int sys2(enum sys num, int d1, int d2) { int a; __asm__ volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)d1), "c"((int)d2)); - return a; + ERRIFY(a); } int sys3(enum sys num, int d1, int d2, int d3) @@ -37,7 +45,7 @@ int sys3(enum sys num, int d1, int d2, int d3) __asm__ volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)d1), "c"((int)d2), "d"((int)d3)); - return a; + ERRIFY(a); } int sys4(enum sys num, int d1, int d2, int d3, int d4) @@ -46,7 +54,7 @@ int sys4(enum sys num, int d1, int d2, int d3, int d4) __asm__ volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)d1), "c"((int)d2), "d"((int)d3), "S"((int)d4)); - return a; + ERRIFY(a); } int sys5(enum sys num, int d1, int d2, int d3, int d4, int d5) @@ -56,7 +64,7 @@ int sys5(enum sys num, int d1, int d2, int d3, int d4, int d5) : "=a"(a) : "0"(num), "b"((int)d1), "c"((int)d2), "d"((int)d3), "S"((int)d4), "D"((int)d5)); - return a; + ERRIFY(a); } int sysv(enum sys num, ...) |