Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3475255rwa; Tue, 23 Aug 2022 05:29:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR6+yh79990XlMkbDmityXRNBNPg9lEQNfALq+DWh/dVqe56a+42FHjWmK/NuGi1PZN1bFzX X-Received: by 2002:a17:902:a982:b0:170:d646:5851 with SMTP id bh2-20020a170902a98200b00170d6465851mr24991802plb.134.1661257785300; Tue, 23 Aug 2022 05:29:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661257785; cv=none; d=google.com; s=arc-20160816; b=jo4KYT3FtNbUHYw1Vq2vCUQ7zOUJgAuD9gFfoVQRJ3VmprSnGfgsgL8A0S8EYdm3W4 dfmziDGyftXN/YBPoW78M8EiXvLTY/E1KCweSGaN9+c/ucBfv6GavNjDQ203G75/xaMh v3zrszkgSI6+eV7oTcBi2CzMm1USxagrrJpTsCcodWvuCQBgL63R9MezDYqfvQjhb4hn lua1uciv+PJ3WybjfV/NW8K6Q6Eua+S6EtIG0cA/2OjHEdt1VV3uS6F+flBcxSJNccLI 3J0jviNAig3kyd92Buvl0smPW5ae2i88SjiaWVvnh6kh16iQ546WfOR0DKfJ0xyJPP12 DsdA== 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=+V1NeUXkkLfCw3NP4ZefodrqqQ8ir0NLZukOQu87kcA=; b=PgXJVn4hGxUCWA+uLtKJx0ze6y5dJPYABmg/emQiw6P5wHkSoKD4wrF0/u4F9Abi5k XRx3r89U6CigFfyTycoJt2OdquDiEGwqVuAgqL7LcDaqrjJxHqpohufmBYBqIyiO2Tb1 m85rgx93aJfm3Hpsu6x9HPJgV8VN7AWrrLAFyAaTJAjm1ooES5So3JwsTZ6NpZyAsjRB 4Vsl8bp+YOJuaPSjj7dX6cHN0fxh2I63VA2hrGQroOoRQtg5m4DceZK9ivfFEUDFRKJN L6bE6EequJKIg7yhYBxFsv9JfZpAFc1dOZUvSQXZBMu+ZHZY69ctxVMXCPMisHxV3//0 FgbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="fG/L3atY"; 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 p18-20020a056a000a1200b005289628be08si14159821pfh.336.2022.08.23.05.29.34; Tue, 23 Aug 2022 05:29:45 -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="fG/L3atY"; 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 S1355737AbiHWKg7 (ORCPT + 99 others); Tue, 23 Aug 2022 06:36:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354774AbiHWKWL (ORCPT ); Tue, 23 Aug 2022 06:22:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3346BC1E; Tue, 23 Aug 2022 02:03:32 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 2530C61499; Tue, 23 Aug 2022 09:03:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B690C433D6; Tue, 23 Aug 2022 09:03:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661245411; bh=PcYROYEH61UkQAPOT/SlqQLXKep9xpR3tyBm9FxnUmA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fG/L3atYgcWIW1sSk8Wetxshgri+t0mNIbI96Rv0BCRvH5gE1zuXf/quCtJM4VzRK r51dWUXIdZYJQaGh6ZeVw8sAbK8kbnuzcp3B7Ut8cTqs4VwhGxeWUci6azETH+Dp3k GhODY7oxSffQe5i4xa1VDs1at/18/qvfS4Lej/OM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, hewenliang , Haibin Zhang , Catalin Marinas , Will Deacon , Sasha Levin Subject: [PATCH 4.19 040/287] arm64: fix oops in concurrently setting insn_emulation sysctls Date: Tue, 23 Aug 2022 10:23:29 +0200 Message-Id: <20220823080101.614892352@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080100.268827165@linuxfoundation.org> References: <20220823080100.268827165@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,T_SCC_BODY_TEXT_LINE 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: haibinzhang (张海斌) [ Upstream commit af483947d472eccb79e42059276c4deed76f99a6 ] emulation_proc_handler() changes table->data for proc_dointvec_minmax and can generate the following Oops if called concurrently with itself: | Unable to handle kernel NULL pointer dereference at virtual address 0000000000000010 | Internal error: Oops: 96000006 [#1] SMP | Call trace: | update_insn_emulation_mode+0xc0/0x148 | emulation_proc_handler+0x64/0xb8 | proc_sys_call_handler+0x9c/0xf8 | proc_sys_write+0x18/0x20 | __vfs_write+0x20/0x48 | vfs_write+0xe4/0x1d0 | ksys_write+0x70/0xf8 | __arm64_sys_write+0x20/0x28 | el0_svc_common.constprop.0+0x7c/0x1c0 | el0_svc_handler+0x2c/0xa0 | el0_svc+0x8/0x200 To fix this issue, keep the table->data as &insn->current_mode and use container_of() to retrieve the insn pointer. Another mutex is used to protect against the current_mode update but not for retrieving insn_emulation as table->data is no longer changing. Co-developed-by: hewenliang Signed-off-by: hewenliang Signed-off-by: Haibin Zhang Reviewed-by: Catalin Marinas Link: https://lore.kernel.org/r/20220128090324.2727688-1-hewenliang4@huawei.com Link: https://lore.kernel.org/r/9A004C03-250B-46C5-BF39-782D7551B00E@tencent.com Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/kernel/armv8_deprecated.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kernel/armv8_deprecated.c b/arch/arm64/kernel/armv8_deprecated.c index 181c29af5617..7c69a203cdf8 100644 --- a/arch/arm64/kernel/armv8_deprecated.c +++ b/arch/arm64/kernel/armv8_deprecated.c @@ -62,6 +62,7 @@ struct insn_emulation { static LIST_HEAD(insn_emulation); static int nr_insn_emulated __initdata; static DEFINE_RAW_SPINLOCK(insn_emulation_lock); +static DEFINE_MUTEX(insn_emulation_mutex); static void register_emulation_hooks(struct insn_emulation_ops *ops) { @@ -210,10 +211,10 @@ static int emulation_proc_handler(struct ctl_table *table, int write, loff_t *ppos) { int ret = 0; - struct insn_emulation *insn = (struct insn_emulation *) table->data; + struct insn_emulation *insn = container_of(table->data, struct insn_emulation, current_mode); enum insn_emulation_mode prev_mode = insn->current_mode; - table->data = &insn->current_mode; + mutex_lock(&insn_emulation_mutex); ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); if (ret || !write || prev_mode == insn->current_mode) @@ -226,7 +227,7 @@ static int emulation_proc_handler(struct ctl_table *table, int write, update_insn_emulation_mode(insn, INSN_UNDEF); } ret: - table->data = insn; + mutex_unlock(&insn_emulation_mutex); return ret; } @@ -250,7 +251,7 @@ static void __init register_insn_emulation_sysctl(void) sysctl->maxlen = sizeof(int); sysctl->procname = insn->ops->name; - sysctl->data = insn; + sysctl->data = &insn->current_mode; sysctl->extra1 = &insn->min; sysctl->extra2 = &insn->max; sysctl->proc_handler = emulation_proc_handler; -- 2.35.1