All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] arch/x86: Fix size overflows in sgx_encl_create()
@ 2025-03-05  5:00 Jarkko Sakkinen
  2025-03-05  8:55 ` [tip: x86/urgent] x86/sgx: " tip-bot2 for Jarkko Sakkinen
  2025-03-05  9:05 ` tip-bot2 for Jarkko Sakkinen
  0 siblings, 2 replies; 3+ messages in thread
From: Jarkko Sakkinen @ 2025-03-05  5:00 UTC (permalink / raw)
  To: linux-sgx, Jarkko Sakkinen, Dave Hansen, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin,
	Sean Christopherson
  Cc: stable, Dan Carpenter, Borislav Petkov, linux-kernel

The total size calculated for EPC can overflow u64 given the added up page
for SECS.  Further, the total size calculated for shmem can overflow even
when the EPC size stays within limits of u64, given that it adds the extra
space for 128 byte PCMD structures (one for each page).

Address this by pre-evaluating the micro-architectural requirement of
SGX: the address space size must be power of two. This is eventually
checked up by ECREATE but the pre-check has the additional benefit of
making sure that there is some space for additional data.

Cc: stable@vger.kernel.org # v5.11+
Fixes: 888d24911787 ("x86/sgx: Add SGX_IOC_ENCLAVE_CREATE")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/linux-sgx/c87e01a0-e7dd-4749-a348-0980d3444f04@stanley.mountain/
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
v3: Updated the commit message according to Dave's suggestion and added
    fixes tag.
v2: Simply check the micro-architetural requirement in order to address
    Dave's comment:
    https://lore.kernel.org/linux-sgx/45e68dea-af6a-4b2a-8249-420f14de3424@intel.com/
---
 arch/x86/kernel/cpu/sgx/ioctl.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index b65ab214bdf5..776a20172867 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -64,6 +64,13 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 	struct file *backing;
 	long ret;
 
+	/*
+	 * ECREATE would detect this too, but checking here also ensures
+	 * that the 'encl_size' calculations below can never overflow.
+	 */
+	if (!is_power_of_2(secs->size))
+		return -EINVAL;
+
 	va_page = sgx_encl_grow(encl, true);
 	if (IS_ERR(va_page))
 		return PTR_ERR(va_page);
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [tip: x86/urgent] x86/sgx: Fix size overflows in sgx_encl_create()
  2025-03-05  5:00 [PATCH v3] arch/x86: Fix size overflows in sgx_encl_create() Jarkko Sakkinen
@ 2025-03-05  8:55 ` tip-bot2 for Jarkko Sakkinen
  2025-03-05  9:05 ` tip-bot2 for Jarkko Sakkinen
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Jarkko Sakkinen @ 2025-03-05  8:55 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Dan Carpenter, Jarkko Sakkinen, Ingo Molnar, Peter Zijlstra,
	H. Peter Anvin, x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     31d20f8f6c419b5e818aff2a2a0819a2a1d65003
Gitweb:        https://git.kernel.org/tip/31d20f8f6c419b5e818aff2a2a0819a2a1d65003
Author:        Jarkko Sakkinen <jarkko@kernel.org>
AuthorDate:    Wed, 05 Mar 2025 07:00:05 +02:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Wed, 05 Mar 2025 09:49:44 +01:00

x86/sgx: Fix size overflows in sgx_encl_create()

The total size calculated for EPC can overflow u64 given the added up page
for SECS.  Further, the total size calculated for shmem can overflow even
when the EPC size stays within limits of u64, given that it adds the extra
space for 128 byte PCMD structures (one for each page).

Address this by pre-evaluating the micro-architectural requirement of
SGX: the address space size must be power of two. This is eventually
checked up by ECREATE but the pre-check has the additional benefit of
making sure that there is some space for additional data.

Fixes: 888d24911787 ("x86/sgx: Add SGX_IOC_ENCLAVE_CREATE")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Link: https://lore.kernel.org/r/20250305050006.43896-1-jarkko@kernel.org

Closes: https://lore.kernel.org/linux-sgx/c87e01a0-e7dd-4749-a348-0980d3444f04@stanley.mountain/
---
 arch/x86/kernel/cpu/sgx/ioctl.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index b65ab21..776a201 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -64,6 +64,13 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 	struct file *backing;
 	long ret;
 
+	/*
+	 * ECREATE would detect this too, but checking here also ensures
+	 * that the 'encl_size' calculations below can never overflow.
+	 */
+	if (!is_power_of_2(secs->size))
+		return -EINVAL;
+
 	va_page = sgx_encl_grow(encl, true);
 	if (IS_ERR(va_page))
 		return PTR_ERR(va_page);

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [tip: x86/urgent] x86/sgx: Fix size overflows in sgx_encl_create()
  2025-03-05  5:00 [PATCH v3] arch/x86: Fix size overflows in sgx_encl_create() Jarkko Sakkinen
  2025-03-05  8:55 ` [tip: x86/urgent] x86/sgx: " tip-bot2 for Jarkko Sakkinen
@ 2025-03-05  9:05 ` tip-bot2 for Jarkko Sakkinen
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Jarkko Sakkinen @ 2025-03-05  9:05 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Dan Carpenter, Jarkko Sakkinen, Ingo Molnar, Dave Hansen,
	Peter Zijlstra, H. Peter Anvin, x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     0d3e0dfd68fb9e6b0ec865be9f3377cc3ff55733
Gitweb:        https://git.kernel.org/tip/0d3e0dfd68fb9e6b0ec865be9f3377cc3ff55733
Author:        Jarkko Sakkinen <jarkko@kernel.org>
AuthorDate:    Wed, 05 Mar 2025 07:00:05 +02:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Wed, 05 Mar 2025 09:51:41 +01:00

x86/sgx: Fix size overflows in sgx_encl_create()

The total size calculated for EPC can overflow u64 given the added up page
for SECS.  Further, the total size calculated for shmem can overflow even
when the EPC size stays within limits of u64, given that it adds the extra
space for 128 byte PCMD structures (one for each page).

Address this by pre-evaluating the micro-architectural requirement of
SGX: the address space size must be power of two. This is eventually
checked up by ECREATE but the pre-check has the additional benefit of
making sure that there is some space for additional data.

Fixes: 888d24911787 ("x86/sgx: Add SGX_IOC_ENCLAVE_CREATE")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Dave Hansen <dave.hansen@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Link: https://lore.kernel.org/r/20250305050006.43896-1-jarkko@kernel.org

Closes: https://lore.kernel.org/linux-sgx/c87e01a0-e7dd-4749-a348-0980d3444f04@stanley.mountain/
---
 arch/x86/kernel/cpu/sgx/ioctl.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index b65ab21..776a201 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -64,6 +64,13 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 	struct file *backing;
 	long ret;
 
+	/*
+	 * ECREATE would detect this too, but checking here also ensures
+	 * that the 'encl_size' calculations below can never overflow.
+	 */
+	if (!is_power_of_2(secs->size))
+		return -EINVAL;
+
 	va_page = sgx_encl_grow(encl, true);
 	if (IS_ERR(va_page))
 		return PTR_ERR(va_page);

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-03-05  9:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-05  5:00 [PATCH v3] arch/x86: Fix size overflows in sgx_encl_create() Jarkko Sakkinen
2025-03-05  8:55 ` [tip: x86/urgent] x86/sgx: " tip-bot2 for Jarkko Sakkinen
2025-03-05  9:05 ` tip-bot2 for Jarkko Sakkinen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.