From mboxrd@z Thu Jan 1 00:00:00 1970 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=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 366D4C07E9B for ; Mon, 5 Jul 2021 11:23:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 17798613B9 for ; Mon, 5 Jul 2021 11:23:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231241AbhGEL0X (ORCPT ); Mon, 5 Jul 2021 07:26:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230174AbhGEL0W (ORCPT ); Mon, 5 Jul 2021 07:26:22 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE82BC061574 for ; Mon, 5 Jul 2021 04:23:45 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id s15so23231960edt.13 for ; Mon, 05 Jul 2021 04:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=YM1PR4H9+B8L8UwdtxpjutwB0rqH38R2O5DOrey1dK4UErgqysTvI+jj6ATz2yvQWG hPXFPXCn4AxF4al8wuVWBHAWFU7Zamp+OpUBbIdBbO9vryr5tC6J3hYvjIK4M5xkTxyw A6kPbU9iiglpyYbCk5dZ4DYIAov+znHDcB5iFbZO/U7EftfericcwXSaBDEw8t31BjCB FncrlAL3zbRDQr3iMPIlPGF1PLZFfHZO+mEOgJputwH/D57Np0JF5OI1UQ/+JiypcY6R rbNYFR9vSubxKeS0TvansMdId29zqxKXA6SrrOpfk7BzauMC51wndZlwa25vNNa8vSvn jAdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=QRHsl5KtsCPZJB2nRDpoNo6ZLEjqi5SKhtZRsAwlR4pQTxW5Y5B1fUtIw71lB7U9Xe 7FnqtbFSnqz5slMCrzi7AZBO7BeM+Jwr/6FH9n90yuDa6TFW206ndKGY0mUzC7a1R0QO O2WaTPDDi2zeJEOMUGBUgG/lt4AS6Is/NWzqTNUDl4wOZYm+j3+Ix8bVquSsM2mBqiB+ AqasOb8hSSNRBFK+2DewVVw1qujuwI4mrvFI7I9O2wcgMHuY5gbEzzv7nS4lYSv8mNJ9 +Yx8nhVOTSGDPxzUdqdNDwn2ydrCKBXNTbABvhkxUDWnHmmJuR8QpyhPQdNZsXvHV+M5 7vbg== X-Gm-Message-State: AOAM531P2NlGayRlXuIyhgQQvLTadNKvISKfU8SHVOx9OfcKkA3rNXKu NNtw62zUv7d7J+uteahbBoBa0DLYpn8q1SnKdS0= X-Google-Smtp-Source: ABdhPJxpa2IOlQc2LEHppS6wdYTu08dyDoHQP1v4Cdpbx6OBtbIxpdca+P2eecENgraXfPvnlr+f9YlLyHcZWvqk2Eo= X-Received: by 2002:a50:fb96:: with SMTP id e22mr8621005edq.95.1625484224390; Mon, 05 Jul 2021 04:23:44 -0700 (PDT) MIME-Version: 1.0 References: <20210705103229.8505-1-yee.lee@mediatek.com> <20210705103229.8505-3-yee.lee@mediatek.com> In-Reply-To: <20210705103229.8505-3-yee.lee@mediatek.com> From: Andrey Konovalov Date: Mon, 5 Jul 2021 13:23:33 +0200 Message-ID: Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG To: yee.lee@mediatek.com Cc: LKML , nicholas.Tang@mediatek.com, Kuan-Ying Lee , chinwen.chang@mediatek.com, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger , "open list:KASAN" , "open list:MEMORY MANAGEMENT" , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 5, 2021 at 12:33 PM wrote: > > 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 > Reviewed-by: Andrey Konovalov From mboxrd@z Thu Jan 1 00:00:00 1970 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=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 0B0B5C07E99 for ; Mon, 5 Jul 2021 11:23:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9C3016135F for ; Mon, 5 Jul 2021 11:23:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9C3016135F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 675286B006C; Mon, 5 Jul 2021 07:23:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 659C16B0070; Mon, 5 Jul 2021 07:23:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4ECCC6B0070; Mon, 5 Jul 2021 07:23:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0055.hostedemail.com [216.40.44.55]) by kanga.kvack.org (Postfix) with ESMTP id 2E6A16B005D for ; Mon, 5 Jul 2021 07:23:46 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A23018248D52 for ; Mon, 5 Jul 2021 11:23:45 +0000 (UTC) X-FDA: 78328299210.28.D3E3EF3 Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) by imf13.hostedemail.com (Postfix) with ESMTP id 5F9361000F41 for ; Mon, 5 Jul 2021 11:23:45 +0000 (UTC) Received: by mail-ed1-f46.google.com with SMTP id x2so4823766edr.10 for ; Mon, 05 Jul 2021 04:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=YM1PR4H9+B8L8UwdtxpjutwB0rqH38R2O5DOrey1dK4UErgqysTvI+jj6ATz2yvQWG hPXFPXCn4AxF4al8wuVWBHAWFU7Zamp+OpUBbIdBbO9vryr5tC6J3hYvjIK4M5xkTxyw A6kPbU9iiglpyYbCk5dZ4DYIAov+znHDcB5iFbZO/U7EftfericcwXSaBDEw8t31BjCB FncrlAL3zbRDQr3iMPIlPGF1PLZFfHZO+mEOgJputwH/D57Np0JF5OI1UQ/+JiypcY6R rbNYFR9vSubxKeS0TvansMdId29zqxKXA6SrrOpfk7BzauMC51wndZlwa25vNNa8vSvn jAdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=N3l5RXteZBJ11pSEi0u/b/dbhn5Z9pbZa8CcVWNzkHd4/7hu1ozhfdAhh+TBAZ0hQB f8qhNIIroXThWLqkxQsZ/jDGv7oDSuYgvWGrTrq63Ywekj7cvdeAyuS6hY7LWRIEz/NJ 1xKFBw0V9omM6bKiQOAwqyFZ5WFSi5xVPXOpZFwsGWpc+wh21obzSqIUGUsyYlH7bXBz ZS0k4edgJu+eGvJLvrqglAQ0FYf3WYv0JVN2QkKdkmZLgSRJyX+JPujZGRnQ7jt21Cuf ivgQqu6qBWA3TtPKvU70s4/zKg7zR8JTG5Ryg7CQQObJYYoji4wkXe45jzfTWeghdCgp FSmw== X-Gm-Message-State: AOAM532+BRwRYT3XD4xpXdZzIl4G4BVQgxScYEktKSrbPLhsUc3MTIuy ZQ1w9iOOOQKwVjeQmY3oeFvxrYPeNI1MCgzzyU8= X-Google-Smtp-Source: ABdhPJxpa2IOlQc2LEHppS6wdYTu08dyDoHQP1v4Cdpbx6OBtbIxpdca+P2eecENgraXfPvnlr+f9YlLyHcZWvqk2Eo= X-Received: by 2002:a50:fb96:: with SMTP id e22mr8621005edq.95.1625484224390; Mon, 05 Jul 2021 04:23:44 -0700 (PDT) MIME-Version: 1.0 References: <20210705103229.8505-1-yee.lee@mediatek.com> <20210705103229.8505-3-yee.lee@mediatek.com> In-Reply-To: <20210705103229.8505-3-yee.lee@mediatek.com> From: Andrey Konovalov Date: Mon, 5 Jul 2021 13:23:33 +0200 Message-ID: Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG To: yee.lee@mediatek.com Cc: LKML , nicholas.Tang@mediatek.com, Kuan-Ying Lee , chinwen.chang@mediatek.com, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger , "open list:KASAN" , "open list:MEMORY MANAGEMENT" , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5F9361000F41 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=YM1PR4H9; spf=pass (imf13.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.208.46 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: 18j4yjq7m8z8twi1dmyazggzno3djxoe X-HE-Tag: 1625484225-691019 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: On Mon, Jul 5, 2021 at 12:33 PM wrote: > > 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 > Reviewed-by: Andrey Konovalov From mboxrd@z Thu Jan 1 00:00:00 1970 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=-10.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 BC72DC07E99 for ; Mon, 5 Jul 2021 11:24:15 +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 88F68613D8 for ; Mon, 5 Jul 2021 11:24:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 88F68613D8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.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:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=A2WxF7w/nfgZMmZmB/9LGqhO3pt1eMKmJdhf3kmNpUA=; b=yy/xsDQvz0LWI3 iC0IinDv+Gz39oJIJMoK+Rldv3w4AP3VGUgZbZwBjvmrwUdvhudgGiqBB0QsCeD6tVLLlVwsplrvY wJvjhbwDDgOM91KcQo7qQEKq5KC3PvJcg7+IqagQMSRo+eS1YsjTmCPVEn5o8Z5Xk8WeV5maoDxf3 aEL21gqHIQkqWoXwTPwwqvJ8mJi9ef7fohxosFWQ28bk9p7bxo+gi8WGmJqpQqiYoDkYnVvypFi4R 5ADPDSfDKLTqFc9/vcVIhqSbWrHhJzt/9bCNbcaOFn9auNbbkDNX31W71bp+Igonx/m9X8WVzVZ+I K+ximGg/G9bRPcGoNt6Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m0Mho-008kMw-Qg; Mon, 05 Jul 2021 11:24:04 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m0MhV-008kH9-Qb; Mon, 05 Jul 2021 11:23:47 +0000 Received: by mail-ed1-x52f.google.com with SMTP id x12so23255989eds.5; Mon, 05 Jul 2021 04:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=YM1PR4H9+B8L8UwdtxpjutwB0rqH38R2O5DOrey1dK4UErgqysTvI+jj6ATz2yvQWG hPXFPXCn4AxF4al8wuVWBHAWFU7Zamp+OpUBbIdBbO9vryr5tC6J3hYvjIK4M5xkTxyw A6kPbU9iiglpyYbCk5dZ4DYIAov+znHDcB5iFbZO/U7EftfericcwXSaBDEw8t31BjCB FncrlAL3zbRDQr3iMPIlPGF1PLZFfHZO+mEOgJputwH/D57Np0JF5OI1UQ/+JiypcY6R rbNYFR9vSubxKeS0TvansMdId29zqxKXA6SrrOpfk7BzauMC51wndZlwa25vNNa8vSvn jAdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=A6dYpbglpZT8mhluF0ZG0EAajZd0RRjVPNKExZKmi1DwgMow+81v6mRU7zNixC9gLL ll3aVL2TDaoCWN2tJLhfFgxQEvePEikMy/5Iq3ArAieV6ib+ltjJRKQII/8/reHxHpUj wnXHuWvEim0DtcPEILyXR93XFhDCpj3PEvEcKlWaQCzosN1UP4WQ1KMxM5UNvZWVuHgc +nb8v/tIcxeIhaqHb4qUDZ6XM0P6KcJCG9e4P9k2GJw2mAekloxk43xrKckmaTJLDOMn tkpqu4KJ5ivJ5TK7ErmFmsDiPOyPPXm/4QW0sjHQXsYf2TYuosgGHAyTBTTHQLfgx4iL X3xA== X-Gm-Message-State: AOAM530D2xRNgti5TBwGwftny6D8zR0esrOOdm4cgOBab/6G77fUlIHZ 0bkmfG6QeqoM90UsBwtcHhbjyafyU6mN/C7n6os= X-Google-Smtp-Source: ABdhPJxpa2IOlQc2LEHppS6wdYTu08dyDoHQP1v4Cdpbx6OBtbIxpdca+P2eecENgraXfPvnlr+f9YlLyHcZWvqk2Eo= X-Received: by 2002:a50:fb96:: with SMTP id e22mr8621005edq.95.1625484224390; Mon, 05 Jul 2021 04:23:44 -0700 (PDT) MIME-Version: 1.0 References: <20210705103229.8505-1-yee.lee@mediatek.com> <20210705103229.8505-3-yee.lee@mediatek.com> In-Reply-To: <20210705103229.8505-3-yee.lee@mediatek.com> From: Andrey Konovalov Date: Mon, 5 Jul 2021 13:23:33 +0200 Message-ID: Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG To: yee.lee@mediatek.com Cc: LKML , nicholas.Tang@mediatek.com, Kuan-Ying Lee , chinwen.chang@mediatek.com, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger , "open list:KASAN" , "open list:MEMORY MANAGEMENT" , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210705_042345_912544_585100C8 X-CRM114-Status: GOOD ( 21.82 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org On Mon, Jul 5, 2021 at 12:33 PM wrote: > > 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 > Reviewed-by: Andrey Konovalov _______________________________________________ Linux-mediatek mailing list Linux-mediatek@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-mediatek From mboxrd@z Thu Jan 1 00:00:00 1970 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=-10.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 00E8EC07E9C for ; Mon, 5 Jul 2021 11:25:27 +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 C40B2613C2 for ; Mon, 5 Jul 2021 11:25:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C40B2613C2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=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:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=CB50wBPtMjwZnmtcJCsNj6K6koScpnQVJAokuMCggNI=; b=fKwVt/b88YzQT/ YvCzXPHL5FDTxPaZNLoV890OawyHL+QK8hbdUul2JRqdvGnlrRbUyAg9mvUCio3W9E27fNeoZDmOV q7lbtTUxJ89QGDrzPVx0MZxm6jBdn6Jt70GA2f7rkt+TpM+VZp3snS3L56Nk9FsFlnK7DxrCyNBki AgbDB3f31WVAknYapOmkhOoNZYlhaLp/do/g+A4p1cW0MCljvJ/S93phaJsL4rNuMI+tTw8Bnxf88 kiDYS8g1OrOY+jQzME5za+5bozwM6r6g7+dzTr2pmsuR/lZ1ft+X7Lu68VtHGQisI17vuFF5J7y1f aNBQhdP1Cuh3qu7vEU6w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m0Mha-008kIn-Eu; Mon, 05 Jul 2021 11:23:50 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m0MhV-008kH9-Qb; Mon, 05 Jul 2021 11:23:47 +0000 Received: by mail-ed1-x52f.google.com with SMTP id x12so23255989eds.5; Mon, 05 Jul 2021 04:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=YM1PR4H9+B8L8UwdtxpjutwB0rqH38R2O5DOrey1dK4UErgqysTvI+jj6ATz2yvQWG hPXFPXCn4AxF4al8wuVWBHAWFU7Zamp+OpUBbIdBbO9vryr5tC6J3hYvjIK4M5xkTxyw A6kPbU9iiglpyYbCk5dZ4DYIAov+znHDcB5iFbZO/U7EftfericcwXSaBDEw8t31BjCB FncrlAL3zbRDQr3iMPIlPGF1PLZFfHZO+mEOgJputwH/D57Np0JF5OI1UQ/+JiypcY6R rbNYFR9vSubxKeS0TvansMdId29zqxKXA6SrrOpfk7BzauMC51wndZlwa25vNNa8vSvn jAdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=x/twNUpcgioeZ8e0ihy4OvgQsOjAP9TO2paDyac3Qkg=; b=A6dYpbglpZT8mhluF0ZG0EAajZd0RRjVPNKExZKmi1DwgMow+81v6mRU7zNixC9gLL ll3aVL2TDaoCWN2tJLhfFgxQEvePEikMy/5Iq3ArAieV6ib+ltjJRKQII/8/reHxHpUj wnXHuWvEim0DtcPEILyXR93XFhDCpj3PEvEcKlWaQCzosN1UP4WQ1KMxM5UNvZWVuHgc +nb8v/tIcxeIhaqHb4qUDZ6XM0P6KcJCG9e4P9k2GJw2mAekloxk43xrKckmaTJLDOMn tkpqu4KJ5ivJ5TK7ErmFmsDiPOyPPXm/4QW0sjHQXsYf2TYuosgGHAyTBTTHQLfgx4iL X3xA== X-Gm-Message-State: AOAM530D2xRNgti5TBwGwftny6D8zR0esrOOdm4cgOBab/6G77fUlIHZ 0bkmfG6QeqoM90UsBwtcHhbjyafyU6mN/C7n6os= X-Google-Smtp-Source: ABdhPJxpa2IOlQc2LEHppS6wdYTu08dyDoHQP1v4Cdpbx6OBtbIxpdca+P2eecENgraXfPvnlr+f9YlLyHcZWvqk2Eo= X-Received: by 2002:a50:fb96:: with SMTP id e22mr8621005edq.95.1625484224390; Mon, 05 Jul 2021 04:23:44 -0700 (PDT) MIME-Version: 1.0 References: <20210705103229.8505-1-yee.lee@mediatek.com> <20210705103229.8505-3-yee.lee@mediatek.com> In-Reply-To: <20210705103229.8505-3-yee.lee@mediatek.com> From: Andrey Konovalov Date: Mon, 5 Jul 2021 13:23:33 +0200 Message-ID: Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG To: yee.lee@mediatek.com Cc: LKML , nicholas.Tang@mediatek.com, Kuan-Ying Lee , chinwen.chang@mediatek.com, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger , "open list:KASAN" , "open list:MEMORY MANAGEMENT" , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210705_042345_912544_585100C8 X-CRM114-Status: GOOD ( 21.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, Jul 5, 2021 at 12:33 PM wrote: > > 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 > Reviewed-by: Andrey Konovalov _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel