From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B4FB3337BA5 for ; Wed, 7 Jan 2026 20:28:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767817708; cv=none; b=NhdBKj6yNnY2BvgPBHoL1NGW0nlXKGpI15TUbO918IbuY5KtZPWyv7vjgnSzHGpqn6wavKoq8vB8vUyRexab9aM9yjkMXu7iX4V1x6krdlkcgqNNEmKs+E4hrVj2xpidPd/LbcGEmgR3MLWVBH6Zll3OCjlFt2rZN4jkQwnoMbQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767817708; c=relaxed/simple; bh=H7h/gH0TCzCbiszMKAa3EWkIHJGcdsYIdjeIZkzHok8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Lzmq2Q7p0fqcOAig3KaqZpnHrd8PRXVEVBAvD2/50q7z9sAcM3lto8cAHfCEhzThQ+2HbG9ixS2z0B7atF0M3Zyrx6MNFVOlO5NOgoGl+gc1L7sFr9nwNQydfQhlg4dP23kAXJ30TYYtsiuXiqLgxwRvu+eVgG4VseQAM5ccGWQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E5IRqCmm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E5IRqCmm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3867EC4CEF1; Wed, 7 Jan 2026 20:28:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1767817708; bh=H7h/gH0TCzCbiszMKAa3EWkIHJGcdsYIdjeIZkzHok8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=E5IRqCmm+zy5R+XgK8ZDidOJvdt3UHkdCGBAOeL7OnfRhmgaxSFlYefWNz5osV8KR y6vx7e4wmpU3GBzVgqEckzKbW3yQTl7KI1iQ7c4JF4FnZA/OX9EWGoZn49qoXw+0kO IuecKJX3QYQ4gLPWcI+oe+Q3c0F1y9KBvxGpkHGeI19PDbruice6ImZI8UIvQg5VUG DXOvh3n/Adxu9c14RNXnKZ5jrLcqy/w5X4Ewhhqi1Zfmw9BcjNwyF8lcsX9ccMucT9 jFKvmYwcApNLjX3WrRzUTE2DyjsK422/mkXgBUPTr7uAkmG37vpP/39W+Qf405NjlK 747IyRkjWg8TA== Date: Wed, 7 Jan 2026 12:28:27 -0800 From: Kees Cook To: Maciej =?utf-8?Q?=C5=BBenczykowski?= Cc: Maciej Wieczor-Retman , joonki.min@samsung-slsi.corp-partner.google.com, Andrew Morton , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Marco Elver , Andrew Morton , Uladzislau Rezki , Danilo Krummrich , jiayuan.chen@linux.dev, syzbot+997752115a851cb0cf36@syzkaller.appspotmail.com, Maciej Wieczor-Retman , kasan-dev@googlegroups.com, Kernel hackers , linux-mm@kvack.org Subject: Re: KASAN vs realloc Message-ID: <202601071226.8DF7C63@keescook> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Tue, Jan 06, 2026 at 01:42:45PM +0100, Maciej Żenczykowski wrote: > We've got internal reports (b/467571011 - from CC'ed Samsung > developer) that kasan realloc is broken for sizes that are not a > multiple of the granule. This appears to be triggered during Android > bootup by some ebpf program loading operations (a struct is 88 bytes > in size, which is a multiple of 8, but not 16, which is the granule > size). > > (this is on 6.18 with > https://lore.kernel.org/all/38dece0a4074c43e48150d1e242f8242c73bf1a5.1764874575.git.m.wieczorretman@pm.me/ > already included) > > joonki.min@samsung-slsi.corp-partner.google.com summarized it as > "When newly requested size is not bigger than allocated size and old > size was not 16 byte aligned, it failed to unpoison extended area." > > and *very* rough comment: > > Right. "size - old_size" is not guaranteed 16-byte alignment in this case. > > I think we may unpoison 16-byte alignment size, but it allowed more > than requested :( > > I'm not sure that's right approach. > > if (size <= alloced_size) { > - kasan_unpoison_vmalloc(p + old_size, size - old_size, > + kasan_unpoison_vmalloc(p + old_size, round_up(size - > old_size, KASAN_GRANULE_SIZE), > KASAN_VMALLOC_PROT_NORMAL | > KASAN_VMALLOC_VM_ALLOC | > KASAN_VMALLOC_KEEP_TAG); > /* > * No need to zero memory here, as unused memory will have > * already been zeroed at initial allocation time or during > * realloc shrink time. > */ > - vm->requested_size = size; > + vm->requested_size = round_up(size, KASAN_GRANULE_SIZE); > > my personal guess is that > > But just above the code you quoted in mm/vmalloc.c I see: > if (size <= old_size) { > ... > kasan_poison_vmalloc(p + size, old_size - size); > > is also likely wrong?? Considering: > > mm/kasan/shadow.c > > void __kasan_poison_vmalloc(const void *start, unsigned long size) > { > if (!is_vmalloc_or_module_addr(start)) > return; > > size = round_up(size, KASAN_GRANULE_SIZE); > kasan_poison(start, size, KASAN_VMALLOC_INVALID, false); > } > > This doesn't look right - if start isn't a multiple of the granule. I don't think we can ever have the start not be a granule multiple, can we? I'm not sure how any of this is supposed to be handled by KASAN, though. It does seem like a round_up() is missing, though? -Kees -- Kees Cook