char/random: Simplify random address requests
Subsystems (5)
cover char/random: Simplify random address requests
Two previous attempts have been made to rework this API. The first can be found at:
https://lkml.kernel.org/r/[email protected]
The second at:
https://lkml.kernel.org/r/[email protected]
The RFC version of this series can been seen at:
https://lkml.kernel.org/r/[email protected]
In addition to incorporating ideas from these two previous efforts, this series adds several desirable features. First, we take the range as an argument directly, which removes math both before the call and inside the function. Second, we return the start address on error. All callers fell back to the start address on error, so we remove the need to check for errors. Third, we cap range to prevent overflow. Last, we use kerneldoc to describe the new function.
If possible, I'd like to request Acks from the various subsystems so that we can merge this as one bisectable branch.
Jason Cooper (7):
random: Simplify API for random address requests
x86: Use simpler API for random address requests
ARM: Use simpler API for random address requests
arm64: Use simpler API for random address requests
tile: Use simpler API for random address requests
unicore32: Use simpler API for random address requests
random: Remove unused randomize_range()
arch/arm/kernel/process.c | 3 +-- arch/arm64/kernel/process.c | 8 ++------ arch/tile/mm/mmap.c | 3 +-- arch/unicore32/kernel/process.c | 3 +-- arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- drivers/char/random.c | 29 ++++++++++++++++++----------- include/linux/random.h | 2 +- 8 files changed, 26 insertions(+), 30 deletions(-)
Signature · 1 line
2.9.2
PATCH 1/7 random: Simplify API for random address requests
To date, all callers of randomize_range() have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.
We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by get_random_int(), since all current range requests are < UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.
Address generation within [start, start + range) behavior is preserved.
All current callers to randomize_range() chose to use the start address if randomize_range() failed. Therefore, we simplify things by just returning the start address on error.
randomize_range() will be removed once all callers have been converted over to randomize_addr().
--- drivers/char/random.c | 26 ++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 27 insertions(+)
drivers/char/random.c +26 -0
@@ -1840,6 +1840,32 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_addr - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* Before page alignment, the random address generated can be any value from +* @start, to @start + @range - 1 inclusive. +* +* If @start + @range would overflow, @range is capped. +* +* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_addr (unsigned long start,unsigned long range) +{ +if (range ==0 ) +return start; + +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + +return PAGE_ALIGN (get_random_long () % range + start); +} +/* Interface for in-kernel drivers of true hardware RNGs. * Those devices may produce endless random bits and will be throttled * when our pool is full.
include/linux/random.h +1 -0
@@ -35,6 +35,7 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void );unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len); +unsigned long randomize_addr (unsigned long start,unsigned long range);u32 prandom_u32 (void );void prandom_bytes (void *buf,size_t nbytes);
Signature · 1 line
2.9.2
Re: [PATCH 1/7] random: Simplify API for random address requests
Hi,
Le jeudi 28 juillet 2016 à 20:47 +0000, Jason Cooper a écrit :
Quoted 43 lines
To date, all callers of
randomize_range()have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by
get_random_int(), since all current range requests are < UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.Address generation within [start, start + range) behavior is preserved.
All current callers to
randomize_range()chose to use the start address ifrandomize_range()failed. Therefore, we simplify things by just returning the start address on error.
randomize_range()will be removed once all callers have been converted over torandomize_addr().Signed-off-by Jason Cooper <[email protected]>---
drivers/char/random.c | 26 ++++++++++++++++++++++++++include/linux/random.h| 1 +2 files changed, 27 insertions(+)drivers/char/random.c +0
-0@@ -1840,6 +1840,32 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len) return PAGE_ALIGN(get_random_int() % range + start); }
+/**
+ *randomize_addr- Generate a random, page aligned address
+ * @start: The smallest acceptable address the caller will take.
+ * @range: The size of the area, starting at @start, within which the
Quoted 8 lines
+ * random address must fall.
+ *
+ * Before page alignment, the random address generated can be any value from
+ * @start, to @start + @range - 1 inclusive.
+ *
+ * If @start + @range would overflow, @range is capped.
+ *
+ * Return: A page aligned address within [start, start + range).
PAGE_ALIGN(start + range - 1) can be greater than start + range ..
In the worst case, when start = 0, range = ULONG_MAX, the result would be 0.
In order to stay in the bounds, the start address must be rounded up, and the random offset must be rounded down.
Something I haven't found the time to send was looking like this:
unsigned long base = PAGE_ALIGN(start);
range -= (base - start);
range >>= PAGE_SHIFT;
return base + ((get_random_int() % range) << PAGE_SHIFT);
Quoted 27 lines
On error,
+ * @start is returned.
+ */drivers/char/random.c +12
-0+unsigned long +randomize_addr (unsigned long start,unsigned long range) +{ +if (range ==0 ) +return start; + +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + +return PAGE_ALIGN (get_random_long () % range + start); +} +/* Interface for in-kernel drivers of true hardware RNGs.
* Those devices may produce endless random bits and will be throttled
* when our pool is full.include/linux/random.h +1
-0@@ -35,6 +35,7 @@ extern const struct file_operations random_fops, urandom_fops; unsigned int get_random_int(void); unsigned long get_random_long(void); unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); +unsigned long randomize_addr (unsigned long start,unsigned long range);
Quoted 3 lines
u32prandom_u32(void);
voidprandom_bytes(void *buf, size_t nbytes);
Regards.
Signature · 2 lines
Yann Droneaud OPTEYA
Re: [PATCH 1/7] random: Simplify API for random address requests
Hi Yann,
First, thanks for the review!
Quoted 46 lines · Yann Droneaud
Le jeudi 28 juillet 2016 à 20:47 +0000, Jason Cooper a écrit :
Quoted 45 lines
To date, all callers of
randomize_range()have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by
get_random_int(), since all current range requests are < UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.Address generation within [start, start + range) behavior is preserved.
All current callers to
randomize_range()chose to use the start address ifrandomize_range()failed. Therefore, we simplify things by just returning the start address on error.
randomize_range()will be removed once all callers have been converted over torandomize_addr().Signed-off-by Jason Cooper <[email protected]>---
drivers/char/random.c | 26 ++++++++++++++++++++++++++include/linux/random.h| 1 +2 files changed, 27 insertions(+)drivers/char/random.c +0
-0@@ -1840,6 +1840,32 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len) return PAGE_ALIGN(get_random_int() % range + start); }
+/**
+ *randomize_addr- Generate a random, page aligned address
+ * @start: The smallest acceptable address the caller will take.
+ * @range: The size of the area, starting at @start, within which the
+ * random address must fall.
+ *
Quoted 8 lines
Quoted 6 lines
+ * Before page alignment, the random address generated can be any value from
+ * @start, to @start + @range - 1 inclusive.
+ *
+ * If @start + @range would overflow, @range is capped.
+ *
+ * Return: A page aligned address within [start, start + range).
PAGE_ALIGN(start + range - 1)can be greater than start + range ..
Ok, so I need to reword my Return desription. :)
Quoted 5 lines
In the worst case, when start = 0, range =
ULONG_MAX, the result would be 0.In order to stay in the bounds, the start address must be rounded up, and the random offset must be rounded down.
Well, I'm trying to preserve existing behavior. Of which, it seems to be presumed that start was page aligned. Since it was used unaltered in all cases when randomize_range failed.
I'll add that to the kerneldoc.
Quoted 5 lines
Something I haven't found the time to send was looking like this:
unsigned long base =
PAGE_ALIGN(start);range -= (base - start);
I think the above two lines are unnecessary due to my comment above.
Quoted 3 lines
range >>=
PAGE_SHIFT;return base + ((
get_random_int()% range) <<PAGE_SHIFT);
However, this is interesting. Instead of a random address, you're picking a random page. If we combine this with the requirement that start be page aligned, we can remove the PAGE_ALIGN(). Which neatly handles your first listed concern.
Quoted 5 lines
Quoted 5 lines
On error,
+ * @start is returned.
+ */drivers/char/random.c +2
-0+unsigned long +randomize_addr (unsigned long start,unsigned long range)
Quoted 8 lines
drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1866
Quoted 8 lines
drivers/char/random.c +8
-0+{ +if (range ==0 ) +return start; + +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + +return PAGE_ALIGN (get_random_long () % range + start);
On digging in to this, I found the following scenario:
start=ULONG_MAX, range=ULONG_MAX
range=0 by our second test
UB by get_random_long() % 0
This should be mitigated by swapping the tests. So, we would have:
unsigned longrandomize_addr(unsigned long start, unsigned long range)
{
if (start > ULONG_MAX - range)
range = ULONG_MAX - start;
range >>= PAGE_SHIFT;
if (range == 0)
return start;
return start + ((get_random_long() % range) << PAGE_SHIFT);
}Look better?
thx,
Jason.
PATCH 2/7 x86: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
--- arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- 2 files changed, 2 insertions(+), 6 deletions(-)
arch/x86/kernel/process.c +1 -2
@@ -507,8 +507,7 @@ 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 randomize_addr (mm->brk,0x02000000 ); }/*
arch/x86/kernel/sys_x86_64.c +1 -4
@@ -101,7 +101,6 @@ static void find_start_end(unsigned long flags, unsigned long *begin, unsigned long *end) {if (!test_thread_flag (TIF_ADDR32 ) && (flags &MAP_32BIT )) { -unsigned long new_begin;/* This is usually used needed to map code in small model, so it needs to be in the first 31bit. Limit it to that. This means we need to move the
@@ -112,9 +111,7 @@ static void find_start_end(unsigned long flags, unsigned long *begin, *begin =0x40000000 ; *end =0x80000000 ;if (current->flags &PF_RANDOMIZE ) { - new_begin =randomize_range (*begin, *begin +0x02000000 ,0 ); -if (new_begin) - *begin = new_begin; + *begin =randomize_addr (*begin,0x02000000 ); } }else { *begin = current->mm->mmap_legacy_base;
Signature · 1 line
2.9.2
PATCH 3/7 ARM: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
--- arch/arm/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/kernel/process.c +1 -2
@@ -314,8 +314,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 randomize_addr (mm->brk,0x02000000 ); }#ifdef CONFIG_MMU
Signature · 1 line
2.9.2
PATCH 4/7 arm64: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
--- arch/arm64/kernel/process.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/arm64/kernel/process.c +2 -6
@@ -374,12 +374,8 @@ unsigned long arch_align_stack(unsigned long sp)unsigned long arch_randomize_brk (struct mm_struct *mm) { -unsigned long range_end = mm->brk; -if (is_compat_task ()) - range_end +=0x02000000 ; +return randomize_addr (mm->brk,0x02000000 );else - range_end +=0x40000000 ; - -return randomize_range (mm->brk, range_end,0 ) ? : mm->brk; +return randomize_addr (mm->brk,0x40000000 ); }
Signature · 1 line
2.9.2
Re: [PATCH 4/7] arm64: Use simpler API for random address requests
Quoted 20 lines · Jason Cooper
Currently, all callers to
randomize_range()set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.Use the new
randomize_addr(start, range)call to set the requested address.Signed-off-by Jason Cooper <[email protected]>--- arch/arm64/kernel/process.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)arch/arm64/kernel/process.c +0
-0@@ -374,12 +374,8 @@ unsigned long arch_align_stack(unsigned long sp)unsigned long arch_randomize_brk (struct mm_struct *mm) {
arch/arm64/kernel/process.c unsigned long arch_align_stack(unsigned long sp) · line 380
Quoted 10 lines
arch/arm64/kernel/process.c +2
-6-unsigned long range_end = mm->brk; -if (is_compat_task ()) - range_end +=0x02000000 ; +return randomize_addr (mm->brk,0x02000000 );else - range_end +=0x40000000 ; - -return randomize_range (mm->brk, range_end,0 ) ? : mm->brk; +return randomize_addr (mm->brk,0x40000000 );
Looks fine to me, once the core code has settled down:
Will
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 5/7 tile: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
--- arch/tile/mm/mmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/tile/mm/mmap.c +1 -2
@@ -88,6 +88,5 @@ void arch_pick_mmap_layout(struct mm_struct *mm)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 randomize_addr (mm->brk,0x02000000 ); }
Signature · 1 line
2.9.2
PATCH 6/7 unicore32: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
--- arch/unicore32/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/unicore32/kernel/process.c +1 -2
@@ -295,8 +295,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 randomize_addr (mm->brk,0x02000000 ); }/*
Signature · 1 line
2.9.2
PATCH 7/7 random: Remove unused randomize_range()
In reply to Jason CooperAll call sites for randomize_range have been updated to use the much simpler and more robust randomize_addr. Remove the now unnecessary code.
--- drivers/char/random.c | 19 ------------------- include/linux/random.h | 1 - 2 files changed, 20 deletions(-)
drivers/char/random.c +0 -19
@@ -1821,25 +1821,6 @@ unsigned long get_random_long(void) }EXPORT_SYMBOL (get_random_long); -/* -* 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); -} -/** * randomize_addr - Generate a random, page aligned address * @start: The smallest acceptable address the caller will take.
include/linux/random.h +0 -1
@@ -34,7 +34,6 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void ); -unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len);unsigned long randomize_addr (unsigned long start,unsigned long range);u32 prandom_u32 (void );
Signature · 1 line
2.9.2
v2 cover char/random: Simplify random address requests
In reply to Jason CooperTwo previous attempts have been made to rework this API. The first can be found at:
https://lkml.kernel.org/r/[email protected]
The second at:
https://lkml.kernel.org/r/[email protected]
The RFC version of this series can been seen at:
https://lkml.kernel.org/r/[email protected]
In addition to incorporating ideas from these two previous efforts, this series adds several desirable features. First, we take the range as an argument directly, which removes math both before the call and inside the function. Second, we return the start address on error. All callers fell back to the start address on error, so we remove the need to check for errors. Third, we cap range to prevent overflow. Last, we use kerneldoc to describe the new function.
If possible, I'd like to request Acks from the various subsystems so that we can merge this as one bisectable branch.
Changes from v1:
- Explicitly mention page_aligned start assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 last
- Add Ack for arm64 (Will Deacon)
Jason Cooper (7):
random: Simplify API for random address requests
x86: Use simpler API for random address requests
ARM: Use simpler API for random address requests
arm64: Use simpler API for random address requests
tile: Use simpler API for random address requests
unicore32: Use simpler API for random address requests
random: Remove unused randomize_range()
arch/arm/kernel/process.c | 3 +-- arch/arm64/kernel/process.c | 8 ++------ arch/tile/mm/mmap.c | 3 +-- arch/unicore32/kernel/process.c | 3 +-- arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- drivers/char/random.c | 31 ++++++++++++++++++++----------- include/linux/random.h | 2 +- 8 files changed, 28 insertions(+), 30 deletions(-)
Signature · 1 line
2.9.2
PATCH v2 1/7 random: Simplify API for random address requests
To date, all callers of randomize_range() have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.
We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by get_random_int(), since all current range requests are < UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.
All current callers to randomize_range() chose to use the start address if randomize_range() failed. Therefore, we simplify things by just returning the start address on error.
randomize_range() will be removed once all callers have been converted over to randomize_addr().
---
Changes from v1:
- Explicitly mention page_aligned start assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 last
drivers/char/random.c | 28 ++++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 29 insertions(+)
drivers/char/random.c +28 -0
@@ -1840,6 +1840,34 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_addr - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. This assumption still holds. +* +* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_addr (unsigned long start,unsigned long range) +{ +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + + range >>=PAGE_SHIFT ; + +if (range ==0 ) +return start; + +return start + (get_random_long () % range <<PAGE_SHIFT ); +} +/* Interface for in-kernel drivers of true hardware RNGs. * Those devices may produce endless random bits and will be throttled * when our pool is full.
include/linux/random.h +1 -0
@@ -35,6 +35,7 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void );unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len); +unsigned long randomize_addr (unsigned long start,unsigned long range);u32 prandom_u32 (void );void prandom_bytes (void *buf,size_t nbytes);
Signature · 1 line
2.9.2
Re: [PATCH v2 1/7] random: Simplify API for random address requests
Quoted 46 lines · Jason Cooper
To date, all callers of
randomize_range()have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by
get_random_int(), since all current range requests are <UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.All current callers to
randomize_range()chose to use the start address ifrandomize_range()failed. Therefore, we simplify things by just returning the start address on error.
randomize_range()will be removed once all callers have been converted over torandomize_addr().Signed-off-by Jason Cooper <[email protected]>---
Changes from v1:
- Explicitly mentionpage_alignedstart assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 lastdrivers/char/random.c | 28 ++++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 29 insertions(+)drivers/char/random.c +6
-0@@ -1840,6 +1840,34 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_addr - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +*
drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1858
Quoted 10 lines
drivers/char/random.c +10
-0+* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. This assumption still holds. +* +* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_addr (unsigned long start,unsigned long range)
Since we're changing other things about this, let's try to document its behavior in its name too and call this "randomize_page" instead. If it requires a page-aligned value, we should probably also BUG_ON it, or adjust the start too.
-Kees
Quoted 30 lines
drivers/char/random.c +12
-0+{ +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + + range >>=PAGE_SHIFT ; + +if (range ==0 ) +return start; + +return start + (get_random_long () % range <<PAGE_SHIFT ); +} +/* Interface for in-kernel drivers of true hardware RNGs. * Those devices may produce endless random bits and will be throttled * when our pool is full. include/linux/random.h +1
-1@@ -35,6 +35,7 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void );unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len); +unsigned long randomize_addr (unsigned long start,unsigned long range);u32 prandom_u32 (void );void prandom_bytes (void *buf,size_t nbytes); --2.9.2
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v2 1/7] random: Simplify API for random address requests
Quoted 52 lines · Kees Cook
Quoted 51 lines · Jason Cooper
To date, all callers of
randomize_range()have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by
get_random_int(), since all current range requests are <UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.All current callers to
randomize_range()chose to use the start address ifrandomize_range()failed. Therefore, we simplify things by just returning the start address on error.
randomize_range()will be removed once all callers have been converted over torandomize_addr().Signed-off-by Jason Cooper <[email protected]>---
Changes from v1:
- Explicitly mentionpage_alignedstart assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 lastdrivers/char/random.c | 28 ++++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 29 insertions(+)drivers/char/random.c +11
-0@@ -1840,6 +1840,34 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_addr - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. This assumption still holds. +*
Quoted 8 lines
drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1858
Quoted 5 lines
drivers/char/random.c +5
-0+* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_addr (unsigned long start,unsigned long range)Since we're changing other things about this, let's try to document its behavior in its name too and call this "
randomize_page" instead.
Ack. Definitely more accurate.
Quoted 2 lines
If it requires a page-aligned value, we should probably also
BUG_ONit, or adjust the start too.
merf. So, this whole series started from a suggested cleanup by William to s/get_random_int/get_random_long/.
The current users have all been stable the way they are for a long time. Like pre-git long. So, if this is just a cleanup for those callers, I don't think we need to do more than we already are.
However, if the intent is for this function to see wider use, then by all means, we need to handle start != PAGE_ALIGN(start).
Do you have any new call sites in mind?
thx,
Jason.
Re: [PATCH v2 1/7] random: Simplify API for random address requests
Quoted 70 lines · Jason Cooper
Quoted 52 lines · Kees Cook
Quoted 51 lines · Jason Cooper
To date, all callers of
randomize_range()have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by
get_random_int(), since all current range requests are <UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.All current callers to
randomize_range()chose to use the start address ifrandomize_range()failed. Therefore, we simplify things by just returning the start address on error.
randomize_range()will be removed once all callers have been converted over torandomize_addr().Signed-off-by Jason Cooper <[email protected]>---
Changes from v1:
- Explicitly mentionpage_alignedstart assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 lastdrivers/char/random.c | 28 ++++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 29 insertions(+)drivers/char/random.c +11
-0@@ -1840,6 +1840,34 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_addr - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. This assumption still holds. +* Quoted 8 lines
drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1858
Quoted 5 lines
drivers/char/random.c +5
-0+* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_addr (unsigned long start,unsigned long range)Since we're changing other things about this, let's try to document its behavior in its name too and call this "
randomize_page" instead.Ack. Definitely more accurate.
Quoted 2 lines
If it requires a page-aligned value, we should probably also
BUG_ONit, or adjust the start too.merf. So, this whole series started from a suggested cleanup by William to s/get_random_int/get_random_long/.
Quoted 8 lines
The current users have all been stable the way they are for a long time. Like pre-git long. So, if this is just a cleanup for those callers, I don't think we need to do more than we already are.
However, if the intent is for this function to see wider use, then by all means, we need to handle start !=
PAGE_ALIGN(start).Do you have any new call sites in mind?
I have no new call sites in mind, but it seems safe to add a BUG_ON to verify we don't gain callers that don't follow the correct expectations. (Or maybe WARN and return start.)
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v2 1/7] random: Simplify API for random address requests
Hi Kees,
Quoted 75 lines · Kees Cook
Quoted 74 lines · Jason Cooper
Quoted 52 lines · Kees Cook
Quoted 51 lines · Jason Cooper
To date, all callers of
randomize_range()have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by
get_random_int(), since all current range requests are <UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.All current callers to
randomize_range()chose to use the start address ifrandomize_range()failed. Therefore, we simplify things by just returning the start address on error.
randomize_range()will be removed once all callers have been converted over torandomize_addr().Signed-off-by Jason Cooper <[email protected]>---
Changes from v1:
- Explicitly mentionpage_alignedstart assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 lastdrivers/char/random.c | 28 ++++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 29 insertions(+)drivers/char/random.c +11
-0@@ -1840,6 +1840,34 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_addr - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. This assumption still holds. +* Quoted 8 lines
drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1858
Quoted 5 lines
drivers/char/random.c +5
-0+* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_addr (unsigned long start,unsigned long range)Since we're changing other things about this, let's try to document its behavior in its name too and call this "
randomize_page" instead.Ack. Definitely more accurate.
Quoted 2 lines
If it requires a page-aligned value, we should probably also
BUG_ONit, or adjust the start too.merf. So, this whole series started from a suggested cleanup by William to s/get_random_int/get_random_long/.
The current users have all been stable the way they are for a long time. Like pre-git long. So, if this is just a cleanup for those callers, I don't think we need to do more than we already are.
Quoted 8 lines
Quoted 4 lines
However, if the intent is for this function to see wider use, then by all means, we need to handle start !=
PAGE_ALIGN(start).Do you have any new call sites in mind?
I have no new call sites in mind, but it seems safe to add a
BUG_ONto verify we don't gain callers that don't follow the correct expectations. (Or maybe WARN and return start.)
No, I think BUG_ON is appropriate. afaict, the only time this will be encountered is during the development process.
thx,
Jason.
Re: [kernel-hardening] Re: [PATCH v2 1/7] random: Simplify API for random address requests
Quoted 9 lines · Jason Cooper
Quoted 5 lines · Kees Cook
On Sun, Jul 31, 2016 at 1:56 PM, Jason Cooper <[email protected]> wrote:
I have no new call sites in mind, but it seems safe to add a
BUG_ONto verify we don't gain callers that don't follow the correct expectations. (Or maybe WARN and return start.)No, I think
BUG_ONis appropriate. afaict, the only time this will be encountered is during the development process.
Unless it's not.
Why crash someone's system when you could just page align the value you're given?
cheers
Re: [kernel-hardening] Re: [PATCH v2 1/7] random: Simplify API for random address requests
Quoted 8 lines · Michael Ellerman
Quoted 7 lines · Jason Cooper
Quoted 5 lines · Kees Cook
On Sun, Jul 31, 2016 at 1:56 PM, Jason Cooper <[email protected]> wrote:
I have no new call sites in mind, but it seems safe to add a
BUG_ONto verify we don't gain callers that don't follow the correct expectations. (Or maybe WARN and return start.)
Quoted 7 lines
Quoted 2 lines
No, I think
BUG_ONis appropriate. afaict, the only time this will be encountered is during the development process.Unless it's not.
Why crash someone's system when you could just page align the value you're given?
Ack, v3 on it's way.
thx,
Jason.
PATCH v2 2/7 x86: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v1:
- none
arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- 2 files changed, 2 insertions(+), 6 deletions(-)
arch/x86/kernel/process.c +1 -2
@@ -507,8 +507,7 @@ 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 randomize_addr (mm->brk,0x02000000 ); }/*
arch/x86/kernel/sys_x86_64.c +1 -4
@@ -101,7 +101,6 @@ static void find_start_end(unsigned long flags, unsigned long *begin, unsigned long *end) {if (!test_thread_flag (TIF_ADDR32 ) && (flags &MAP_32BIT )) { -unsigned long new_begin;/* This is usually used needed to map code in small model, so it needs to be in the first 31bit. Limit it to that. This means we need to move the
@@ -112,9 +111,7 @@ static void find_start_end(unsigned long flags, unsigned long *begin, *begin =0x40000000 ; *end =0x80000000 ;if (current->flags &PF_RANDOMIZE ) { - new_begin =randomize_range (*begin, *begin +0x02000000 ,0 ); -if (new_begin) - *begin = new_begin; + *begin =randomize_addr (*begin,0x02000000 ); } }else { *begin = current->mm->mmap_legacy_base;
Signature · 1 line
2.9.2
PATCH v2 3/7 ARM: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v1:
- none
arch/arm/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/kernel/process.c +1 -2
@@ -314,8 +314,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 randomize_addr (mm->brk,0x02000000 ); }#ifdef CONFIG_MMU
Signature · 1 line
2.9.2
PATCH v2 4/7 arm64: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v1:
- Add Ack for arm64 (Will Deacon)
arch/arm64/kernel/process.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/arm64/kernel/process.c +2 -6
@@ -374,12 +374,8 @@ unsigned long arch_align_stack(unsigned long sp)unsigned long arch_randomize_brk (struct mm_struct *mm) { -unsigned long range_end = mm->brk; -if (is_compat_task ()) - range_end +=0x02000000 ; +return randomize_addr (mm->brk,0x02000000 );else - range_end +=0x40000000 ; - -return randomize_range (mm->brk, range_end,0 ) ? : mm->brk; +return randomize_addr (mm->brk,0x40000000 ); }
Signature · 1 line
2.9.2
PATCH v2 5/7 tile: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v1:
- none
arch/tile/mm/mmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/tile/mm/mmap.c +1 -2
@@ -88,6 +88,5 @@ void arch_pick_mmap_layout(struct mm_struct *mm)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 randomize_addr (mm->brk,0x02000000 ); }
Signature · 1 line
2.9.2
PATCH v2 6/7 unicore32: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v1:
- none
arch/unicore32/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/unicore32/kernel/process.c +1 -2
@@ -295,8 +295,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 randomize_addr (mm->brk,0x02000000 ); }/*
Signature · 1 line
2.9.2
PATCH v2 7/7 random: Remove unused randomize_range()
In reply to Jason CooperAll call sites for randomize_range have been updated to use the much simpler and more robust randomize_addr. Remove the now unnecessary code.
---
Changes from v1:
- none
drivers/char/random.c | 19 ------------------- include/linux/random.h | 1 - 2 files changed, 20 deletions(-)
drivers/char/random.c +0 -19
@@ -1821,25 +1821,6 @@ unsigned long get_random_long(void) }EXPORT_SYMBOL (get_random_long); -/* -* 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); -} -/** * randomize_addr - Generate a random, page aligned address * @start: The smallest acceptable address the caller will take.
include/linux/random.h +0 -1
@@ -34,7 +34,6 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void ); -unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len);unsigned long randomize_addr (unsigned long start,unsigned long range);u32 prandom_u32 (void );
Signature · 1 line
2.9.2
v3 cover char/random: Simplify random address requests
In reply to Jason CooperTwo previous attempts have been made to rework this API. The first can be found at:
https://lkml.kernel.org/r/[email protected]
The second at:
https://lkml.kernel.org/r/[email protected]
Previous versions of this series can been seen at:
RFC: https://lkml.kernel.org/r/[email protected] v1: https://lkml.kernel.org/r/[email protected] v2: https://lkml.kernel.org/r/[email protected]
In addition to incorporating ideas from these two previous efforts, this series adds several desirable features. First, we take the range as an argument directly, which removes math both before the call and inside the function. Second, we return the start address on error. All callers fell back to the start address on error, so we remove the need to check for errors. Third, we cap range to prevent overflow. Last, we use kerneldoc to describe the new function.
If possible, I'd like to request Acks from the various subsystems so that we can merge this as one bisectable branch.
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
- PAGE_ALIGN(start) if it wasn't (Kees Cook, Michael Ellerman)
Changes from v1:
- Explicitly mention page_aligned start assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 last
- Add Ack for arm64 (Will Deacon)
Jason Cooper (7):
random: Simplify API for random address requests
x86: Use simpler API for random address requests
ARM: Use simpler API for random address requests
arm64: Use simpler API for random address requests
tile: Use simpler API for random address requests
unicore32: Use simpler API for random address requests
random: Remove unused randomize_range()
arch/arm/kernel/process.c | 3 +-- arch/arm64/kernel/process.c | 8 ++------ arch/tile/mm/mmap.c | 3 +-- arch/unicore32/kernel/process.c | 3 +-- arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- drivers/char/random.c | 36 +++++++++++++++++++++++++----------- include/linux/random.h | 2 +- 8 files changed, 33 insertions(+), 30 deletions(-)
Signature · 1 line
2.9.2
PATCH v3 1/7 random: Simplify API for random address requests
To date, all callers of randomize_range() have set the length to 0, and check for a zero return value. For the current callers, the only way to get zero returned is if end <= start. Since they are all adding a constant to the start address, this is unnecessary.
We can remove a bunch of needless checks by simplifying the API to do just what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site is adversely affected by get_random_int(), since all current range requests are < UINT_MAX. However, we should match caller expectations to avoid coming up short (ha!) in the future.
All current callers to randomize_range() chose to use the start address if randomize_range() failed. Therefore, we simplify things by just returning the start address on error.
randomize_range() will be removed once all callers have been converted over to randomize_addr().
---
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
- PAGE_ALIGN(start) if it wasn't (Kees Cook, Michael Ellerman)
drivers/char/random.c | 33 +++++++++++++++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 34 insertions(+)
drivers/char/random.c +33 -0
@@ -1840,6 +1840,39 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)return PAGE_ALIGN (get_random_int () % range + start); } +/** +* randomize_page - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. We now align it regardless. +* +* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long +randomize_page (unsigned long start,unsigned long range) +{ +if (!PAGE_ALIGNED (start)) { + range -=PAGE_ALIGN (start) - start; + start =PAGE_ALIGN (start); + } + +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + + range >>=PAGE_SHIFT ; + +if (range ==0 ) +return start; + +return start + (get_random_long () % range <<PAGE_SHIFT ); +} +/* Interface for in-kernel drivers of true hardware RNGs. * Those devices may produce endless random bits and will be throttled * when our pool is full.
include/linux/random.h +1 -0
@@ -35,6 +35,7 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void );unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len); +unsigned long randomize_page (unsigned long start,unsigned long range);u32 prandom_u32 (void );void prandom_bytes (void *buf,size_t nbytes);
Signature · 1 line
2.9.2
Re: [PATCH v3 1/7] random: Simplify API for random address requests
Hi,
Le mercredi 03 août 2016 à 23:39 +0000, Jason Cooper a écrit :
Quoted 28 lines
drivers/char/random.c +0
-0@@ -1840,6 +1840,39 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len) return PAGE_ALIGN(get_random_int() % range + start); }
+/**
+ *randomize_page- Generate a random, page aligned address
+ * @start: The smallest acceptable address the caller will
take.
+ * @range: The size of the area, starting at @start, within
which the
+ * random address must fall.
+ *
+ * If @start + @range would overflow, @range is capped.
+ *
+ * NOTE: Historical use ofrandomize_range, which this replaces, presumed that
+ * @start was already page aligned. We now align it regardless.
+ *
+ * Return: A page aligned address within [start, start + range). On error,
+ * @start is returned.
+ */
drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1859
Quoted 3 lines
drivers/char/random.c +3
-0+unsigned long +randomize_page (unsigned long start,unsigned long range) +{
To prevent an underflow if start is not page aligned (but will one would ever use a non aligned start address *and* range ? ...)
if (range < PAGE_SIZE)
return start;
Quoted 16 lines
drivers/char/random.c +16
-0+if (!PAGE_ALIGNED (start)) { + range -=PAGE_ALIGN (start) - start; + start =PAGE_ALIGN (start); + } + +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + + range >>=PAGE_SHIFT ; + +if (range ==0 ) +return start; + +return start + (get_random_long () % range <<PAGE_SHIFT ); +} +
Quoted 5 lines
/* Interface for in-kernel drivers of true hardware RNGs.
* Those devices may produce endless random bits and will be throttled
* when our pool is full.
Regards.
--
Yann Droneaud
OPTEYA
PATCH v3 2/7 x86: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- 2 files changed, 2 insertions(+), 6 deletions(-)
arch/x86/kernel/process.c +1 -2
@@ -507,8 +507,7 @@ 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 randomize_page (mm->brk,0x02000000 ); }/*
arch/x86/kernel/sys_x86_64.c +1 -4
@@ -101,7 +101,6 @@ static void find_start_end(unsigned long flags, unsigned long *begin, unsigned long *end) {if (!test_thread_flag (TIF_ADDR32 ) && (flags &MAP_32BIT )) { -unsigned long new_begin;/* This is usually used needed to map code in small model, so it needs to be in the first 31bit. Limit it to that. This means we need to move the
@@ -112,9 +111,7 @@ static void find_start_end(unsigned long flags, unsigned long *begin, *begin =0x40000000 ; *end =0x80000000 ;if (current->flags &PF_RANDOMIZE ) { - new_begin =randomize_range (*begin, *begin +0x02000000 ,0 ); -if (new_begin) - *begin = new_begin; + *begin =randomize_page (*begin,0x02000000 ); } }else { *begin = current->mm->mmap_legacy_base;
Signature · 1 line
2.9.2
PATCH v3 3/7 ARM: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
arch/arm/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/kernel/process.c +1 -2
@@ -314,8 +314,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 randomize_page (mm->brk,0x02000000 ); }#ifdef CONFIG_MMU
Signature · 1 line
2.9.2
PATCH v3 4/7 arm64: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
arch/arm64/kernel/process.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/arm64/kernel/process.c +2 -6
@@ -374,12 +374,8 @@ unsigned long arch_align_stack(unsigned long sp)unsigned long arch_randomize_brk (struct mm_struct *mm) { -unsigned long range_end = mm->brk; -if (is_compat_task ()) - range_end +=0x02000000 ; +return randomize_page (mm->brk,0x02000000 );else - range_end +=0x40000000 ; - -return randomize_range (mm->brk, range_end,0 ) ? : mm->brk; +return randomize_page (mm->brk,0x40000000 ); }
Signature · 1 line
2.9.2
PATCH v3 5/7 tile: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
arch/tile/mm/mmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/tile/mm/mmap.c +1 -2
@@ -88,6 +88,5 @@ void arch_pick_mmap_layout(struct mm_struct *mm)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 randomize_page (mm->brk,0x02000000 ); }
Signature · 1 line
2.9.2
PATCH v3 6/7 unicore32: Use simpler API for random address requests
In reply to Jason CooperCurrently, all callers to randomize_range() set the length to 0 and calculate end by adding a constant to the start address. We can simplify the API to remove a bunch of needless checks and variables.
Use the new randomize_addr(start, range) call to set the requested address.
---
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
arch/unicore32/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/unicore32/kernel/process.c +1 -2
@@ -295,8 +295,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 randomize_page (mm->brk,0x02000000 ); }/*
Signature · 1 line
2.9.2
PATCH v3 7/7 random: Remove unused randomize_range()
In reply to Jason CooperAll call sites for randomize_range have been updated to use the much simpler and more robust randomize_addr. Remove the now unnecessary code.
--- drivers/char/random.c | 19 ------------------- include/linux/random.h | 1 - 2 files changed, 20 deletions(-)
drivers/char/random.c +0 -19
@@ -1821,25 +1821,6 @@ unsigned long get_random_long(void) }EXPORT_SYMBOL (get_random_long); -/* -* 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); -} -/** * randomize_page - Generate a random, page aligned address * @start: The smallest acceptable address the caller will take.
include/linux/random.h +0 -1
@@ -34,7 +34,6 @@ extern const struct file_operations random_fops, urandom_fops;unsigned int get_random_int (void );unsigned long get_random_long (void ); -unsigned long randomize_range (unsigned long start,unsigned long end,unsigned long len);unsigned long randomize_page (unsigned long start,unsigned long range);u32 prandom_u32 (void );
Signature · 1 line
2.9.2
Andrew Morton
maintainerRe: [PATCH v3 7/7] random: Remove unused randomize_range()
Quoted 3 lines · On Wed, 3 Aug 2016 23:39:13 +0000 Jason Cooper <[email protected]> wrote:
All call sites for
randomize_rangehave been updated to use the much simpler and more robustrandomize_addr. Remove the now unnecessary code.
"randomize_page'.
I think I'll grab these patches, see if anybody emits any squeaks.
Re: [PATCH v3 7/7] random: Remove unused randomize_range()
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Wed, 3 Aug 2016 23:39:13 +0000 Jason Cooper <[email protected]> wrote:
All call sites for
randomize_rangehave been updated to use the much simpler and more robustrandomize_addr. Remove the now unnecessary code."
randomize_page'.
Doh!
Quoted 1 line
I think I'll grab these patches, see if anybody emits any squeaks.
Thanks, Andrew!
thx,
Jason.
Quoted 45 lines · Jason Cooper
Two previous attempts have been made to rework this API. The first can be found at:
https://lkml.kernel.org/r/[email protected]
The second at:
https://lkml.kernel.org/r/[email protected]
Previous versions of this series can been seen at:
RFC: https://lkml.kernel.org/r/[email protected] v1: https://lkml.kernel.org/r/[email protected] v2: https://lkml.kernel.org/r/[email protected]In addition to incorporating ideas from these two previous efforts, this series adds several desirable features. First, we take the range as an argument directly, which removes math both before the call and inside the function. Second, we return the start address on error. All callers fell back to the start address on error, so we remove the need to check for errors. Third, we cap range to prevent overflow. Last, we use kerneldoc to describe the new function.
If possible, I'd like to request Acks from the various subsystems so that we can merge this as one bisectable branch.
Changes from v2:
- s/randomize_addr/randomize_page/ (Kees Cook)
-PAGE_ALIGN(start)if it wasn't (Kees Cook, Michael Ellerman)Changes from v1:
- Explicitly mentionpage_alignedstart assumption (Yann Droneaud)
- pick random pages vice random addresses (Yann Droneaud)
- catch range=0 last
- Add Ack for arm64 (Will Deacon)Jason Cooper (7):
random: Simplify API for random address requests
x86: Use simpler API for random address requests
ARM: Use simpler API for random address requests
arm64: Use simpler API for random address requests
tile: Use simpler API for random address requests
unicore32: Use simpler API for random address requests
random: Remove unusedrandomize_range()
Quoted 9 lines
arch/arm/kernel/process.c | 3 +-- arch/arm64/kernel/process.c | 8 ++------ arch/tile/mm/mmap.c | 3 +-- arch/unicore32/kernel/process.c | 3 +-- arch/x86/kernel/process.c | 3 +-- arch/x86/kernel/sys_x86_64.c | 5 +---- drivers/char/random.c | 36 +++++++++++++++++++++++++----------- include/linux/random.h | 2 +- 8 files changed, 33 insertions(+), 30 deletions(-)
This looks great! Thanks for the v3. :)
-Kees
Signature · 2 lines
Kees Cook Brillo & Chrome OS Security