Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2553782rwb; Mon, 3 Oct 2022 02:27:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5AbVxk83aXtwCNBSTm3yQr/c49/VgsnixyNz8ljTpuM9qefojfje6aOHp/TwY8BymVXVBl X-Received: by 2002:aa7:d95a:0:b0:458:896b:3cb9 with SMTP id l26-20020aa7d95a000000b00458896b3cb9mr11892400eds.253.1664789258030; Mon, 03 Oct 2022 02:27:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664789258; cv=none; d=google.com; s=arc-20160816; b=N0jtRz5p1x4AkJwf4DtsT+kOVLYZzY51YeKprZ3Mw9f3Qrnc0PlSNCWGj2TTN2eSBT 2vAD3vNm4Nvwbl/zIOOef3CfXv85o+z7NW0k9dMxqbbswDPjbpGI1E8Aig/zPsRsuS+R rN+xA+a3oj/NmtPqZwC6YzPxk9hW5CVSloCENjZZkMRZ7NCI5mF+JBXmJ6pFtn970gAE oxl+VQOfWSLinFl3A8TCUpv/H6k/d4UxgPo6uMyqvR5ZjqVy+czGryK62RAE1y2UW8qn Abh+pz3QcFbKR46YjRo9OsJIJBAsR3qFoiLQFr5Ll8hV5hYAIHnGlpq9ZUR5Hbycc5t5 86kw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IoxapkNsL1Lccpw7nlUdEZUpnXACGmPj2qxrHbJAbak=; b=rsan02K30kL+Vxw3aaXl6W+51YBbTYMU0LwsL3K0Z39lStgrqsP9UgkWUKyQaxg7R/ 023SBKv+gaPlPLniyRA+DH1wS0IXuYm3/xIGXHEce1DguZPPxM7k3dt4t6/g0lu/Wfin NtCs0F2jyjb5IiXxtKMkaIwprQQVEOxS/igK3kIvXBJdqhNIb6oBX9rJ0WdLdMWKhdqe JNP9flt+5dnsFCJtmCF0EAoyo8xb8yQWwUHi/tdsib1RSHyAYttCjRk2Mt8J3bEgCdeD 8FCDQSYhJN6G99SyJOydzHIPOwP+Yqeypd717vvODksZ0KUo+9/ukgUSjCJi/UlDsvHI Wlfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yxtSLWW5; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o7-20020a17090608c700b0073de493b83esi7819142eje.147.2022.10.03.02.27.12; Mon, 03 Oct 2022 02:27:38 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yxtSLWW5; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229906AbiJCIwT (ORCPT + 99 others); Mon, 3 Oct 2022 04:52:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229747AbiJCIvv (ORCPT ); Mon, 3 Oct 2022 04:51:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9756C5A8AE; Mon, 3 Oct 2022 01:35:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id EFCCDB80E6F; Mon, 3 Oct 2022 07:17:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67261C433D6; Mon, 3 Oct 2022 07:17:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664781426; bh=yk41U2/x1V1jtnOx1oAS+KXJCG6Br0e4E/NOlvbPDTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yxtSLWW5PdKIGJjQxcO2DiMay9Eqk0h0kvKqgu8X3mIUSvw0GdkftiqNJyHFvO29O w8QrN1mgGWiiszoa59pY8POA4oOr9YdrhbjFfh10pAKCTQ92eAUE0xNcNG9qd8qsDa GT3/bSqd6QuBuNMZ+KmyeLpMn1YlEhlm4uzgcLm0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Menzel , Jarkko Sakkinen , Dave Hansen , Reinette Chatre Subject: [PATCH 5.15 16/83] x86/sgx: Do not fail on incomplete sanitization on premature stop of ksgxd Date: Mon, 3 Oct 2022 09:10:41 +0200 Message-Id: <20221003070722.390669625@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221003070721.971297651@linuxfoundation.org> References: <20221003070721.971297651@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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: Jarkko Sakkinen commit 133e049a3f8c91b175029fb6a59b6039d5e79cba upstream. Unsanitized pages trigger WARN_ON() unconditionally, which can panic the whole computer, if /proc/sys/kernel/panic_on_warn is set. In sgx_init(), if misc_register() fails or misc_register() succeeds but neither sgx_drv_init() nor sgx_vepc_init() succeeds, then ksgxd will be prematurely stopped. This may leave unsanitized pages, which will result a false warning. Refine __sgx_sanitize_pages() to return: 1. Zero when the sanitization process is complete or ksgxd has been requested to stop. 2. The number of unsanitized pages otherwise. Fixes: 51ab30eb2ad4 ("x86/sgx: Replace section->init_laundry_list with sgx_dirty_page_list") Reported-by: Paul Menzel Signed-off-by: Jarkko Sakkinen Signed-off-by: Dave Hansen Reviewed-by: Reinette Chatre Cc: stable@vger.kernel.org Link: https://lore.kernel.org/linux-sgx/20220825051827.246698-1-jarkko@kernel.org/T/#u Link: https://lkml.kernel.org/r/20220906000221.34286-2-jarkko@kernel.org Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/cpu/sgx/main.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -46,9 +46,13 @@ static LIST_HEAD(sgx_dirty_page_list); * Reset post-kexec EPC pages to the uninitialized state. The pages are removed * from the input list, and made available for the page allocator. SECS pages * prepending their children in the input list are left intact. + * + * Return 0 when sanitization was successful or kthread was stopped, and the + * number of unsanitized pages otherwise. */ -static void __sgx_sanitize_pages(struct list_head *dirty_page_list) +static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list) { + unsigned long left_dirty = 0; struct sgx_epc_page *page; LIST_HEAD(dirty); int ret; @@ -56,7 +60,7 @@ static void __sgx_sanitize_pages(struct /* dirty_page_list is thread-local, no need for a lock: */ while (!list_empty(dirty_page_list)) { if (kthread_should_stop()) - return; + return 0; page = list_first_entry(dirty_page_list, struct sgx_epc_page, list); @@ -71,12 +75,14 @@ static void __sgx_sanitize_pages(struct } else { /* The page is not yet clean - move to the dirty list. */ list_move_tail(&page->list, &dirty); + left_dirty++; } cond_resched(); } list_splice(&dirty, dirty_page_list); + return left_dirty; } static bool sgx_reclaimer_age(struct sgx_epc_page *epc_page) @@ -427,10 +433,7 @@ static int ksgxd(void *p) * required for SECS pages, whose child pages blocked EREMOVE. */ __sgx_sanitize_pages(&sgx_dirty_page_list); - __sgx_sanitize_pages(&sgx_dirty_page_list); - - /* sanity check: */ - WARN_ON(!list_empty(&sgx_dirty_page_list)); + WARN_ON(__sgx_sanitize_pages(&sgx_dirty_page_list)); while (!kthread_should_stop()) { if (try_to_freeze())