* [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create()
@ 2025-03-05 0:06 Jarkko Sakkinen
2025-03-05 0:09 ` Jarkko Sakkinen
2025-03-05 0:18 ` Dave Hansen
0 siblings, 2 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2025-03-05 0:06 UTC (permalink / raw)
To: linux-integrity, Jarkko Sakkinen, Dave Hansen, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin
Cc: Dan Carpenter, linux-sgx, 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.
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>
---
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 | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index b65ab214bdf5..fedd5f666b5e 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -64,6 +64,14 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
struct file *backing;
long ret;
+ /*
+ * This is a micro-architectural requirement. ECREATE would detect this
+ * too without mentionable overhead but this check guarantees also that
+ * the space calculations for EPC and shmem allocations 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] 6+ messages in thread
* Re: [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create()
2025-03-05 0:06 [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create() Jarkko Sakkinen
@ 2025-03-05 0:09 ` Jarkko Sakkinen
2025-03-05 0:18 ` Dave Hansen
1 sibling, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2025-03-05 0:09 UTC (permalink / raw)
To: linux-integrity, Dave Hansen, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, x86, H. Peter Anvin
Cc: Dan Carpenter, linux-sgx, linux-kernel
On Wed, Mar 05, 2025 at 02:06:02AM +0200, Jarkko Sakkinen wrote:
> 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.
>
> 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>
Cc: stable@vger.kernel.org # v5.11+
Fixes: 888d24911787 ("x86/sgx: Add SGX_IOC_ENCLAVE_CREATE")
BR, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create()
2025-03-05 0:06 [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create() Jarkko Sakkinen
2025-03-05 0:09 ` Jarkko Sakkinen
@ 2025-03-05 0:18 ` Dave Hansen
2025-03-05 0:19 ` Jarkko Sakkinen
1 sibling, 1 reply; 6+ messages in thread
From: Dave Hansen @ 2025-03-05 0:18 UTC (permalink / raw)
To: Jarkko Sakkinen, linux-integrity, Dave Hansen, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin
Cc: Dan Carpenter, linux-sgx, linux-kernel
On 3/4/25 16:06, Jarkko Sakkinen wrote:
> + /*
> + * This is a micro-architectural requirement. ECREATE would detect this
> + * too without mentionable overhead but this check guarantees also that
> + * the space calculations for EPC and shmem allocations never overflow.
> + */
> + if (!is_power_of_2(secs->size))
> + return -EINVAL;
Isn't it a plain old documented architectural requirement?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create()
2025-03-05 0:18 ` Dave Hansen
@ 2025-03-05 0:19 ` Jarkko Sakkinen
2025-03-05 0:30 ` Dave Hansen
0 siblings, 1 reply; 6+ messages in thread
From: Jarkko Sakkinen @ 2025-03-05 0:19 UTC (permalink / raw)
To: Dave Hansen
Cc: linux-integrity, Dave Hansen, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, x86, H. Peter Anvin, Dan Carpenter, linux-sgx,
linux-kernel
On Tue, Mar 04, 2025 at 04:18:03PM -0800, Dave Hansen wrote:
> On 3/4/25 16:06, Jarkko Sakkinen wrote:
> > + /*
> > + * This is a micro-architectural requirement. ECREATE would detect this
> > + * too without mentionable overhead but this check guarantees also that
> > + * the space calculations for EPC and shmem allocations never overflow.
> > + */
> > + if (!is_power_of_2(secs->size))
> > + return -EINVAL;
>
> Isn't it a plain old documented architectural requirement?
Yes, but it requires some explanation why it exists here instead of
counting on ECREATE.
What do you suggest?
BR, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create()
2025-03-05 0:19 ` Jarkko Sakkinen
@ 2025-03-05 0:30 ` Dave Hansen
2025-03-05 1:26 ` Jarkko Sakkinen
0 siblings, 1 reply; 6+ messages in thread
From: Dave Hansen @ 2025-03-05 0:30 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: linux-integrity, Dave Hansen, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, x86, H. Peter Anvin, Dan Carpenter, linux-sgx,
linux-kernel
On 3/4/25 16:19, Jarkko Sakkinen wrote:
> On Tue, Mar 04, 2025 at 04:18:03PM -0800, Dave Hansen wrote:
>> On 3/4/25 16:06, Jarkko Sakkinen wrote:
>>> + /*
>>> + * This is a micro-architectural requirement. ECREATE would detect this
>>> + * too without mentionable overhead but this check guarantees also that
>>> + * the space calculations for EPC and shmem allocations never overflow.
>>> + */
>>> + if (!is_power_of_2(secs->size))
>>> + return -EINVAL;
>> Isn't it a plain old documented architectural requirement?
> Yes, but it requires some explanation why it exists here instead of
> counting on ECREATE.
>
> What do you suggest?
/*
* ECREATE would detect this too, but checking here also ensures
* that the 'encl_size' calculations below can never overflow.
*/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create()
2025-03-05 0:30 ` Dave Hansen
@ 2025-03-05 1:26 ` Jarkko Sakkinen
0 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2025-03-05 1:26 UTC (permalink / raw)
To: Dave Hansen
Cc: linux-integrity, Dave Hansen, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, x86, H. Peter Anvin, Dan Carpenter, linux-sgx,
linux-kernel
On Tue, Mar 04, 2025 at 04:30:21PM -0800, Dave Hansen wrote:
> On 3/4/25 16:19, Jarkko Sakkinen wrote:
> > On Tue, Mar 04, 2025 at 04:18:03PM -0800, Dave Hansen wrote:
> >> On 3/4/25 16:06, Jarkko Sakkinen wrote:
> >>> + /*
> >>> + * This is a micro-architectural requirement. ECREATE would detect this
> >>> + * too without mentionable overhead but this check guarantees also that
> >>> + * the space calculations for EPC and shmem allocations never overflow.
> >>> + */
> >>> + if (!is_power_of_2(secs->size))
> >>> + return -EINVAL;
> >> Isn't it a plain old documented architectural requirement?
> > Yes, but it requires some explanation why it exists here instead of
> > counting on ECREATE.
> >
> > What do you suggest?
>
> /*
> * ECREATE would detect this too, but checking here also ensures
> * that the 'encl_size' calculations below can never overflow.
> */
Looks reasonable to me!
BR, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-03-05 1:26 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-05 0:06 [PATCH v2] arch/x86: Fix size overflows in sgx_encl_create() Jarkko Sakkinen
2025-03-05 0:09 ` Jarkko Sakkinen
2025-03-05 0:18 ` Dave Hansen
2025-03-05 0:19 ` Jarkko Sakkinen
2025-03-05 0:30 ` Dave Hansen
2025-03-05 1:26 ` Jarkko Sakkinen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).