Skip to contentSkip to conversation

vsprintf: protect kernel from panic due to non-canonical pointer dereference

PATCH In review Jane Chu 5 participants 8 messages Oct 19, 2022 – Oct 25, 2022

Subsystems (3)
lore
Share & tools
lore

Jane Chu

author
#0 lore raw

PATCH v3 1/1 vsprintf: protect kernel from panic due to non-canonical pointer dereference

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic. And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

---
 lib/vsprintf.c | 3 +++
 1 file changed, 3 insertions(+)
lib/vsprintf.c +3 -0
index c414a8d9f1ea..b38c12ef1e45 100644
@@ -698,6 +698,9 @@ static const char *check_pointer_msg(const void *ptr)
 	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
 		return "(efault)";
 
+	if (!kern_addr_valid((unsigned long)ptr))
+		return "(efault)";
+
 	return NULL;
 }
 
Signature · 1 line
2.18.4
#1 lore raw
Quoted 15 lines · Jane Chu

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic. And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

Quoted 3 lines

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

What if there is no other dereference except the one happened in printf()?

Just to point out here, that I formally NAKed this on the basis that NULL and error pointers are special, for the bogus pointers we need crash ASAP, no matter what the code issues it. I.o.w. printf() is not special for that kind of pointers (i.e. bogus pointers, but not special).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 20 lines · Andy Shevchenko
Quoted 15 lines · Jane Chu

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic. And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

Quoted 3 lines

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

Quoted 6 lines

What if there is no other dereference except the one happened in printf()?

Just to point out here, that I formally NAKed this on the basis that NULL and error pointers are special, for the bogus pointers we need crash ASAP, no matter what the code issues it. I.o.w. printf() is not special for that kind of pointers (i.e. bogus pointers, but not special).

Hey Andy,

Do we want to have user space programs crash the kernel?

This patch leads to making the kernel more harden so that we do not crash when there are bugs but continue on.

Would we not want that experience for users ?

Quoted 6 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 28 lines · Konrad Rzeszutek Wilk
Quoted 20 lines · Andy Shevchenko
Quoted 15 lines · Jane Chu

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic. And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

Quoted 3 lines

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

Quoted 6 lines

What if there is no other dereference except the one happened in printf()?

Just to point out here, that I formally NAKed this on the basis that NULL and error pointers are special, for the bogus pointers we need crash ASAP, no matter what the code issues it. I.o.w. printf() is not special for that kind of pointers (i.e. bogus pointers, but not special).

Quoted 6 lines

Hey Andy,

Do we want to have user space programs crash the kernel?

This patch leads to making the kernel more harden so that we do not crash when there are bugs but continue on.

Fine, how to push a user to report a bug in the kernel if for them there is no bug?

OK, let's assume user recognizes this as a bug, what should they do in order to provide a better description of the bug, so developer can easily debug and fix it?

Quoted 1 line

Would we not want that experience for users ?

Yes, if it is a bug in the kernel we want to know it with all possible details. Hiding bugs is a way to nowhere.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw
Quoted 36 lines · Andy Shevchenko
Quoted 28 lines · Konrad Rzeszutek Wilk
Quoted 20 lines · Andy Shevchenko
Quoted 15 lines · Jane Chu

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic. And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

Quoted 3 lines

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

Quoted 6 lines

What if there is no other dereference except the one happened in printf()?

Just to point out here, that I formally NAKed this on the basis that NULL and error pointers are special, for the bogus pointers we need crash ASAP, no matter what the code issues it. I.o.w. printf() is not special for that kind of pointers (i.e. bogus pointers, but not special).

Quoted 6 lines

Hey Andy,

Do we want to have user space programs crash the kernel?

This patch leads to making the kernel more harden so that we do not crash when there are bugs but continue on.

Quoted 6 lines

Fine, how to push a user to report a bug in the kernel if for them there is no bug?

OK, let's assume user recognizes this as a bug, what should they do in order to provide a better description of the bug, so developer can easily debug and fix it?

WARN() would provide similar information as panic() without actually crashing the kernel.

Quoted 4 lines
Quoted 1 line

Would we not want that experience for users ?

Yes, if it is a bug in the kernel we want to know it with all possible details. Hiding bugs is a way to nowhere.

I agree but we should always distinguish between fatal problems where the system could hardly continue working and unexpected behavior that is not critical.

Many error code paths handle unexpected situations. Some problems are caused by users and some by bugs in the code. The kernel could always refuse doing some operation rather than crash. People will report it because it does not work. And there are non-destructive ways how to show useful debugging information.

Best Regards,
Petr

#5 lore raw
Quoted 2 lines · Petr Mladek
Quoted 1 line · Andy Shevchenko

On Thu, Oct 20, 2022 at 10:52:03AM -0400, Konrad Rzeszutek Wilk wrote:

...

Quoted 6 lines
Quoted 3 lines

OK, let's assume user recognizes this as a bug, what should they do in order to provide a better description of the bug, so developer can easily debug and fix it?

WARN() would provide similar information as panic() without actually crashing the kernel.

Unless one provides panic_on_warn (or how is it called?).

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Would we not want that experience for users ?

Yes, if it is a bug in the kernel we want to know it with all possible details. Hiding bugs is a way to nowhere.

I agree but we should always distinguish between fatal problems where the system could hardly continue working and unexpected behavior that is not critical.

Quoted 5 lines

Many error code paths handle unexpected situations. Some problems are caused by users and some by bugs in the code. The kernel could always refuse doing some operation rather than crash. People will report it because it does not work. And there are non-destructive ways how to show useful debugging information.

Initially, if I understand correctly, the idea of that check was exactly to guard against special pointers (NULL and error). Now this is getting wider and I'm not sure hiding a crash is good thing to go.

Hypothetical situation: the "invalid" pointer is just one that gets LSB shuffled a bit (some of the frameworks use lower bits to keep some information there). That said, kernel is not going to crash elsewhere. How user will know that unmasked pointer went to the printf()?

I honestly think that this or similar change will bring more harm than help.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
In reply to Jane Chu
Quoted 2 lines · Jane Chu

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic.

Just to be completely clear, the out-of-bounds dereference did not happen in vsprintf if I understand your description right. Essentially you have an array of char* pointers, and you accessed beyond that array, where of course some random memory contents then turned out not to be a real pointer, and that bogus pointer value was passed into vsprintf() as a %s argument.

Quoted 2 lines

And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

That's ved_cade , or more properly edac_dev ...

Quoted 11 lines

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

Quoted 3 lines

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

I'm with Andy on this one, we don't add random checks like this in the kernel, not in vsprintf or elsewhere.

check_pointer_msg is/was actually more about checking the various %p<foo> extensions, where it is (more) expected that somebody does

  struct foo *f = get_a_foo();
  pr_debug("got %pfoo\n", f);
  if (IS_ERR(f)) { ... }

[possibly in a not so obvious path], and the PAGE_SIZE check is similarly for cases where the "base" pointer is actually NULL but what is passed is &f->member.

Rasmus

maintainer
#7 lore raw
In reply to Jane Chu
Quoted 15 lines · Jane Chu

Having stepped on a local kernel bug where reading sysfs has led to out-of-bound pointer dereference by vsprintf() which led to GPF panic. And the reason for GPF is that the OOB pointer was turned to a non-canonical address such as 0x7665645f63616465.

vsprintf() already has this line of defense

	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
                return "(efault)";

Since a non-canonical pointer can be detected by kern_addr_valid() on architectures that present VM holes as well as meaningful implementation of kern_addr_valid() that detects the non-canonical addresses, this patch adds a check on non-canonical string pointer by kern_addr_valid() and "(efault)" to alert user that something is wrong instead of unecessarily panic the server.

Quoted 3 lines

On the other hand, if the non-canonical string pointer is dereferenced else where in the kernel, by virtue of being non-canonical, a crash is expected to be immediate.

Just for record, this patch is going to be abandoned.

Some reasons are mentioned in this thread. Others are in the threads for previous versions, see
https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]

Best Regards,
Petr