BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / commit / 665af0a43af0d406733bda816ca1a7b2f9184319 / memory / user.c

// Related

Nucleus

Barry Improved page fault handling and mmap system call 665af0a (3 years, 2 months ago)
diff --git a/memory/user.c b/memory/user.c
index cdf4bcd..ec19d1f 100644
--- a/memory/user.c
+++ b/memory/user.c
@@ -6,19 +6,27 @@
  */
 
 #include <nucleus/memory.h>
+#include <nucleus/task.h>
+#include "namespace.h"
+
+VMRegion *find_region(uintptr_t addr);
 
 /* Check if user has access to a region of memory */
 int
 verify_access(const void *addr, size_t len, int prot)
 {
-	/*
-	 * This should iterate all memory regions to check if the address range
-	 * fits inside one, if not then access should be denied.  If a matching
-	 * region is found, the prot parameter should be checked against the
-	 * protection value for that region.  If this function is called from
-	 * the kernel directly, or the user did not pass an address or a length
-	 * (NULL and 0, respectively), then it should just grant access
-	 * immediately.  This can be implemented when userspace is implemented.
-	 */
-	return 1;
+	if (!len || !(current && current->inSyscall))
+		return 1;
+
+	VMRegion *region;
+	int minprot = ~0;
+	uintptr_t end = (uintptr_t) addr;
+	do {
+		region = find_region(end);
+		if (!region)
+			return 0;
+		minprot &= region->prot;
+		end = region->end;
+	} while (end < (uintptr_t) addr + len);
+	return (minprot & prot);
 }