Re: [PATCH] get_random_long() and AT_ENTROPY for auxv, kernel 2.6.21.5

Previous thread: [2.6 patch] mm/slab.c: start_cpu_timer() should be __cpuinit by Adrian Bunk on Sunday, June 17, 2007 - 4:43 pm. (1 message)

Next thread: PCI userland access non-mmap APIs, kernel access to legacy space by Benjamin Herrenschmidt on Sunday, June 17, 2007 - 7:16 pm. (3 messages)
From: Alexander Gabert
Date: Sunday, June 17, 2007 - 4:40 pm

Hello glibc and kernel maintainers,

could you please add two AT_ entries to include/linux/auxvec.h in the 
upstream kernel and the respective elf/elf.h definitions in glibc.

#define AT_ENTROPY1 24   /* kernel entropy in auxiliary vector */
#define AT_ENTROPY2 25   /* second field for kernel entropy */

The accompanying kernel patch follows, it is basically a config option 
to give userland entropy.
We are using the appended patch for Gentoo Hardened.
You don't have to put it into the upstream linux kernel, it is only 
there for explaining what i need the AT_ values for.
Also the patch contains a subtle "bug" because the repetive call to 
get_random_int()
seems to yield the same result when done immediately after another:

$$ od -tx8 /proc/self/auxv
0000000 ffffe40000000020 ffffe00000000021
0000020 0383f9ff00000010 0000100000000006
0000040 0000006400000011 8000003400000003
0000060 0000002000000004 0000000a00000005
0000100 b7ef200000000007 0000000000000008
0000120 800013f000000009 000000000000000b
0000140 000000000000000c 000000000000000d
0000160 000000000000000e 0000000000000017
0000200 0c1f4d8100000018 0c1f4d8100000019
0000220 bfa7da8b0000000f 0000000000000000
0000240


However, this is just a proof of concept, all i'm asking for is to get 
the two numbers into the upstream
kernel and glibc auxvec.h to avoid future clashes with the numbers, thx 
in advance.

-- patch --

diff -Nru linux-2.6.21.5.ORIG/fs/binfmt_elf.c linux-2.6.21.5/fs/binfmt_elf.c
--- linux-2.6.21.5.ORIG/fs/binfmt_elf.c	2007-06-11 20:37:06.000000000 +0200
+++ linux-2.6.21.5/fs/binfmt_elf.c	2007-06-18 00:22:59.000000000 +0200
@@ -201,6 +201,13 @@
 	NEW_AUX_ENT(AT_GID, tsk->gid);
 	NEW_AUX_ENT(AT_EGID, tsk->egid);
  	NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
+#if defined(CONFIG_AT_ENTROPY1)
+  NEW_AUX_ENT(AT_ENTROPY1, get_random_int());
+#endif
+#if defined(CONFIG_AT_ENTROPY2)
+  NEW_AUX_ENT(AT_ENTROPY2, get_random_int());
+#endif
+
 	if (k_platform) {
 ...
From: Arjan van de Ven
Date: Sunday, June 17, 2007 - 6:06 pm

while I'm not per se against adding such aux vectors, I think it's a bad
mistake to make them a config option (and 2 options at that!!)

Stack protector is very widely deployed today
(Fedora,RHEL,SLES,OpenSUSE,Gentoo etc), so I can sure see the point of
helping it a bit... but I'd like to see a little more data on how the
current approach isn't sufficient.


-

From: Alexander Gabert
Date: Sunday, June 17, 2007 - 6:28 pm

Well our glibc patch (still under development, here comes a snippet) 
will conditionally check for the auxv and if not "fall back" to normal 
SSP entropy:

-  /* Set up the stack checker's canary.  */
-  uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard ();
+  /* Set up the stack checker's canary, optional kernel entropy  */
+  uintptr_t stack_chk_guard;
+
+  if (GLRO(dl_entropy) != 0) {
+    stack_chk_guard = GLRO(dl_entropy);
+  }
+  else {
+    stack_chk_guard = _dl_setup_stack_chk_guard ();
+  }

Hence the config option for the kernel- it's philosophy at Gentoo to 
make choices available to users how they want their systems to behave, 
even on the expense of added complexity and need to "understand" how 
things work in the first place.

If you add the aux vector behaviour as a default, there would surely be 
a backport of the default behaviour to the more "Gentoo flavoured" 
choice-based system of activating/deactivating it to your own needs.

For your second question, why two config options: entropy is a precious 
resource and many critical apps rely on it.
Until SSPx (http://dev.gentoo.org/~pappy/sspx/ssxp.pdf) is not 
available, we don't want to force users to have two vector entries with 
one containing unneeded (wasted?) entropy.  SSP already works good, so 
if this stuff makes it's way into the kernel or kernel and glibc folks 
tell me which numbers i can use, i can make my patches and add the logic 
to Gentoo kernel sources and glibc.

Sincere thanks,


Alex
-

From: Arjan van de Ven
Date: Sunday, June 17, 2007 - 6:38 pm

While I am not opposed to choice, I am opposed to having too finegrained
kernel config options. In your view, every single kernel patch would be
a config option... I much rather have config options for "important" big
changes, not for something this small. Another argument is that this
kind of userspace interface is better off being always there or never;
making this variable serves no-one.



-- 
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-

From: Alexander Gabert
Date: Monday, June 18, 2007 - 3:36 am

Hi again,

as said, i'm not voting for getting the specific example patch into 
kernel source.
All i came for and would like to see is the two numbers into the 
upstream header file to avoid future namespace clashes (as said in the 
original mail).

I honestly don't know how to follow the discussion to implement this 
kind of entropy given via auxv to userland- if you (the kernel devs) 
think it makes sense the way you want it you as upstream devs can do it 
all the way you like it- i can always change it with a patch for my own 
needs in my own sources.  I can't say anything at all about all of this 
because i don't know what is important from your point of view as kernel 
maintainers and what is not.  I understand your concerns and know that 
we are talking from different perspectives, thus it is probably normal 
that our interests and technical solutions vary.

However, as said, the only thing important for me is to not use 
"arbitrary" chosen numbers for the AT_ENTROPY1 and 2 values and later 
have problems when my "private" numbers suddenly are assigned to a 
different thing by "official" kernel devs.  That would be bad for me and 
this is what i want to avoid in the first place.

So please tell me what i can do about the requirements and the time and 
what the usual process is for asking how to get the numbers into the 
kernel so i can start using them for my own work.

Thanks in advance,


Alex



-

From: Alexander Gabert
Date: Wednesday, June 20, 2007 - 8:34 am

Hi,

http://dev.gentoo.org/~pappy/kernel/linux-2.6.21.5-get_urandom_long-AT_ENTROPY.patch

this patch adds the function drivers/char/random.c:get_random_long()
and adds an AT_ENTROPY field in the auxv without config option
(the config option was removed as suggested by Arjan on LKML).

README: get_random_long() and AT_ENTROPY support for auxv
NAME: Alexander Gabert
EMAIL: pappy@gentoo.org



diff -Nru linux-2.6.21.5.ORIG/drivers/char/random.c 
linux-2.6.21.5/drivers/char/random.c
--- linux-2.6.21.5.ORIG/drivers/char/random.c    2007-06-11 
20:37:06.000000000 +0200
+++ linux-2.6.21.5/drivers/char/random.c    2007-06-20 
17:00:35.000000000 +0200
@@ -1654,6 +1654,53 @@
 }
 
 /*
+ * get_random_long() returns a randomized unsigned long word.
+ * It recycles it's entropy cache for a given time period and
+ * uses half_md4_transform to generate a unique return value.
+ * Every REKEY_INTERVAL the cache is reloaded with fresh
+ * randomization data using get_random_bytes().
+ * This function is not intended for strong cryptographic routines.
+ */
+unsigned long get_random_long(void)
+{
+  /* remember the last time we refreshed the cache with random entropy */
+  static time_t   rekey_time;
+
+  time_t          t;
+
+  /*
+   * the following data in the buffer is unchanged during REKEY_INTERVAL:
+   * |----|----|KKKK|KKKK|KKKK|KKKK|KKKK|KKKK|----|----|----|----|
+   * ___0____1____2____3____4____5____6____7____8____9___10___11__
+   *
+   * the following data is updated during the first half_md4_transform call
+   * |----|YYYY|----|----|----|----|----|----|ZZZZ|ZZZZ|ZZZZ|ZZZZ|
+   * ___0____1____2____3____4____5____6____7____8____9___10___11__
+   *
+   * the following data is updated during the second half_md4_transform
+   * |XXXX|----|----|----|----|----|----|----|ZZZZ|ZZZZ|ZZZZ|ZZZZ|
+   * ___0____1____2____3____4____5____6____7____8____9___10___11__
+   */
+  static __u32    entropycache[12];
+
+  /* get the current time in seconds */
+  t = ...
From: Arjan van de Ven
Date: Wednesday, June 20, 2007 - 8:38 am

your patch has some whitespace damage it seems...
other than that I can agree with the approach.

-- 
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-

From: Linus Torvalds
Date: Wednesday, June 20, 2007 - 9:39 am

It's totally whitespace-damaged, and I wonder why the "int" vs "long" is 
so important. Also, what are the performance implications for execve?

		Linus
-

From: Eric Dumazet
Date: Wednesday, June 20, 2007 - 10:04 am

On Wed, 20 Jun 2007 17:34:13 +0200



This is not valid on some arches, as entropycache[] alignment (u32 -> 4) might be smaller then alignment for a long (4 or 8). 

This also adds about 400 instructions (half_md4_transform() is about 200 instructions, about 700 bytes of code on x86_64) in exec() path, but this is probably minor given the cost of exec()

I am not sure why you unconditionally call half_md4_transform() twice, since the entropycache[1] wont be used on 32bits platforms.

I suggest spliting your entropycache into two parts :

One part, with 8 u32, that is read_mostly (and shared by all cpus), updated once every 300 seconds in rekey_seq_generator()
static u32 entropycache_shared[8] __read_mostly;

One part, with (16/sizeof(long)) long, percpu to avoid false sharing between cpus.
static DEFINE_PER_CPU(unsigned long , entropycache_pcpu)[16 / sizeof(unsigned long)];

then call half_md4_transform() once :

half_md4_transform((u32 *)entropycache_pcpu, entropycache_shared);
return entropycache_pcpu[0];

-

From: Matt Mackall
Date: Wednesday, June 20, 2007 - 1:30 pm

What's the point of this?

-- 
Mathematics is the supreme nostalgia of our time.
-

From: Alexander Gabert
Date: Sunday, June 24, 2007 - 10:45 am

Hi Linus,
hi LKML,

i would like to thank LKML and especially Eric (thanks for the per_cpu 
macro tips and design guidelines!) and the other contributors to this idea.

This time the patch is rather big because it also removes 
get_random_int() and introduces get_random_long() throughout the kernel.
The function get_random_int was primarily used for setting up the 
randomization of memory used for process stacks and initializing a stack 
canary in a tsk.
However, using get_random_long works exactly the same way to generate 
randomization deltas for this purpose and i have applied it to all 
locations where get_random_int was previously used.

Now to explain the reasoning for replacing get_random_int...
My findings with get_random_int was that when i called it two times very 
fast after another because of setting up two SSP randomized guard values 
for the same process, it returned the same, yet randomized, integer number.

I learned that get_random_int uses the current process pid and the 
current jiffies for feeding the half_md4_transform function that's 
following deeper in the code.  However, when called in a relatively 
short timeframe (lesser than a jiffie can increase it's count), this 
information given to the underlying "randomization" generation is not 
changing, which means when you call get_random_int two or more times in 
short time, it will use the same data for generating the randomized 
data, which is the pid of the process and current jiffies.  The returned 
randomized integer numbers are the same because the pid and the jiffies 
used as the input for the randomization function did not change in such 
a short time.  Sorry that i cannot explain it more detailed or in better 
english, you should be able to see it from the code looking at 
get_random_int and how it's working by calling the other function and 
looking at the input to the function.
Also remember that the comment in the function is probably a bit 
outdated because rekeying is happening not every 1 ...
From: Matt Mackall
Date: Sunday, June 24, 2007 - 8:45 pm

Stop right there. You still haven't answered my original question.
What is the point of this exercise in the first place, please?

Am I right in thinking you have three unrelated patches here?

- something to do with aux vector headers
- something to do with get_random_int repeating itself
- sweeping change of get_random_int to get_random_long for no obvious reason


Send me a patch that fixes the above and nothing else, please.


This probably breaks Sparc64.

-- 
Mathematics is the supreme nostalgia of our time.
-

From: Arjan van de Ven
Date: Sunday, June 24, 2007 - 9:43 pm

the primary goal is to pass a random value to userspace at process
start; this to save glibc from having to open /dev/urandom on ever
program start (which it does now for all apps compiled with

and this is because Alexander wants 2 and not 1 random int to be passed
for his own glibc proposed change (combined with get_random_int() being
designed for only 4 bytes per process ;-)

-- 
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-

From: Matt Mackall
Date: Sunday, June 24, 2007 - 10:12 pm

Interesting.

What are our requirements here? Defending against local attackers who

Sure. Still, separate patches.

-- 
Mathematics is the supreme nostalgia of our time.
-

From: Jakub Jelinek
Date: Monday, June 25, 2007 - 12:09 am

There are 2 ways to compile -fstack-protector supporting glibc actually,
only one opens /dev/urandom on every program initialization, the other
computes the stack guard from some bits of the stack address (so indirectly
depends on get_random_int() in stack randomization).
Nevertheless, having one random long (32-bit for 32-bit arches, 64-bit
otherwise) in aux vector would be useful.

	Jakub
-

From: Alexander Gabert
Date: Monday, June 25, 2007 - 8:02 am

Hi Matt,
sorry for not answering your questions in the first place, i hope this 
did not mean to make a bad impression
It is needed for properly initializing a SSP guard which is (afaik) a 
Probably ... but bear in mind that the goal is still the same: allowing 
glibc to use SSP with /proc/self/auxv instead of fopen(/dev/urandom) as 
it is now.
Effectively saving three syscalls (open,read,close) and making life 
easier for glibc because randomization "generated" in the kernel does 
not deplete /dev/urandom too much for high coverage SSP userlands (i.e. 
Gentoo Hardened).

I can imagine that Redhat would do the same with the SSP implementation 
in glibc, i think if this patch moves into kernel, they will bring out a 
glibc patch that is checking for AT_ENTROPY and using the opening of 
/dev/urandom for retrieving randomized data as a fallback for machines 
where such a kernel is not available.  This is a win-win situation for 
both sides- the kernel wins because the pressure on /dev/urandom is 
released a bit (applicable to SSP environments) and the glibc wins 
because it has a reliable, fast, cheap and easy to use source for 
randomization.


Thank you,

Alex

-

From: Matt Mackall
Date: Monday, June 25, 2007 - 8:20 am

The point is: the way we do development here is to break things down
into a series of simple, obvious pieces and send them individually.
Each patch should build and run when applied on top of the earlier
patches. Then each part can be reviewed, tested, and applied on its
own. It also makes figuring out what happened in the future and
tracking down bugs much easier.

So send three patches in this order:

- fix get_random_int
- rename it
- add auxv bits

Also, you might want to drop Linus from the cc: list unless he's
chimed in on this topic already. The likely path here is for the first
two patches to go through me and the last to go through Arjan, then on
to Andrew Morton for his -mm tree, and then to Linus after a bit.

-- 
Mathematics is the supreme nostalgia of our time.
-

Previous thread: [2.6 patch] mm/slab.c: start_cpu_timer() should be __cpuinit by Adrian Bunk on Sunday, June 17, 2007 - 4:43 pm. (1 message)

Next thread: PCI userland access non-mmap APIs, kernel access to legacy space by Benjamin Herrenschmidt on Sunday, June 17, 2007 - 7:16 pm. (3 messages)