Skip to contentSkip to conversation

lib/vsprintf: make-printk-non-secret printks all addresses as unhashed

PATCH RESEND In review Timur Tabi 9 participants 19 messages Feb 2, 2021 – Feb 10, 2021

Subsystems (3)
lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH RESEND 1/1 lib/vsprintf: make-printk-non-secret printks all addresses as unhashed

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

---
 lib/vsprintf.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
lib/vsprintf.c +32 -2
index 3b53c73580c5..b9f87084afb0 100644
@@ -2090,6 +2090,30 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
 	return widen_string(buf, buf - buf_start, end, spec);
 }
 
+/* Disable pointer hashing if requested */
+static bool debug_never_hash_pointers __ro_after_init;
+
+static int __init debug_never_hash_pointers_enable(char *str)
+{
+	debug_never_hash_pointers = true;
+	pr_warn("**********************************************************\n");
+	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** All pointers that are printed to the console will    **\n");
+	pr_warn("** be printed as unhashed.                              **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** Kernel memory addresses are exposed, which may       **\n");
+	pr_warn("** compromise security on your system.                  **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** If you see this message and you are not debugging    **\n");
+	pr_warn("** the kernel, report this immediately to your vendor!  **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
+	pr_warn("**********************************************************\n");
+	return 0;
+}
+early_param("make-printk-non-secret", debug_never_hash_pointers_enable);
+
 /*
  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
  * by an extra set of alphanumeric characters that are extended format
@@ -2297,8 +2321,14 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 		}
 	}
 
-	/* default is to _not_ leak addresses, hash before printing */
-	return ptr_to_id(buf, end, ptr, spec);
+	/*
+	 * default is to _not_ leak addresses, so hash before printing, unless
+	 * make-printk-non-secret is specified on the command line.
+	 */
+	if (unlikely(debug_never_hash_pointers))
+		return pointer_string(buf, end, ptr, spec);
+	else
+		return ptr_to_id(buf, end, ptr, spec);
 }
 
 /*
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Timur Tabi

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

Looks good to me.

	-ss
Acked-by
#2 lore raw
Quoted 9 lines · Sergey Senozhatsky
Quoted 8 lines · Timur Tabi

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

Quoted 7 lines
Quoted 1 line

Looks good to me.

	-ss

thanks.

Signature · 1 line
~Randy
maintainer
#3 lore raw
In reply to Timur Tabi
Quoted 4 lines · Timur Tabi

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

Quoted 8 lines

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

---
 lib/vsprintf.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

Please, add also entry into
Documentation/admin-guide/kernel-parameters.txt.

If we agree that the parameter is acceptable then let's make it properly.

Quoted 15 lines
lib/vsprintf.c +7 -0
index 3b53c73580c5..b9f87084afb0 100644
@@ -2090,6 +2090,30 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
 	return widen_string(buf, buf - buf_start, end, spec);
 }
 
+/* Disable pointer hashing if requested */
+static bool debug_never_hash_pointers __ro_after_init;
+
+static int __init debug_never_hash_pointers_enable(char *str)
+{
+	debug_never_hash_pointers = true;
+	pr_warn("**********************************************************\n");

lib/vsprintf.c char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, · line 2109

Quoted 10 lines
lib/vsprintf.c +10 -0
+	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** All pointers that are printed to the console will    **\n");
+	pr_warn("** be printed as unhashed.                              **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** Kernel memory addresses are exposed, which may       **\n");
+	pr_warn("** compromise security on your system.                  **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** If you see this message and you are not debugging    **\n");
+	pr_warn("** the kernel, report this immediately to your vendor!  **\n");

It is a boot parameter. So it should be "system administrtor" instead of vendor.

Otherwise, it looks good to me.

Best Regards,
Petr

maintainer
#4 lore raw
Quoted 9 lines · Petr Mladek
Quoted 8 lines · Timur Tabi

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

Quoted 7 lines
Quoted 4 lines
---
 lib/vsprintf.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

Please, add also entry into
Documentation/admin-guide/kernel-parameters.txt.

Adding Andy and Rasmus into CC. They are official vsprintf co-maintainers and reviewers (MAINTAINERS file).

Also please make sure that lib/test_printf.c will work with the new option.

Best Regards,
Petr

author
#5 lore raw
Quoted 2 lines · Petr Mladek

Also please make sure that lib/test_printf.c will work with the new option.

As you suspected, it doesn't work:

[  206.966478] test_printf: loaded.
[  206.966528] test_printf: plain 'p' does not appear to be hashed
[  206.966740] test_printf: failed 1 out of 388 tests

What should I do about this?

On one hand, it is working as expected: %p is not hashed, and that should be a warning.

On the other hand, maybe test_printf should be aware of the command line parameter and test to make sure that %p is NOT hashed?

#6 lore raw
Quoted 10 lines · Timur Tabi
Quoted 2 lines · Petr Mladek

Also please make sure that lib/test_printf.c will work with the new option.

As you suspected, it doesn't work:

[  206.966478] test_printf: loaded.
[  206.966528] test_printf: plain 'p' does not appear to be hashed
[  206.966740] test_printf: failed 1 out of 388 tests
Quoted 7 lines

What should I do about this?

On one hand, it is working as expected: %p is not hashed, and that should be a warning.

On the other hand, maybe test_printf should be aware of the command line parameter and test to make sure that %p is NOT hashed?

test_printf.c should be altered accordingly to avoid any failed test cases. I.o.w. you need to have some kind of conditional there:

if (kernel_cmdline_parameter_foo)
expect bar
else
expect baz

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
In reply to Timur Tabi
Quoted 10 lines · Timur Tabi
Quoted 2 lines · Petr Mladek

Also please make sure that lib/test_printf.c will work with the new option.

As you suspected, it doesn't work:

[  206.966478] test_printf: loaded.
[  206.966528] test_printf: plain 'p' does not appear to be hashed
[  206.966740] test_printf: failed 1 out of 388 tests
Quoted 7 lines

What should I do about this?

On one hand, it is working as expected: %p is not hashed, and that should be a warning.

On the other hand, maybe test_printf should be aware of the command line parameter and test to make sure that %p is NOT hashed?

It seems like it'd be best for the test to fail, yes? It _is_ a problem that %p is unhashed; it's just that the failure was intended.

Signature · 1 line
Kees Cook
maintainer
#8 lore raw
Quoted 13 lines · On Wed, 3 Feb 2021 12:02:05 -0800 Kees Cook <[email protected]> wrote:
Quoted 12 lines · Timur Tabi
Quoted 2 lines · Petr Mladek

Also please make sure that lib/test_printf.c will work with the new option.

As you suspected, it doesn't work:

[  206.966478] test_printf: loaded.
[  206.966528] test_printf: plain 'p' does not appear to be hashed
[  206.966740] test_printf: failed 1 out of 388 tests

What should I do about this?

Quoted 9 lines
Quoted 5 lines

On one hand, it is working as expected: %p is not hashed, and that should be a warning.

On the other hand, maybe test_printf should be aware of the command line parameter and test to make sure that %p is NOT hashed?

It seems like it'd be best for the test to fail, yes? It _is_ a problem that %p is unhashed; it's just that the failure was intended.

I disagree.

With a big notice that all pointers of unhashed, I don't think we need to print it failed when we expect it to fail.

If anything, skip the test and state:

test_printf: hash test skipped because "make-printk-non-secret" is on the command line.

-- Steve

#9 lore raw
Quoted 28 lines · Steven Rostedt
Quoted 13 lines · On Wed, 3 Feb 2021 12:02:05 -0800 Kees Cook <[email protected]> wrote:
Quoted 12 lines · Timur Tabi
Quoted 2 lines · Petr Mladek

Also please make sure that lib/test_printf.c will work with the new option.

As you suspected, it doesn't work:

[  206.966478] test_printf: loaded.
[  206.966528] test_printf: plain 'p' does not appear to be hashed
[  206.966740] test_printf: failed 1 out of 388 tests

What should I do about this?

Quoted 9 lines
Quoted 5 lines

On one hand, it is working as expected: %p is not hashed, and that should be a warning.

On the other hand, maybe test_printf should be aware of the command line parameter and test to make sure that %p is NOT hashed?

It seems like it'd be best for the test to fail, yes? It _is_ a problem that %p is unhashed; it's just that the failure was intended.

I disagree.

Quoted 7 lines

With a big notice that all pointers of unhashed, I don't think we need to print it failed when we expect it to fail.

If anything, skip the test and state:

test_printf: hash test skipped because "make-printk-non-secret" is on the command line.

Yeah, I'm fine with "fail" or "skip". "pass" is mainly what I don't like. :)

Signature · 1 line
Kees Cook
maintainer
#10 lore raw
Quoted 10 lines · On Wed, 3 Feb 2021 12:35:07 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines

With a big notice that all pointers of unhashed, I don't think we need to print it failed when we expect it to fail.

If anything, skip the test and state:

test_printf: hash test skipped because "make-printk-non-secret" is on the command line.

Yeah, I'm fine with "fail" or "skip". "pass" is mainly what I don't like. :)

Is there any printing of the tests being done? Looks to me that the tests only print something if they fail. Thus "skip" and "pass" are basically the same (if "skip" is simply not to do the test).

I mean, we could simply have:

static void __init
plain(void)
{
int err;

+ if (debug_never_hash_pointers)
+ return;

-- Steve

author
#11 lore raw

Review

Quoted 7 lines · Steven Rostedt
  static void __init
  plain(void)
  {
  	int err;
(quoted diff) +2 -0
+	if (debug_never_hash_pointers)
+		return;

So, I have a stupid question. What's the best way for test_printf.c to read the command line parameter? Should I just do this in vsprintf.c:

/* Disable pointer hashing if requested */ static bool debug_never_hash_pointers __ro_after_init; EXPORT_SYMBOL_GPL(debug_never_hash_pointers);

I'm not crazy about exporting this variable to other drivers. It could be used to disable hashing by any driver.

AFAIK, the only command-line parameter code that works in drivers is module_parm, and that expects the module prefix on the command-line.

maintainer
#12 lore raw
Quoted 6 lines · On Wed, 3 Feb 2021 15:56:20 -0600 Timur Tabi <[email protected]> wrote:
Quoted 5 lines · Steven Rostedt
  static void __init
  plain(void)
  {
  	int err;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (debug_never_hash_pointers)
+		return;  

So, I have a stupid question. What's the best way for test_printf.c to read the command line parameter? Should I just do this in vsprintf.c:

/* Disable pointer hashing if requested */ static bool debug_never_hash_pointers __ro_after_init;

It wont be static.

Quoted 4 lines

EXPORT_SYMBOL_GPL(debug_never_hash_pointers);

I'm not crazy about exporting this variable to other drivers. It could be used to disable hashing by any driver.

But it is set as "__ro_after_init". That is, every module will see it as read only. IOW, they wont be able to modify it.

Quoted 3 lines

AFAIK, the only command-line parameter code that works in drivers is module_parm, and that expects the module prefix on the command-line.

This is just a constant variable for others to see. The command line itself is visible (see saved_command_line, it's even exported to modules in sparc).

-- Steve

maintainer
#13 lore raw
In reply to Steven Rostedt
Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Wed, 3 Feb 2021 12:35:07 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines

With a big notice that all pointers of unhashed, I don't think we need to print it failed when we expect it to fail.

If anything, skip the test and state:

test_printf: hash test skipped because "make-printk-non-secret" is on the command line.

Quoted 6 lines
Quoted 2 lines

Yeah, I'm fine with "fail" or "skip". "pass" is mainly what I don't like. :)

Is there any printing of the tests being done? Looks to me that the tests only print something if they fail. Thus "skip" and "pass" are basically the same (if "skip" is simply not to do the test).

It prints the total number of tests done. It should not count the skipped tests.

We actually print a warning when crng is not initialized. In this case, the test passes because we actually check the value and it is an expected one.

Review

Quoted 10 lines

I mean, we could simply have:

static void __init
plain(void)
{

(quoted diff) +2 -0
 	int err;
 
+	if (debug_never_hash_pointers)
+		return;

I am not 100% sure. But this might work. Just please print a warning about the tests are skipped.

Best Regards,
Petr

#14 lore raw
In reply to Timur Tabi
Quoted 9 lines · Timur Tabi

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

Thanks a lot. Should this also affect %pK though? IIUC, there's currently no way to achieve non-mangled %pK in all cases, even with the most permissive kptr_restrict=1 setting:
- in IRQ, there's "pK-error" instead
- in a context of non-CAP_SYSLOG process, nulls are printed

Yes, neither should matter if %pK were only used for prints that generate content of some kind of /proc file read by a CAP_SYSLOG process, but that doesn't seem to be the case and there are %pK used for printing to dmesg too...

Quoted 57 lines
---
 lib/vsprintf.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
lib/vsprintf.c +32 -2
index 3b53c73580c5..b9f87084afb0 100644
@@ -2090,6 +2090,30 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
 	return widen_string(buf, buf - buf_start, end, spec);
 }
 
+/* Disable pointer hashing if requested */
+static bool debug_never_hash_pointers __ro_after_init;
+
+static int __init debug_never_hash_pointers_enable(char *str)
+{
+	debug_never_hash_pointers = true;
+	pr_warn("**********************************************************\n");
+	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** All pointers that are printed to the console will    **\n");
+	pr_warn("** be printed as unhashed.                              **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** Kernel memory addresses are exposed, which may       **\n");
+	pr_warn("** compromise security on your system.                  **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("** If you see this message and you are not debugging    **\n");
+	pr_warn("** the kernel, report this immediately to your vendor!  **\n");
+	pr_warn("**                                                      **\n");
+	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
+	pr_warn("**********************************************************\n");
+	return 0;
+}
+early_param("make-printk-non-secret", debug_never_hash_pointers_enable);
+
 /*
  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
  * by an extra set of alphanumeric characters that are extended format
@@ -2297,8 +2321,14 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 		}
 	}
 
-	/* default is to _not_ leak addresses, hash before printing */
-	return ptr_to_id(buf, end, ptr, spec);
+	/*
+	 * default is to _not_ leak addresses, so hash before printing, unless
+	 * make-printk-non-secret is specified on the command line.
+	 */
+	if (unlikely(debug_never_hash_pointers))
+		return pointer_string(buf, end, ptr, spec);
+	else
+		return ptr_to_id(buf, end, ptr, spec);
 }
 
 /*
author
#15 lore raw
Quoted 5 lines · Vlastimil Babka

Thanks a lot. Should this also affect %pK though? IIUC, there's currently no way to achieve non-mangled %pK in all cases, even with the most permissive kptr_restrict=1 setting:
- in IRQ, there's "pK-error" instead
- in a context of non-CAP_SYSLOG process, nulls are printed

Hmmm.. I thought %pK prints an unhashed pointer when the user is root, at least in situations where the user can be known (e.g. during an ioctl call).

Quoted 3 lines

Yes, neither should matter if %pK were only used for prints that generate content of some kind of /proc file read by a CAP_SYSLOG process, but that doesn't seem to be the case and there are %pK used for printing to dmesg too...

I thought about that. On one hand, people who use %pK probably really wanted a hashed pointer printed. On the other hand, I agree that %pK should not be used for dmesg prints.

I get the feeling that some (most?) people who use %pK don't really understand how it's supposed to be used.

I can extend make-printk-non-secret to %pK if everyone agrees.

#16 lore raw
Quoted 1 line · Timur Tabi

I can extend make-printk-non-secret to %pK if everyone agrees.

Let's just leave those alone. There is already a toggle for that in /proc.

Signature · 1 line
Kees Cook
#17 lore raw
In reply to Timur Tabi
Quoted 18 lines · Timur Tabi

If the make-printk-non-secret command-line parameter is set, then printk("%p") will print addresses as unhashed. This is useful for debugging purposes.

A large warning message is displayed if this option is enabled, because unhashed addresses, while useful for debugging, exposes kernel addresses which can be a security risk.

---
 lib/vsprintf.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
lib/vsprintf.c +0 -0
index 3b53c73580c5..b9f87084afb0 100644

lib/vsprintf.c char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, · line 2094

Quoted 5 lines
lib/vsprintf.c +2 -0
@@ -2090,6 +2090,30 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
 	return widen_string(buf, buf - buf_start, end, spec);
 }
 
+/* Disable pointer hashing if requested */
+static bool debug_never_hash_pointers __ro_after_init;

Would it be reasonable to make this non-static? Or somehow make it possible to get this flag from other subsystems?

There are other places in the kernel that dump sensitive data such as registers. We'd like to be able to use 'debug_never_hash_pointers' to decide if our debugging tools can dump registers etc. What we really need is info if the kernel is in debug mode and we can dump all kinds of sensitive info; debug_never_hash_pointers is would be a good enough proxy for that.

Thanks,
-- Marco

author
#18 lore raw
Quoted 9 lines · Marco Elver

Would it be reasonable to make this non-static? Or somehow make it possible to get this flag from other subsystems?

There are other places in the kernel that dump sensitive data such as registers. We'd like to be able to use 'debug_never_hash_pointers' to decide if our debugging tools can dump registers etc. What we really need is info if the kernel is in debug mode and we can dump all kinds of sensitive info; debug_never_hash_pointers is would be a good enough proxy for that.

The next version of my patch (coming soon) will export the symbol. It's intended for test_printf, but if you think it can be used by others, so much the better.