Skip to contentSkip to conversation

rcu: Fix sign bug making rcu_random always return the same sequence

PATCH In review Josh Triplett 2 participants 2 messages Aug 19, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rcu: Fix sign bug making rcu_random always return the same sequence

rcu_random uses a counter rrs_count to occasionally mix data from get_random_bytes into the state of its pseudorandom generator. However, the rrs_counter gets declared as an unsigned long, and rcu_random checks for --rrs_count < 0, so this code will never mix any real random data into the state, and will thus always return the same sequence of random numbers.

Also, change the return value of rcu_random from long to unsigned long, to avoid potential issues caused by the use of the % operator, which can return negative values for negative left operands.

---
 kernel/rcutorture.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/rcutorture.c +2 -2
index 8b09c95..ef6a124 100644
@@ -146,7 +146,7 @@ rcu_torture_free(struct rcu_torture *p)
 
 struct rcu_random_state {
 	unsigned long rrs_state;
-	unsigned long rrs_count;
+	long rrs_count;
 };
 
 #define RCU_RANDOM_MULT 39916801  /* prime */
@@ -159,7 +159,7 @@ #define DEFINE_RCU_RANDOM(name) struct r
  * Crude but fast random-number generator.  Uses a linear congruential
  * generator, with occasional help from get_random_bytes().
  */
-static long
+static unsigned long
 rcu_random(struct rcu_random_state *rrsp)
 {
 	long refresh;
Signature · 1 line
1.4.1.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Josh Triplett

rcu_random uses a counter rrs_count to occasionally mix data from get_random_bytes into the state of its pseudorandom generator. However, the rrs_counter gets declared as an unsigned long, and rcu_random checks for --rrs_count < 0, so this code will never mix any real random data into the state, and will thus always return the same sequence of random numbers.

Also, change the return value of rcu_random from long to unsigned long, to avoid potential issues caused by the use of the % operator, which can return negative values for negative left operands.

Quoted 31 lines
---
 kernel/rcutorture.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/rcutorture.c +2 -2
index 8b09c95..ef6a124 100644
@@ -146,7 +146,7 @@ rcu_torture_free(struct rcu_torture *p)
 
 struct rcu_random_state {
 	unsigned long rrs_state;
-	unsigned long rrs_count;
+	long rrs_count;
 };
 
 #define RCU_RANDOM_MULT 39916801  /* prime */
@@ -159,7 +159,7 @@ #define DEFINE_RCU_RANDOM(name) struct r
  * Crude but fast random-number generator.  Uses a linear congruential
  * generator, with occasional help from get_random_bytes().
  */
-static long
+static unsigned long
 rcu_random(struct rcu_random_state *rrsp)
 {
 	long refresh;
Signature · 1 line
1.4.1.1