mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Marek Szyprowski <m.szyprowski@samsung.com>
To: Alexander Potapenko <glider@google.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Christoph Hellwig <hch@infradead.org>,
	Leon Romanovsky <leonro@nvidia.com>,
	mhklinux@outlook.com
Cc: anthony.l.nguyen@intel.com, przemyslaw.kitszel@intel.com,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com,
	Aleksandr Nogikh <nogikh@google.com>
Subject: Re: KMSAN: uninit-value in eth_type_trans
Date: Mon, 13 Oct 2025 09:31:08 +0200	[thread overview]
Message-ID: <0d5f2179-b89b-42b6-805d-6a50d2268eca@samsung.com> (raw)
In-Reply-To: <CAG_fn=WUGta-paG1BgsGRoAR+fmuCgh3xo=R3XdzOt_-DqSdHw@mail.gmail.com>

On 08.10.2025 10:43, Alexander Potapenko wrote:
> On Tue, Oct 7, 2025 at 8:51 AM Alexander Potapenko <glider@google.com> wrote:
>> Folks, as far as I understand, dma_direct_sync_single_for_cpu() and
>> dma_direct_sync_single_for_device() are the places where we send data
>> to or from the device.
>> Should we add KMSAN annotations to those functions to catch infoleaks
>> and mark data from devices as initialized?
> Something along the lines of:
>
> ======================================
> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> index 0d37da3d95b65..7f59de19c1c87 100644
> --- a/kernel/dma/swiotlb.c
> +++ b/kernel/dma/swiotlb.c
> @@ -42,6 +42,7 @@
>   #include <linux/string.h>
>   #include <linux/swiotlb.h>
>   #include <linux/types.h>
> +#include <linux/kmsan-checks.h>
>   #ifdef CONFIG_DMA_RESTRICTED_POOL
>   #include <linux/of.h>
>   #include <linux/of_fdt.h>
> @@ -903,10 +904,13 @@ static void swiotlb_bounce(struct device *dev,
> phys_addr_t tlb_addr, size_t size
>
>                          local_irq_save(flags);
>                          page = pfn_to_page(pfn);
> -                       if (dir == DMA_TO_DEVICE)
> +                       if (dir == DMA_TO_DEVICE) {
> +                               kmsan_check_highmem_page(page, offset, sz);
>                                  memcpy_from_page(vaddr, page, offset, sz);
> -                       else
> +                       } else {
> +                               kmsan_unpoison_memory(vaddr, sz);
>                                  memcpy_to_page(page, offset, vaddr, sz);
> +                       }
>                          local_irq_restore(flags);
>
>                          size -= sz;
> @@ -915,8 +919,10 @@ static void swiotlb_bounce(struct device *dev,
> phys_addr_t tlb_addr, size_t size
>                          offset = 0;
>                  }
>          } else if (dir == DMA_TO_DEVICE) {
> +               kmsan_check_memory(phys_to_virt(orig_addr), size);
>                  memcpy(vaddr, phys_to_virt(orig_addr), size);
>          } else {
> +               kmsan_unpoison_memory(vaddr, size);
>                  memcpy(phys_to_virt(orig_addr), vaddr, size);
>          }
>   }
> ======================================
>
> should be conceptually right, but according to the comment in
> swiotlb_tbl_map_single()
> (https://protect2.fireeye.com/v1/url?k=837a6d67-dce15478-837be628-000babdfecba-aa25926458f9fd30&q=1&e=a3963b1b-328b-4f69-8ca5-ffd6fc777dd7&u=https%3A%2F%2Felixir.bootlin.com%2Flinux%2Fv6.17.1%2Fsource%2Fkernel%2Fdma%2Fswiotlb.c%23L1431),
> that function is deliberately copying the buffer to the device, even
> when it is uninitialized - and KMSAN actually started reporting that
> when I applied the above patch.
>
> How should we handle this case?
> Not adding the kmsan_check_memory() calls will solve the problem, but
> there might be real infoleaks that we won't detect.
> We could unpoison the buffer before passing it to
> swiotlb_tbl_map_single() to ignore just the first infoleak on the
> buffer.
> Alternatively, we could require callers to always initialize the
> buffer passed to swiotlb_tbl_map_single().

Well, I didn't consider swiotlb a special case so far. I did a simple 
test with my PoC patch mentioned earlier in this thread with 
'swiotlb=force' kernel parameter and I didn't observe any kmsan issues, 
but I admin that this wasn't exhaustive test.

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


  reply	other threads:[~2025-10-13  7:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-25 22:36 Aleksandr Nogikh
2025-10-07  6:51 ` Alexander Potapenko
2025-10-08  8:43   ` Alexander Potapenko
2025-10-13  7:31     ` Marek Szyprowski [this message]
2026-03-20 17:30       ` Helen Koike
2025-10-08  8:53   ` Marek Szyprowski

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=0d5f2179-b89b-42b6-805d-6a50d2268eca@samsung.com \
    --to=m.szyprowski@samsung.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=anthony.l.nguyen@intel.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=glider@google.com \
    --cc=hch@infradead.org \
    --cc=kuba@kernel.org \
    --cc=leonro@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhklinux@outlook.com \
    --cc=netdev@vger.kernel.org \
    --cc=nogikh@google.com \
    --cc=pabeni@redhat.com \
    --cc=przemyslaw.kitszel@intel.com \
    --cc=robin.murphy@arm.com \
    --cc=syzkaller-bugs@googlegroups.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

all inboxes | Powered by JetHome®