Skip to contentSkip to conversation

vfsprintf: only hash addresses in security environment

PATCH In review Jason Yan 5 participants 7 messages Mar 4, 2020 – Mar 6, 2020

Subsystems (2)
lore
Share & tools
lore

Jason Yan

author
#0 lore raw

PATCH 1/1 vfsprintf: only hash addresses in security environment

When I am implementing KASLR for powerpc, Scott Wood argued that format specifier "%p" always hashes the addresses that people do not have a choice to shut it down: https://patchwork.kernel.org/cover/11367547/

It's true that if in a debug environment or security is not concerned, such as KASLR is absent or kptr_restrict = 0, there is no way to shut the hashing down except changing the code and build the kernel again to use a different format specifier like "%px". And when we want to turn to security environment, the format specifier has to be changed back and rebuild the kernel.

As KASLR is available on most popular platforms and enabled by default, print the raw value of address while KASLR is absent and kptr_restrict is zero. Those who concerns about security must have KASLR enabled or kptr_restrict set properly.

Cc Scott Wood <[email protected]>
Cc Kees Cook <[email protected]>
Cc "Tobin C . Harding" <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Daniel Axtens <[email protected]>
---
 lib/vsprintf.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
lib/vsprintf.c +9 -2
index 7c488a1ce318..f74131b152a1 100644
@@ -2253,8 +2253,15 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 		return err_ptr(buf, end, ptr, spec);
 	}
 
-	/* default is to _not_ leak addresses, hash before printing */
-	return ptr_to_id(buf, end, ptr, spec);
+	/*
+	 * In security environment, while kaslr is enabled or kptr_restrict is
+	 * not zero, hash before printing so that addresses will not be
+	 * leaked. And if not in a security environment, print the raw value
+	 */
+	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) || kptr_restrict)
+		return ptr_to_id(buf, end, ptr, spec);
+	else
+		return pointer_string(buf, end, ptr, spec);
 }
 
 /*
Signature · 1 line
2.17.2
#1 lore raw
Quoted 11 lines · Jason Yan

When I am implementing KASLR for powerpc, Scott Wood argued that format specifier "%p" always hashes the addresses that people do not have a choice to shut it down: https://patchwork.kernel.org/cover/11367547/

It's true that if in a debug environment or security is not concerned, such as KASLR is absent or kptr_restrict = 0, there is no way to shut the hashing down except changing the code and build the kernel again to use a different format specifier like "%px". And when we want to turn to security environment, the format specifier has to be changed back and rebuild the kernel.

Quoted 4 lines

As KASLR is available on most popular platforms and enabled by default, print the raw value of address while KASLR is absent and kptr_restrict is zero. Those who concerns about security must have KASLR enabled or kptr_restrict set properly.

Even w/o KASLR the kernel address is sensitive material.
However, as a developer, I would like to have means to shut the hashing down.

Btw, when pass 'nokaslr' to the kernel it should turned off as well.

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char *end, void *ptr, · line 2257

Quoted 2 lines
lib/vsprintf.c +2 -0
+	/*
+	 * In security environment, while kaslr is enabled or kptr_restrict is

kaslr -> KASLR

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char *end, void *ptr, · line 2259

Quoted 2 lines
lib/vsprintf.c +2 -0
+	 * not zero, hash before printing so that addresses will not be
+	 * leaked. And if not in a security environment, print the raw value

Missed period at the end of sentence.

Quoted 6 lines
lib/vsprintf.c +5 -0
+	 */
+	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) || kptr_restrict)
+		return ptr_to_id(buf, end, ptr, spec);
+	else
+		return pointer_string(buf, end, ptr, spec);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Jason Yan
Quoted 11 lines · Jason Yan

When I am implementing KASLR for powerpc, Scott Wood argued that format specifier "%p" always hashes the addresses that people do not have a choice to shut it down: https://patchwork.kernel.org/cover/11367547/

It's true that if in a debug environment or security is not concerned, such as KASLR is absent or kptr_restrict = 0, there is no way to shut the hashing down except changing the code and build the kernel again to use a different format specifier like "%px". And when we want to turn to security environment, the format specifier has to be changed back and rebuild the kernel.

Quoted 4 lines

As KASLR is available on most popular platforms and enabled by default, print the raw value of address while KASLR is absent and kptr_restrict is zero. Those who concerns about security must have KASLR enabled or kptr_restrict set properly.

Sorry, but no: %p censoring is there to stem the flood of endless pointer leaks into logs, sysfs, proc, etc. These are used for attacks to build reliable kernel memory targets, regardless of KASLR. (KASLR can be bypassed with all kinds of sampling methods, side-channels, etc.)

Linus has rejected past suggestions to provide a flag for it[1], wanting %p to stay unconditionally censored.

-Kees

[1] https://lore.kernel.org/lkml/CA+55aFwieC1-nAs+NFq9RTwaR8ef9hWa4MjNBWL41F-8wM49eA@mail.gmail.com/

Quoted 36 lines
Cc Scott Wood <[email protected]>
Cc Kees Cook <[email protected]>
Cc "Tobin C . Harding" <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Daniel Axtens <[email protected]>
---
 lib/vsprintf.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
lib/vsprintf.c +9 -2
index 7c488a1ce318..f74131b152a1 100644
@@ -2253,8 +2253,15 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 		return err_ptr(buf, end, ptr, spec);
 	}
 
-	/* default is to _not_ leak addresses, hash before printing */
-	return ptr_to_id(buf, end, ptr, spec);
+	/*
+	 * In security environment, while kaslr is enabled or kptr_restrict is
+	 * not zero, hash before printing so that addresses will not be
+	 * leaked. And if not in a security environment, print the raw value
+	 */
+	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) || kptr_restrict)
+		return ptr_to_id(buf, end, ptr, spec);
+	else
+		return pointer_string(buf, end, ptr, spec);
 }
 
 /*
Signature · 1 line
2.17.2
Signature · 1 line
Kees Cook
#3 lore raw

Re: [PATCH v3 0/6] implement KASLR for powerpc/fsl_booke/64

Quoted 17 lines · Kees Cook
Quoted 11 lines · Jason Yan

When I am implementing KASLR for powerpc, Scott Wood argued that format specifier "%p" always hashes the addresses that people do not have a choice to shut it down: https://patchwork.kernel.org/cover/11367547/

It's true that if in a debug environment or security is not concerned, such as KASLR is absent or kptr_restrict = 0, there is no way to shut the hashing down except changing the code and build the kernel again to use a different format specifier like "%px". And when we want to turn to security environment, the format specifier has to be changed back and rebuild the kernel.

Quoted 4 lines

As KASLR is available on most popular platforms and enabled by default, print the raw value of address while KASLR is absent and kptr_restrict is zero. Those who concerns about security must have KASLR enabled or kptr_restrict set properly.

Quoted 7 lines

Sorry, but no: %p censoring is there to stem the flood of endless pointer leaks into logs, sysfs, proc, etc. These are used for attacks to build reliable kernel memory targets, regardless of KASLR. (KASLR can be bypassed with all kinds of sampling methods, side-channels, etc.)

Linus has rejected past suggestions to provide a flag for it[1], wanting %p to stay unconditionally censored.

The frustration is with the inability to set a flag to say, "I'm debugging and don't care about leaks... in fact I'd like as much information as possible to leak to me." Hashed addresses only allow comparison to other hashes which doesn't help when looking at (or trying to find) data structures in kernel memory, etc. I get what Linus is saying about "you can't have nice things because people won't test the normal config" but it's still annoying. :-P

In any case, this came up now due to a question about what to use when printing crash dumps. PowerPC currently prints stack and return addresses with %lx (in addition to %pS in the latter case) and someone proposed converting them to %p and/or removing them altogether. Is there a consensus on whether crash dumps need to be sanitized of this stuff as well? It seems like you'd have the addresses in the register dump as well (please don't take that away too...). Maybe crash dumps would be a less problematic place to make the hashing conditional (i.e. less likely to break something in userspace that wasn't expecting a hash)?

-Scott

#4 lore raw

Re: [PATCH v3 0/6] implement KASLR for powerpc/fsl_booke/64

Quoted 3 lines · Scott Wood

In any case, this came up now due to a question about what to use when printing crash dumps. PowerPC currently prints stack and return addresses with %lx (in addition to %pS in the latter case) and someone proposed

Right -- I think other archs moved entirely to %pS and just removed %lx and %p uses.

Quoted 6 lines

converting them to %p and/or removing them altogether. Is there a consensus on whether crash dumps need to be sanitized of this stuff as well? It seems like you'd have the addresses in the register dump as well (please don't take that away too...). Maybe crash dumps would be a less problematic place to make the hashing conditional (i.e. less likely to break something in userspace that wasn't expecting a hash)?

Actual _crash_ dumps print all kinds of stuff, even the KASLR offset, but for generic stack traces, it's been mainly %pS, with things like registers using %lx.

I defer to Linus, obviously. I just wanted to repeat what he'd said before.

Signature · 1 line
Kees Cook
#5 lore raw

Re: [PATCH v3 0/6] implement KASLR for powerpc/fsl_booke/64

In reply to Scott Wood
Quoted 4 lines · Scott Wood

The frustration is with the inability to set a flag to say, "I'm debugging and don't care about leaks... in fact I'd like as much information as possible to leak to me."

Well, I definitely don't want to tie it to "I turned off kaslr in order to help debugging". That just means that now you're debugging a kernel that is fundamentally different from what people are running.

So I'd much rather have people just set a really magic flag, perhaps when kgdb is in use or something.

Quoted 4 lines

In any case, this came up now due to a question about what to use when printing crash dumps. PowerPC currently prints stack and return addresses with %lx (in addition to %pS in the latter case) and someone proposed converting them to %p and/or removing them altogether.

Please just use '%pS'.

The symbol and offset is what is useful when users send crash-dumps. The hex value is entirely pointless with kaslr - which should basically be the default.

Note that this isn't about security at that point - crash dumps are something that shouldn't happen, but if they do happen, we want the pointers. But the random hex value just isn't _useful_, so it's just making things less legible.

Linus

#6 lore raw

Re: [PATCH v3 0/6] implement KASLR for powerpc/fsl_booke/64

Quoted 11 lines · Linus Torvalds
Quoted 6 lines · Scott Wood

The frustration is with the inability to set a flag to say, "I'm debugging and
don't care about leaks... in fact I'd like as much information as possible to
leak to me."

Well, I definitely don't want to tie it to "I turned off kaslr in order to help debugging". That just means that now you're debugging a kernel that is fundamentally different from what people are running.

One shouldn't *test* with something different from what people are running but once a problem has been identified I don't see the problem with changing the kernel to make diagnosis easier (assuming the problem is reproduceable). Though I suppose one could just locally apply a "no pointer hashing" patch when debugging...

Quoted 10 lines

So I'd much rather have people just set a really magic flag, perhaps when kgdb is in use or something.

Quoted 4 lines

In any case, this came up now due to a question about what to use when printing crash dumps. PowerPC currently prints stack and return addresses with %lx (in addition to %pS in the latter case) and someone proposed converting them to %p and/or removing them altogether.

Please just use '%pS'.

Quoted 8 lines

The symbol and offset is what is useful when users send crash-dumps. The hex value is entirely pointless with kaslr - which should basically be the default.

Note that this isn't about security at that point - crash dumps are something that shouldn't happen, but if they do happen, we want the pointers. But the random hex value just isn't _useful_, so it's just making things less legible.

Losing %lx on the return address would be a minor annoyance (harder to verify that you're looking at the right stack frame in a dump, more steps to look up the line number when modules and kaslr aren't involved, etc), but %pS doesn't help with stack addresses themselves -- and yes, digging into the actual stack data (via kdump, external debugger, etc.) is sometimes useful. Maybe condition it on it being an actual crash dump and not some other caller of show_stack()?

-Scott