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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61245C433F5 for ; Tue, 21 Dec 2021 08:42:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234728AbhLUImf (ORCPT ); Tue, 21 Dec 2021 03:42:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234305AbhLUIme (ORCPT ); Tue, 21 Dec 2021 03:42:34 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D3DAC061574 for ; Tue, 21 Dec 2021 00:42:34 -0800 (PST) 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 D1D4161466 for ; Tue, 21 Dec 2021 08:42:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8627C36AE2; Tue, 21 Dec 2021 08:42:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1640076153; bh=6yaXjq48asIAwWT2Tuh0uo1XoqGmEzs3VE/9bUAKKR0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=sHT12kXip+Kj6UfqMizL5yz+iQm0K5TDU0fTo1qoZ4gyl5S6pScISxIwGrGuPHpQZ 8Vm7etFspJh4eVWrtiPIJGq7bNTD5vqc85aWOI1AsczdcQwU4uMHcGMBxy1/z3J0of OBq6RcUwF6+LO2fscYSJ71TLR9dAcLTzi7HGUh8Kkyh/RsNlrR5BYTPcob1Sdo63w9 CdeR1o7x0rNj/h3f6+nphIKpjQG8Sw8ba6o2s2fgfpN1AUvklbMDWkzSWGaU7AkacV oeni3qfISgJ6GLyBfcShnQaBAnad2YK3tzi+HwjkDIV4sWj9xgTl+XUrLr9RXP878x dEE2fL2PnxyuA== Date: Tue, 21 Dec 2021 10:42:32 +0200 From: Jarkko Sakkinen To: Dave Hansen Cc: dave@sr71.net, nathan@kernel.org, gregkh@linuxfoundation.org, linux-sgx@vger.kernel.org, x86@kernel.org Subject: Re: [PATCH] x86/sgx: Fix NULL pointer dereference on non-SGX systems Message-ID: References: <20211217223153.837591E0@davehans-spike.ostc.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211217223153.837591E0@davehans-spike.ostc.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org On Fri, Dec 17, 2021 at 02:31:53PM -0800, Dave Hansen wrote: > > From: Dave Hansen > > Nathan Chancellor reported an oops when aceessing the > 'sgx_total_bytes' sysfs file: > > https://lore.kernel.org/all/YbzhBrimHGGpddDM@archlinux-ax161/ > > The sysfs output code accesses the sgx_numa_nodes[] array > unconditionally. However, this array is allocated during SGX > initialization, which only occurs on systems where SGX is > supported. > > If the sysfs file is accessed on systems without SGX support, > sgx_numa_nodes[] is NULL and an oops occurs. > > Add a check to ensure that SGX has been initialized to the point > where sgx_numa_nodes[] is allocated, before accessing it. > > Reported-by: Nathan Chancellor > CC: Greg Kroah-Hartman > Cc: Jarkko Sakkinen > Cc: linux-sgx@vger.kernel.org > Cc: x86@kernel.org > Signed-off-by: Dave Hansen > --- > > b/arch/x86/kernel/cpu/sgx/main.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff -puN arch/x86/kernel/cpu/sgx/main.c~sgx-null-ptr arch/x86/kernel/cpu/sgx/main.c > --- a/arch/x86/kernel/cpu/sgx/main.c~sgx-null-ptr 2021-12-17 13:38:00.217312383 -0800 > +++ b/arch/x86/kernel/cpu/sgx/main.c 2021-12-17 14:00:36.293044390 -0800 > @@ -906,7 +906,13 @@ EXPORT_SYMBOL_GPL(sgx_set_attribute); > #ifdef CONFIG_NUMA > static ssize_t sgx_total_bytes_show(struct device *dev, struct device_attribute *attr, char *buf) > { > - return sysfs_emit(buf, "%lu\n", sgx_numa_nodes[dev->id].size); > + unsigned long node_bytes = 0; > + > + /* Avoid acccessing sgx_numa_nodes[] when it is not allocated: */ > + if (!nodes_empty(sgx_numa_mask)) > + node_bytes = sgx_numa_nodes[dev->id].size; > + > + return sysfs_emit(buf, "%lu\n", node_bytes); > } > static DEVICE_ATTR_RO(sgx_total_bytes); > > _ Reviewed-by: Jarkko Sakkinen [Some latency in response time is because off-work up until end of the year] /Jarkko