Skip to contentSkip to conversation

vsprintf: distinguish between (null), (err) and (invalid) pointer derefs

PATCH Alexey Dobriyan 2 participants 2 messages Mar 6, 2018

lore
Share & tools
lore
#0 lore raw
Quoted 1 line

+#define BAD_PTR_STRING(x) (!(x) ? "(null)" : IS_ERR(x) ? "(err)" : "(invalid)")

This is getting ridiculous.

Instead of simply printing a pointer as %08lx or %016llx, not only glibc (null) stupidity is propagated but expanded and "improved".

I assure you reading 0000000000000000 is just as obvious as (null) and reading fffffffffffffffa is just as good as -ENOMEM.

In fact printing with hex is more information. Maybe it is important that buggy pointer is small value but it's value is lost.

Sure don't dereference a pointer for very small or very erry values but print it without all the bell and whistles.

#1 lore raw
Quoted 6 lines · Alexey Dobriyan
Quoted 1 line

+#define BAD_PTR_STRING(x) (!(x) ? "(null)" : IS_ERR(x) ? "(err)" : "(invalid)")

This is getting ridiculous.

Instead of simply printing a pointer as %08lx or %016llx, not only glibc (null) stupidity is propagated but expanded and "improved".

This is not about printing a pointer, this is about attempting to print an object referenced by such a bad pointer. Which leads to a crash: in userspace, you get a segfault; in the kernel, at least in the case I tested, the system is dead without even a squeal on either console or serial.

Quoted 8 lines

I assure you reading 0000000000000000 is just as obvious as (null) and reading fffffffffffffffa is just as good as -ENOMEM.

In fact printing with hex is more information. Maybe it is important that buggy pointer is small value but it's value is lost.

Sure don't dereference a pointer for very small or very erry values but print it without all the bell and whistles.

That's a reasonable suggestion, but it still needs to be special cased. Note the difference between printk("%px", 42) and printk("%s", 42).

See this part:
- if (!ptr && *fmt != 'K' && *fmt != 'x') {
+ if (IS_BAD_PTR(ptr) && *fmt != 'K' && *fmt != 'x') {
Printing the pointer is already excluded; what I'm fixing is:
1. lying that the bad pointer was (null) when it was -ENOMEM (commit 1)
2. crash when some bad code tries to printk("%s", -ENOMEM) (commit 2)

So, if what you propose is applying commit 2, and changing 1 to print the raw value instead of (null)/(err)/(invalid), that sounds good.

Meow!

Signature · 4 lines
⢀⣴⠾⠻⢶⣦⠀ 
⣾⠁⢠⠒⠀⣿⡁ A dumb species has no way to open a tuna can.
⢿⡄⠘⠷⠚⠋⠀ A smart species invents a can opener.
⠈⠳⣄⠀⠀⠀⠀ A master species delegates.