Skip to contentSkip to conversation

x86/sgx: Check that the address is within ELRANGE

RFC PATCH In review Jarkko Sakkinen 2 participants 3 messages Jun 13, 2019 – Jun 19, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86/sgx: Check that the address is within ELRANGE

In sgx_encl_page_alloc() check that the given address within the ELRANGE. Return -EINVAL if not.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +7 -0
index d17c60dca114..9d3fc770b4d9 100644
@@ -240,19 +240,26 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	struct sgx_encl_page *encl_page;
 	int ret;
 
+	if (addr < encl->base || addr > (encl->base + encl->size))
+		return ERR_PTR(-EINVAL);
+
 	if (radix_tree_lookup(&encl->page_tree, PFN_DOWN(addr)))
 		return ERR_PTR(-EEXIST);
+
 	encl_page = kzalloc(sizeof(*encl_page), GFP_KERNEL);
 	if (!encl_page)
 		return ERR_PTR(-ENOMEM);
+
 	encl_page->desc = addr;
 	encl_page->encl = encl;
+
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
 	if (ret) {
 		kfree(encl_page);
 		return ERR_PTR(ret);
 	}
+
 	return encl_page;
 }
 
Signature · 1 line
2.20.1
#1 lore raw
Quoted 15 lines · Jarkko Sakkinen

In sgx_encl_page_alloc() check that the given address within the ELRANGE. Return -EINVAL if not.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -0
index d17c60dca114..9d3fc770b4d9 100644

arch/x86/kernel/cpu/sgx/driver/ioctl.c static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl, · line 243

Quoted 4 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
@@ -240,19 +240,26 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	struct sgx_encl_page *encl_page;
 	int ret;
 
+	if (addr < encl->base || addr > (encl->base + encl->size))

The upper bounds check should be "addr >= (encl->base + encl->size)" or "addr > (encl->base + encl->size - 1)"

Quoted 25 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6 -0
+		return ERR_PTR(-EINVAL);
+
 	if (radix_tree_lookup(&encl->page_tree, PFN_DOWN(addr)))
 		return ERR_PTR(-EEXIST);
+
 	encl_page = kzalloc(sizeof(*encl_page), GFP_KERNEL);
 	if (!encl_page)
 		return ERR_PTR(-ENOMEM);
+
 	encl_page->desc = addr;
 	encl_page->encl = encl;
+
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
 	if (ret) {
 		kfree(encl_page);
 		return ERR_PTR(ret);
 	}
+
 	return encl_page;
 }
 
Signature · 1 line
2.20.1
#2 lore raw
Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Jarkko Sakkinen

In sgx_encl_page_alloc() check that the given address within the ELRANGE. Return -EINVAL if not.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -0
index d17c60dca114..9d3fc770b4d9 100644
@@ -240,19 +240,26 @@ static struct sgx_encl_page
 *sgx_encl_page_alloc(struct sgx_encl *encl,
 	struct sgx_encl_page *encl_page;
 	int ret;
 
Quoted 4 lines

arch/x86/kernel/cpu/sgx/driver/ioctl.c static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl, · line 243

Quoted 1 line
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
+	if (addr < encl->base || addr > (encl->base + encl->size))

The upper bounds check should be "addr >= (encl->base + encl->size)" or "addr > (encl->base + encl->size - 1)"

Right, thanks :-) Shay told me that this did not happen with earlier patch set versions but I could not spot why. Have to look again before merging.

/Jarkko