From: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
To: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Samuel Holland <samuel.holland@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
<linux-riscv@lists.infradead.org>,
Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Alexander Potapenko <glider@google.com>,
"Dmitry Vyukov" <dvyukov@google.com>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
<kasan-dev@googlegroups.com>, <llvm@lists.linux.dev>,
Catalin Marinas <catalin.marinas@arm.com>,
<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>,
Alexandre Ghiti <alexghiti@rivosinc.com>,
Will Deacon <will@kernel.org>,
Evgenii Stepanov <eugenis@google.com>,
Andrew Morton <akpm@linux-foundation.org>,
<linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH v2 1/9] kasan: sw_tags: Use arithmetic shift for shadow computation
Date: Mon, 17 Feb 2025 19:37:12 +0100 [thread overview]
Message-ID: <kmibbbrtlwds6td64hloau7pf3smqth4wff33soebvujohsvli@kni5dtvpwsxf> (raw)
In-Reply-To: <CA+fCnZcHnWr0++8omB5ju8E3uSK+s+JOFZ3=UqgtVEcBzrm2Lg@mail.gmail.com>
On 2025-02-17 at 17:13:23 +0100, Andrey Konovalov wrote:
>On Fri, Feb 14, 2025 at 9:21 AM Maciej Wieczor-Retman
><maciej.wieczor-retman@intel.com> wrote:
>>
>> On 2025-02-13 at 17:20:22 +0100, Maciej Wieczor-Retman wrote:
>> >On 2025-02-13 at 02:28:08 +0100, Andrey Konovalov wrote:
>> >>On Thu, Feb 13, 2025 at 2:21 AM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>> >>>
>> >>> On Tue, Feb 11, 2025 at 7:07 PM Maciej Wieczor-Retman
>> >>> <maciej.wieczor-retman@intel.com> wrote:
>> >>> >
>> >>> > I did some experiments with multiple addresses passed through
>> >>> > kasan_mem_to_shadow(). And it seems like we can get almost any address out when
>> >>> > we consider any random bogus pointers.
>> >>> >
>> >>> > I used the KASAN_SHADOW_OFFSET from your example above. Userspace addresses seem
>> >>> > to map to the range [KASAN_SHADOW_OFFSET - 0xffff8fffffffffff]. Then going
>> >>> > through non-canonical addresses until 0x0007ffffffffffff we reach the end of
>> >>> > kernel LA and we loop around. Then the addresses seem to go from 0 until we
>> >>> > again start reaching the kernel space and then it maps into the proper shadow
>> >>> > memory.
>> >>> >
>> >>> > It gave me the same results when using the previous version of
>> >>> > kasan_mem_to_shadow() so I'm wondering whether I'm doing this experiment
>> >>> > incorrectly or if there aren't any addresses we can rule out here?
>> >>>
>> >>> By the definition of the shadow mapping, if we apply that mapping to
>> >>> the whole 64-bit address space, the result will only contain 1/8th
>> >>> (1/16th for SW/HW_TAGS) of that space.
>> >>>
>> >>> For example, with the current upstream value of KASAN_SHADOW_OFFSET on
>> >>> x86 and arm64, the value of the top 3 bits (4 for SW/HW_TAGS) of any
>> >>> shadow address are always the same: KASAN_SHADOW_OFFSET's value is
>> >>> such that the shadow address calculation never overflows. Addresses
>> >>> that have a different value for those top 3 bits are the once we can
>> >>> rule out.
>> >>
>> >>Eh, scratch that, the 3rd bit from the top changes, as
>> >>KASAN_SHADOW_OFFSET is not a that-well-aligned value, the overall size
>> >>of the mapping holds.
>> >>
>> >>> The KASAN_SHADOW_OFFSET value from my example does rely on the
>> >>> overflow (arguably, this makes things more confusing [1]). But still,
>> >>> the possible values of shadow addresses should only cover 1/16th of
>> >>> the address space.
>> >>>
>> >>> So whether the address belongs to that 1/8th (1/16th) of the address
>> >>> space is what we want to check in kasan_non_canonical_hook().
>> >>>
>> >
>> >Right, I somehow forgot that obviously the whole LA has to map to 1/16th of the
>> >address space and it shold stay contiguous.
>> >
>> >After rethinking how the mapping worked before and will work after making stuff
>> >signed I thought this patch could make use of the overflow?
>> >
>> >From what I noticed, all the Kconfig values for KASAN_SHADOW_OFFSET should make
>> >it so there will be overflow when inputing more and more positive addresses.
>> >
>> >So maybe we should first find what the most negative and most positive (signed)
>> >addresses map to in shadow memory address space. And then when looking for
>> >invalid values that aren't the product of kasan_mem_to_shadow() we should check
>> >
>> > if (addr > kasan_mem_to_shadow(biggest_positive_address) &&
>> > addr < kasan_mem_to_shadow(smallest_negative_address))
>> > return;
>> >
>> >Is this correct?
>>
>> I suppose the original code in the patch does the same thing when you change the
>> || into &&:
>>
>> if (addr < KASAN_SHADOW_OFFSET - max_shadow_size / 2 &&
>> addr >= KASAN_SHADOW_OFFSET + max_shadow_size / 2)
>> return;
>>
>> kasan_mem_to_shadow(0x7FFFFFFFFFFFFFFF) -> 0x07ff7fffffffffff
>> kasan_mem_to_shadow(0x8000000000000000) -> 0xf7ff800000000000
>
>I'm a bit lost with these calculations at this point. Please send the
>full patch, including the new values for KASAN_SHADOW_OFFSET (do I
>understand correctly that you want to change them?). It'll be easier
>to look at the code.
Sorry, this thread became a little bit confusing. No, I think the
KASAN_SHADOW_OFFSET values are fine. I just wanted to embrace the idea of
overflow for the purpose of the check in kasan_non_canonical_hook().
But I'll put down my train of thought about the overflow + calculations in the
patch message.
>
>Feel free to send this patch separately from the rest of the series,
>so that we can finalize it first.
I have the x86 tag-based series basically ready (just need to re-read it) so I
think I can send it as whole with this patch and 3 others from this series.
--
Kind regards
Maciej Wieczór-Retman
next prev parent reply other threads:[~2025-02-17 18:38 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-22 1:57 [PATCH v2 0/9] kasan: RISC-V support for KASAN_SW_TAGS using pointer masking Samuel Holland
2024-10-22 1:57 ` [PATCH v2 1/9] kasan: sw_tags: Use arithmetic shift for shadow computation Samuel Holland
2024-10-23 18:41 ` Andrey Konovalov
2025-02-10 15:22 ` Maciej Wieczor-Retman
2025-02-10 15:52 ` Maciej Wieczor-Retman
2025-02-10 22:57 ` Andrey Konovalov
2025-02-11 8:58 ` Maciej Wieczor-Retman
2025-02-11 13:42 ` Maciej Wieczor-Retman
2025-02-11 18:06 ` Maciej Wieczor-Retman
2025-02-13 1:21 ` Andrey Konovalov
2025-02-13 1:28 ` Andrey Konovalov
2025-02-13 16:20 ` Maciej Wieczor-Retman
2025-02-14 8:20 ` Maciej Wieczor-Retman
2025-02-17 16:13 ` Andrey Konovalov
2025-02-17 18:37 ` Maciej Wieczor-Retman [this message]
2025-02-17 19:00 ` Andrey Konovalov
2024-10-22 1:57 ` [PATCH v2 2/9] kasan: sw_tags: Check kasan_flag_enabled at runtime Samuel Holland
2024-10-22 1:57 ` [PATCH v2 3/9] kasan: sw_tags: Support outline stack tag generation Samuel Holland
2024-10-23 18:42 ` Andrey Konovalov
2024-10-22 1:57 ` [PATCH v2 4/9] kasan: sw_tags: Support tag widths less than 8 bits Samuel Holland
2024-10-22 19:30 ` kernel test robot
2024-10-22 19:51 ` kernel test robot
2024-10-22 1:57 ` [PATCH v2 5/9] riscv: mm: Log potential KASAN shadow alias Samuel Holland
2024-11-05 13:44 ` Alexandre Ghiti
2024-10-22 1:57 ` [PATCH v2 6/9] riscv: Do not rely on KASAN to define the memory layout Samuel Holland
2024-11-05 13:47 ` Alexandre Ghiti
2024-10-22 1:57 ` [PATCH v2 7/9] riscv: Align the sv39 linear map to 16 GiB Samuel Holland
2024-11-05 13:55 ` Alexandre Ghiti
2024-10-22 1:57 ` [PATCH v2 8/9] riscv: Add SBI Firmware Features extension definitions Samuel Holland
2024-10-22 1:57 ` [PATCH v2 9/9] riscv: Implement KASAN_SW_TAGS Samuel Holland
2024-10-23 18:42 ` Andrey Konovalov
2025-05-28 5:38 ` [PATCH v2 0/9] kasan: RISC-V support for KASAN_SW_TAGS using pointer masking JiaJie Ho
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=kmibbbrtlwds6td64hloau7pf3smqth4wff33soebvujohsvli@kni5dtvpwsxf \
--to=maciej.wieczor-retman@intel.com \
--cc=akpm@linux-foundation.org \
--cc=alexghiti@rivosinc.com \
--cc=andreyknvl@gmail.com \
--cc=catalin.marinas@arm.com \
--cc=dvyukov@google.com \
--cc=eugenis@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=llvm@lists.linux.dev \
--cc=palmer@dabbelt.com \
--cc=ryabinin.a.a@gmail.com \
--cc=samuel.holland@sifive.com \
--cc=vincenzo.frascino@arm.com \
--cc=will@kernel.org \
/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