Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp341179pxv; Wed, 30 Jun 2021 06:54:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAOJJhEUwTMpdpoOTkByHLx/XsJctX6XwIeOpsbSYbMjZjYQpieimCnAInNi9+SsAq0PVn X-Received: by 2002:a5e:a612:: with SMTP id q18mr8003527ioi.76.1625061276647; Wed, 30 Jun 2021 06:54:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625061276; cv=none; d=google.com; s=arc-20160816; b=RFLQTISGk/5xSic6woVmH7ofF5a92BnEWLEDtLKIEjrlhJisVTHq/G7nlblaPC48hD FcUyVZAokQZq6BM8q21VyqTUpkqsCKiuipsAdF043UCa2U+yhQ2i71QS/fssbSgcDiVZ eC7SNJZlKDh3WfAAkWMHe5OR9g1/f/V3NXQIHPl+RruwqAQ6BU7fTwp4SDnb5HBOIYu4 R96TPALrt5jvm9Q9SqwyDDaS5/yCwtn0bNHhjP9GYf1PfZOlW+44H2oZObtRbnc708d7 ywPLLpHjcpy8ZvKbm8HCjrgNgF8SVvJT1ebMAsaMKrnv3Xn22tbQcAAHzK4nDLqJZJ5Y DjHQ== 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=CTq4kUSBN6FWuulf2iRn9qGOuD0hjmBJj1YRqK7dUBA=; b=M1byf+PjKQ88T8y61websyHfqZpudfVVaYm8OYXe3XOziG9xYLBTjHJxJ42OhjMoLw D0grccFdHaWCSrABdTzSnJTsj7gKMETKLDq+Aa6WjzjY4bl+CPu5Cqo7WK5aUzzPlaKm rYIdzy1XbHbc0Z1I/ytWlidmNUY7noJjwQhZu0N/PtO+UQR2FvMkKL31xbAboOmftKrQ /T8JVcBcjSGIsAOOJ0hpLQFZtGyCSeu1Ng4/pMWA3DdJBVemwFjCCyWDki4fevYRPkLq a3/Y6jpXu0nKN8rDCm3jXaiQxMyF79hExRfE4L2BQEf4YIrgM2OmZxcgDeOykGQ2sJlG S4VQ== 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 j16si21245605ioo.11.2021.06.30.06.54.24; Wed, 30 Jun 2021 06:54:36 -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 S236574AbhF3N4P (ORCPT + 99 others); Wed, 30 Jun 2021 09:56:15 -0400 Received: from mailgw02.mediatek.com ([210.61.82.184]:36743 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S235497AbhF3NwT (ORCPT ); Wed, 30 Jun 2021 09:52:19 -0400 X-UUID: e7d4bf8d56984e219f26e258a12eef49-20210630 X-UUID: e7d4bf8d56984e219f26e258a12eef49-20210630 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1842347633; Wed, 30 Jun 2021 21:49:47 +0800 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 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. 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 --- 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); -- 2.18.0