From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 285AD2DC77B for ; Thu, 4 Dec 2025 15:35:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764862536; cv=none; b=mU2ic0HEGCTbZls4Je6hlnvL8DtpuFFCTVmmiQTXr9gP4j42c6We2b9WGcF+sx+MLcd/X81RERrHVZ86geXUTEbazNXea4yLFFF+GUYDcBIkFs5CowaSRdDZn3T/goPlX09A5SDMTGP+P1B9QK/CLsoiToodXqgw92HkToanj7w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764862536; c=relaxed/simple; bh=YD8H5lXp48nPEJjMmK72jkBVzWv8aQ7jxjV7/MKnlHk=; h=MIME-Version:Date:Content-Type:From:Message-ID:Subject:To:Cc: In-Reply-To:References; b=ZKRnw0wwfEVTZwOuItEXc+ibK7Re/wiQ6iGvfwD3DFdbxSyxI/x7ttQ+svM0v9Gwf/U6g7mYnerMnJVpNoOoTj7CHop7e3erXB8nCR3NTBTeTZJElSgyPTAQ05jOH0OpNIRkO6E62IiTqy+VyHTh1NfvrmIhyz91ZzRWjNkxiXw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=bG4Wr0P6; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="bG4Wr0P6" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1764862530; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cSWBjoUbSEGuP/Fanmcv3C5LwDckSbaugls2GY15bOc=; b=bG4Wr0P67KtfulsfKzMry8tAb3VX9KIxE4P5MGAIP5w/WvaSB3Fj6/iEdv7wZ9Z2+RVUhz wm6ZEgXrB8evf8flgtEYNc1/6lV7Qa7gKbwHpF09C6OjKmaE/FvTiOMkRWqdZF2SdPsk4j tH/L73A0Hsl3lE5/iUcH5pjsy7ELGcc= Date: Thu, 04 Dec 2025 15:35:23 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: "Jiayuan Chen" Message-ID: <5dce1baa4672646c54beaa94355e32fab856fa2b@linux.dev> TLS-Required: No Subject: Re: [PATCH v1] mm/kasan: Fix incorrect unpoisoning in vrealloc for KASAN To: "Andrey Konovalov" Cc: "Maciej Wieczor-Retman" , "Maciej Wieczor-Retman" , linux-mm@kvack.org, syzbot+997752115a851cb0cf36@syzkaller.appspotmail.com, "Andrey Ryabinin" , "Alexander Potapenko" , "Dmitry Vyukov" , "Vincenzo Frascino" , "Andrew Morton" , "Uladzislau Rezki" , "Danilo Krummrich" , "Kees Cook" , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org In-Reply-To: References: <5o7owlr4ap5fridqlkerrnuvwwlgldr35gvkcf6df4fufatrr6@yn5rmfn54i62> X-Migadu-Flow: FLOW_OUT December 4, 2025 at 23:06, "Andrey Konovalov" wrote: >=20 >=20On Thu, Dec 4, 2025 at 3:38 PM Jiayuan Chen = wrote: >=20 >=20>=20 >=20> I think I don't need KEEP_TAG flag anymore, following patch works w= ell and all kasan tests run successfully > > with CONFIG_KASAN_SW_TAGS/CONFIG_KASAN_HW_TAGS/CONFIG_KASAN_GENERIC > >=20 >=20Thanks for working on improving the vrealloc annotations! >=20 >=20But I think we need to first fix the vrealloc issue you discovered in > a separate patch (so that it can be backported), and then we can apply > your other vrealloc changes on top later. >=20 >=20So please implement a version of your fix with KEEP_TAG -- this would > also allow Maciej to build on top. Thanks, i will just use KEEP_TAG and make the patch as simple as possible= . CC Maciej Thanks. > >=20 >=20> diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c > > index 1c373cc4b3fa..8b819a9b2a27 100644 > > --- a/mm/kasan/hw_tags.c > > +++ b/mm/kasan/hw_tags.c > > @@ -394,6 +394,11 @@ void __kasan_poison_vmalloc(const void *start, = unsigned long size) > > * The physical pages backing the vmalloc() allocation are poisoned > > * through the usual page_alloc paths. > > */ > > + if (!is_vmalloc_or_module_addr(start)) > > + return; > > + > > + size =3D round_up(size, KASAN_GRANULE_SIZE); > > + kasan_poison(start, size, KASAN_VMALLOC_INVALID, false); > >=20 >=20This does not look good - we will end up poisoning the same memory > twice, once here and once it's freed to page_alloc. >=20 >=20Is this change required? >=20 >=20>=20 >=20> } > >=20 >=20> #endif > > diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c > > index 2cafca31b092..a5f683c3abde 100644 > > --- a/mm/kasan/kasan_test_c.c > > +++ b/mm/kasan/kasan_test_c.c > > @@ -1840,6 +1840,84 @@ static void vmalloc_helpers_tags(struct kunit= *test) > > vfree(ptr); > > } > >=20 >=20> + > > +static void vrealloc_helpers(struct kunit *test, bool tags) > > +{ > > + char *ptr; > > + size_t size =3D PAGE_SIZE / 2 - KASAN_GRANULE_SIZE - 5; > > + > > + if (!kasan_vmalloc_enabled()) > > + kunit_skip(test, "Test requires kasan.vmalloc=3Don"); > > + > > + ptr =3D (char *)vmalloc(size); > > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); > > + > > + OPTIMIZER_HIDE_VAR(ptr); > > + > > + size +=3D PAGE_SIZE / 2; > > + ptr =3D vrealloc(ptr, size, GFP_KERNEL); > > + /* Check that the returned pointer is tagged. */ > > + if (tags) { > > + KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); > > + KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); > > + } > > + /* Make sure in-bounds accesses are valid. */ > > + ptr[0] =3D 0; > > + ptr[size - 1] =3D 0; > > + > > + /* Make sure exported vmalloc helpers handle tagged pointers. */ > > + KUNIT_ASSERT_TRUE(test, is_vmalloc_addr(ptr)); > > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, vmalloc_to_page(ptr)); > > + > > + size -=3D PAGE_SIZE / 2; > > + ptr =3D vrealloc(ptr, size, GFP_KERNEL); > > + > > + /* Check that the returned pointer is tagged. */ > > + KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); > > + KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); > > + > > + /* Make sure exported vmalloc helpers handle tagged pointers. */ > > + KUNIT_ASSERT_TRUE(test, is_vmalloc_addr(ptr)); > > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, vmalloc_to_page(ptr)); > > + > > + > > + /* This access must cause a KASAN report. */ > > + KUNIT_EXPECT_KASAN_FAIL_READ(test, ((volatile char *)ptr)[size + 5= ]); > > + > > + > > +#if !IS_MODULE(CONFIG_KASAN_KUNIT_TEST) > > + { > > + int rv; > > + > > + /* Make sure vrealloc'ed memory permissions can be changed. */ > > + rv =3D set_memory_ro((unsigned long)ptr, 1); > > + KUNIT_ASSERT_GE(test, rv, 0); > > + rv =3D set_memory_rw((unsigned long)ptr, 1); > > + KUNIT_ASSERT_GE(test, rv, 0); > > + } > > +#endif > > + > > + vfree(ptr); > > +} > > + > > +static void vrealloc_helpers_tags(struct kunit *test) > > +{ > > + /* This test is intended for tag-based modes. */ > > + KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); > > + > > + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC); > > + vrealloc_helpers(test, true); > > +} > > + > > +static void vrealloc_helpers_generic(struct kunit *test) > > +{ > > + /* This test is intended for tag-based modes. */ > > + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); > > + > > + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC); > > + vrealloc_helpers(test, false); > > +} > > + > > static void vmalloc_oob(struct kunit *test) > > { > > char *v_ptr, *p_ptr; > > @@ -2241,6 +2319,8 @@ static struct kunit_case kasan_kunit_test_case= s[] =3D { > > KUNIT_CASE_SLOW(kasan_atomics), > > KUNIT_CASE(vmalloc_helpers_tags), > > KUNIT_CASE(vmalloc_oob), > > + KUNIT_CASE(vrealloc_helpers_tags), > > + KUNIT_CASE(vrealloc_helpers_generic), > > KUNIT_CASE(vmap_tags), > > KUNIT_CASE(vm_map_ram_tags), > > KUNIT_CASE(match_all_not_assigned), > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > > index 798b2ed21e46..9ba2e8a346d6 100644 > > --- a/mm/vmalloc.c > > +++ b/mm/vmalloc.c > > @@ -4128,6 +4128,7 @@ EXPORT_SYMBOL(vzalloc_node_noprof); > > void *vrealloc_node_align_noprof(const void *p, size_t size, unsigne= d long align, > > gfp_t flags, int nid) > > { > > + asan_vmalloc_flags_t flags; > > struct vm_struct *vm =3D NULL; > > size_t alloced_size =3D 0; > > size_t old_size =3D 0; > > @@ -4158,25 +4159,26 @@ void *vrealloc_node_align_noprof(const void = *p, size_t size, unsigned long align > > goto need_realloc; > > } > >=20 >=20> + flags =3D KASAN_VMALLOC_PROT_NORMAL | KASAN_VMALLOC_VM_ALLOC; > > /* > > * TODO: Shrink the vm_area, i.e. unmap and free unused pages. What > > * would be a good heuristic for when to shrink the vm_area? > > */ > > - if (size <=3D old_size) { > > + if (p && size <=3D old_size) { > > /* Zero out "freed" memory, potentially for future realloc. */ > > if (want_init_on_free() || want_init_on_alloc(flags)) > > memset((void *)p + size, 0, old_size - size); > > vm->requested_size =3D size; > > - kasan_poison_vmalloc(p + size, old_size - size); > > + kasan_poison_vmalloc(p, alloced_size); > > + p =3D kasan_unpoison_vmalloc(p, size, flags); > > return (void *)p; > > } > >=20 >=20> /* > > * We already have the bytes available in the allocation; use them. > > */ > > - if (size <=3D alloced_size) { > > - kasan_unpoison_vmalloc(p + old_size, size - old_size, > > - KASAN_VMALLOC_PROT_NORMAL); > > + if (p && size <=3D alloced_size) { > > + p =3D kasan_unpoison_vmalloc(p, size, flags); > > /* > > * No need to zero memory here, as unused memory will have > > * already been zeroed at initial allocation time or during > > >