Skip to content

Commit 483fc19

Browse files
ereshetovahansendc
authored andcommitted
x86/sgx: Introduce functions to count the sgx_(vepc_)open()
Currently, when SGX is compromised and the microcode update fix is applied, the machine needs to be rebooted to invalidate old SGX crypto-assets and make SGX be in an updated safe state. It's not friendly for the cloud. To avoid having to reboot, a new ENCLS[EUPDATESVN] is introduced to update SGX environment at runtime. This process needs to be done when there's no SGX users to make sure no compromised enclaves can survive from the update and allow the system to regenerate crypto-assets. For now there's no counter to track the active SGX users of host enclave and virtual EPC. Introduce such counter mechanism so that the EUPDATESVN can be done only when there's no SGX users. Define placeholder functions sgx_inc/dec_usage_count() that are used to increment and decrement such a counter. Also, wire the call sites for these functions. Encapsulate the current sgx_(vepc_)open() to __sgx_(vepc_)open() to make the new sgx_(vepc_)open() easy to read. The definition of the counter itself and the actual implementation of sgx_inc/dec_usage_count() functions come next. Note: The EUPDATESVN, which may fail, will be done in sgx_inc_usage_count(). Make it return 'int' to make subsequent patches which implement EUPDATESVN easier to review. For now it always returns success. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Tested-by: Nataliia Bondarevska <bondarn@google.com>
1 parent 3a86608 commit 483fc19

5 files changed

Lines changed: 51 additions & 2 deletions

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ u64 sgx_attributes_reserved_mask;
1414
u64 sgx_xfrm_reserved_mask = ~0x3;
1515
u32 sgx_misc_reserved_mask;
1616

17-
static int sgx_open(struct inode *inode, struct file *file)
17+
static int __sgx_open(struct inode *inode, struct file *file)
1818
{
1919
struct sgx_encl *encl;
2020
int ret;
@@ -41,6 +41,23 @@ static int sgx_open(struct inode *inode, struct file *file)
4141
return 0;
4242
}
4343

44+
static int sgx_open(struct inode *inode, struct file *file)
45+
{
46+
int ret;
47+
48+
ret = sgx_inc_usage_count();
49+
if (ret)
50+
return ret;
51+
52+
ret = __sgx_open(inode, file);
53+
if (ret) {
54+
sgx_dec_usage_count();
55+
return ret;
56+
}
57+
58+
return 0;
59+
}
60+
4461
static int sgx_release(struct inode *inode, struct file *file)
4562
{
4663
struct sgx_encl *encl = file->private_data;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ void sgx_encl_release(struct kref *ref)
765765
WARN_ON_ONCE(encl->secs.epc_page);
766766

767767
kfree(encl);
768+
sgx_dec_usage_count();
768769
}
769770

770771
/*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,16 @@ int sgx_set_attribute(unsigned long *allowed_attributes,
917917
}
918918
EXPORT_SYMBOL_GPL(sgx_set_attribute);
919919

920+
int sgx_inc_usage_count(void)
921+
{
922+
return 0;
923+
}
924+
925+
void sgx_dec_usage_count(void)
926+
{
927+
return;
928+
}
929+
920930
static int __init sgx_init(void)
921931
{
922932
int ret;

arch/x86/kernel/cpu/sgx/sgx.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ static inline int __init sgx_vepc_init(void)
102102
}
103103
#endif
104104

105+
int sgx_inc_usage_count(void);
106+
void sgx_dec_usage_count(void);
107+
105108
void sgx_update_lepubkeyhash(u64 *lepubkeyhash);
106109

107110
#endif /* _X86_SGX_H */

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,11 @@ static int sgx_vepc_release(struct inode *inode, struct file *file)
255255
xa_destroy(&vepc->page_array);
256256
kfree(vepc);
257257

258+
sgx_dec_usage_count();
258259
return 0;
259260
}
260261

261-
static int sgx_vepc_open(struct inode *inode, struct file *file)
262+
static int __sgx_vepc_open(struct inode *inode, struct file *file)
262263
{
263264
struct sgx_vepc *vepc;
264265

@@ -273,6 +274,23 @@ static int sgx_vepc_open(struct inode *inode, struct file *file)
273274
return 0;
274275
}
275276

277+
static int sgx_vepc_open(struct inode *inode, struct file *file)
278+
{
279+
int ret;
280+
281+
ret = sgx_inc_usage_count();
282+
if (ret)
283+
return ret;
284+
285+
ret = __sgx_vepc_open(inode, file);
286+
if (ret) {
287+
sgx_dec_usage_count();
288+
return ret;
289+
}
290+
291+
return 0;
292+
}
293+
276294
static long sgx_vepc_ioctl(struct file *file,
277295
unsigned int cmd, unsigned long arg)
278296
{

0 commit comments

Comments
 (0)