From 5683afd1adee798a9288c7d01afc8029c65fe94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 14 Mar 2021 12:59:00 +0100 Subject: check stack pointer --- src/userprog/build/fail | 2 -- src/userprog/syscall.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/userprog/build/fail b/src/userprog/build/fail index d20593f..fc47d60 100644 --- a/src/userprog/build/fail +++ b/src/userprog/build/fail @@ -1,4 +1,3 @@ -FAIL tests/userprog/sc-bad-sp FAIL tests/userprog/sc-bad-arg FAIL tests/userprog/create-null FAIL tests/userprog/create-bad-ptr @@ -8,4 +7,3 @@ FAIL tests/userprog/read-bad-ptr FAIL tests/userprog/write-bad-ptr FAIL tests/userprog/exec-bad-ptr FAIL tests/userprog/wait-twice -FAIL tests/userprog/wait-killed diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 52526aa..a699784 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -9,9 +9,11 @@ #include "filesys/filesys.h" #include "filesys/off_t.h" #include "lib/pid_t.h" +#include "userprog/pagedir.h" #include "userprog/process.h" #include "threads/init.h" #include "threads/malloc.h" +#include "threads/vaddr.h" static void syscall_handler (struct intr_frame *); @@ -162,13 +164,40 @@ exec (const char *file_name) return process_execute (file_name); } +static bool +ptr_is_valid (const void *ptr) +{ + // uintptr_t ptr = _ptr; + if (!is_user_vaddr (ptr)) + return false; + + struct thread *t = thread_current (); + + if (pagedir_get_page (t->pagedir, ptr) == NULL) + return false; + + return true; +} + // cast to TYPE and deref argument N from f->esp #define INTR_ESP(N, TYPE) *(TYPE *)(f->esp+(4*(N))) +#define CHECK_ESP_AND_MAYBE_EXIT(N) \ + do { \ + if (!ptr_is_valid ((f->esp+(4*(N))))) { \ + exit (-1); \ + return; \ + } \ + } while (0) + + static void syscall_handler (struct intr_frame *f UNUSED) { + // check esp + CHECK_ESP_AND_MAYBE_EXIT (0); int syscall_number = INTR_ESP (0, int); + switch (syscall_number) { case 0: // halt -- cgit v1.2.1