Skip to content

Commit 2056e29

Browse files
committed
x86/sgx: Fix NULL pointer dereference on non-SGX systems
== Problem == 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. == Solution == To fix this, hide the entire nodeX/x86/ attribute group on systems without SGX support using the ->is_visible attribute group callback. Unfortunately, SGX is initialized via a device_initcall() which occurs _after_ the ->is_visible() callback. Instead of moving SGX initialization earlier, call sysfs_update_group() during SGX initialization to update the group visiblility. This update requires moving the SGX sysfs code earlier in sgx/main.c. There are no code changes other than the addition of arch_update_sysfs_visibility() and a minor whitespace fixup to arch_node_attr_is_visible() which checkpatch caught. CC: Greg Kroah-Hartman <[email protected]> Cc: [email protected] Cc: [email protected] Fixes: 50468e4 ("x86/sgx: Add an attribute for the amount of SGX memory in a NUMA node") Reported-by: Nathan Chancellor <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Nathan Chancellor <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 572a0a6 commit 2056e29

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

arch/x86/kernel/cpu/sgx/main.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
#include <linux/highmem.h>
77
#include <linux/kthread.h>
88
#include <linux/miscdevice.h>
9+
#include <linux/node.h>
910
#include <linux/pagemap.h>
1011
#include <linux/ratelimit.h>
1112
#include <linux/sched/mm.h>
1213
#include <linux/sched/signal.h>
1314
#include <linux/slab.h>
15+
#include <linux/sysfs.h>
1416
#include <asm/sgx.h>
1517
#include "driver.h"
1618
#include "encl.h"
@@ -780,6 +782,48 @@ static inline u64 __init sgx_calc_section_metric(u64 low, u64 high)
780782
((high & GENMASK_ULL(19, 0)) << 32);
781783
}
782784

785+
#ifdef CONFIG_NUMA
786+
static ssize_t sgx_total_bytes_show(struct device *dev, struct device_attribute *attr, char *buf)
787+
{
788+
return sysfs_emit(buf, "%lu\n", sgx_numa_nodes[dev->id].size);
789+
}
790+
static DEVICE_ATTR_RO(sgx_total_bytes);
791+
792+
static umode_t arch_node_attr_is_visible(struct kobject *kobj,
793+
struct attribute *attr, int idx)
794+
{
795+
/* Make all x86/ attributes invisible when SGX is not initialized: */
796+
if (nodes_empty(sgx_numa_mask))
797+
return 0;
798+
799+
return attr->mode;
800+
}
801+
802+
static struct attribute *arch_node_dev_attrs[] = {
803+
&dev_attr_sgx_total_bytes.attr,
804+
NULL,
805+
};
806+
807+
const struct attribute_group arch_node_dev_group = {
808+
.name = "x86",
809+
.attrs = arch_node_dev_attrs,
810+
.is_visible = arch_node_attr_is_visible,
811+
};
812+
813+
static void __init arch_update_sysfs_visibility(int nid)
814+
{
815+
struct node *node = node_devices[nid];
816+
int ret;
817+
818+
ret = sysfs_update_group(&node->dev.kobj, &arch_node_dev_group);
819+
820+
if (ret)
821+
pr_err("sysfs update failed (%d), files may be invisible", ret);
822+
}
823+
#else /* !CONFIG_NUMA */
824+
static void __init arch_update_sysfs_visibility(int nid) {}
825+
#endif
826+
783827
static bool __init sgx_page_cache_init(void)
784828
{
785829
u32 eax, ebx, ecx, edx, type;
@@ -826,6 +870,9 @@ static bool __init sgx_page_cache_init(void)
826870
INIT_LIST_HEAD(&sgx_numa_nodes[nid].sgx_poison_page_list);
827871
node_set(nid, sgx_numa_mask);
828872
sgx_numa_nodes[nid].size = 0;
873+
874+
/* Make SGX-specific node sysfs files visible: */
875+
arch_update_sysfs_visibility(nid);
829876
}
830877

831878
sgx_epc_sections[i].node = &sgx_numa_nodes[nid];
@@ -903,24 +950,6 @@ int sgx_set_attribute(unsigned long *allowed_attributes,
903950
}
904951
EXPORT_SYMBOL_GPL(sgx_set_attribute);
905952

906-
#ifdef CONFIG_NUMA
907-
static ssize_t sgx_total_bytes_show(struct device *dev, struct device_attribute *attr, char *buf)
908-
{
909-
return sysfs_emit(buf, "%lu\n", sgx_numa_nodes[dev->id].size);
910-
}
911-
static DEVICE_ATTR_RO(sgx_total_bytes);
912-
913-
static struct attribute *arch_node_dev_attrs[] = {
914-
&dev_attr_sgx_total_bytes.attr,
915-
NULL,
916-
};
917-
918-
const struct attribute_group arch_node_dev_group = {
919-
.name = "x86",
920-
.attrs = arch_node_dev_attrs,
921-
};
922-
#endif /* CONFIG_NUMA */
923-
924953
static int __init sgx_init(void)
925954
{
926955
int ret;

0 commit comments

Comments
 (0)