[PATCH 1/9] drivers/char/random: Eliminate randomize_range().

From: George Spelvin
Date: Wed Mar 16 2011 - 11:42:52 EST


This is only called in three places, each of which is trivially
replaced by a call to get_random_int() followed by a bit mask.
(It's to randomize the start of the brk() range by 0..32M bytes,
0..8K pages, which is 13 bits of entropy.)

There is a slight behaviour change, as randomize_range() used PAGE_ALIGN()
which rounds up, but it appears that rounding down was the intention.
---
arch/arm/kernel/process.c | 3 +--
arch/x86/kernel/process.c | 3 +--
arch/x86/kernel/sys_x86_64.c | 7 ++-----
drivers/char/random.c | 19 -------------------
include/linux/random.h | 1 -
5 files changed, 4 insertions(+), 29 deletions(-)

diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
index 94bbedb..ffb7c87 100644
--- a/arch/arm/kernel/process.c
+++ b/arch/arm/kernel/process.c
@@ -479,8 +479,7 @@ unsigned long get_wchan(struct task_struct *p)

unsigned long arch_randomize_brk(struct mm_struct *mm)
{
- unsigned long range_end = mm->brk + 0x02000000;
- return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
+ return mm->brk + (get_random_int() & 0x01ffffff & PAGE_MASK);
}

#ifdef CONFIG_MMU
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index ff45541..dcec1a1 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -677,7 +677,6 @@ unsigned long arch_align_stack(unsigned long sp)

unsigned long arch_randomize_brk(struct mm_struct *mm)
{
- unsigned long range_end = mm->brk + 0x02000000;
- return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
+ return mm->brk + (get_random_int() & 0x01ffffff & PAGE_MASK);
}

diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c
index ff14a50..0f874f7 100644
--- a/arch/x86/kernel/sys_x86_64.c
+++ b/arch/x86/kernel/sys_x86_64.c
@@ -46,11 +46,8 @@ static void find_start_end(unsigned long flags, unsigned long *begin,
of playground for now. -AK */
*begin = 0x40000000;
*end = 0x80000000;
- if (current->flags & PF_RANDOMIZE) {
- new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
- if (new_begin)
- *begin = new_begin;
- }
+ if (current->flags & PF_RANDOMIZE)
+ *begin += (get_random_int() & 0x01ffffff & PAGE_MASK);
} else {
*begin = TASK_UNMAPPED_BASE;
*end = TASK_SIZE;
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 72a4fcb..cea9ddc 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1639,22 +1639,3 @@ unsigned int get_random_int(void)

return ret;
}
-
-/*
- * randomize_range() returns a start address such that
- *
- * [...... <range> .....]
- * start end
- *
- * a <range> with size "len" starting at the return value is inside in the
- * area defined by [start, end], but is otherwise randomized.
- */
-unsigned long
-randomize_range(unsigned long start, unsigned long end, unsigned long len)
-{
- unsigned long range = end - len - start;
-
- if (end <= start + len)
- return 0;
- return PAGE_ALIGN(get_random_int() % range + start);
-}
diff --git a/include/linux/random.h b/include/linux/random.h
index fb7ab9d..0e17434 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -73,7 +73,6 @@ extern const struct file_operations random_fops, urandom_fops;
#endif

unsigned int get_random_int(void);
-unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len);

u32 random32(void);
void srandom32(u32 seed);
--
1.7.4.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/