aboutsummaryrefslogtreecommitdiff
path: root/kernel/features
diff options
context:
space:
mode:
authorMarvin Borner2021-04-01 19:39:14 +0200
committerMarvin Borner2021-04-01 19:39:14 +0200
commitafa00abb2b68205bee539d7947130d6b1b1ec6e9 (patch)
tree3a821a75af6c4d4ff1bd4128c4859d77abf87e66 /kernel/features
parent4c168fb34c15a1b8981abef7ccef1542a6fb05ca (diff)
Hardened entire system
By using the nonnull attribute and replace buffer-overflow-prone functions like strcpy, strcat and sprintf by strlcpy, strlcat and snprintf.
Diffstat (limited to 'kernel/features')
-rw-r--r--kernel/features/fs.c24
-rw-r--r--kernel/features/load.c4
-rw-r--r--kernel/features/mm.c4
-rw-r--r--kernel/features/net.c8
-rw-r--r--kernel/features/proc.c6
5 files changed, 22 insertions, 24 deletions
diff --git a/kernel/features/fs.c b/kernel/features/fs.c
index d16b7b4..753adb4 100644
--- a/kernel/features/fs.c
+++ b/kernel/features/fs.c
@@ -109,10 +109,10 @@ static void vfs_list_mounts()
res vfs_mount(struct device *dev, const char *path)
{
- if (!path || !memory_valid(path))
+ if (!memory_valid(path))
return -EFAULT;
- if (!dev || !memory_valid(dev) || !dev->id)
+ if (!memory_valid(dev) || !dev->id)
return -EFAULT;
if (vfs_mounted(dev, path))
@@ -131,10 +131,10 @@ res vfs_mount(struct device *dev, const char *path)
res vfs_read(const char *path, void *buf, u32 offset, u32 count)
{
/* printf("%s READ: %s\n", proc_current() ? proc_current()->name : "Unknown", path); */
- if (!path || !memory_valid(path))
+ if (!memory_valid(path))
return -EFAULT;
- if (!buf || !memory_valid(buf))
+ if (!memory_valid(buf))
return -EFAULT;
struct mount_info *m = vfs_find_mount_info(path);
@@ -160,10 +160,10 @@ res vfs_read(const char *path, void *buf, u32 offset, u32 count)
res vfs_write(const char *path, void *buf, u32 offset, u32 count)
{
/* printf("%s WRITE: %s\n", proc_current() ? proc_current()->name : "Unknown", path); */
- if (!path || !memory_valid(path))
+ if (!memory_valid(path))
return -EFAULT;
- if (!buf || !memory_valid(buf))
+ if (!memory_valid(buf))
return -EFAULT;
struct mount_info *m = vfs_find_mount_info(path);
@@ -188,7 +188,7 @@ res vfs_write(const char *path, void *buf, u32 offset, u32 count)
res vfs_ioctl(const char *path, u32 request, void *arg1, void *arg2, void *arg3)
{
- if (!path || !memory_valid(path))
+ if (!memory_valid(path))
return -EFAULT;
struct mount_info *m = vfs_find_mount_info(path);
@@ -210,10 +210,10 @@ res vfs_ioctl(const char *path, u32 request, void *arg1, void *arg2, void *arg3)
res vfs_stat(const char *path, struct stat *buf)
{
- if (!path || !memory_valid(path))
+ if (!memory_valid(path))
return -EFAULT;
- if (!buf || !memory_valid(buf))
+ if (!memory_valid(buf))
return -EFAULT;
struct mount_info *m = vfs_find_mount_info(path);
@@ -235,7 +235,7 @@ res vfs_stat(const char *path, struct stat *buf)
res vfs_wait(const char *path, u32 func_ptr)
{
- if (!path || !func_ptr || !memory_valid(path))
+ if (!func_ptr || !memory_valid(path))
return -EFAULT;
struct mount_info *m = vfs_find_mount_info(path);
@@ -257,7 +257,7 @@ res vfs_wait(const char *path, u32 func_ptr)
res vfs_poll(const char **files)
{
- if (!files || !memory_valid(files))
+ if (!memory_valid(files))
return -EFAULT;
for (const char **p = files; *p && memory_valid(*p) && **p; p++) {
@@ -276,7 +276,7 @@ res vfs_poll(const char **files)
res vfs_ready(const char *path)
{
- if (!path || !memory_valid(path))
+ if (!memory_valid(path))
return -EFAULT;
struct mount_info *m = vfs_find_mount_info(path);
diff --git a/kernel/features/load.c b/kernel/features/load.c
index b46f772..c5039ed 100644
--- a/kernel/features/load.c
+++ b/kernel/features/load.c
@@ -11,7 +11,7 @@
res elf_load(const char *path, struct proc *proc)
{
- if (!path || !memory_valid(path) || !proc)
+ if (!memory_valid(path))
return -EFAULT;
struct stat s = { 0 };
@@ -30,7 +30,7 @@ res elf_load(const char *path, struct proc *proc)
if (read != sizeof(header))
return -ENOEXEC;
- strcpy(proc->name, path);
+ strlcpy(proc->name, path, sizeof(proc->name));
// Valid?
u8 *magic = header.ident;
diff --git a/kernel/features/mm.c b/kernel/features/mm.c
index d01c978..5356ab4 100644
--- a/kernel/features/mm.c
+++ b/kernel/features/mm.c
@@ -417,7 +417,7 @@ struct memory_proc_link {
static struct list *memory_objects = NULL;
res memory_sys_alloc(struct page_dir *dir, u32 size, u32 *addr, u32 *id, u8 shared)
{
- if (!addr || !memory_valid(addr) || !id || !memory_valid(id))
+ if (!memory_valid(addr) || !memory_valid(id))
return -EFAULT;
size = PAGE_ALIGN_UP(size);
@@ -474,7 +474,7 @@ res memory_sys_free(struct page_dir *dir, u32 addr)
res memory_sys_shaccess(struct page_dir *dir, u32 id, u32 *addr, u32 *size)
{
- if (!addr || !memory_valid(addr) || !size || !memory_valid(size))
+ if (!memory_valid(addr) || !memory_valid(size))
return -EFAULT;
*addr = 0;
diff --git a/kernel/features/net.c b/kernel/features/net.c
index 104538f..48cbf55 100644
--- a/kernel/features/net.c
+++ b/kernel/features/net.c
@@ -732,7 +732,7 @@ struct socket *net_open(enum socket_type type)
int net_close(struct socket *socket)
{
- if (!net_installed() || !socket)
+ if (!net_installed())
return 1;
if (socket->state == S_CLOSING)
@@ -750,7 +750,7 @@ int net_close(struct socket *socket)
int net_connect(struct socket *socket, u32 ip_addr, u16 dst_port)
{
- if (!net_installed() || !socket || socket->state != S_OPEN || !ip_addr || !dst_port)
+ if (!net_installed() || socket->state != S_OPEN || !ip_addr || !dst_port)
return 0;
socket->ip_addr = ip_addr;
@@ -777,7 +777,7 @@ int net_connect(struct socket *socket, u32 ip_addr, u16 dst_port)
void net_send(struct socket *socket, void *data, u32 len)
{
- if (!net_installed() || !socket || socket->state != S_CONNECTED)
+ if (!net_installed() || socket->state != S_CONNECTED)
return;
if (socket->type == S_TCP) {
@@ -792,7 +792,7 @@ void net_send(struct socket *socket, void *data, u32 len)
int net_receive(struct socket *socket, void *buf, u32 len)
{
- if (!net_installed() || !socket || !socket->packets)
+ if (!net_installed() || !socket->packets)
return 0;
u32 offset = 0;
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index ceaf27b..fde49bd 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -24,7 +24,7 @@ struct node *current = NULL;
// TODO: Use less memcpy and only copy relevant registers (rewrite for efficiency argh)
// TODO: 20 priority queues (https://www.kernel.org/doc/html/latest/scheduler/sched-nice-design.html)
// TODO: Optimize scheduler
-void scheduler(struct regs *regs)
+HOT FLATTEN void scheduler(struct regs *regs)
{
if (quantum == 0) {
quantum = PROC_QUANTUM;
@@ -119,8 +119,6 @@ void proc_clear_quantum(void)
void proc_exit(struct proc *proc, s32 status)
{
- assert(proc);
-
u8 found = 0;
struct node *iterator = proc_list->head;
while (iterator) {
@@ -272,7 +270,7 @@ struct proc *proc_make(enum proc_priv priv)
void proc_stack_push(struct proc *proc, u32 data)
{
- assert(proc && proc->regs.useresp > sizeof(data));
+ assert(proc->regs.useresp > sizeof(data));
struct page_dir *prev;
memory_backup_dir(&prev);