linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>,
	hpa@zytor.com, hch@infradead.org,
	nick.desaulniers+lkml@gmail.com, kuan-ying.lee@canonical.com,
	masahiroy@kernel.org, samuel.holland@sifive.com,
	mingo@redhat.com, corbet@lwn.net, ryabinin.a.a@gmail.com,
	guoweikang.kernel@gmail.com, jpoimboe@kernel.org,
	ardb@kernel.org, vincenzo.frascino@arm.com, glider@google.com,
	kirill.shutemov@linux.intel.com, apopple@nvidia.com,
	samitolvanen@google.com, kaleshsingh@google.com, jgross@suse.com,
	andreyknvl@gmail.com, scott@os.amperecomputing.com,
	tony.luck@intel.com, dvyukov@google.com,
	pasha.tatashin@soleen.com, ziy@nvidia.com, broonie@kernel.org,
	gatlin.newhouse@gmail.com, jackmanb@google.com,
	wangkefeng.wang@huawei.com, thiago.bauermann@linaro.org,
	tglx@linutronix.de, kees@kernel.org, akpm@linux-foundation.org,
	jason.andryuk@amd.com, snovitoll@gmail.com, xin@zytor.com,
	jan.kiszka@siemens.com, bp@alien8.de, rppt@kernel.org,
	peterz@infradead.org, pankaj.gupta@amd.com, thuth@redhat.com,
	andriy.shevchenko@linux.intel.com, joel.granados@kernel.org,
	kbingham@kernel.org, nicolas@fjasle.eu, mark.rutland@arm.com,
	surenb@google.com, catalin.marinas@arm.com, morbo@google.com,
	justinstitt@google.com, ubizjak@gmail.com, jhubbard@nvidia.com,
	urezki@gmail.com, dave.hansen@linux.intel.com, bhe@redhat.com,
	luto@kernel.org, baohua@kernel.org, nathan@kernel.org,
	will@kernel.org, brgerst@gmail.com
Cc: llvm@lists.linux.dev, linux-mm@kvack.org,
	linux-doc@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
	kasan-dev@googlegroups.com, x86@kernel.org
Subject: Re: [PATCH v3 10/14] x86: Update the KASAN non-canonical hook
Date: Fri, 4 Apr 2025 10:37:53 -0700	[thread overview]
Message-ID: <8416848c-700a-4ff0-8a22-aa62579d60cd@intel.com> (raw)
In-Reply-To: <c37c89e71ed5a8e404b24b31e23457af12f872f2.1743772053.git.maciej.wieczor-retman@intel.com>

On 4/4/25 06:14, Maciej Wieczor-Retman wrote:
> The kasan_non_canonical_hook() is useful in pointing out that an address
> which caused some kind of error could be the result of
> kasan_mem_to_shadow() mapping. Currently it's called only in the general
> protection handler code path but can give helpful information also in
> page fault oops reports.
> 
> For example consider a page fault for address 0xffdefc0000000000 on a
> 5-level paging system. It could have been accessed from KASAN's
> kasan_mem_to_shadow() called on 0xfef0000000000000 address. Without the
> kasan_non_canonical_hook() in the page fault case it might be hard to
> figure out why an error occurred.
> 
> Add kasan_non_canonical_hook() to the beginning of show_fault_oops().
> 
> Update kasan_non_canonical_hook() to take into account the possible
> memory to shadow mappings in the software tag-based mode of x86.
> 
> Patch was tested with positive results by accessing the following
> addresses, causing #GPs and #PFs.
> 
> Valid mappings (showing kasan_non_canonical_hook() message):
> 	0xFFFFFFFF8FFFFFFF
> 	0xFEF0000000000000
> 	0x7FFFFF4FFFFFFFFF
> 	0x7EF0000000000000
> Invalid mappings (not showing kasan_non_canonical_hook() message):
> 	0xFFFFFFFFF8FFFFFF
> 	0xFFBFFC0000000000
> 	0x07EFFC0000000000
> 	0x000E000000000000
> 
> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
> ---
> Changelog v3:
> - Move the report.c part from first patch in the series to this new
>   patch to have x86 changes in one place.
> - Add the call in fault oops.
> - Extend the comment in report.c with a graphical representation of what
>   addresses are valid and invalid in memory to shadow mapping.
> 
>  arch/x86/mm/fault.c |  2 ++
>  mm/kasan/report.c   | 36 +++++++++++++++++++++++++++++++++++-
>  2 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
> index 697432f63c59..16366af60ae5 100644
> --- a/arch/x86/mm/fault.c
> +++ b/arch/x86/mm/fault.c
> @@ -511,6 +511,8 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad
>  	if (!oops_may_print())
>  		return;
>  
> +	kasan_non_canonical_hook(address);
> +
>  	if (error_code & X86_PF_INSTR) {
>  		unsigned int level;
>  		bool nx, rw;
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index f24f11cc644a..135307c93c2c 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -700,7 +700,7 @@ void kasan_non_canonical_hook(unsigned long addr)
>  	 * operation would overflow only for some memory addresses. However, due
>  	 * to the chosen KASAN_SHADOW_OFFSET values and the fact the
>  	 * kasan_mem_to_shadow() only operates on pointers with the tag reset,
> -	 * the overflow always happens.
> +	 * the overflow always happens (for both x86 and arm64).
>  	 *
>  	 * For arm64, the top byte of the pointer gets reset to 0xFF. Thus, the
>  	 * possible shadow addresses belong to a region that is the result of
> @@ -715,6 +715,40 @@ void kasan_non_canonical_hook(unsigned long addr)
>  			return;
>  	}
>  
> +	 /*
> +	  * For x86-64, only the pointer bits [62:57] get reset, and bits #63
> +	  * and #56 can be 0 or 1. Thus, kasan_mem_to_shadow() can be possibly
> +	  * applied to two regions of memory:
> +	  * [0x7E00000000000000, 0x7FFFFFFFFFFFFFFF] and
> +	  * [0xFE00000000000000, 0xFFFFFFFFFFFFFFFF]. As the overflow happens
> +	  * for both ends of both memory ranges, both possible shadow regions
> +	  * are contiguous.
> +	  *
> +	  * Given the KASAN_SHADOW_OFFSET equal to 0xffeffc0000000000, the
> +	  * following ranges are valid mem-to-shadow mappings:
> +	  *
> +	  * 0xFFFFFFFFFFFFFFFF
> +	  *         INVALID
> +	  * 0xFFEFFBFFFFFFFFFF - kasan_mem_to_shadow(~0UL)
> +	  *         VALID   - kasan shadow mem
> +	  *         VALID   - non-canonical kernel virtual address
> +	  * 0xFFCFFC0000000000 - kasan_mem_to_shadow(0xFEUL << 56)
> +	  *         INVALID
> +	  * 0x07EFFBFFFFFFFFFF - kasan_mem_to_shadow(~0UL >> 1)
> +	  *         VALID   - non-canonical user virtual addresses
> +	  *         VALID   - user addresses
> +	  * 0x07CFFC0000000000 - kasan_mem_to_shadow(0x7EUL << 56)
> +	  *         INVALID
> +	  * 0x0000000000000000
> +	  */
> +	if (IS_ENABLED(CONFIG_KASAN_SW_TAGS) && IS_ENABLED(CONFIG_X86_64)) {

One overall comment on this series: there's a lot of unnecessary
complexity. Case in point:

	config ADDRESS_MASKING
	        depends on X86_64

and

	select HAVE_ARCH_KASAN_SW_TAGS		if ADDRESS_MASKING

and

	config KASAN_SW_TAGS
        	depends on HAVE_ARCH_KASAN_SW_TAGS ...


So you can't have CONFIG_KASAN_SW_TAGS set without CONFIG_X86_64.

> +		if ((addr < (u64)kasan_mem_to_shadow((void *)(0x7E UL << 56)) ||
> +		     addr > (u64)kasan_mem_to_shadow((void *)(~0UL >> 1))) &&
> +		    (addr < (u64)kasan_mem_to_shadow((void *)(0xFE UL << 56)) ||
> +		     addr > (u64)kasan_mem_to_shadow((void *)(~0UL))))
> +			return;
> +	}
This isn't looking great.

I'd much rather have those kasan_mem_to_shadow() arguments be built up
programmatically.

I'm also not following the description of where these ranges come from:

	[0x7E00000000000000, 0x7FFFFFFFFFFFFFFF]
	[0xFE00000000000000, 0xFFFFFFFFFFFFFFFF]

I obviously recognize the top kernel and top userspace addresses, but
there do 0x7E... and 0xFE... come from? Is that because both of them
only have 56 actual bits of address space?

Wouldn't we be better off writing that as, say:

#define HIGHEST_KER_ADDR (void *)0xFFFFFFFFFFFFFFFF
// ^ we probably have some macro for that already
#define LOWEST_KERN_ADDR (void *)(HIGHEST_KERNEL_ADDRESS - \
					(1<<56) + 1)
// ^ or can this be calculated by tag manipulation?

which yields:

   void *_addr = (u64)addr;
   ...

   in_kern_shadow = (_addr >= kasan_mem_to_shadow(LOWEST_KERN_ADDR) ||
		    (_addr <= kasan_mem_to_shadow(HIGHEST_KERN_ADDR);
   in_user_shadow = (_addr >= kasan_mem_to_shadow(LOWEST_USER_ADDR) ||
		    (_addr <= kasan_mem_to_shadow(HIGHEST_USER_ADDR);

   if (!in_kern_shadow &&
       !in_user_shadow)
	return;

I _think_ that's the same logic you have. Isn't it slightly more readable?


  reply	other threads:[~2025-04-04 17:38 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-04 13:14 [PATCH v3 00/14] kasan: x86: arm64: KASAN tag-based mode for x86 Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 01/14] kasan: sw_tags: Use arithmetic shift for shadow computation Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 02/14] kasan: sw_tags: Support tag widths less than 8 bits Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 03/14] x86: Add arch specific kasan functions Maciej Wieczor-Retman
2025-04-04 16:06   ` Dave Hansen
2025-04-09  7:16     ` Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 04/14] kasan: arm64: x86: Make special tags arch specific Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 05/14] x86: Reset tag for virtual to physical address conversions Maciej Wieczor-Retman
2025-04-04 16:42   ` Dave Hansen
2025-04-09  7:36     ` Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 06/14] x86: Physical address comparisons in fill_p*d/pte Maciej Wieczor-Retman
2025-04-04 16:56   ` Dave Hansen
2025-04-09  7:49     ` Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 07/14] x86: KASAN raw shadow memory PTE init Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 08/14] x86: LAM initialization Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 09/14] x86: Minimal SLAB alignment Maciej Wieczor-Retman
2025-04-04 16:59   ` Dave Hansen
2025-04-09 12:49     ` Maciej Wieczor-Retman
2025-04-09 15:24       ` Dave Hansen
2025-04-04 13:14 ` [PATCH v3 10/14] x86: Update the KASAN non-canonical hook Maciej Wieczor-Retman
2025-04-04 17:37   ` Dave Hansen [this message]
2025-04-09 14:34     ` Maciej Wieczor-Retman
2025-04-09 18:29       ` Dave Hansen
2025-04-04 13:14 ` [PATCH v3 11/14] x86: Handle int3 for inline KASAN reports Maciej Wieczor-Retman
2025-04-04 17:55   ` Dave Hansen
2025-04-09 14:48     ` Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 12/14] kasan: Fix inline mode for x86 tag-based mode Maciej Wieczor-Retman
2025-04-04 13:14 ` [PATCH v3 13/14] mm: Unpoison pcpu chunks with base address tag Maciej Wieczor-Retman
2025-04-04 18:08   ` Dave Hansen
2025-04-09 16:32     ` Maciej Wieczor-Retman
2025-04-09 17:12       ` Dave Hansen
2025-04-04 13:14 ` [PATCH v3 14/14] x86: Make software tag-based kasan available Maciej Wieczor-Retman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8416848c-700a-4ff0-8a22-aa62579d60cd@intel.com \
    --to=dave.hansen@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=apopple@nvidia.com \
    --cc=ardb@kernel.org \
    --cc=baohua@kernel.org \
    --cc=bhe@redhat.com \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=broonie@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dvyukov@google.com \
    --cc=gatlin.newhouse@gmail.com \
    --cc=glider@google.com \
    --cc=guoweikang.kernel@gmail.com \
    --cc=hch@infradead.org \
    --cc=hpa@zytor.com \
    --cc=jackmanb@google.com \
    --cc=jan.kiszka@siemens.com \
    --cc=jason.andryuk@amd.com \
    --cc=jgross@suse.com \
    --cc=jhubbard@nvidia.com \
    --cc=joel.granados@kernel.org \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kaleshsingh@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kbingham@kernel.org \
    --cc=kees@kernel.org \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=kuan-ying.lee@canonical.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=llvm@lists.linux.dev \
    --cc=luto@kernel.org \
    --cc=maciej.wieczor-retman@intel.com \
    --cc=mark.rutland@arm.com \
    --cc=masahiroy@kernel.org \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=nick.desaulniers+lkml@gmail.com \
    --cc=nicolas@fjasle.eu \
    --cc=pankaj.gupta@amd.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=peterz@infradead.org \
    --cc=rppt@kernel.org \
    --cc=ryabinin.a.a@gmail.com \
    --cc=samitolvanen@google.com \
    --cc=samuel.holland@sifive.com \
    --cc=scott@os.amperecomputing.com \
    --cc=snovitoll@gmail.com \
    --cc=surenb@google.com \
    --cc=tglx@linutronix.de \
    --cc=thiago.bauermann@linaro.org \
    --cc=thuth@redhat.com \
    --cc=tony.luck@intel.com \
    --cc=ubizjak@gmail.com \
    --cc=urezki@gmail.com \
    --cc=vincenzo.frascino@arm.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).