linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: lauraa@codeaurora.org (Laura Abbott)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC PATCH 4/5] arm: mm: restrict kernel memory permissions if CONFIG_STRICT_MEMORY_RWX set
Date: Tue,  8 Oct 2013 18:31:31 -0700	[thread overview]
Message-ID: <1381282292-25251-5-git-send-email-lauraa@codeaurora.org> (raw)
In-Reply-To: <1381282292-25251-1-git-send-email-lauraa@codeaurora.org>

If CONFIG_STRICT_MEMORY_RWX is set, make kernel text RX,
kernel data/stack RW and rodata RO so that writing
on kernel text, executing kernel data or stack, or
writing on or executing read-only data is prohibited.

Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>
---
 arch/arm/mm/mmu.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 1 deletions(-)

diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c
index d846334..91db2a0 100644
--- a/arch/arm/mm/mmu.c
+++ b/arch/arm/mm/mmu.c
@@ -1311,6 +1311,60 @@ static void __init kmap_init(void)
 #endif
 }
 
+struct custom_map {
+	unsigned long start;
+	unsigned long end;
+	unsigned int type;
+};
+
+struct custom_map __initdata custom_maps[] = {
+	{
+		.start	= _stext,
+		.end	= __start_rodata,
+		.type	= MT_MEMORY_RX,
+	},
+	{
+		.start	= __start_rodata,
+		.end	= __init_begin,
+		.type	= MT_MEMORY_R
+	},
+	{
+		.start	= __init_begin,
+		.end	= __arch_info_begin,
+		.type	= MT_MEMORY_RX,
+	}
+};
+
+static void __init map_custom_regions(void)
+{
+#ifdef CONFIG_STRICT_MEMORY_RWX
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(custom_maps); i++) {
+		struct map_desc map;
+		unsigned long addr;
+
+		if (!IS_ALIGNED(custom_maps[i].start, PMD_SIZE) ||
+		    !IS_ALIGNED(custom_maps[i].end, PMD_SIZE)) {
+			pr_err("BUG: section %x-%x not aligned to %x\n",
+				custom_maps[i].start, custom_maps[i].end,
+				PMD_SIZE);
+			continue;
+		}
+
+		for (addr = custom_maps[i].start;
+			addr < custom_maps[i].end; addr += PMD_SIZE)
+				pmd_clear(pmd_off_k(addr));
+
+		map.virtual = custom_maps[i].start;
+		map.pfn = __phys_to_pfn(__virt_to_phys(custom_maps[i].start));
+		map.length = custom_maps[i].end - custom_maps[i].start;
+		map.type = custom_maps[i].type;
+		create_mapping(&map);
+	}
+#endif
+}
+
 static void __init map_lowmem(void)
 {
 	struct memblock_region *reg;
@@ -1329,10 +1383,11 @@ static void __init map_lowmem(void)
 		map.pfn = __phys_to_pfn(start);
 		map.virtual = __phys_to_virt(start);
 		map.length = end - start;
-		map.type = MT_MEMORY;
+		map.type = MT_MEMORY_RW;
 
 		create_mapping(&map);
 	}
+	map_custom_regions();
 }
 
 /*
-- 
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation

  parent reply	other threads:[~2013-10-09  1:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-09  1:31 [RFC] Stricter kernel memory permissions Laura Abbott
2013-10-09  1:31 ` [RFC PATCH 1/5] arm: mm: add CONFIG_STRICT_MEMORY_RWX Laura Abbott
2013-10-09 16:47   ` Larry Bassel
2013-10-09 20:04   ` Kees Cook
2013-10-10  0:18     ` Laura Abbott
2013-10-09  1:31 ` [RFC PATCH 2/5] arm: mm: add new memory mapping types Laura Abbott
2013-10-09  1:31 ` [RFC PATCH 3/5] arm: align init, text, rodata to PMD_SIZE if CONFIG_STRICT_MEMORY_RWX is set Laura Abbott
2013-10-09  1:31 ` Laura Abbott [this message]
2013-10-09  1:31 ` [RFC PATCH 5/5] arm: Don't free init text if CONFIG_STRICT_MEMORY_RWX is enabled Laura Abbott
2013-10-17 21:15 ` [RFC] Stricter kernel memory permissions Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1381282292-25251-5-git-send-email-lauraa@codeaurora.org \
    --to=lauraa@codeaurora.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).