From patchwork Tue Jul 4 00:52:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13300561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED4B5EB64DC for ; Tue, 4 Jul 2023 00:52:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0B72280046; Mon, 3 Jul 2023 20:52:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBAB628003F; Mon, 3 Jul 2023 20:52:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAA0F280046; Mon, 3 Jul 2023 20:52:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A7AE428003F for ; Mon, 3 Jul 2023 20:52:15 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 57EFC801B2 for ; Tue, 4 Jul 2023 00:52:15 +0000 (UTC) X-FDA: 80972103030.22.14EEF14 Received: from out-11.mta1.migadu.com (out-11.mta1.migadu.com [95.215.58.11]) by imf14.hostedemail.com (Postfix) with ESMTP id 1F3A9100009 for ; Tue, 4 Jul 2023 00:52:11 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=QYXS2UfB; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.11 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688431932; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=9PvMF+hM/tczQlWv3CZcoKeqGsQQ6VNHOk9FPhNfzNc=; b=O8R9A1Lq/mfLFMA8ZcoDTMKvC1MC56XwXqd19aAzZ2XFqETMVkUk+gn7mXomT5SXpn5sfg gSTGSdu9XnwV3N3X9VMpBJ2HrIHcBfCTe/EbXMD0FXYZ/YYUuiI+VP2HJhr998it5h+0Hb 27j/Eixsi4Q5UN4deq92Bf2VSUa/ExU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688431932; a=rsa-sha256; cv=none; b=L0REbPxaz0PdSxhGly3JsF2HMdcFXla2QB9z+EqJypMrYL2vUugP+ky8mG2jbRYwJn2mY4 47V9bmuJ3fOOUeNumsLzMgHuQbeyVrwylYK1O9SUlDF0eIF1g6pgMBVcsastDY+wiR2uxG e/PgyIR1UeQUBSvpoHJkwcv6zYr4lh0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=QYXS2UfB; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.11 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1688431928; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=9PvMF+hM/tczQlWv3CZcoKeqGsQQ6VNHOk9FPhNfzNc=; b=QYXS2UfB9fTOuQpIwrkK8of/jfOc1fLqLKs6PHdxgbFg8nLcpk3/ConRj1BiU7j0WCo28n daS2Mm1suhkZfCV9ZqOjFCpyOONbNFubKyg6htLLwJBilt9OjYbjTlOpmT57JWEevu0M2T nWPQm6VALEzldSnhheqDa/Q0KUbBky0= From: andrey.konovalov@linux.dev To: Marco Elver Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, Arnd Bergmann , stable@vger.kernel.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH] kasan: fix type cast in memory_is_poisoned_n Date: Tue, 4 Jul 2023 02:52:05 +0200 Message-Id: <8c9e0251c2b8b81016255709d4ec42942dcaf018.1688431866.git.andreyknvl@google.com> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 1F3A9100009 X-Rspam-User: X-Stat-Signature: tmwrkiejhxf5n78zzf44fw7dirpz3z9c X-Rspamd-Server: rspam03 X-HE-Tag: 1688431931-724820 X-HE-Meta: U2FsdGVkX19Pcmw5XByCnzfL/5JM2e+UxV4aGEnlPLEy7IysPMTkr4PS1DAcEaC6s9c82oA1kIRYayiNEgZMHpVfNvjZ/IRkPYJbF2ZEUmpmatdyzTRnt1BhwVRxbalsSYyx2PwUlsIKviLf1g/YdE06VhusrkMxGLc3iJsPIsKBc/gBGI5/KXmCJwISv8uQg+TMGV73C2QGMHntW3K832YLso5N+z9O75S1AXk5xeYI29d/LGbBcEoayToWZ1QiFbQUACez5jCKauFsj4gq1mUC5qACvzpg3U8IHetXSg7Vaq6UXcNf4zIEiHbrx/j9HB67xmiq0Y82ZViCg90Wa+oxgPh82Lwjij5YVif2OcWd7lP5yAbvLAnz3HfcMe62hJVHX5Di/pIYTnkB8hU/nQ1VS3K4HWTsLNG7MgvUx+Gqbx4YAq2FtehMerPkuFlqroUSSwRClAPtUijUiG586WwOB7dkhynJEE3HtS/d7OgMcxmr4Q84z+UV06hHCWh53rTucqsvvDPpggcnhk7htmRbAfPH72XOTptyhb2MeWDSmZnu7x1j0R7t/PGMU/sriWyah77fEdcnXGzsvN3BavwJTl6FyhPKY3B2byTUzdcqpgaAea2BYIRM00H3op466dK2zKesMqAPxRga4CvkgE2h8bWjWw3EUxAiJtuVUoYYZbsRbUtrIjh3BqwajA1N57To484Y16EB8dwbZo/eSsIEH/xBFoKayZ3RmaF4eGO1VrqZOeJW/t7rG7AM1aZyxDOKkDrMq24yCJwkOHqT8TZjlIRFYjSJIKehVQYT+c6MWuOCZERMTxLb+N8qk4jK6Pmt7lpUZILBZmTMBNcW4q4D0VANFhcvnZJHVobB7u0oXc9h3Rqm4CGtDWMoU1RdPN8cxWEdJ/c4AMcYnD/lW63BZafsAEij8u1axn6CyG6EYaEd+GqgcpWPS8HsukcAvjJ8RCkwXWndssAa81W GNPm0P5S gK23HxptXASkjfdgz8gk3SzIQkQRE5RZ7VLx6qNWsNLHQucMX3aKGOGl5SrhEdutb9Tj3uHLQ+ddZe9IMgCP1QhWUxngDggMg9BAXlElKUN6xPdkkS7ouRVQaiPqVQDfyEhqCWJzHuKi8pTIA8VmiZukNGPKw0FXW0MeSN2zJIQ7DHskahWJ8uyjfC0Lpe6stPczejRwWX83l4x272Iyc7MZsyVIuFmdjy+9/l+QlnzTiFfiTpFJLRkm7L1zHCC8GJFHNgU01HIplb4RYU1wA2zapQA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Commit bb6e04a173f0 ("kasan: use internal prototypes matching gcc-13 builtins") introduced a bug into the memory_is_poisoned_n implementation: it effectively removed the cast to a signed integer type after applying KASAN_GRANULE_MASK. As a result, KASAN started failing to properly check memset, memcpy, and other similar functions. Fix the bug by adding the cast back (through an additional signed integer variable to make the code more readable). Fixes: bb6e04a173f0 ("kasan: use internal prototypes matching gcc-13 builtins") Cc: Signed-off-by: Andrey Konovalov --- mm/kasan/generic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 5b4c97baa656..4d837ab83f08 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -130,9 +130,10 @@ static __always_inline bool memory_is_poisoned_n(const void *addr, size_t size) if (unlikely(ret)) { const void *last_byte = addr + size - 1; s8 *last_shadow = (s8 *)kasan_mem_to_shadow(last_byte); + s8 last_accessible_byte = (unsigned long)last_byte & KASAN_GRANULE_MASK; if (unlikely(ret != (unsigned long)last_shadow || - (((long)last_byte & KASAN_GRANULE_MASK) >= *last_shadow))) + last_accessible_byte >= *last_shadow)) return true; } return false;