Re: [PATCH v2] vsprintf: protect kernel from panic due to non-canonical pointer dereference

From: Jane Chu
Date: Wed Oct 19 2022 - 21:00:32 EST


On 10/19/2022 1:02 PM, Jane Chu wrote:
> Hi, Petr,
>
> Sorry, I didn't catch this email prior to sending out v3.
>
> [..]
>>>
>>> Yes, kern_addr_valid() is used by read_kcore() which is architecturally
>>> independent and applies everywhere, so does that imply that it is
>>> defined in all architectures?
>>
>> It is more complicated. fs/proc/kcore.c is built when
>> CONFIG_PROC_KCORE is set. It is defined in fs/proc/Kconfig as:
>>
>> config PROC_KCORE
>> bool "/proc/kcore support" if !ARM
>> depends on PROC_FS && MMU
>>
>> So, it is not built on ARM.
>
> Indeed, it's defined on ARM though.
>
>>
>> More importantly, kern_addr_valid() seems to be implemented only for x86_64.
>> It is always true (1) on all other architectures, see
>>
>> $> git grep kern_addr_valid
>> arch/alpha/include/asm/pgtable.h:#define kern_addr_valid(addr) (1)
>> arch/arc/include/asm/pgtable-bits-arcv2.h:#define kern_addr_valid(addr) (1)
>> arch/arm/include/asm/pgtable-nommu.h:#define kern_addr_valid(addr) (1)
>> arch/arm/include/asm/pgtable.h:#define kern_addr_valid(addr) (1)
>> [...]
>>
>> Wait, it is actually always false (0) on x86 when SPARSEMEM is used,
>> see arch/x86/include/asm/pgtable_32.h:
>>
>> #ifdef CONFIG_FLATMEM
>> #define kern_addr_valid(addr) (1)
>> #else
>> #define kern_addr_valid(kaddr) (0)
>> #endif
>>
>
> Thanks for pointing this out. Let me do some digging ...

So I tried to dig, the history of kern_addr_valid() and its connection
with PROC_KCORE went way back, I'm unable to find out why on old memory
models such as x86 SPARSEMEM & DISCONTIGMEM, kern_addr_valid() is
defined as '(0)'. My guess is perhaps PROC_KCORE isn't supported on
those memory model, and having kern_addr_valid() to reject the start
address is a convenient way to fail the load - just a guess, with no
evidence for support. Anyway a generic use of kern_addr_valid() will
break platforms with SPARSEMEM & DISCONTIGMEM memory model. And this is
beside the fact that kern_addr_valid() is going away, and I don't see a
good replacement.

I understand folks' rejecting the patch on the ground of dereferencing
bogus pointers anywhere in the kernel including vsprintf() is not worth
protecting. I'm not going to insist on any further, I'd just like to
thank all of you who've spent time reviewing the patch, and providing
comments and explanations.

Regards,
-jane