Feat: pointer validation checks string across multiple pages and handle kernel page faults

This commit is contained in:
EDiasAlberto
2024-11-29 23:49:49 +00:00
parent 5f40d83e66
commit 5c661c2e24
4 changed files with 37 additions and 27 deletions

View File

@@ -10,9 +10,6 @@
#include "threads/synch.h"
#include "userprog/process.h"
#include "userprog/pagedir.h"
#ifdef VM
#include "vm/stackgrowth.h"
#endif
#include <stdio.h>
#include <stdbool.h>
#include <syscall-nr.h>
@@ -465,17 +462,17 @@ validate_user_pointer (const void *ptr, size_t size, bool check_write)
valid user virtual memory address. */
void *last = ptr + size - 1;
if (!is_user_vaddr (last))
thread_exit ();
syscall_exit (EXIT_FAILURE);
ptr = pg_round_down (ptr);
while (ptr <= last)
{
int result;
/* Check read access to pointer. */
if ((result = get_user (ptr)) == -1)
thread_exit ();
syscall_exit (EXIT_FAILURE);
/* Check write access to pointer (if required). */
if (check_write && !put_user (ptr, result))
thread_exit ();
syscall_exit (EXIT_FAILURE);
ptr += PGSIZE;
}
}
@@ -485,18 +482,33 @@ validate_user_pointer (const void *ptr, size_t size, bool check_write)
static void
validate_user_string (const char *ptr, bool check_write)
{
while (true)
size_t offset = (uintptr_t) ptr % PGSIZE;
for (;;)
{
void *page = pg_round_down (ptr);
if (!is_user_vaddr (page))
syscall_exit (EXIT_FAILURE);
if (!is_user_vaddr (ptr))
thread_exit ();
syscall_exit (EXIT_FAILURE);
int result;
if ((result = get_user ((const uint8_t *)ptr)) == -1)
thread_exit ();
syscall_exit (EXIT_FAILURE);
if (check_write && !put_user ((uint8_t *)ptr, result))
thread_exit ();
if (*ptr == '\0')
return;
ptr++;
syscall_exit (EXIT_FAILURE);
while (offset < PGSIZE)
{
if (*ptr == '\0')
return; /* We reached the end of the string without issues. */
ptr++;
offset++;
}
offset = 0;
}
}