From patchwork Mon Jul 5 10:32:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WWVlIExlZSAo5p2O5bu66Kq8KQ==?= X-Patchwork-Id: 12358781 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MIME_BASE64_TEXT,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 387DFC07E9C for ; Mon, 5 Jul 2021 10:33:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C0B87613F7 for ; Mon, 5 Jul 2021 10:33:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C0B87613F7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 756396B0036; Mon, 5 Jul 2021 06:33:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 707EF6B005D; Mon, 5 Jul 2021 06:33:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B3E66B006C; Mon, 5 Jul 2021 06:33:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0231.hostedemail.com [216.40.44.231]) by kanga.kvack.org (Postfix) with ESMTP id 26E6E6B0036 for ; Mon, 5 Jul 2021 06:33:36 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 974A4181B04A7 for ; Mon, 5 Jul 2021 10:33:35 +0000 (UTC) X-FDA: 78328172790.03.E7FE8F2 Received: from mailgw01.mediatek.com (mailgw01.mediatek.com [216.200.240.184]) by imf22.hostedemail.com (Postfix) with ESMTP id 7254C19A2 for ; Mon, 5 Jul 2021 10:33:34 +0000 (UTC) X-UUID: 71b738b91a2044029e182eacee257f62-20210705 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=c3apyg+1j9K6wAJpMo0RJJyr5s7DwbhlyZZLh0X8Vlg=; b=HkqXizvaJJyEEK6mB2PALIHc4r8jp/M7xUCcy7AdGe3LlB0ySmRlc0DdaFqiGHYDEpTqN4UYT89CjBiQipc5oILB8ma6rKsvPl0Y7zTSQaePdxhUfPYI3Cpl2dQ3CVbFO0egOBc+TTDSTykTTOPizqqM6VyLxotsEbivs26/h6c=; X-UUID: 71b738b91a2044029e182eacee257f62-20210705 Received: from mtkcas68.mediatek.inc [(172.29.94.19)] by mailgw01.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 88766971; Mon, 05 Jul 2021 03:33:28 -0700 Received: from MTKMBS01N2.mediatek.inc (172.21.101.79) by MTKMBS62DR.mediatek.inc (172.29.94.18) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 5 Jul 2021 03:33:27 -0700 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 X-MTK: N Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=HkqXizva; spf=pass (imf22.hostedemail.com: domain of yee.lee@mediatek.com designates 216.200.240.184 as permitted sender) smtp.mailfrom=yee.lee@mediatek.com; dmarc=pass (policy=none) header.from=mediatek.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 7254C19A2 X-Stat-Signature: kqwrxzbp7wtc8dmdeafyikwbh5d1kta3 X-HE-Tag: 1625481214-513484 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: 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 Reviewed-by: Marco Elver Reviewed-by: Andrey Konovalov --- 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);