public inbox for linux-sgx@vger.kernel.org
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Dave Hansen <dave.hansen@linux.intel.com>
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
Date: Tue, 21 Dec 2021 10:42:32 +0200	[thread overview]
Message-ID: <YcGTePmWDMOQU1pn@iki.fi> (raw)
In-Reply-To: <20211217223153.837591E0@davehans-spike.ostc.intel.com>

On Fri, Dec 17, 2021 at 02:31:53PM -0800, Dave Hansen wrote:
> 
> From: Dave Hansen <dave.hansen@linux.intel.com>
> 
> 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 <nathan@kernel.org>
> CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Cc: linux-sgx@vger.kernel.org
> Cc: x86@kernel.org
> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
> ---
> 
>  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 <jarkko@kernel.org>

[Some latency in response time is because off-work up until end of the year]

/Jarkko

      parent reply	other threads:[~2021-12-21  8:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-17 22:31 [PATCH] x86/sgx: Fix NULL pointer dereference on non-SGX systems Dave Hansen
2021-12-17 22:57 ` Nathan Chancellor
2021-12-17 23:15 ` Greg KH
2021-12-17 23:30   ` Greg KH
2021-12-17 23:55     ` Dave Hansen
2021-12-18  0:07       ` Greg KH
2021-12-18  0:13         ` Dave Hansen
2021-12-21  8:42 ` Jarkko Sakkinen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YcGTePmWDMOQU1pn@iki.fi \
    --to=jarkko@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=dave@sr71.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox