Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp26976459rwd; Mon, 3 Jul 2023 18:39:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7u5qcSA5QkNIrIoINioP+zWkQ1sW/hzpynBe1rm9eTGfCv48zu/F/pXhmDZZulx/+rJ/z3 X-Received: by 2002:a05:6808:311:b0:3a3:753e:84a9 with SMTP id i17-20020a056808031100b003a3753e84a9mr9014998oie.43.1688434748018; Mon, 03 Jul 2023 18:39:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688434747; cv=none; d=google.com; s=arc-20160816; b=tAmhnuoxSdllPMmZz4sctxc9vezQcr3WaEabgQFx11Ie5DH8g2qN1dVGYThM4Qt4Nj 9MTeXmWBMNdpYLM/QuqmBaSQwO1h0PdmuNFoO7YiiQmYNrxRaCbH5Qoyryf9MvvcFbmO VkiPq3Ah1UIV9l7+kobHtBkiiG8LhJAkr4KyQU2xmxmZsdHC8MNcQweMH93KEF3nhBoO 7nkPSkZhV8RNu7jqi0jXlGKlIUSeL7hRWDTGmHPekFzJe36WugRNAM1YVQWpP+0HOlHj qArlin7VgyBdFLsb8rOEomVCvsdBjoyJVUG3WK64w0jgMbg3bU/eKT8mZvOBnzwl7QMS NSYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=9PvMF+hM/tczQlWv3CZcoKeqGsQQ6VNHOk9FPhNfzNc=; fh=tDHA86gZ9oTHBveGBBRxAm8hjiFFPfD6zeX1tbGJ9Qw=; b=FftXymq6a7ysy82F8yviHqDRf/1VPny024dbASBBLwchpN9QZSN8PsURfcmIumd9pW ujgvBsc5bCi9fb6zNVHnQ8/RkCwyIoZ+kXOAZ0j5UWv9i/LSxS+eW1dIiZCCxTtEi+1P AtYxHC4dL8mvqMZWJ2LtA4fo/j1pXgkBUgJ7xpSXznuHG/BoYZh4AKhANBHDZdlmdGGD UwMkgy28wmG7FXPqIjBFrvAhscIXhK+UHCVzh8Uj7DSvuc+AqYnhg6abI2Y0CL5nlvnf dpewxDjzGI+oOvdpHVwE256/RmTF5uZZ5hHA2UkwD5IKa9cFqosW8PfkHT24n8HqRpCS t2Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=QYXS2UfB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g37-20020a635665000000b00553ced07d1bsi18627447pgm.36.2023.07.03.18.38.53; Mon, 03 Jul 2023 18:39:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=QYXS2UfB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230014AbjGDA5f (ORCPT + 99 others); Mon, 3 Jul 2023 20:57:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbjGDA5d (ORCPT ); Mon, 3 Jul 2023 20:57:33 -0400 X-Greylist: delayed 320 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 03 Jul 2023 17:57:32 PDT Received: from out-8.mta1.migadu.com (out-8.mta1.migadu.com [95.215.58.8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80956137 for ; Mon, 3 Jul 2023 17:57:32 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.25.1