From patchwork Wed Jun 30 13:49:40 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: 12352259 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.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, URIBL_BLOCKED,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 8BAC9C11F65 for ; Wed, 30 Jun 2021 14:20:42 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 563BA6144B for ; Wed, 30 Jun 2021 14:20:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 563BA6144B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=lQqTYpDm1GWh/60ovina/7Z6jnaYuJL8wVoCr3pg0js=; b=1ilVYotHHDmOtD UcCpoKLChx3sj2aoxLulJzpCRO0HVEYa5oUpwl4jjaHkR2h3u5+59Z0lgFPJlcKq+c8ltmxGtFtXS 0Rqv7mnkOG5eQNMBqJ0tnuN0oQR7z+HAxB3A7LUGHILPqmwUt692G64Fmi0S0nYmH4i4YRU0fH7t8 PTyYCOeDMOCX2UtKcHHsJ0YyYQTsHrr72RKEexEdaWkDu8/V0HvDCZHh1KpfQOMB58CJqVq1bNP+y +d041kNwZrNtdClKy5lcpI2cI6cI06bNF/Z5f4+cC3c2M3aKOeFITwzj4OVzplvvjNDSrkVp5q255 ziOv1jmfRlQ/MotUCcVw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lyb4o-00EKmE-Eo; Wed, 30 Jun 2021 14:20:30 +0000 Received: from mailgw02.mediatek.com ([216.200.240.185]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lyal3-00EEiL-Jy; Wed, 30 Jun 2021 14:00:08 +0000 X-UUID: f4d15d8d3205492aad25730a73497fc9-20210630 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=iAdvjTkEqwlfrHns7lkpk95pCB2ZsD06CI4y7nRY1/U=; b=S2zxrmS5iyZRE6A7GCiDD7q96ho+a7jouKDJc7j02q6wTcqzLlKxIPIBvRqzu2hpnUkJdA0gZPDqBEoQal1Pz7jdcLH7EaVUjiP+ep5UAiVzufvxRpsLZqDSJBnlyCKKE1we0DtYkZBOQ4Jy+nhMNbGhv5FYt3YpmMuj4MQ5W3Q=; X-UUID: f4d15d8d3205492aad25730a73497fc9-20210630 Received: from mtkcas66.mediatek.inc [(172.29.193.44)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 460160406; Wed, 30 Jun 2021 07:00:01 -0700 Received: from MTKMBS02N2.mediatek.inc (172.21.101.101) by MTKMBS62N2.mediatek.inc (172.29.193.42) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 30 Jun 2021 06:49:59 -0700 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs02n2.mediatek.inc (172.21.101.101) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 30 Jun 2021 21:49:45 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Wed, 30 Jun 2021 21:49:45 +0800 From: To: CC: , Yee Lee , "Andrey Ryabinin" , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger , "open list:KASAN" , "open list:MEMORY MANAGEMENT" , open list , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" Subject: [PATCH v3 1/1] kasan: Add memzero init for unaligned size under SLUB debug Date: Wed, 30 Jun 2021 21:49:40 +0800 Message-ID: <20210630134943.20781-2-yee.lee@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20210630134943.20781-1-yee.lee@mediatek.com> References: <20210630134943.20781-1-yee.lee@mediatek.com> MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210630_070005_740207_8B4252A9 X-CRM114-Status: GOOD ( 14.39 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.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. Signed-off-by: Yee Lee Suggested-by: Marco Elver Suggested-by: Andrey Konovalov Cc: Andrey Ryabinin Cc: Alexander Potapenko Cc: Dmitry Vyukov Cc: Andrew Morton Signed-off-by: Marco Elver --- mm/kasan/kasan.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 8f450bc28045..6f698f13dbe6 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -387,6 +387,16 @@ 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 (IS_ENABLED(CONFIG_SLUB_DEBUG) && 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);