Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2514178pxb; Sun, 24 Apr 2022 17:28:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/WsCd+bzekaI+TYs4xiRIUvhNXJAaxxZJHsb/3e8AxSpsZZp4e3aQvkfQF/0OtwnvNLVK X-Received: by 2002:a63:fd43:0:b0:39c:d17a:62af with SMTP id m3-20020a63fd43000000b0039cd17a62afmr12925461pgj.450.1650846538410; Sun, 24 Apr 2022 17:28:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650846538; cv=none; d=google.com; s=arc-20160816; b=BYn5xcHFVhvoUmSrAqikb98yiWo6EcO1OvF71PP2bAOQ8MfYfaWVNC6WZnU3+Av/F5 1Mu1c7vD5reoM2KVg3FXwq12iFLux+THOTVUbDZ/rWDDU4M6qCPfuWZDKSbRDJRoj4v8 QULTJxtmcnUayvkOkGoPp6HIahImYVuhi6Ib6FOHoA30hlur84OljDv1DFU+tM/juMp/ MGmrDIdND7I9Lxvs+ACZqfw7y+6W2wZP+wIc8ovZD/Ng3V/3EHFkXgaZ8JeG3rXaXtX3 /a9kh5rCa2DuzpJ+10bCDxPvFYqsi5wDkZrBt5HEgmJfNDowH/4JONHB1q/LKmEc/ORd eN1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=qHpngez5DphifoPrXNbj8mL8YKDsg5IJNceMCO923So=; b=ReQjubu5ooihDLq7JiBIeMICtVa9FvUvnU1Ui57N2e8C7k/yTFmcerFumm/Js5dhvS PlBL2OeVcafrG0s6TDSqFwr2dWa+DEJoh0/4wFpKP6oQdYQU8Z6ZbSAqPs2AMssIa38O D1jwPtuFA7QBtxAXW/DHcfbACdk1AKBUP/HpuJtFYk4Z18xubpJWlQn/5T20fap3+GJk P7r+Cw0gp+aUJh+JVoCvOS6bca6SCxH55xKXGp1mYP8i6MYZY++j9BWtg5T2zSww2ic/ BDdrQGUR1vgEJFlX4+OLrhLjXu352UX3+kIr5c6EdDHV7IAMb193X2ueDO2qXf/ZqLbX /WDQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s13-20020a056a00194d00b004faa8dcd125si15466484pfk.72.2022.04.24.17.28.30; Sun, 24 Apr 2022 17:28:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239203AbiDXLDc (ORCPT + 99 others); Sun, 24 Apr 2022 07:03:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239199AbiDXLDb (ORCPT ); Sun, 24 Apr 2022 07:03:31 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0162CD63 for ; Sun, 24 Apr 2022 04:00:28 -0700 (PDT) Received: from kwepemi100005.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4KmQCc1z70zhYT6; Sun, 24 Apr 2022 19:00:12 +0800 (CST) Received: from kwepemm600020.china.huawei.com (7.193.23.147) by kwepemi100005.china.huawei.com (7.221.188.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sun, 24 Apr 2022 19:00:26 +0800 Received: from DESKTOP-E0KHRBE.china.huawei.com (10.67.111.5) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sun, 24 Apr 2022 19:00:25 +0800 From: Shaobo Huang To: , , , , , , CC: , , , , , , , Subject: [PATCH v2] kfence: enable check kfence canary in panic via boot param Date: Sun, 24 Apr 2022 18:59:49 +0800 Message-ID: <20220424105949.50016-1-huangshaobo6@huawei.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.111.5] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600020.china.huawei.com (7.193.23.147) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: huangshaobo when writing out of bounds to the red zone, it can only be detected at kfree. However, the system may have been reset before freeing the memory, which would result in undetected oob. Therefore, it is necessary to detect oob behavior in panic. Since only the allocated mem call stack is available, it may be difficult to find the oob maker. Therefore, this feature is disabled by default and can only be enabled via boot parameter. Suggested-by: chenzefeng Signed-off-by: huangshaobo --- v2: - it is only detected in panic. - it is disabled by default. - can only be enabled via boot parameter. - the code is moved to the specified partition. Thanks to Marco for the valuable modification suggestion. --- mm/kfence/core.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 9b2b5f56f4ae..0b2b934a1666 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include @@ -99,6 +101,10 @@ module_param_named(skip_covered_thresh, kfence_skip_covered_thresh, ulong, 0644) static bool kfence_deferrable __read_mostly = IS_ENABLED(CONFIG_KFENCE_DEFERRABLE); module_param_named(deferrable, kfence_deferrable, bool, 0444); +/* If true, check kfence canary in panic. */ +static bool kfence_check_on_panic; +module_param_named(check_on_panic, kfence_check_on_panic, bool, 0444); + /* The pool of pages used for guard pages and objects. */ char *__kfence_pool __read_mostly; EXPORT_SYMBOL(__kfence_pool); /* Export for test modules. */ @@ -727,6 +733,30 @@ static int __init kfence_debugfs_init(void) late_initcall(kfence_debugfs_init); +/* === Panic Notifier ====================================================== */ +static void kfence_check_all_canary(void) +{ + int i; + + for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) { + struct kfence_metadata *meta = &kfence_metadata[i]; + + if (meta->state == KFENCE_OBJECT_ALLOCATED) + for_each_canary(meta, check_canary_byte); + } +} + +static int kfence_check_canary_callback(struct notifier_block *nb, + unsigned long reason, void *arg) +{ + kfence_check_all_canary(); + return NOTIFY_OK; +} + +static struct notifier_block kfence_check_canary_notifier = { + .notifier_call = kfence_check_canary_callback, +}; + /* === Allocation Gate Timer ================================================ */ static struct delayed_work kfence_timer; @@ -804,6 +834,9 @@ static void kfence_init_enable(void) else INIT_DELAYED_WORK(&kfence_timer, toggle_allocation_gate); + if (kfence_check_on_panic) + atomic_notifier_chain_register(&panic_notifier_list, &kfence_check_canary_notifier); + WRITE_ONCE(kfence_enabled, true); queue_delayed_work(system_unbound_wq, &kfence_timer, 0); -- 2.12.3