x86/sgx: Roof the number of pages process in SGX_IOC_ENCLAVE_ADD_PAGES
PATCH 1/1 x86/sgx: Roof the number of pages process in SGX_IOC_ENCLAVE_ADD_PAGES
Remove 'count' from struct sgx_enclave_add_pages and return number of pages processed as a positive return as there is no reasonable use for -EINTR for the caller. Then, roof the processing with a fixed constant and break out the loop also when this value is surpassed.
--- arch/x86/include/uapi/asm/sgx.h | 2 -- arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++------ arch/x86/kernel/cpu/sgx/sgx.h | 1 + 3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/include/uapi/asm/sgx.h +0 -2
@@ -45,7 +45,6 @@ struct sgx_enclave_create {* @length: length of the data (multiple of the page size) * @secinfo: address for the SECINFO data * @flags: page control flags -* @count: number of bytes added (multiple of the page size) */ struct sgx_enclave_add_pages {__u64 src;
@@ -53,7 +52,6 @@ struct sgx_enclave_add_pages {__u64 length;__u64 secinfo;__u64 flags; -__u64 count; };/**
arch/x86/kernel/cpu/sgx/ioctl.c +4 -6
@@ -490,7 +490,7 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,* caller. * * Return: -* 0 on success, +* length of the data processed on success, * -EACCES if an executable source page is located in a noexec partition, * -EIO if either ENCLS[EADD] or ENCLS[EEXTEND] fails * -errno otherwise
@@ -530,8 +530,8 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)return -EINVAL ;for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) { + ret = c;break ; }
@@ -544,12 +544,10 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)break ; } - addp.count = c; -if (copy_to_user (arg, &addp,sizeof (addp)))return -EFAULT ; -return ret; +return c; } static int __sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus,
arch/x86/kernel/cpu/sgx/sgx.h +1 -0
@@ -37,6 +37,7 @@ struct sgx_epc_section {#define SGX_EPC_SECTION_MASK GENMASK(7, 0)#define SGX_MAX_EPC_SECTIONS (SGX_EPC_SECTION_MASK + 1)#define SGX_EPC_PAGE_RECLAIMABLE BIT(8) +#define SGX_MAX_ADD_PAGES_LENGTH 0x100000#define SGX_NR_TO_SCAN 16#define SGX_NR_LOW_PAGES 32#define SGX_NR_HIGH_PAGES 64
Signature · 1 line
2.25.1
Quoted 49 lines · Jarkko Sakkinen
Remove 'count' from struct
sgx_enclave_add_pagesand return number of pages processed as a positive return as there is no reasonable use for-EINTRfor the caller. Then, roof the processing with a fixed constant and break out the loop also when this value is surpassed.Cc Sean Christopherson <[email protected]>Cc Borislav Petkov <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>--- arch/x86/include/uapi/asm/sgx.h | 2 -- arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++------ arch/x86/kernel/cpu/sgx/sgx.h | 1 + 3 files changed, 5 insertions(+), 8 deletions(-)arch/x86/include/uapi/asm/sgx.h +0
-2@@ -45,7 +45,6 @@ struct sgx_enclave_create {* @length: length of the data (multiple of the page size) * @secinfo: address for the SECINFO data * @flags: page control flags -* @count: number of bytes added (multiple of the page size) */ struct sgx_enclave_add_pages {__u64 src;@@ -53,7 +52,6 @@ struct sgx_enclave_add_pages {__u64 length;__u64 secinfo;__u64 flags; -__u64 count; };/** arch/x86/kernel/cpu/sgx/ioctl.c +1
-1@@ -490,7 +490,7 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,* caller. * * Return: -* 0 on success, +* length of the data processed on success, * -EACCES if an executable source page is located in a noexec partition, * -EIO if either ENCLS[EADD] or ENCLS[EEXTEND] fails * -errno otherwise
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 534
Quoted 7 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-2@@ -530,8 +530,8 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)return -EINVAL ;for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) { + ret = c;
I don't have an opinion on returning count vs. EINTR, but I don't see the point in arbitrarily capping the number of pages that can be added in a single ioctl(). It doesn't provide any real protection, e.g. userspace can simply restart the ioctl() with updated offsets and continue spamming EADDs. We are relying on other limits, e.g. memcg, rlimits, etc... to reign in malicious/broken userspace.
There is nothing inherently dangerous about spending time in the kernel so long as appropriate checks are made, e.g. for a pending signel and resched. If we're missing checks, adding an arbitrary limit won't fix the underlying problem, at least not in a deterministic way.
If we really want a limit of some form, adding a knob to control the max size of an enclave seems like the way to go. But even that is of dubious value as I'd rather rely on existing limits for virtual and physical memory, and add a proper EPC cgroup to account and limit EPC memory.
Quoted 32 lines
arch/x86/kernel/cpu/sgx/ioctl.c +1
-3break ; }@@ -544,12 +544,10 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)break ; } - addp.count = c; -if (copy_to_user (arg, &addp,sizeof (addp)))return -EFAULT ; -return ret; +return c; } static int __sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus,arch/x86/kernel/cpu/sgx/sgx.h +1
-0@@ -37,6 +37,7 @@ struct sgx_epc_section {#define SGX_EPC_SECTION_MASK GENMASK(7, 0)#define SGX_MAX_EPC_SECTIONS (SGX_EPC_SECTION_MASK + 1)#define SGX_EPC_PAGE_RECLAIMABLE BIT(8) +#define SGX_MAX_ADD_PAGES_LENGTH 0x100000#define SGX_NR_TO_SCAN 16#define SGX_NR_LOW_PAGES 32#define SGX_NR_HIGH_PAGES 64Signature · 1 line
2.25.1
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 19 lines · Jarkko Sakkinen
Quoted 18 lines · Sean Christopherson
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 534
Quoted 5 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-2for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) { + ret = c;I don't have an opinion on returning count vs. EINTR, but I don't see the point in arbitrarily capping the number of pages that can be added in a single
ioctl(). It doesn't provide any real protection, e.g. userspace can simply restart theioctl()with updated offsets and continue spamming EADDs. We are relying on other limits, e.g. memcg, rlimits, etc... to reign in malicious/broken userspace.There is nothing inherently dangerous about spending time in the kernel so long as appropriate checks are made, e.g. for a pending signel and resched. If we're missing checks, adding an arbitrary limit won't fix the underlying problem, at least not in a deterministic way.
Quoted 7 lines
Quoted 4 lines
If we really want a limit of some form, adding a knob to control the max size of an enclave seems like the way to go. But even that is of dubious value as I'd rather rely on existing limits for virtual and physical memory, and add a proper EPC cgroup to account and limit EPC memory.
It is better to have a contract in the API that the number of processed pages can be less than given, not unlike in syscalls such as
write().
That can be handled by a comment, no? If we want to "enforce" the behavior, I'd rather bail out of the loop after a random number of pages than have a completely arbitrary limit. The arbitrary limit will create a contract of its own and may lead to weird guest implementations.
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Jarkko Sakkinen
Quoted 7 lines · Sean Christopherson
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 534
Quoted 5 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-2for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) { + ret = c;I don't have an opinion on returning count vs. EINTR, but I don't see
the
Quoted 1 line
point in arbitrarily capping the number of pages that can be added in
a
Quoted 1 line
single
ioctl(). It doesn't provide any real protection, e.g.userspace
Quoted 1 line
can simply restart the
ioctl()with updated offsets and continuespamming
Quoted 4 lines
EADDs. We are relying on other limits, e.g. memcg, rlimits, etc... to reign in malicious/broken userspace.
There is nothing inherently dangerous about spending time in the
kernel so
Quoted 1 line
long as appropriate checks are made, e.g. for a pending signel and
resched.
Quoted 1 line
If we're missing checks, adding an arbitrary limit won't fix the
underlying
Quoted 3 lines
problem, at least not in a deterministic way.
If we really want a limit of some form, adding a knob to control the
max
Quoted 1 line
size of an enclave seems like the way to go. But even that is of
dubious
Quoted 1 line
value as I'd rather rely on existing limits for virtual and physical
memory,
Quoted 1 line
and add a proper EPC cgroup to account and limit EPC memory.
Quoted 7 lines
Quoted 2 lines
It is better to have a contract in the API that the number of processed pages can be less than given, not unlike in syscalls such as
write().That can be handled by a comment, no? If we want to "enforce" the behavior, I'd rather bail out of the loop after a random number of pages than have a completely arbitrary limit. The arbitrary limit will create a contract of its own and may lead to weird guest implementations.
I agree with Sean on potential issues with the arbitrary hard coded limit. Also returning -EINTR is better way to express to user space that operations are interrupted by signal and can be retried, which is a known pattern for this kind of situations.
Quoted 50 lines · Haitao Huang
nOn Fri, 11 Sep 2020 10:51:27 -0500, Sean Christopherson
Quoted 38 lines · <[email protected]> wrote:
Quoted 28 lines · Jarkko Sakkinen
Quoted 7 lines · Sean Christopherson
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 534
Quoted 5 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-2for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) { + ret = c;I don't have an opinion on returning count vs. EINTR, but I don't
see the
Quoted 1 line
point in arbitrarily capping the number of pages that can be added
in a
Quoted 1 line
single
ioctl(). It doesn't provide any real protection, e.g.userspace
Quoted 1 line
can simply restart the
ioctl()with updated offsets and continuespamming
Quoted 4 lines
EADDs. We are relying on other limits, e.g. memcg, rlimits, etc... to reign in malicious/broken userspace.
There is nothing inherently dangerous about spending time in the
kernel so
Quoted 1 line
long as appropriate checks are made, e.g. for a pending signel and
resched.
Quoted 1 line
If we're missing checks, adding an arbitrary limit won't fix the
underlying
Quoted 3 lines
problem, at least not in a deterministic way.
If we really want a limit of some form, adding a knob to control
the max
Quoted 8 lines
Quoted 1 line
size of an enclave seems like the way to go. But even that is of
dubious
Quoted 1 line
value as I'd rather rely on existing limits for virtual and
physical memory,
Quoted 1 line
and add a proper EPC cgroup to account and limit EPC memory.
It is better to have a contract in the API that the number of processed pages can be less than given, not unlike in syscalls such as
write().Quoted 7 lines
That can be handled by a comment, no? If we want to "enforce" the behavior,
I'd rather bail out of the loop after a random number of pages than have a
completely arbitrary limit. The arbitrary limit will create a contract of
its own and may lead to weird guest implementations.
Quoted 4 lines
I agree with Sean on potential issues with the arbitrary hard coded limit. Also returning
-EINTRis better way to express to user space that operations are interrupted by signal and can be retried, which is a known pattern for this kind of situations.
In read() -EINTR is returned only when zero amount of data is processed.
Otherwise, it returns just the count.
/Jarkko
Quoted 25 lines · Sean Christopherson
Quoted 24 lines · Jarkko Sakkinen
Quoted 22 lines · Sean Christopherson
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 534
Quoted 5 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-2for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) { + ret = c;I don't have an opinion on returning count vs. EINTR, but I don't see the point in arbitrarily capping the number of pages that can be added in a single
ioctl(). It doesn't provide any real protection, e.g. userspace can simply restart theioctl()with updated offsets and continue spamming EADDs. We are relying on other limits, e.g. memcg, rlimits, etc... to reign in malicious/broken userspace.There is nothing inherently dangerous about spending time in the kernel so long as appropriate checks are made, e.g. for a pending signel and resched. If we're missing checks, adding an arbitrary limit won't fix the underlying problem, at least not in a deterministic way.
If we really want a limit of some form, adding a knob to control the max size of an enclave seems like the way to go. But even that is of dubious value as I'd rather rely on existing limits for virtual and physical memory, and add a proper EPC cgroup to account and limit EPC memory.
Quoted 7 lines
Quoted 2 lines
It is better to have a contract in the API that the number of processed pages can be less than given, not unlike in syscalls such as
write().That can be handled by a comment, no? If we want to "enforce" the behavior, I'd rather bail out of the loop after a random number of pages than have a completely arbitrary limit. The arbitrary limit will create a contract of its own and may lead to weird guest implementations.
I don't understand.
It is already a random number given that also signal can cause this.
/Jarkko
Quoted 49 lines · Jarkko Sakkinen
Remove 'count' from struct
sgx_enclave_add_pagesand return number of pages processed as a positive return as there is no reasonable use for-EINTRfor the caller. Then, roof the processing with a fixed constant and break out the loop also when this value is surpassed.Cc Sean Christopherson <[email protected]>Cc Borislav Petkov <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>--- arch/x86/include/uapi/asm/sgx.h | 2 -- arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++------ arch/x86/kernel/cpu/sgx/sgx.h | 1 + 3 files changed, 5 insertions(+), 8 deletions(-)arch/x86/include/uapi/asm/sgx.h +0
-2@@ -45,7 +45,6 @@ struct sgx_enclave_create {* @length: length of the data (multiple of the page size) * @secinfo: address for the SECINFO data * @flags: page control flags -* @count: number of bytes added (multiple of the page size) */ struct sgx_enclave_add_pages {__u64 src;@@ -53,7 +52,6 @@ struct sgx_enclave_add_pages {__u64 length;__u64 secinfo;__u64 flags; -__u64 count; };/** arch/x86/kernel/cpu/sgx/ioctl.c +1
-1@@ -490,7 +490,7 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,* caller. * * Return: -* 0 on success, +* length of the data processed on success, * -EACCES if an executable source page is located in a noexec partition, * -EIO if either ENCLS[EADD] or ENCLS[EEXTEND] fails * -errno otherwise
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 533
Quoted 6 lines
arch/x86/kernel/cpu/sgx/ioctl.c +1
-2@@ -530,8 +530,8 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)return -EINVAL ;for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) {
I know that SGX_MAX_ADD_PAGES_LENGTH may be a multiple of PAGE_SIZE, but to be sure that nothing breaks here in the future, I think it would be better to use '>=' as the comparison rather than just '=='.
Also, the indentation seems off here w.r.t. using TABs when the break below is using spaces - would be good to have them the same at least.
Thanks,
Darren.
Quoted 32 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-3+ ret = c;break ; }@@ -544,12 +544,10 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)break ; } - addp.count = c; -if (copy_to_user (arg, &addp,sizeof (addp)))return -EFAULT ; -return ret; +return c; } static int __sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus,arch/x86/kernel/cpu/sgx/sgx.h +1
-0@@ -37,6 +37,7 @@ struct sgx_epc_section {#define SGX_EPC_SECTION_MASK GENMASK(7, 0)#define SGX_MAX_EPC_SECTIONS (SGX_EPC_SECTION_MASK + 1)#define SGX_EPC_PAGE_RECLAIMABLE BIT(8) +#define SGX_MAX_ADD_PAGES_LENGTH 0x100000#define SGX_NR_TO_SCAN 16#define SGX_NR_LOW_PAGES 32#define SGX_NR_HIGH_PAGES 64Signature · 1 line
2.25.1
Quoted 52 lines · Darren Kenny
Quoted 51 lines · Jarkko Sakkinen
Remove 'count' from struct
sgx_enclave_add_pagesand return number of pages processed as a positive return as there is no reasonable use for-EINTRfor the caller. Then, roof the processing with a fixed constant and break out the loop also when this value is surpassed.Cc Sean Christopherson <[email protected]>Cc Borislav Petkov <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>--- arch/x86/include/uapi/asm/sgx.h | 2 -- arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++------ arch/x86/kernel/cpu/sgx/sgx.h | 1 + 3 files changed, 5 insertions(+), 8 deletions(-)arch/x86/include/uapi/asm/sgx.h +0
-2@@ -45,7 +45,6 @@ struct sgx_enclave_create {* @length: length of the data (multiple of the page size) * @secinfo: address for the SECINFO data * @flags: page control flags -* @count: number of bytes added (multiple of the page size) */ struct sgx_enclave_add_pages {__u64 src;@@ -53,7 +52,6 @@ struct sgx_enclave_add_pages {__u64 length;__u64 secinfo;__u64 flags; -__u64 count; };/** arch/x86/kernel/cpu/sgx/ioctl.c +1
-1@@ -490,7 +490,7 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,* caller. * * Return: -* 0 on success, +* length of the data processed on success, * -EACCES if an executable source page is located in a noexec partition, * -EIO if either ENCLS[EADD] or ENCLS[EEXTEND] fails * -errno otherwise @@ -530,8 +530,8 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)return -EINVAL ;
Quoted 8 lines
arch/x86/kernel/cpu/sgx/ioctl.c static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) · line 533
Quoted 4 lines
arch/x86/kernel/cpu/sgx/ioctl.c +1
-2for (c =0 ; c < addp.length; c +=PAGE_SIZE ) { -if (signal_pending (current)) { - ret = -EINTR ; +if (c ==SGX_MAX_ADD_PAGES_LENGTH ||signal_pending (current)) {I know that
SGX_MAX_ADD_PAGES_LENGTHmay be a multiple ofPAGE_SIZE, but to be sure that nothing breaks here in the future, I think it would be better to use '>=' as the comparison rather than just '=='.
I'm not forseeing this. We will change the comparison or remove it if the constant is no longer required.
Quoted 3 lines
Also, the indentation seems off here w.r.t. using TABs when the break below is using spaces - would be good to have them the same at least.
Ugh, that must be my mistake. I'll check these before squashing this.
Thank you.
/Jarkko