소스 검색

work around linux bug in mprotect

per POSIX: The mprotect() function shall change the access protections
to be that specified by prot for those whole pages containing any part
of the address space of the process starting at address addr and
continuing for len bytes.

on the other hand, linux mprotect fails with EINVAL if the base
address and/or length is not page-aligned, so we have to align them
before making the syscall.
Rich Felker 13 년 전
부모
커밋
af3d5405b8
1개의 변경된 파일5개의 추가작업 그리고 1개의 파일을 삭제
  1. 5 1
      src/mman/mprotect.c

+ 5 - 1
src/mman/mprotect.c

@@ -1,7 +1,11 @@
 #include <sys/mman.h>
+#include <limits.h>
 #include "syscall.h"
 
 int mprotect(void *addr, size_t len, int prot)
 {
-	return syscall(SYS_mprotect, addr, len, prot);
+	size_t start, end;
+	start = (size_t)addr & -PAGE_SIZE;
+	end = (size_t)((char *)addr + len + PAGE_SIZE-1) & -PAGE_SIZE;
+	return syscall(SYS_mprotect, start, end-start, prot);
 }