Skip to contentSkip to conversation

printk: introduce kptr_restrict level 3

PATCH In review [email protected] 6 participants 24 messages Oct 5, 2016 – Oct 11, 2016

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 printk: introduce kptr_restrict level 3

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

Sample Output:
kptr_restrict == 2:
p: 00000000604369f4
pK: 0000000000000000

kptr_restrict == 3:
p: 0000000000000000
pK: 0000000000000000

---
 Documentation/sysctl/kernel.txt |   3 ++
 kernel/sysctl.c                 |   3 +-
 lib/vsprintf.c                  | 107 ++++++++++++++++++++++++----------------
 3 files changed, 69 insertions(+), 44 deletions(-)
Documentation/sysctl/kernel.txt +3 -0
index ffab8b5..bca72a0 100644
@@ -393,6 +393,9 @@ values to unprivileged users is a concern.
 When kptr_restrict is set to (2), kernel pointers printed using
 %pK will be replaced with 0's regardless of privileges.
 
+When kptr_restrict is set to (3), kernel pointers printed using
+%p and %pK will be replaced with 0's regardless of privileges.
+
 ==============================================================
 
 kstack_depth_to_print: (X86 only)
kernel/sysctl.c +2 -1
index a377bfa..0d4e4af 100644
@@ -128,6 +128,7 @@ static unsigned long one_ul = 1;
 static int one_hundred = 100;
 static int one_thousand = 1000;
 #ifdef CONFIG_PRINTK
+static int three = 3;
 static int ten_thousand = 10000;
 #endif
 #ifdef CONFIG_PERF_EVENTS
@@ -847,7 +848,7 @@ static struct ctl_table kern_table[] = {
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax_sysadmin,
 		.extra1		= &zero,
-		.extra2		= &two,
+		.extra2		= &three,
 	},
 #endif
 	{
lib/vsprintf.c +64 -43

137 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt !=
'X' && *fmt != 'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting. (For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with upstream.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#2 lore raw
Quoted 20 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Quoted 7 lines

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Not saying they don't I didn't find any.

Quoted 22 lines

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved
dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt != 'X' && *fmt !=
'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting.
(For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Quoted 3 lines

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with upstream.

I could likely take this on. I would need to read up on the links and have a better concept of what it is.

Quoted 6 lines

-Kees

--
Kees Cook
Nexus Security

#3 lore raw
In reply to Kees Cook
Quoted 49 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved
dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt != 'X' && *fmt !=
'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting.
(For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Quoted 3 lines

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with upstream.

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a module That it's dynamically allocating the .bss section or some equivalent. I would Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

Quoted 6 lines

-Kees

--
Kees Cook
Nexus Security

#4 lore raw
Quoted 53 lines · William C
Quoted 49 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved
dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt != 'X' && *fmt !=
'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting.
(For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Quoted 3 lines

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with upstream.

Quoted 6 lines

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a module That it's dynamically allocating the .bss section or some equivalent. I would Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

It looks like what Grsecurity is doing is saying "if the address is outside of user-space" (" > TASK_SIZE") and it's not whitelisted ('P', 'X') and it's going to land in a user-space buffer ("is_usercopy_object()", censor it. ("K" is already censored -- they're just optimizing to avoid re-checking it needlessly.)

So, in this case, all kernel memory, bss and stack included, would be outside the user-space address range. (I am curious, however, how to apply this to an architecture like s390 which has overlapping address ranges... probably the TASK_SIZE test needs to use some other "is in kernel memory" check that compiles down to TASK_SIZE on non-s390, and DTRT on s390, etc.)

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#5 lore raw
Quoted 82 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Thursday, October 6, 2016 5:05 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 51 lines · William C
Quoted 51 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt !=
'X' && *fmt != 'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting. (For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Quoted 3 lines
Quoted 3 lines

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with

upstream.

Quoted 3 lines

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a

module

Quoted 4 lines

That it's dynamically allocating the .bss section or some equivalent. I would Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

It looks like what Grsecurity is doing is saying "if the address is outside of user- space" (" > TASK_SIZE") and it's not whitelisted ('P',
'X') and it's going to land in a user-space buffer ("is_usercopy_object()", censor it. ("K" is already censored -- they're just optimizing to avoid re-checking it needlessly.)

Quoted 6 lines

So, in this case, all kernel memory, bss and stack included, would be outside the user-space address range. (I am curious, however, how to apply this to an architecture like s390 which has overlapping address ranges... probably the TASK_SIZE test needs to use some other "is in kernel memory" check that compiles down to TASK_SIZE on non-s390, and DTRT on s390, etc.)

Before I go off and attempt this, I just have another dumb question to ask:

If the printk copies it into the kernel ring buffer, at some point, someone comes And asks for a copy into a userspace buffer either via dmesg or proc/kmsg interfaces. Heck, it may even be on an open uart serial. How would the check actually work, My understanding, at the time %p is resolved into a string, it might not be heading to a userspace buffer. Perhaps you can help fill in what I am missing?

#6 lore raw
Quoted 89 lines · William C
Quoted 82 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Thursday, October 6, 2016 5:05 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 51 lines · William C
Quoted 51 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt !=
'X' && *fmt != 'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting. (For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Quoted 3 lines
Quoted 3 lines

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with

upstream.

Quoted 3 lines

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a

module

Quoted 4 lines

That it's dynamically allocating the .bss section or some equivalent. I would Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

It looks like what Grsecurity is doing is saying "if the address is outside of user- space" (" > TASK_SIZE") and it's not whitelisted ('P',
'X') and it's going to land in a user-space buffer ("is_usercopy_object()", censor it. ("K" is already censored -- they're just optimizing to avoid re-checking it needlessly.)

Quoted 6 lines

So, in this case, all kernel memory, bss and stack included, would be outside the user-space address range. (I am curious, however, how to apply this to an architecture like s390 which has overlapping address ranges... probably the TASK_SIZE test needs to use some other "is in kernel memory" check that compiles down to TASK_SIZE on non-s390, and DTRT on s390, etc.)

Quoted 4 lines

Before I go off and attempt this, I just have another dumb question to ask:

If the printk copies it into the kernel ring buffer, at some point, someone comes And asks for a copy into a userspace buffer either via dmesg or proc/kmsg interfaces.

IMO that's fine - I don't think pointers in the kernel ring buffer should be restricted. Instead, access to dmesg / proc/kmsg should be restricted appropriately.

I guess it depends on what the goal here is. Do we really want to stop root from ever seeing a kernel pointer (in which case OOPS messages wouldn't really work anymore)? My view is that restricting these interfaces so far that only root can access them and it's unlikely that root accidentally does so is sufficient.

#7 lore raw
Quoted 116 lines

-----Original Message-----
From: Jann Horn [mailto:[email protected]]
Sent: Friday, October 7, 2016 10:30 AM
To: Roberts, William C <[email protected]>
Cc: Kees Cook <[email protected]>; kernel- [email protected]
Subject: Re: [kernel-hardening] RE: [PATCH] printk: introduce kptr_restrict level 3

Quoted 34 lines · William C
Quoted 34 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Thursday, October 6, 2016 5:05 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 21 lines · William C
Quoted 21 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't

exist...

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt
!= 'X' && *fmt != 'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting. (For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Would you have time/interest to add the slab flags and

is_usercopy_object()?

Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines

The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with

upstream.

Quoted 4 lines

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a

module

Quoted 1 line

That it's dynamically allocating the .bss section or some equivalent. I

would

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

It looks like what Grsecurity is doing is saying "if the address is outside of user- space" (" > TASK_SIZE") and it's not whitelisted ('P',
'X') and it's going to land in a user-space buffer ("is_usercopy_object()",

censor it.

Quoted 12 lines
Quoted 11 lines

("K" is already censored -- they're just optimizing to avoid re-checking it
needlessly.)

So, in this case, all kernel memory, bss and stack included, would be outside the user-space address range. (I am curious, however, how to apply this to an architecture like s390 which has overlapping address ranges... probably the TASK_SIZE test needs to use some other "is in kernel memory" check that compiles down to TASK_SIZE on non-s390, and DTRT on s390, etc.)

Quoted 4 lines

Before I go off and attempt this, I just have another dumb question to ask:

If the printk copies it into the kernel ring buffer, at some point, someone comes And asks for a copy into a userspace buffer either via dmesg or

proc/kmsg interfaces.

Quoted 8 lines

IMO that's fine - I don't think pointers in the kernel ring buffer should be restricted.
Instead, access to dmesg / proc/kmsg should be restricted appropriately.

I guess it depends on what the goal here is. Do we really want to stop root from ever seeing a kernel pointer (in which case OOPS messages wouldn't really work anymore)? My view is that restricting these interfaces so far that only root can access them and it's unlikely that root accidentally does so is sufficient.

I'm running Ubuntu 14.04, and perhaps they just got it wrong, but I can do dmesg as an unprivileged user. I usually only work on Android, and its restricted to root. I'm not sure how other distro's set this up. Ideally, on non-debug systems, I'd like to see %p's filtered no matter what, I'd think opt in (whitelist) is ok. At least than, you did it intentionally. I verified that %p filtering gets us coredumps and kernel oops's have addresses.

Ideally on user images debug interfaces are closed, but I would like to make rogue %p's without opt in go away.

I did a little research, and /proc/sys/kernel/dmesg_restrict seems to enable or disable this ability.

After setting that to 1, it appears I got the restriction I wanted:

$ dmesg
dmesg: klogctl failed: Operation not permitted

One of my friends who does hypervisor development and is familiar with a few systems told me that Apple products just kill %p on production builds from logs.

I'm kind of torn between the various approaches discussed on this thread.

#8 lore raw
Quoted 125 lines · William C
Quoted 116 lines

-----Original Message-----
From: Jann Horn [mailto:[email protected]]
Sent: Friday, October 7, 2016 10:30 AM
To: Roberts, William C <[email protected]>
Cc: Kees Cook <[email protected]>; kernel- [email protected]
Subject: Re: [kernel-hardening] RE: [PATCH] printk: introduce kptr_restrict level 3

Quoted 34 lines · William C
Quoted 34 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Thursday, October 6, 2016 5:05 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 21 lines · William C
Quoted 21 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't

exist...

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt
!= 'X' && *fmt != 'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting. (For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Would you have time/interest to add the slab flags and

is_usercopy_object()?

Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines

The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with

upstream.

Quoted 4 lines

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a

module

Quoted 1 line

That it's dynamically allocating the .bss section or some equivalent. I

would

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

It looks like what Grsecurity is doing is saying "if the address is outside of user- space" (" > TASK_SIZE") and it's not whitelisted ('P',
'X') and it's going to land in a user-space buffer ("is_usercopy_object()",

censor it.

Quoted 12 lines
Quoted 11 lines

("K" is already censored -- they're just optimizing to avoid re-checking it
needlessly.)

So, in this case, all kernel memory, bss and stack included, would be outside the user-space address range. (I am curious, however, how to apply this to an architecture like s390 which has overlapping address ranges... probably the TASK_SIZE test needs to use some other "is in kernel memory" check that compiles down to TASK_SIZE on non-s390, and DTRT on s390, etc.)

Quoted 4 lines

Before I go off and attempt this, I just have another dumb question to ask:

If the printk copies it into the kernel ring buffer, at some point, someone comes And asks for a copy into a userspace buffer either via dmesg or

proc/kmsg interfaces.

Quoted 8 lines

IMO that's fine - I don't think pointers in the kernel ring buffer should be restricted.
Instead, access to dmesg / proc/kmsg should be restricted appropriately.

I guess it depends on what the goal here is. Do we really want to stop root from ever seeing a kernel pointer (in which case OOPS messages wouldn't really work anymore)? My view is that restricting these interfaces so far that only root can access them and it's unlikely that root accidentally does so is sufficient.

Quoted 6 lines

I'm running Ubuntu 14.04, and perhaps they just got it wrong, but I can do dmesg as an unprivileged user. I usually only work on Android, and its restricted to root. I'm not sure how other distro's set this up. Ideally, on non-debug systems, I'd like to see %p's filtered no matter what, I'd think opt in (whitelist) is ok. At least than, you did it intentionally. I verified that %p filtering gets us coredumps and kernel oops's have addresses.

On a system with panic_on_oops=0 and dmesg_restrict=0, if you want to leak where the kernel and data are to be able to exploit a memory corruption properly, you can probably often just trigger the bug, get the addresses from the resulting oops, set up the addresses in your exploit properly and retry the attack.

Quoted 10 lines

Ideally on user images debug interfaces are closed, but I would like to make rogue %p's without opt in go away.

I did a little research, and /proc/sys/kernel/dmesg_restrict seems to enable or disable this ability.

After setting that to 1, it appears I got the restriction I wanted:

$ dmesg
dmesg: klogctl failed: Operation not permitted

Yup, exactly.

Quoted 4 lines

One of my friends who does hypervisor development and is familiar with a few systems told me that Apple products just kill %p on production builds from logs.

I'm kind of torn between the various approaches discussed on this thread.

#9 lore raw
In reply to Jann Horn
Quoted 95 lines · Jann Horn
Quoted 89 lines · William C
Quoted 82 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Thursday, October 6, 2016 5:05 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 51 lines · William C
Quoted 51 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kees Cook
Sent: Wednesday, October 5, 2016 3:34 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; Jonathan Corbet <[email protected]>; [email protected]; LKML <[email protected]>; Nick Desaulniers <[email protected]>; Dave Weinstein <[email protected]>
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 5, 2016 at 11:04 AM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Solving this is certainly a good idea -- I'm all for finding a solid solution.

Quoted 2 lines

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

I'm worried that this could break kernel internals where %p is being used and not exposed to userspace. Maybe those situations don't exist...

Regardless, I would rather do what Grsecurity has done in this area, and whitelist known-safe values instead. For example, they have %pP for approved pointers, and %pX for approved dereference_function_descriptor() output. Everything else is censored if it is a value in kernel memory and destined for a user-space memory
buffer:

        if ((unsigned long)ptr > TASK_SIZE && *fmt != 'P' && *fmt !=
'X' && *fmt != 'K' && is_usercopy_object(buf)) {
                printk(KERN_ALERT "grsec: kernel infoleak detected!
Please report this log to [email protected].\n");
                dump_stack();
                ptr = NULL;
        }

The "is_usercopy_object()" test is something we can add, which is testing for a new SLAB flag that is used to mark slab caches as either used by user-space or not, which is done also through whitelisting. (For more details on this, see:
http://www.openwall.com/lists/kernel-hardening/2016/06/08/10)

Quoted 3 lines
Quoted 3 lines

Would you have time/interest to add the slab flags and is_usercopy_object()? The hardened usercopy part of the slab whitelisting can be separate, since it likely needs a different usercopy interface to sanely integrate with

upstream.

Quoted 3 lines

A couple of questions off hand:
1. What about bss statics? I am assuming that when the loader loads up a

module

Quoted 4 lines

That it's dynamically allocating the .bss section or some equivalent. I would Also assume the method you describe would catch that, is that correct?

2. What about stack variables?

It looks like what Grsecurity is doing is saying "if the address is outside of user- space" (" > TASK_SIZE") and it's not whitelisted ('P',
'X') and it's going to land in a user-space buffer ("is_usercopy_object()", censor it. ("K" is already censored -- they're just optimizing to avoid re-checking it needlessly.)

Quoted 6 lines

So, in this case, all kernel memory, bss and stack included, would be outside the user-space address range. (I am curious, however, how to apply this to an architecture like s390 which has overlapping address ranges... probably the TASK_SIZE test needs to use some other "is in kernel memory" check that compiles down to TASK_SIZE on non-s390, and DTRT on s390, etc.)

Quoted 4 lines

Before I go off and attempt this, I just have another dumb question to ask:

If the printk copies it into the kernel ring buffer, at some point, someone comes And asks for a copy into a userspace buffer either via dmesg or proc/kmsg interfaces.

Quoted 7 lines

IMO that's fine - I don't think pointers in the kernel ring buffer should be restricted. Instead, access to dmesg / proc/kmsg should be restricted appropriately.

I guess it depends on what the goal here is. Do we really want to stop root from ever seeing a kernel pointer (in which case OOPS messages wouldn't really work anymore)? My view is that restricting these interfaces so far that only root can access them and it's unlikely that root accidentally does so is sufficient.

I don't think it's worth worrying about dmesg right now. Maybe later on, but I don't think it's worth it right now. Assuming it'll work for things landing in /proc and /sys, focusing on the user-buffer-destined stuff seems the best use of time to me.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#10 lore raw

<snip>

Quoted 11 lines
Quoted 4 lines

I guess it depends on what the goal here is. Do we really want to stop root from ever seeing a kernel pointer (in which case OOPS messages wouldn't really work anymore)? My view is that restricting these interfaces so far that only root can access them and it's unlikely that root

accidentally does so is sufficient.

I don't think it's worth worrying about dmesg right now. Maybe later on, but I don't think it's worth it right now. Assuming it'll work for things landing in /proc and /sys, focusing on the user-buffer-destined stuff seems the best use of time to me.

SGTM, I'll start this soon once I get a few other things off of my plate.

#11 lore raw
In reply to [email protected]
Quoted 19 lines · On Wed, Oct 05 2016, [email protected] wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

Sample Output:
kptr_restrict == 2:
p: 00000000604369f4
pK: 0000000000000000

kptr_restrict == 3:
p: 0000000000000000
pK: 0000000000000000

Quoted 5 lines
---
 Documentation/sysctl/kernel.txt |   3 ++
 kernel/sysctl.c                 |   3 +-
 lib/vsprintf.c                  | 107 ++++++++++++++++++++++++----------------

That's a lot of changed lines. Why isn't this just

lib/vsprintf.c +2 -0
@@ -1719,6 +1719,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
        case 'G':
                return flags_string(buf, end, ptr, fmt);
        }
+       if (kptr_restrict == 3)
+               ptr = NULL;
        spec.flags |= SMALL;
        if (spec.field_width == -1) {
                spec.field_width = default_width;

?

#12 lore raw
Quoted 31 lines

-----Original Message-----
From: Rasmus Villemoes [mailto:[email protected]]
Sent: Wednesday, October 5, 2016 4:53 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 19 lines · On Wed, Oct 05 2016, [email protected] wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Introduce kptr_restrict level 3 that causes the kernel to treat %p as if it was %pK and thus always prints zeros.

Sample Output:
kptr_restrict == 2:
p: 00000000604369f4
pK: 0000000000000000

kptr_restrict == 3:
p: 0000000000000000
pK: 0000000000000000

Quoted 7 lines
Quoted 5 lines
---
 Documentation/sysctl/kernel.txt |   3 ++
 kernel/sysctl.c                 |   3 +-
 lib/vsprintf.c                  | 107 ++++++++++++++++++++++++----------------

That's a lot of changed lines. Why isn't this just

I moved the nested case into a static local function, I thought it was easier to read than the existing nested switches. The other reason was so we didn't have kptr_restrict littering that code and it was contained within the default and K values of the switch.

Quoted 15 lines
lib/vsprintf.c +2 -0
@@ -1719,6 +1719,8 @@ char *pointer(const char *fmt, char *buf, char *end,
 void *ptr,
        case 'G':
                return flags_string(buf, end, ptr, fmt);
        }
+       if (kptr_restrict == 3)
+               ptr = NULL;
        spec.flags |= SMALL;
        if (spec.field_width == -1) {
                spec.field_width = default_width;

?

#13 lore raw
In reply to [email protected]
Quoted 6 lines · On Wed, Oct 05, 2016 at 02:04:46PM -0400, [email protected] wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

So what? We a) don't care about out of tree modules and b) you could just triviall fix them up if you care.

No need to bloat the kernel with crap like this.

#14 lore raw
Quoted 11 lines

-----Original Message-----
From: Christoph Hellwig [mailto:[email protected]]
Sent: Thursday, October 6, 2016 9:32 AM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 2 lines · On Wed, Oct 05, 2016 at 02:04:46PM -0400, [email protected] wrote:

From: William Roberts <[email protected]>

Quoted 7 lines
Quoted 4 lines

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

So what? We a) don't care about out of tree modules and b) you could just triviall fix them up if you care.

Out of tree modules still affect core kernel security. I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK. So this method is just quite error prone. We currently have a blacklist approach versus whitelist.

Quoted 2 lines

No need to bloat the kernel with crap like this.

It's unconstructive comments like this that do the whole community harm. Notice how responses from Kees Cook were aimed at finding a different solution to the problem and were very constructive. As far as "bloating" goes, it really didn't change a whole lot, most of it was moved lines, and adds maybe a few lines of code.

#15 lore raw
Quoted 1 line · William C

Out of tree modules still affect core kernel security.

So don't use them.

Quoted 2 lines

I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK.

So fix them.

Quoted 2 lines

So this method is just quite error
prone. We currently have a blacklist approach versus whitelist.

Or fix the entire thing, get rid of %pK and always protect %p if you can show that it doesn't break anything.

But stop posting patches with bullshit arguments like out of tree modules.

#16 lore raw
Quoted 12 lines

-----Original Message-----
From: Christoph Hellwig [mailto:[email protected]]
Sent: Thursday, October 6, 2016 9:56 AM
To: Roberts, William C <[email protected]>
Cc: Christoph Hellwig <[email protected]>; kernel- [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

On Thu, Oct 06, 2016 at 01:47:47PM +0000, Roberts, William C wrote:

Quoted 8 lines
Quoted 1 line

Out of tree modules still affect core kernel security.

So don't use them.

Quoted 2 lines

I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK.

So fix them.

As Jann Horn points out, "And in total, there are 13578 matches for %p[^FfSsBRrhbMmIiEUVKNadCDgG] throughout the kernel. Reviewing all of those manually would suck."

Quoted 8 lines
Quoted 3 lines

So this method is just quite error
prone. We currently have a blacklist approach versus whitelist.

Or fix the entire thing, get rid of %pK and always protect %p if you can show that it doesn't break anything.

But stop posting patches with bullshit arguments like out of tree modules.

Ok perhaps the commit message sucks, and I should have included the large spread usages of %p throughout the kernel, I assumed those would just be known, I shouldn't have made that assumption.

We should care about out-of-tree modules wrt security as they affect the security of the whole system, especially when the modules are linking to core symbols like printing and string routines. There are tons of %p usages throughout the kernel as noted above.

This is pretty low hanging fruit and we should fix this, as Kees points out.

#17 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 5 lines · William C
Quoted 2 lines · Christoph Hellwig

So what? We a) don't care about out of tree modules and b) you could just triviall fix them up if you care.

Out of tree modules still affect core kernel security.

So don't use them.

I'm really interested in keeping these discussions productive. Declaring that the upstream kernel community (your implied "we") doesn't care about out-of-tree code is both false and short-sighted. Huge numbers of people depend on Linux on their phones, their IoT devices, cars, etc, and huge volumes of those (unfortunately) are running out-of-tree code.

This isn't about supporting some special interface that a single third party driver uses; this is about robust engineering and security practices that benefits everything, even out-of-tree code.

Quoted 4 lines
Quoted 2 lines

I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK.

So fix them.

%pK is an ugly situation that I'd like to fix correctly. It was designed originally as a blacklisting method, when it should have been designed as a whitelist (as William mentions). This is what will need fixing (and is what I was suggesting in my original reply to the patch).

All that said, "so fix them" sounds very much like "just fix the bugs" which is another troublesome attitude to have for dealing with potential security flaws. The kernel community already finds/fixes obvious flaws; the efforts with changes like what William is proposing are based around assuming (correctly, I can argue) that there are already bugs present, and they will remain in the kernel for years to come. Designing the kernel to safely deal with bugs being present is a fundamental design principle for kernel security self-defense technologies.

The challenge is not "fix them", the challenge is "design the kernel to do the right thing even when the bugs are unfixed".

Quoted 8 lines
Quoted 2 lines

So this method is just quite error
prone. We currently have a blacklist approach versus whitelist.

Or fix the entire thing, get rid of %pK and always protect %p if you can show that it doesn't break anything.

But stop posting patches with bullshit arguments like out of tree modules.

As a _singlular_ argument, "it's for out-of-tree code" is weak. As an _additional_ argument, it has value. Saying "this only helps out-of-tree code" doesn't carry much weight. Saying "this helps kernel security, even for out-of-tree code" is perfectly valid. And a wrinkle in this is that some day, either that out-of-tree code, or brand new code, will land in the kernel, and we don't want to continue to require authors be aware of an opt-in security feature. The kernel should protect itself (and all of itself, including out-of-tree or future code) by default.

And based on my read of this thread, we all appear to be in violent agreement. :) "always protect %p" is absolutely the goal, and we can figure out the best way to get there.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#18 lore raw
Quoted 3 lines · Kees Cook

And based on my read of this thread, we all appear to be in violent agreement. :) "always protect %p" is absolutely the goal, and we can figure out the best way to get there.

I proposed emitting pointers from the const and text sections by default and using NULL for data pointers.

https://lkml.org/lkml/2016/8/5/380

#19 lore raw
Quoted 10 lines · Joe Perches
Quoted 3 lines · Kees Cook

And based on my read of this thread, we all appear to be in violent agreement. :) "always protect %p" is absolutely the goal, and we can figure out the best way to get there.

I proposed emitting pointers from the const and text sections by default and using NULL for data pointers.

https://lkml.org/lkml/2016/8/5/380

Leaks of const and text (while not useful for write-attacks) can leak KASLR offset (though yes, yes, there are many existing leaks -- but we should avoid adding a new one regardless).

I think the logic of "is this destined for userspace" is likely the cleanest approach. There still may be many things this breaks, though. (I expect perf. Everything breaks perf. ;)

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#20 lore raw
In reply to Kees Cook

<snip>

Quoted 9 lines

As a _singlular_ argument, "it's for out-of-tree code" is weak. As an _additional_ argument, it has value. Saying "this only helps out-of-tree code" doesn't carry much weight. Saying "this helps kernel security, even for out-of-tree code" is perfectly valid. And a wrinkle in this is that some day, either that out-of-tree code, or brand new code, will land in the kernel, and we don't want to continue to require authors be aware of an opt-in security feature. The kernel should protect itself (and all of itself, including out-of-tree or future code) by default.

I should have made this more clear in my message, this was in my head and I assumed that people would just get it. But I shouldn't have made such an assumption.

Quoted 9 lines

And based on my read of this thread, we all appear to be in violent agreement. :) "always protect %p" is absolutely the goal, and we can figure out the best way to get there.

-Kees

--
Kees Cook
Nexus Security

#21 lore raw
In reply to Roberts, William C
Quoted 16 lines · William C
Quoted 16 lines

-----Original Message-----
From: Christoph Hellwig [mailto:[email protected]]
Sent: Thursday, October 6, 2016 9:32 AM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 05, 2016 at 02:04:46PM -0400, [email protected] wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Quoted 6 lines
Quoted 2 lines

So what? We a) don't care about out of tree modules and b) you could just triviall fix them up if you care.

Out of tree modules still affect core kernel security. I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK. So this method is just quite error prone. We currently have a blacklist approach versus whitelist.

grep says you have a point:

$ grep -IR 'seq_printf.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/dma/qcom/hidma_dbg.c:	seq_printf(s, "dev_trca=%p\n", &dmadev->dev_trca);
drivers/dma/qcom/hidma_dbg.c:	seq_printf(s, "dev_evca=%p\n", &dmadev->dev_evca);
$ grep -IR 'pr_info.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/misc/lkdtm_heap.c:	pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
$ grep -IR 'pr_err.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/net/ethernet/qlogic/qlge/qlge_dbg.c:	pr_err("rx_ring->cqicb = %p\n", &rx_ring->cqicb);

And these are just trivially greppable, low-hanging-fruit ones. With somewhat broader greps, there seem to be lots more, but they'd require manual review.

And in total, there are 13578 matches for %p[^FfSsBRrhbMmIiEUVKNadCDgG] throughout the kernel. Reviewing all of those manually would suck.

#22 lore raw
Quoted 33 lines · Jann Horn
Quoted 16 lines · William C
Quoted 16 lines

-----Original Message-----
From: Christoph Hellwig [mailto:[email protected]]
Sent: Thursday, October 6, 2016 9:32 AM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 05, 2016 at 02:04:46PM -0400, [email protected] wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Quoted 6 lines
Quoted 2 lines

So what? We a) don't care about out of tree modules and b) you could just triviall fix them up if you care.

Out of tree modules still affect core kernel security. I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK. So this method is just quite error prone. We currently have a blacklist approach versus whitelist.

grep says you have a point:

$ grep -IR 'seq_printf.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/dma/qcom/hidma_dbg.c:	seq_printf(s, "dev_trca=%p\n", &dmadev->dev_trca);
drivers/dma/qcom/hidma_dbg.c:	seq_printf(s, "dev_evca=%p\n", &dmadev->dev_evca);
$ grep -IR 'pr_info.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/misc/lkdtm_heap.c:	pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
Quoted 6 lines
$ grep -IR 'pr_err.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/net/ethernet/qlogic/qlge/qlge_dbg.c:	pr_err("rx_ring->cqicb = %p\n", &rx_ring->cqicb);

And these are just trivially greppable, low-hanging-fruit ones. With somewhat broader greps, there seem to be lots more, but they'd require manual review.

(Although, of course, most matches for seq_printf are in debugfs files or stuff that's only enabled with some DEBUG config option or so. But there are also e.g. pr_warn() users of %p that are not in debug code.)

#23 lore raw
In reply to Jann Horn
Quoted 30 lines · Jann Horn
Quoted 16 lines · William C
Quoted 16 lines

-----Original Message-----
From: Christoph Hellwig [mailto:[email protected]]
Sent: Thursday, October 6, 2016 9:32 AM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] printk: introduce kptr_restrict level 3

Quoted 6 lines · On Wed, Oct 05, 2016 at 02:04:46PM -0400, [email protected] wrote:

From: William Roberts <[email protected]>

Some out-of-tree modules do not use %pK and just use %p, as it's the common C paradigm for printing pointers. Because of this, kptr_restrict has no affect on the output and thus, no way to contain the kernel address leak.

Quoted 6 lines
Quoted 2 lines

So what? We a) don't care about out of tree modules and b) you could just triviall fix them up if you care.

Out of tree modules still affect core kernel security. I would also bet money, that somewhere In-tree someone has put a %p when they wanted a %pK. So this method is just quite error prone. We currently have a blacklist approach versus whitelist.

grep says you have a point:

$ grep -IR 'seq_printf.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/dma/qcom/hidma_dbg.c:	seq_printf(s, "dev_trca=%p\n", &dmadev->dev_trca);
drivers/dma/qcom/hidma_dbg.c:	seq_printf(s, "dev_evca=%p\n", &dmadev->dev_evca);
Quoted 5 lines
$ grep -IR 'pr_info.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/misc/lkdtm_heap.c:	pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
$ grep -IR 'pr_err.*%p[^FfSsBRrhbMmIiEUVKNadCDgG].*&'
drivers/net/ethernet/qlogic/qlge/qlge_dbg.c:	pr_err("rx_ring->cqicb = %p\n", &rx_ring->cqicb);

Actually, I think I missed something here.

We don't really want to censor pointers in dmesg, right? dmesg can contain pointers by design - and in particular, when an oops happens, the register dump will reveal pointers. dmesg should just be restricted using dmesg_restrict.

(What is annoying, though, is that e.g. Debian's default rsyslog config sends all KERN_EMERG messages to all active PTYs by default, meaning that on a system with such a config, making the kernel oops can be used to leak some ASLR information.)

(And why does __die() in arch/x86/kernel/dumpstack.c use KERN_EMERG for CONFIG_X86_32, but KERN_ALERT for !CONFIG_X86_32?)