Unverified Commit ade26024 authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!3749 support nokaslr and memmap parameter for kaslr collision detection

Merge Pull Request from: @foyjog2 
 
support nokaslr and memmap parameter for kaslr collision detection

Issue
https://gitee.com/openeuler/kernel/issues/I8RJ1I

Config Change
Add:
KASLR_SKIP_MEM_RANGE 
 
Link:https://gitee.com/openeuler/kernel/pulls/3749

 

Reviewed-by: default avatarZhang Jianhua <chris.zjh@huawei.com>
Reviewed-by: default avatarLiu Chao <liuchao173@huawei.com>
Signed-off-by: default avatarZheng Zengkai <zhengzengkai@huawei.com>
Acked-by: default avatarXie XiuQi <xiexiuqi@huawei.com>
parents 311b3705 95019500
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -2290,6 +2290,20 @@ config RANDOMIZE_MODULE_REGION_FULL
	  the region is exhausted. In this particular case of region
	  exhaustion, modules might be able to fall back to a larger 2GB area.

config KASLR_SKIP_MEM_RANGE
	bool "Skip specified memory range when randomize the kernel image"
	depends on RANDOMIZE_BASE
	default n
	help
	  Skip specified memory range when randomize the kernel image. This
	  feature support memmap and nokalsr kernel parameter in arm64.
	  Memmap kernel parameter are described by the memmap=nn[KMG]$ss[KMG],
	  Region of memory to be reserved is from ss to ss+nn. Nokaslr
	  kernel parameters are described by the nokaslr=nn[KMG]-ss[KMG].
	  Region of memory to be reserved is from nn to ss. The region must
	  be in the range of existed memory, otherwise will be ignored.This
	  feature support up to 32 memmap regions and 4 nokaslr regions.

config CC_HAVE_STACKPROTECTOR_SYSREG
	def_bool $(cc-option,-mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 -mstack-protector-guard-offset=0)

+1 −0
Original line number Diff line number Diff line
@@ -535,6 +535,7 @@ CONFIG_ARM64_PSEUDO_NMI=y
# CONFIG_ARM64_DEBUG_PRIORITY_MASKING is not set
CONFIG_RELOCATABLE=y
CONFIG_RANDOMIZE_BASE=y
CONFIG_KASLR_SKIP_MEM_RANGE=y
CONFIG_RANDOMIZE_MODULE_REGION_FULL=y
CONFIG_CC_HAVE_STACKPROTECTOR_SYSREG=y
CONFIG_STACKPROTECTOR_PER_TASK=y
+12 −0
Original line number Diff line number Diff line
@@ -2156,6 +2156,18 @@ config RANDOMIZE_BASE

	  If unsure, say Y.

config KASLR_SKIP_MEM_RANGE
	bool "Skip specified memory range when randomize the kernel image"
	depends on RANDOMIZE_BASE
	default n
	help
	  Skip specified memory range when randomize the kernel image. This
	  feature support nokalsr kernel parameter in x86. Nokaslr kernel
	  parameters are described by the nokaslr=nn[KMG]-ss[KMG].
	  Region of memory to be reserved is from nn to ss. The region must
	  be in the range of existed memory, otherwise will be ignored.This
	  feature support up to 4 nokaslr regions.

# Relocation on x86 needs some additional build support
config X86_NEED_RELOCS
	def_bool y
+45 −0
Original line number Diff line number Diff line
@@ -75,6 +75,10 @@ static unsigned long get_boot_seed(void)
/* Only supporting at most 4 unusable memmap regions with kaslr */
#define MAX_MEMMAP_REGIONS	4

#ifdef CONFIG_KASLR_SKIP_MEM_RANGE
#define MAX_MEM_NOKASLR_REGIONS 4
#endif

static bool memmap_too_large;


@@ -94,6 +98,10 @@ enum mem_avoid_index {
	MEM_AVOID_BOOTPARAMS,
	MEM_AVOID_MEMMAP_BEGIN,
	MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
#ifdef CONFIG_KASLR_SKIP_MEM_RANGE
	MEM_AVOID_MEM_NOKASLR_BEGIN,
	MEM_AVOID_MEM_NOKASLR_END = MEM_AVOID_MEM_NOKASLR_BEGIN + MAX_MEM_NOKASLR_REGIONS - 1,
#endif
	MEM_AVOID_MAX,
};

@@ -223,6 +231,39 @@ static void mem_avoid_memmap(enum parse_mode mode, char *str)
		memmap_too_large = true;
}

#ifdef CONFIG_KASLR_SKIP_MEM_RANGE
static void mem_avoid_mem_nokaslr(char *str)
{
	int i = 0;

	while (str && (i < MAX_MEM_NOKASLR_REGIONS)) {
		char *oldstr;
		u64 start, end;
		char *k = strchr(str, ',');

		if (k)
			*k++ = 0;

		oldstr = str;
		start = memparse(str, &str);
		if (str == oldstr || *str != '-') {
			warn("Nokaslr values error.\n");
			break;
		}

		end = memparse(str + 1, &str);
		if (start >= end) {
			warn("Nokaslr values error, start should be less than end.\n");
			break;
		}

		mem_avoid[MEM_AVOID_MEM_NOKASLR_BEGIN + i].start = start;
		mem_avoid[MEM_AVOID_MEM_NOKASLR_BEGIN + i].size = end - start;
		str = k;
		i++;
	}
}
#endif
/* Store the number of 1GB huge pages which users specified: */
static unsigned long max_gb_huge_pages;

@@ -298,6 +339,10 @@ static void handle_mem_options(void)
		} else if (!strcmp(param, "efi_fake_mem")) {
			mem_avoid_memmap(PARSE_EFI, val);
		}
#ifdef CONFIG_KASLR_SKIP_MEM_RANGE
		else if (!strcmp(param, "nokaslr") && val)
			mem_avoid_mem_nokaslr(val);
#endif
	}

	free(tmp_cmdline);
+1 −0
Original line number Diff line number Diff line
@@ -497,6 +497,7 @@ CONFIG_ARCH_SUPPORTS_CRASH_HOTPLUG=y
CONFIG_PHYSICAL_START=0x1000000
CONFIG_RELOCATABLE=y
CONFIG_RANDOMIZE_BASE=y
CONFIG_KASLR_SKIP_MEM_RANGE=y
CONFIG_X86_NEED_RELOCS=y
CONFIG_PHYSICAL_ALIGN=0x200000
CONFIG_DYNAMIC_MEMORY_LAYOUT=y
Loading