Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3695270pxv; Mon, 5 Jul 2021 03:35:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDaGYofPj2FJExf0/CxbngPXizSej/GkYlxb9m7BqfSmTOCY6EOEtNQKZVY1d2rXrLS8JP X-Received: by 2002:a5e:d911:: with SMTP id n17mr11667330iop.178.1625481321898; Mon, 05 Jul 2021 03:35:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625481321; cv=none; d=google.com; s=arc-20160816; b=sa5Ky29hAth3jzTxOfrpxMq/qTOyjDRlWSjKvDYhu6tC9TKtWKB55fJverp2S5IPrF crGGqYg9Zgb/+kDvxOZoROkDpwIT7P6ZRpeZF/gFAYlQvyqmGvHG4ljQywBXrhdsFCw5 aOGSbYHsGYHAwQMRLLgFQKwowvA156RvQY6e4UXCxFXM3H5z/UGRVfr74V0hLzd8pf0A zyeCUKcFSnU7U0fCp1pvtpT5DEgXj4pKdsGd4VzTpn8xGIjN3qCrny7beE9uH9xi7/ZA 2XKFJsGnAKG5c/T3Ftl+ZpUzbBILVSCancBsqnTcIkpH1UUO4ZSTl8X32llJdW3a6sAr Cjiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=ayLPuPlTrAdgUpdGkSUQJ+xvmZ5NcrxOu2vb6vL7I9Q=; b=DWvZuh24ROuohx4J9hSErpqR+PQoNvWAjj4MEfz1O2odqfYR1JBVAgIGaePekW/X+M RM27V6jdC3x3Y4wCprcz3mn0k5a3bCbbkSzRxHChReklFmsvlMkGcw4P6R9hYu7Z6ane tbdqfdg3Gfb2Ckba1JAj1uJm4vJV7TCIDC0Ii6j38u9GobWgASRqrnBoXGdt+oO3VoBX fWIt0ALm8RoFusbAWWZuN72KH/Khu1oCNYXZdqKz64E+sJAk5aCZlHhiKOvzpkyMoVDQ 5fDv57boT9G7OME4ccogWGohWOme+1vcbLctbUIWju8q/1lBSekQNX1PCe+MVcLHoI4J dI2A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e127si14247855iof.13.2021.07.05.03.35.10; Mon, 05 Jul 2021 03:35:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231172AbhGEKgJ (ORCPT + 99 others); Mon, 5 Jul 2021 06:36:09 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:56995 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S231163AbhGEKgH (ORCPT ); Mon, 5 Jul 2021 06:36:07 -0400 X-UUID: 9608859d40f24c88ad40d555c4f80150-20210705 X-UUID: 9608859d40f24c88ad40d555c4f80150-20210705 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 742014454; Mon, 05 Jul 2021 18:33:27 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by mtkmbs01n2.mediatek.inc (172.21.101.79) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 5 Jul 2021 18:33:26 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 5 Jul 2021 18:33:25 +0800 From: To: CC: , , , Yee Lee , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Andrey Konovalov , Matthias Brugger , "open list:KASAN" , "open list:MEMORY MANAGEMENT" , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" Subject: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG Date: Mon, 5 Jul 2021 18:32:27 +0800 Message-ID: <20210705103229.8505-3-yee.lee@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20210705103229.8505-1-yee.lee@mediatek.com> References: <20210705103229.8505-1-yee.lee@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yee Lee Issue: when SLUB debug is on, hwtag kasan_unpoison() would overwrite the redzone of object with unaligned size. An additional memzero_explicit() path is added to replacing init by hwtag instruction for those unaligned size at SLUB debug mode. The penalty is acceptable since they are only enabled in debug mode, not production builds. A block of comment is added for explanation. Cc: Andrey Ryabinin Cc: Alexander Potapenko Cc: Dmitry Vyukov Cc: Andrew Morton Suggested-by: Marco Elver Suggested-by: Andrey Konovalov Signed-off-by: Yee Lee --- mm/kasan/kasan.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 98e3059bfea4..d739cdd1621a 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -9,6 +9,7 @@ #ifdef CONFIG_KASAN_HW_TAGS #include +#include "../slab.h" DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace); extern bool kasan_flag_async __ro_after_init; @@ -387,6 +388,17 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init) if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK)) return; + /* + * Explicitly initialize the memory with the precise object size to + * avoid overwriting the SLAB redzone. This disables initialization in + * the arch code and may thus lead to performance penalty. The penalty + * is accepted since SLAB redzones aren't enabled in production builds. + */ + if (__slub_debug_enabled() && + init && ((unsigned long)size & KASAN_GRANULE_MASK)) { + init = false; + memzero_explicit((void *)addr, size); + } size = round_up(size, KASAN_GRANULE_SIZE); hw_set_mem_tag_range((void *)addr, size, tag, init); -- 2.18.0