Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1156903ybf; Thu, 27 Feb 2020 06:04:57 -0800 (PST) X-Google-Smtp-Source: APXvYqxlUTTS6MMneXRUJVFl/jr3vX8V5ijZdFw7RZfq5x8Cw0WDKNccorAlYYfJ+8hPwlnYcEPw X-Received: by 2002:a9d:12a2:: with SMTP id g31mr3535455otg.283.1582812297253; Thu, 27 Feb 2020 06:04:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582812297; cv=none; d=google.com; s=arc-20160816; b=tFrR6aomCT0vlCMCxGY/up3b7YsVLGz1yBhHHzc4JgCmWzAjdLdFNehLVWNymqb6Bd QdWQHyAUbpaQQPNS/pq7zi/oxchyJ8X89vQmh2YTcjU8XMMyN4xXAt+c5EdzxPqxQ7on UMWCEDpeBxUW+7HXrRBAV4ROhgF5Dz7+b+xe1V8kXLiDVvwjmikKlFC6/1ppDLu+pOZn MtGXnm4kx08nK+FGG0T8pfifsosGGMIe0Ir7nlG4EVLhxN1+5mcKmrNqKMijfABAWFkM jDAW8rg7IDrTdlXc2DaH11WhI32PKGw2ScV8bOMv4FVsLVMC3XYzBz/2x/P2PxdEBW8m ltEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=rUkwNqrFfoGgdne2CKlA34SYqMYsRTzHaeslhI8/6Wg=; b=i0EfTOCCsqZ9EroCkA57ZufaAtRcYp4ufcWj16YVT2hyrLCTuZtSIphJk5GOPt5Ly8 557fKSG8eoieRNeHDffq8+nCNXYNq8hCbL3VRuXQTRMr6v/WO7HZaHRBXSi8QeCvASaP jMg4EiLQcSW6F7cRf+NHXSz66z7TSRyNg93r8xqviAyzJ9MigtaY1lAKJoHgMuCuA+Mw QAhaQmEU8TxQuWd/VKz6RgA4Eyz+HOVZvtPjzUU7cv2T9kCuWzplwMNAG26u70C0pIFO YQh5Usl9/LVg5ddGAj3zWA+Oq+vmRHPFeDVGxIlhtNZ4bmgIgzgvJmbWHH3+mVysy+4Y irag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KwNRhbTa; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g26si1520517otk.324.2020.02.27.06.04.36; Thu, 27 Feb 2020 06:04:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KwNRhbTa; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387521AbgB0OEG (ORCPT + 99 others); Thu, 27 Feb 2020 09:04:06 -0500 Received: from mail.kernel.org ([198.145.29.99]:39662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387491AbgB0ODz (ORCPT ); Thu, 27 Feb 2020 09:03:55 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 45D5020578; Thu, 27 Feb 2020 14:03:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582812234; bh=mrtOhv2bAUzWQQXwPb0/OSm7lZmeSixm0DRUqFTk9Mo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KwNRhbTaKEY3yFa6X0LlSMhZ5TOdyR0Caoup2c/YVX4IYAAtOvCPasarJr8D9ql2O wGkjCHun35oK5cOHOiKxCVzkxmMGQwAoPrmnEqPdSaUKHXlZDo8o+pqDC1BJjFCyHO Owz91VG5lvF7m816Qv4DUBLPBdm9petMqgoGTEU4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Saar Amar , Dan Carpenter , Borislav Petkov Subject: [PATCH 4.19 37/97] x86/mce/amd: Publish the bank pointer only after setup has succeeded Date: Thu, 27 Feb 2020 14:36:45 +0100 Message-Id: <20200227132220.626313876@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227132214.553656188@linuxfoundation.org> References: <20200227132214.553656188@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov commit 6e5cf31fbe651bed7ba1df768f2e123531132417 upstream. threshold_create_bank() creates a bank descriptor per MCA error thresholding counter which can be controlled over sysfs. It publishes the pointer to that bank in a per-CPU variable and then goes on to create additional thresholding blocks if the bank has such. However, that creation of additional blocks in allocate_threshold_blocks() can fail, leading to a use-after-free through the per-CPU pointer. Therefore, publish that pointer only after all blocks have been setup successfully. Fixes: 019f34fccfd5 ("x86, MCE, AMD: Move shared bank to node descriptor") Reported-by: Saar Amar Reported-by: Dan Carpenter Signed-off-by: Borislav Petkov Cc: Link: http://lkml.kernel.org/r/20200128140846.phctkvx5btiexvbx@kili.mountain Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/cpu/mcheck/mce_amd.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) --- a/arch/x86/kernel/cpu/mcheck/mce_amd.c +++ b/arch/x86/kernel/cpu/mcheck/mce_amd.c @@ -1152,8 +1152,9 @@ static const char *get_name(unsigned int return buf_mcatype; } -static int allocate_threshold_blocks(unsigned int cpu, unsigned int bank, - unsigned int block, u32 address) +static int allocate_threshold_blocks(unsigned int cpu, struct threshold_bank *tb, + unsigned int bank, unsigned int block, + u32 address) { struct threshold_block *b = NULL; u32 low, high; @@ -1197,16 +1198,12 @@ static int allocate_threshold_blocks(uns INIT_LIST_HEAD(&b->miscj); - if (per_cpu(threshold_banks, cpu)[bank]->blocks) { - list_add(&b->miscj, - &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj); - } else { - per_cpu(threshold_banks, cpu)[bank]->blocks = b; - } + if (tb->blocks) + list_add(&b->miscj, &tb->blocks->miscj); + else + tb->blocks = b; - err = kobject_init_and_add(&b->kobj, &threshold_ktype, - per_cpu(threshold_banks, cpu)[bank]->kobj, - get_name(bank, b)); + err = kobject_init_and_add(&b->kobj, &threshold_ktype, tb->kobj, get_name(bank, b)); if (err) goto out_free; recurse: @@ -1214,7 +1211,7 @@ recurse: if (!address) return 0; - err = allocate_threshold_blocks(cpu, bank, block, address); + err = allocate_threshold_blocks(cpu, tb, bank, block, address); if (err) goto out_free; @@ -1299,8 +1296,6 @@ static int threshold_create_bank(unsigne goto out_free; } - per_cpu(threshold_banks, cpu)[bank] = b; - if (is_shared_bank(bank)) { refcount_set(&b->cpus, 1); @@ -1311,9 +1306,13 @@ static int threshold_create_bank(unsigne } } - err = allocate_threshold_blocks(cpu, bank, 0, msr_ops.misc(bank)); - if (!err) - goto out; + err = allocate_threshold_blocks(cpu, b, bank, 0, msr_ops.misc(bank)); + if (err) + goto out_free; + + per_cpu(threshold_banks, cpu)[bank] = b; + + return 0; out_free: kfree(b);