public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arjan van de Ven <arjan@infradead.org>
To: linux-kernel@vger.kernel.org, akpm@osdl.org, torvalds@osdl.org
Subject: Re: Patch 2/6 introduce helper infrastructure
Date: Thu, 27 Jan 2005 10:12:28 +0000	[thread overview]
Message-ID: <20050127101228.GC9760@infradead.org> (raw)
In-Reply-To: <20050127101117.GA9760@infradead.org>



The patch below introduces get_random_int() and randomize_range(), two
helpers used in later patches in the series. get_random_int() shares the
tcp/ip random number stuff so the CONFIG_INET ifdef needs to move slightly,
and to reduce the damange due to that, secure_ip_id() needs to move inside
random.c


Signed-off-by: Arjan van de Ven <arjan@infradead.org>


diff -purN step1/drivers/char/random.c step2/drivers/char/random.c
--- step1/drivers/char/random.c	2005-01-26 18:24:36.000000000 +0100
+++ step2/drivers/char/random.c	2005-01-27 11:03:01.000000000 +0100
@@ -1965,7 +1965,6 @@ static void sysctl_init_random(struct en
  *
  ********************************************************************/
 
-#ifdef CONFIG_INET
 /*
  * TCP initial sequence number picking.  This uses the random number
  * generator to pick an initial secret value.  This value is hashed
@@ -2202,6 +2201,31 @@ __u32 secure_tcpv6_sequence_number(__u32
 EXPORT_SYMBOL(secure_tcpv6_sequence_number);
 #endif
 
+/*  The code below is shamelessly stolen from secure_tcp_sequence_number().
+ *  All blames to Andrey V. Savochkin <saw@msu.ru>.
+ */
+__u32 secure_ip_id(__u32 daddr)
+{
+	struct keydata *keyptr;
+	__u32 hash[4];
+
+	keyptr = get_keyptr();
+
+	/*
+	 *  Pick a unique starting offset for each IP destination.
+	 *  The dest ip address is placed in the starting vector,
+	 *  which is then hashed with random data.
+	 */
+	hash[0] = daddr;
+	hash[1] = keyptr->secret[9];
+	hash[2] = keyptr->secret[10];
+	hash[3] = keyptr->secret[11];
+
+	return halfMD4Transform(hash, keyptr->secret);
+}
+
+#ifdef CONFIG_INET
+
 __u32 secure_tcp_sequence_number(__u32 saddr, __u32 daddr,
 				 __u16 sport, __u16 dport)
 {
@@ -2242,28 +2266,7 @@ __u32 secure_tcp_sequence_number(__u32 s
 
 EXPORT_SYMBOL(secure_tcp_sequence_number);
 
-/*  The code below is shamelessly stolen from secure_tcp_sequence_number().
- *  All blames to Andrey V. Savochkin <saw@msu.ru>.
- */
-__u32 secure_ip_id(__u32 daddr)
-{
-	struct keydata *keyptr;
-	__u32 hash[4];
-
-	keyptr = get_keyptr();
-
-	/*
-	 *  Pick a unique starting offset for each IP destination.
-	 *  The dest ip address is placed in the starting vector,
-	 *  which is then hashed with random data.
-	 */
-	hash[0] = daddr;
-	hash[1] = keyptr->secret[9];
-	hash[2] = keyptr->secret[10];
-	hash[3] = keyptr->secret[11];
 
-	return halfMD4Transform(hash, keyptr->secret);
-}
 
 /* Generate secure starting point for ephemeral TCP port search */
 u32 secure_tcp_port_ephemeral(__u32 saddr, __u32 daddr, __u16 dport)
@@ -2383,3 +2386,41 @@ __u32 check_tcp_syn_cookie(__u32 cookie,
 }
 #endif
 #endif /* CONFIG_INET */
+
+
+/*
+ * Get a random word for internal kernel use only. Similar to urandom but with the goal
+ * of minimal entropy pool depletion. As a result, the random value is not cryptographically
+ * secure but for several uses the cost of depleting entropy is too high
+ */
+unsigned int get_random_int(void)
+{
+	static unsigned int val = 0;
+
+	val += current->pid + jiffies;
+
+	/*
+	 * Use IP's RNG. It suits our purpose perfectly: it re-keys itself
+	 * every second, from the entropy pool (and thus creates a limited
+	 * drain on it), and uses halfMD4Transform within the second. We
+	 * also mix it with jiffies and the PID:
+	 */
+	return secure_ip_id(val);
+}
+
+/* 
+ * randomize_range() returns a start address such that
+ *
+ *    [...... <range> .....]
+ *  start                  end
+ *
+ * a <range> with size "len" starting at the return value is inside in the
+ * area defined by [start, end], but is otherwise randomized.
+ */
+unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len)
+{
+	unsigned long range = end - len - start;
+	if (end <= start + len)
+		return 0;
+	return PAGE_ALIGN(get_random_int() % range + start);
+}
diff -purN step1/include/linux/random.h step2/include/linux/random.h
--- step1/include/linux/random.h	2005-01-26 18:24:39.000000000 +0100
+++ step2/include/linux/random.h	2005-01-27 10:52:56.000000000 +0100
@@ -70,6 +70,9 @@ extern __u32 secure_tcpv6_sequence_numbe
 extern struct file_operations random_fops, urandom_fops;
 #endif
 
+unsigned int get_random_int(void);
+unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len);
+
 #endif /* __KERNEL___ */
 
 #endif /* _LINUX_RANDOM_H */

  parent reply	other threads:[~2005-01-27 10:13 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-27 10:11 Patch 0/6 virtual address space randomisation Arjan van de Ven
2005-01-27 10:12 ` Patch 1/6 introduce sysctl Arjan van de Ven
2005-01-27 10:36   ` Andi Kleen
2005-01-27 11:13     ` Arjan van de Ven
2005-01-27 18:16   ` Pavel Machek
2005-01-27 19:11     ` Ingo Molnar
2005-01-27 19:46       ` Dave Jones
2005-01-27 19:53         ` Ingo Molnar
2005-01-27 19:53         ` Arjan van de Ven
2005-02-04 21:27   ` Benoit Boissinot
2005-01-27 10:12 ` Arjan van de Ven [this message]
2005-01-27 10:41   ` Patch 2/6 introduce helper infrastructure Andi Kleen
2005-01-27 11:58     ` Arjan van de Ven
2005-01-27 12:27       ` Andi Kleen
2005-01-27 12:43         ` Arjan van de Ven
2005-02-01 21:14   ` Matt Mackall
2005-01-27 10:12 ` Patch 3/6 per process flag Arjan van de Ven
2005-01-27 10:13 ` Patch 4/6 randomize the stack pointer Arjan van de Ven
2005-01-27 10:21   ` Christoph Hellwig
2005-01-27 17:38   ` John Richard Moser
2005-01-27 17:47     ` Arjan van de Ven
2005-01-27 18:04       ` John Richard Moser
2005-01-27 18:09         ` Arjan van de Ven
2005-01-27 18:12         ` Christoph Hellwig
2005-01-27 18:16         ` Linus Torvalds
2005-01-27 18:28           ` Linus Torvalds
2005-01-27 18:55             ` John Richard Moser
2005-01-27 18:49           ` John Richard Moser
2005-01-27 19:30             ` Linus Torvalds
2005-01-27 19:48               ` Arjan van de Ven
2005-01-27 19:59                 ` Linus Torvalds
2005-01-27 20:04                   ` Arjan van de Ven
2005-01-27 20:08               ` John Richard Moser
2005-01-27 19:19           ` linux-os
2005-01-27 19:52             ` Julien TINNES
2005-01-27 20:02             ` Arjan van de Ven
2005-01-27 20:13               ` John Richard Moser
2005-01-27 21:33                 ` jnf
2005-01-28 17:22                 ` Paulo Marques
2005-01-28 17:51                   ` John Richard Moser
2005-01-28 18:42                   ` Ingo Molnar
2005-01-29  6:04                     ` John Richard Moser
2005-01-27 20:37               ` linux-os
2005-01-27 20:45                 ` John Richard Moser
2005-01-27 21:39           ` John Richard Moser
2005-01-27 21:53             ` Arjan van de Ven
2005-01-27 22:34               ` John Richard Moser
2005-01-29  2:50                 ` Rik van Riel
2005-01-29  6:31                   ` John Richard Moser
2005-01-29  8:10                     ` Arjan van de Ven
     [not found]                       ` <41FBB821.3000403@comcast.net>
2005-01-29 16:42                         ` Arjan van de Ven
2005-01-29 16:59                           ` John Richard Moser
2005-01-29 16:46                         ` Arjan van de Ven
2005-01-29 17:04                           ` John Richard Moser
2005-01-29 17:37                     ` Jakub Jelinek
2005-01-29 17:49                       ` John Richard Moser
2005-01-29 17:55                         ` Christoph Hellwig
2005-01-29 18:10                           ` John Richard Moser
2005-01-29 18:12                             ` Rik van Riel
2005-01-29 18:16                             ` Christoph Hellwig
2005-01-29  7:46           ` John Richard Moser
2005-01-27 18:40         ` Felipe Alfaro Solana
2005-01-27 22:31     ` Jirka Kosina
2005-01-28  5:58       ` Ingo Molnar
2005-01-28 19:02         ` David Lang
2005-01-28  7:33       ` Arjan van de Ven
2005-01-27 19:43   ` Julien TINNES
2005-01-28  0:10     ` H. Peter Anvin
2005-01-28  0:23       ` Roland Dreier
2005-01-28  1:06         ` H. Peter Anvin
2005-01-28  2:03     ` Horst von Brand
2005-01-28  8:45       ` Julien TINNES
2005-01-27 20:23   ` Christoph Hellwig
2005-01-27 20:27     ` Arjan van de Ven
2005-01-27 20:32       ` Christoph Hellwig
2005-01-27 20:35         ` Arjan van de Ven
2005-01-27 20:40         ` Rik van Riel
2005-01-27 20:42           ` Christoph Hellwig
2005-01-27 20:56             ` Arjan van de Ven
2005-01-27 21:13               ` Linus Torvalds
2005-01-27 10:13 ` Patch 5/6 randomize mmap addresses Arjan van de Ven
2005-01-27 10:14 ` Patch 6/6 default enable randomisation for -mm Arjan van de Ven
2005-01-27 11:45 ` Patch 0/6 virtual address space randomisation Julien TINNES
2005-01-27 11:57   ` Arjan van de Ven
2005-01-27 17:42     ` John Richard Moser
2005-01-27 19:34       ` Julien TINNES
2005-01-27 19:57         ` John Richard Moser
2005-01-27 20:13         ` Arjan van de Ven
2005-01-28  8:45           ` David Weinehall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050127101228.GC9760@infradead.org \
    --to=arjan@infradead.org \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox