Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/kprobes: Set instruction page as executable

Set the page as executable after allocation. This patch is a
preparatory patch for a following patch that makes module allocated
pages non-executable.

While at it, do some small cleanup of what appears to be unnecessary
masking.

Signed-off-by: Nadav Amit <namit@vmware.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: <akpm@linux-foundation.org>
Cc: <ard.biesheuvel@linaro.org>
Cc: <deneen.t.dock@intel.com>
Cc: <kernel-hardening@lists.openwall.com>
Cc: <kristen@linux.intel.com>
Cc: <linux_dti@icloud.com>
Cc: <will.deacon@arm.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Rik van Riel <riel@surriel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/20190426001143.4983-11-namit@vmware.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Nadav Amit and committed by
Ingo Molnar
7298e24f 3c0dab44

+20 -4
+20 -4
arch/x86/kernel/kprobes/core.c
··· 431 431 void *page; 432 432 433 433 page = module_alloc(PAGE_SIZE); 434 - if (page) 435 - set_memory_ro((unsigned long)page & PAGE_MASK, 1); 434 + if (!page) 435 + return NULL; 436 + 437 + /* 438 + * First make the page read-only, and only then make it executable to 439 + * prevent it from being W+X in between. 440 + */ 441 + set_memory_ro((unsigned long)page, 1); 442 + 443 + /* 444 + * TODO: Once additional kernel code protection mechanisms are set, ensure 445 + * that the page was not maliciously altered and it is still zeroed. 446 + */ 447 + set_memory_x((unsigned long)page, 1); 436 448 437 449 return page; 438 450 } ··· 452 440 /* Recover page to RW mode before releasing it */ 453 441 void free_insn_page(void *page) 454 442 { 455 - set_memory_nx((unsigned long)page & PAGE_MASK, 1); 456 - set_memory_rw((unsigned long)page & PAGE_MASK, 1); 443 + /* 444 + * First make the page non-executable, and only then make it writable to 445 + * prevent it from being W+X in between. 446 + */ 447 + set_memory_nx((unsigned long)page, 1); 448 + set_memory_rw((unsigned long)page, 1); 457 449 module_memfree(page); 458 450 } 459 451