randomize_range: use random long instead of int
Subsystems (4)
PATCH 1/1 randomize_range: use random long instead of int
From: William Roberts <[email protected]>
Use a long when generating the random range rather than an int. This will produce better random distributions as well as matching all the types at hand.
--- drivers/char/random.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/char/random.c +2 -1
@@ -1837,7 +1837,8 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)if (end <= start + len)return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); + +return PAGE_ALIGN (get_random_long () % range + start); }/* Interface for in-kernel drivers of true hardware RNGs.
Signature · 1 line
1.9.1
Quoted 7 lines · On Mon, Jul 25, 2016 at 11:25 AM, <[email protected]> wrote:
From: William Roberts <[email protected]>
Use a long when generating the random range rather than an int. This will produce better random distributions as well as matching all the types at hand.
Signed-off-by William Roberts <[email protected]>
I agree, this is what I pointed out back in Feb:
https://lkml.org/lkml/2016/2/4/854
Andrew, can you pick this up for 4.8?
-Kees
Quoted 21 lines
--- drivers/char/random.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/char/random.c +2
-2@@ -1837,7 +1837,8 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)if (end <= start + len)return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); + +return PAGE_ALIGN (get_random_long () % range + start); }/* Interface for in-kernel drivers of true hardware RNGs. -- 1.9.1
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Hi William, Kees,
Quoted 10 lines · On Mon, Jul 25, 2016 at 11:25:41AM -0700, [email protected] wrote:
From: William Roberts <[email protected]>
Use a long when generating the random range rather than an int. This will produce better random distributions as well as matching all the types at hand.
Signed-off-by William Roberts <[email protected]>--- drivers/char/random.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Upon further review, I think we should dig into this a little bit deeper. Standby, I'll post an RFC series shortly.
thx,
Jason.
Quoted 17 lines
drivers/char/random.c +2
-1@@ -1837,7 +1837,8 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)if (end <= start + len)return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); + +return PAGE_ALIGN (get_random_long () % range + start); }/* Interface for in-kernel drivers of true hardware RNGs. Signature · 1 line
1.9.1
RFC PATCH 1/6 random: Simplify API for random address requests
In reply to [email protected]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 < MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.
--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)
drivers/char/random.c +4 -13
@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ unsigned long -randomize_range (unsigned long start,unsigned long end,unsigned long len) +randomize_addr (unsigned long start,unsigned long range) { -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }/* Interface for in-kernel drivers of true hardware RNGs.
include/linux/random.h +1 -1
@@ -34,7 +34,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
RFC PATCH 2/6 x86: Use simpler API for random address requests
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.
--- 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
RFC PATCH 3/6 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
RFC PATCH 4/6 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
RFC PATCH 5/6 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
RFC PATCH 6/6 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
Re: [RFC patch 1/6] random: Simplify API for random address requests
In reply to Jason CooperAll,
Quoted 39 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 <MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +2
-7@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ unsigned long
drivers/char/random.c unsigned long get_random_long(void) · line 1832
Quoted 10 lines
drivers/char/random.c +2
-6-randomize_range(unsigned long start, unsigned long end, unsigned long len) +randomize_addr(unsigned long start, unsigned long range) { -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }
bah! old patch file. This should have been:
if (range == 0)
return start; else return PAGE_ALIGN(get_random_long() % range + start);
sorry,
Jason.
Quoted 18 lines
drivers/char/random.c +0
-0/* Interface for in-kernel drivers of true hardware RNGs. include/linux/random.h +1
-1@@ -34,7 +34,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: [RFC patch 1/6] random: Simplify API for random address requests
Quoted 55 lines · Jason Cooper
All,
Quoted 39 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 <MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +2
-7@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ unsigned long drivers/char/random.c unsigned long get_random_long(void) · line 1832
Quoted 10 lines
drivers/char/random.c +2
-6-randomize_range(unsigned long start, unsigned long end, unsigned long len) +randomize_addr(unsigned long start, unsigned long range) { -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }bah! old patch file. This should have been:
Quoted 4 lines
if (range == 0)
return start; else return PAGE_ALIGN(get_random_long() % range + start);
I think range should be limited to start + range < UINTMAX, and it should be very clear if the range is inclusive or exclusive. start = 0, range = 4096. does this mean 1 page, or 2 pages possible?
-Kees
Quoted 23 lines
sorry,
Jason.
Quoted 18 lines
drivers/char/random.c +0
-0/* Interface for in-kernel drivers of true hardware RNGs. include/linux/random.h +1
-2@@ -34,7 +34,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: [RFC patch 1/6] random: Simplify API for random address requests
Hi Kees,
Quoted 7 lines · Kees Cook
Quoted 6 lines · Jason Cooper
Quoted 5 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.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
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 <MAX_UINT. However, we should match caller expectations
merf. UINT_MAX.
Quoted 40 lines
Quoted 40 lines
Quoted 27 lines
to avoid coming up short (ha!) in the future.
Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +2
-7@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ unsigned long drivers/char/random.c unsigned long get_random_long(void) · line 1832
Quoted 10 lines
drivers/char/random.c +2
-6-randomize_range(unsigned long start, unsigned long end, unsigned long len) +randomize_addr(unsigned long start, unsigned long range) { -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }bah! old patch file. This should have been:
Quoted 6 lines
Quoted 4 lines
if (range == 0)
return start; else return PAGE_ALIGN(get_random_long() % range + start);I think range should be limited to start + range < UINTMAX,
ULONG_MAX? I agree.
if (range == 0 || ULONG_MAX - range < start)
return start; else return PAGE_ALIGN(get_random_long() % range + start);
?
Quoted 1 line
and it should be very clear if the range is inclusive or exclusive.
Sorry, I was reading the original comment, '[start, end]' with square brackets denoting inclusive.
Regardless, the math in randomize_range() was just undoing the math at each of the call sites. This proposed change to randomize_addr() doesn't alter the current state of affairs wrt inclusive, exclusive.
Quoted 1 line
start = 0, range = 4096. does this mean 1 page, or 2 pages possible?
ooh, good spot. What we have right now is [start, start + range), which is matching previous behavior. But does not match the old comment, [start, end]. It should have been [start, end).
So, you're correct, I need to clarify this in the comments.
thx,
Jason.
Re: [RFC patch 1/6] random: Simplify API for random address requests
Quoted 60 lines · Jason Cooper
Hi Kees,
Quoted 7 lines · Kees Cook
Quoted 6 lines · Jason Cooper
Quoted 5 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.Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
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 <MAX_UINT. However, we should match caller expectationsmerf.
UINT_MAX.Quoted 40 lines
Quoted 40 lines
Quoted 27 lines
to avoid coming up short (ha!) in the future.
Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +2
-7@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ unsigned long drivers/char/random.c unsigned long get_random_long(void) · line 1832
Quoted 10 lines
drivers/char/random.c +2
-6-randomize_range(unsigned long start, unsigned long end, unsigned long len) +randomize_addr(unsigned long start, unsigned long range) { -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }bah! old patch file. This should have been:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
if (range == 0)
return start; else return PAGE_ALIGN(get_random_long() % range + start);I think range should be limited to start + range < UINTMAX,
ULONG_MAX? I agree.
Heh, I am plagued by misspelling these constants, and yes, sorry, ULONG_MAX. :)
Quoted 2 lines
if (range == 0 ||
ULONG_MAX- range < start)return start;
Should it "abort" like this? I was thinking just cap the range, something like:
if (range > ULONG_MAX - start)
range = ULONG_MAX - start
Quoted 14 lines
else
return PAGE_ALIGN(get_random_long() % range + start);?
Quoted 1 line
and it should be very clear if the range is inclusive or exclusive.
Sorry, I was reading the original comment, '[start, end]' with square brackets denoting inclusive.
Regardless, the math in
randomize_range()was just undoing the math at each of the call sites. This proposed change torandomize_addr()doesn't alter the current state of affairs wrt inclusive, exclusive.
Quoted 7 lines
Quoted 1 line
start = 0, range = 4096. does this mean 1 page, or 2 pages possible?
ooh, good spot. What we have right now is [start, start + range), which is matching previous behavior. But does not match the old comment, [start, end]. It should have been [start, end).
So, you're correct, I need to clarify this in the comments.
Okay, cool. Thanks! I'm glad to have this clean-up. :)
-Kees
Quoted 4 lines
thx,
Jason.
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [RFC patch 1/6] random: Simplify API for random address requests
Quoted 1 line · Kees Cook
On Tue, Jul 26, 2016 at 10:00 AM, Jason Cooper <[email protected]> wrote:
...
Quoted 7 lines
Quoted 2 lines
if (range == 0 ||
ULONG_MAX- range < start)return start;Should it "abort" like this? I was thinking just cap the range, something like:
if (range >
ULONG_MAX- start)
range =ULONG_MAX- start
yes, will do.
thx,
Jason.
RE: [RFC patch 1/6] random: Simplify API for random address requests
In reply to Jason CooperQuoted 76 lines
-----Original Message-----
From: Jason Cooper [mailto:[email protected]]
Sent: Monday, July 25, 2016 8:31 PM
To: Roberts, William C <[email protected]>; linux- [email protected]; [email protected]; kernel- [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC patch 1/6] random: Simplify API for random address requestsAll,
Quoted 43 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 <MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>---
drivers/char/random.c| 17 ++++-------------include/linux/random.h
| 2 +-2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c index renamed from drivers/char/random.c +0
-0
0158d3bff7e5..1251cb2cbab2100644drivers/char/random.c +0
-6@@ -1822,22 +1822,13 @@ 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.
+ *randomize_addr()returns a page aligned address within [start,
+ start +
+ * range]
*/
unsigned long
-randomize_range(unsigned long start, unsigned long end, unsigned long len)drivers/char/random.c unsigned long get_random_long(void) · line 1832
Quoted 9 lines
drivers/char/random.c +2
-5+randomize_addr(unsigned long start, unsigned long range) { -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }bah! old patch file. This should have been:
Quoted 6 lines
if (range == 0)
return start; else return PAGE_ALIGN(get_random_long() % range + start);sorry,
Yeah that looks better. I had a similar intended set of changes locally, because of the issues Jason pointed out. I ended up in the old case where if end - start == len it returns 0 instead of start. Jason's change is better though :-P
Quoted 24 lines
Jason.
Quoted 21 lines
/* Interface for in-kernel drivers of true hardware RNGs.
include/linux/random.h index renamed from include/linux/random.h +0
-0
e47e533742b5..1ad877a98186100644include/linux/random.h +2
-2@@ -34,7 +34,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
Re: [RFC patch 1/6] random: Simplify API for random address requests
In reply to Jason CooperQuoted 31 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 <MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +0
-3@@ -1822,22 +1822,13 @@ unsigned long get_random_long(void)EXPORT_SYMBOL (get_random_long);/* -* randomize_range() returns a start address such that -* -* [...... <range> .....]
drivers/char/random.c unsigned long get_random_long(void) · line 1829
Quoted 10 lines
drivers/char/random.c +3
-5-* 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ unsigned long -randomize_range (unsigned long start,unsigned long end,unsigned long len) +randomize_addr (unsigned long start,unsigned long range)
Also, this series isn't bisectable since randomize_range gets removed here before the callers are updated. Perhaps add a macro that calls randomize_addr with a BUG_ON for len != 0? (And then remove it in the last patch?)
-Kees
Quoted 26 lines
drivers/char/random.c +1
-5{ -unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start); }/* Interface for in-kernel drivers of true hardware RNGs. include/linux/random.h +1
-2@@ -34,7 +34,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: [RFC patch 1/6] random: Simplify API for random address requests
Quoted 39 lines · Kees Cook
Quoted 38 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 <MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +2
-7@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */
Quoted 8 lines
drivers/char/random.c unsigned long get_random_long(void) · line 1829
Quoted 3 lines
drivers/char/random.c +1
-1unsigned long -randomize_range (unsigned long start,unsigned long end,unsigned long len) +randomize_addr (unsigned long start,unsigned long range)Also, this series isn't bisectable since
randomize_rangegets removed here before the callers are updated. Perhaps add a macro that callsrandomize_addrwith aBUG_ONfor len != 0? (And then remove it in the last patch?)
No, I was thinking just add randomize_addr() in the first patch, convert all the callers, then the last patch would remove randomize_range().
That way the last patch can be a cleanup in a later merge window if needed.
thx,
Jason.
Re: [RFC patch 1/6] random: Simplify API for random address requests
Quoted 49 lines · Jason Cooper
Quoted 39 lines · Kees Cook
Quoted 38 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 <MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>--- drivers/char/random.c | 17 ++++------------- include/linux/random.h | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +2
-7@@ -1822,22 +1822,13 @@ 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. +* randomize_addr() returns a page aligned address within [start, start + +* range] */ Quoted 8 lines
drivers/char/random.c unsigned long get_random_long(void) · line 1829
Quoted 3 lines
drivers/char/random.c +1
-1unsigned long -randomize_range (unsigned long start,unsigned long end,unsigned long len) +randomize_addr (unsigned long start,unsigned long range)Also, this series isn't bisectable since
randomize_rangegets removed here before the callers are updated. Perhaps add a macro that callsrandomize_addrwith aBUG_ONfor len != 0? (And then remove it in the last patch?)
Quoted 5 lines
No, I was thinking just add
randomize_addr()in the first patch, convert all the callers, then the last patch would removerandomize_range().That way the last patch can be a cleanup in a later merge window if needed.
That works too! :)
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [kernel-hardening] [RFC patch 1/6] random: Simplify API for random address requests
In reply to Jason CooperHi,
Le mardi 26 juillet 2016 à 03:01 +0000, Jason Cooper a écrit :
Quoted 5 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.
I agree.
Quoted 4 lines
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].
I agree.
For the record:
http://lkml.kernel.org/r/[email protected]
Quoted 61 lines
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 < MAX_UINT. However, we should match caller expectations to avoid coming up short (ha!) in the future.Signed-off-by Jason Cooper <[email protected]>---
drivers/char/random.c | 17 ++++-------------
include/linux/random.h | 2 +-2 files changed, 5 insertions(+), 14 deletions(-)drivers/char/random.c +0
-0@@ -1822,22 +1822,13 @@ 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.
+ *randomize_addr()returns a page aligned address within [start, start +
+ * range]
*/
unsigned long
-randomize_range(unsigned long start, unsigned long end, unsigned long len)
+randomize_addr(unsigned long start, unsigned long range) {drivers/char/random.c +1
-5-unsigned long range = end - len - start; - -if (end <= start + len) -return 0 ; -return PAGE_ALIGN (get_random_int () % range + start); +return PAGE_ALIGN (get_random_long () % range + start);}
/* Interface for in-kernel drivers of true hardware RNGs.include/linux/random.h +0
-0@@ -34,7 +34,7 @@ extern const struct file_operations random_fops, urandom_fops;
unsigned intget_random_int(void);
unsigned longget_random_long(void);
-unsigned longrandomize_range(unsigned long start, unsigned long end, unsigned long len);
+unsigned longrandomize_addr(unsigned long start, unsigned long range);
u32prandom_u32(void);
voidprandom_bytes(void *buf, size_t nbytes);