All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Shivank Garg <shivankg@amd.com>
Cc: "sashiko-reviews@lists.linux.dev"
	<sashiko-reviews@lists.linux.dev>,
	 "kvm@vger.kernel.org" <kvm@vger.kernel.org>
Subject: Re: [PATCH v2 4/4] KVM: selftests: use allowed NUMA nodes in guest_memfd_test
Date: Tue, 1 Sep 2026 08:07:43 -0700	[thread overview]
Message-ID: <apbqPyRIyR2qZeH_@google.com> (raw)
In-Reply-To: <0a98f85d164cc7eac4ca584b094aa402508efb10.camel@amd.com>

On Tue, Sep 01, 2026, Shivank Garg wrote:
> On Tue, 2026-09-01 at 08:39 +0000, sashiko-bot@kernel.org wrote:
> > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> > - [Medium] Stack buffer overflow in test_mbind when retrieving memory policy on 32-bit architectures.
> > --
> > 
> > commit c29a637608e8b3ee37278728fd941b62df184a8b
> > Author: Shivank Garg <shivankg@amd.com>
> > 
> > KVM: selftests: use allowed NUMA nodes in guest_memfd_test
> > 
> > guest_memfd_test assumes that nodes 0 and 1 exist and have memory. Select
> > the required nodes from MPOL_F_MEMS_ALLOWED instead. The sysfs helpers for
> > finding maxnode are no longer needed.
> > 
> > > diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
> > > index 2233d871a38f4..aee80dda62295 100644
> > > --- a/tools/testing/selftests/kvm/guest_memfd_test.c
> > > +++ b/tools/testing/selftests/kvm/guest_memfd_test.c
> > [ ... ]
> > >  static void test_mbind(int fd, size_t total_size)
> > >  {
> > > -	const unsigned long nodemask_0 = 1; /* nid: 0 */
> > > -	unsigned long nodemask = 0;
> > > -	unsigned long maxnode = BITS_PER_TYPE(nodemask);
> > > +	unsigned long nodemask, bind_nodemask;
> > > +	unsigned long maxnode = BITS_PER_TYPE(nodemask) + 1;
> > 
> > [Severity: Medium]
> > Can this cause a stack buffer overflow in test_mbind() on 32-bit architectures?
> > 
> > The nodemask variable is declared as a single unsigned long, which is
> > 4 bytes on 32-bit architectures. However, maxnode is set to
> > BITS_PER_TYPE(nodemask) + 1, which evaluates to 33.
> > 
> > >  	int policy;
> > >  	char *mem;
> > > +	int nid;
> > >  	int ret;
> > >  
> > > -	if (!is_multi_numa_node_system())
> > > +	if (!get_numa_node_ids(&nid, 1))
> > >  		return;
> > >  
> > > +	bind_nodemask = 1UL << nid;
> > >  	mem = kvm_mmap(total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd);
> > >  
> > >  	/* Test MPOL_INTERLEAVE policy */
> > > -	kvm_mbind(mem, page_size * 2, MPOL_INTERLEAVE, &nodemask_0, maxnode, 0);
> > > +	kvm_mbind(mem, page_size * 2, MPOL_INTERLEAVE, &bind_nodemask, maxnode, 0);
> > >  	kvm_get_mempolicy(&policy, &nodemask, maxnode, mem, MPOL_F_ADDR);
> > 
> > When maxnode is passed to kvm_get_mempolicy() here, the kernel must
> > write at least 5 bytes to return 33 bits of node status. This rounds up to
> > 8 bytes (two 32-bit words).
> > 
> > Will this overwrite the 4-byte nodemask buffer and corrupt adjacent
> > variables on the stack?
> > 
> 
> Thanks sashiko for report. This looks like false-positive.
> 32-bit KVM selftest are not supported. In kvm_util.h, no VM_MODE_DEFAULT
> exist for 32-bit arch.

Unless I'm misreading a pile of things, this has nothing to do with 32-bit
architectures.  Sashiko is simply confused by the kernel's godwaful, poorly
documented off-by-one bug.

  reply	other threads:[~2026-09-01 15:07 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-01  8:22 [PATCH v2 0/4] KVM: guest_memfd: fix NUMA selftests Shivank Garg
2026-09-01  8:22 ` [PATCH v2 1/4] KVM: selftests: fix maxnode arguments in xapic_ipi_test Shivank Garg
2026-09-01 15:04   ` Sean Christopherson
2026-09-05  9:10     ` Garg, Shivank
2026-09-01  8:22 ` [PATCH v2 2/4] KVM: selftests: use BITS_PER_TYPE() for NUMA masks Shivank Garg
2026-09-01  8:22 ` [PATCH v2 3/4] KVM: selftests: add get_numa_mem_nodes() Shivank Garg
2026-09-01 15:06   ` Sean Christopherson
2026-09-05 18:52     ` Garg, Shivank
2026-09-01  8:22 ` [PATCH v2 4/4] KVM: selftests: use allowed NUMA nodes in guest_memfd_test Shivank Garg
2026-09-01  8:39   ` sashiko-bot
2026-09-01  9:38     ` Garg, Shivank
2026-09-01 15:07       ` Sean Christopherson [this message]
2026-09-01 17:28   ` Sean Christopherson
2026-09-05 19:45     ` Garg, Shivank

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=apbqPyRIyR2qZeH_@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shivankg@amd.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.