Sfoglia il codice sorgente

Linux Kernel: mprotect address must be page-aligned

Alexander Shiryaev 12 anni fa
parent
commit
b7ed10f86d

BIN
BlackBox/Linux/Lin/Mod/Kernel.odc


+ 11 - 1
BlackBox/Linux/Lin/Mod/Kernel.txt

@@ -262,6 +262,7 @@ MODULE Kernel;
 		watcher*: PROCEDURE (event: INTEGER);	(* for debugging *)
 
 		zerofd: INTEGER;
+		pageSize: INTEGER;
 
 		loopContext: Libc.sigjmp_buf; (* trap return context, if no Kernel.Try has been used. *)
 		currentTryContext: POINTER TO Libc.sigjmp_buf; (* trap return context, if Kernel.Try has been used. *)
@@ -824,7 +825,10 @@ MODULE Kernel;
 			actual := mod;
 
 			(* A. V. Shiryaev: Allow execution on code pages *)
-				res := Libc.mprotect(mod.code, mod.csize,
+			(* Linux: must be page-aligned *)
+				res := Libc.mprotect(
+					(mod.code DIV pageSize) * pageSize,
+					((mod.csize + mod.code MOD pageSize - 1) DIV pageSize) * pageSize + pageSize,
 					Libc.PROT_READ + Libc.PROT_WRITE + Libc.PROT_EXEC);
 				IF res = -1 THEN
 					S.GET( Libc.__errno_location(), errno );
@@ -1923,6 +1927,12 @@ MODULE Kernel;
 				Msg("ERROR: Kernel.Init: can not open /dev/zero!");
 				Libc.exit(1)
 			END;
+		(* for mprotect *)
+			pageSize := Libc.sysconf(Libc._SC_PAGESIZE);
+			IF pageSize < 0 THEN
+				Msg("ERROR: Kernel.Init: pageSize < 0!");
+				Libc.exit(1)
+			END;
 
 		InstallSignals; (* init exception handling *)
 		currentTryContext := NIL;

BIN
BlackBox/Linux/Lin/Mod/Libc.odc


+ 4 - 0
BlackBox/Linux/Lin/Mod/Libc.txt

@@ -237,6 +237,8 @@ MODULE LinLibc ["libc.so.6"];
 		O_WRONLY* = {0} ;	(* Open write-only *)
 		O_RDWR* = {1} ;	(* Open read/write *)
 
+		_SC_PAGESIZE* = 30;
+
 	TYPE 
 		__ftw_func_t* = PROCEDURE (fileName: PtrSTR; VAR [nil] stat: stat_t; flag: INTEGER): INTEGER;
 		PtrVoid* = INTEGER;
@@ -449,4 +451,6 @@ MODULE LinLibc ["libc.so.6"];
 
 	PROCEDURE [ccall] getenv* (name: PtrSTR): PtrSTR;
 
+	PROCEDURE [ccall] sysconf* (name: INTEGER): INTEGER;
+
 END LinLibc.