Skip to content

Commit e4f4b01

Browse files
author
Jethro Beekman
committed
Support initializing enclaves without a launch token
1 parent 2d2b795 commit e4f4b01

File tree

5 files changed

+101
-13
lines changed

5 files changed

+101
-13
lines changed

sgx.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ int sgx_encl_create(struct sgx_secs *secs);
226226
int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, void *data,
227227
struct sgx_secinfo *secinfo, unsigned int mrmask);
228228
int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,
229-
struct sgx_einittoken *einittoken);
229+
struct sgx_einittoken *einittoken, bool use_flc);
230230
struct sgx_encl_page *sgx_encl_augment(struct vm_area_struct *vma,
231231
unsigned long addr, bool write);
232232
void sgx_encl_release(struct kref *ref);
@@ -282,4 +282,6 @@ long modify_range(struct sgx_range *rg, unsigned long flags);
282282
int remove_page(struct sgx_encl *encl, unsigned long address, bool trim);
283283
int sgx_get_encl(unsigned long addr, struct sgx_encl **encl);
284284
int sgx_vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, resource_size_t pa);
285+
286+
void sgx_reset_pubkey_hash(void *failed);
285287
#endif /* __ARCH_X86_INTEL_SGX_H__ */

sgx_encl.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ static int sgx_einit(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,
916916
* @encl: an enclave
917917
* @sigstruct: SIGSTRUCT for the enclave
918918
* @token: EINITTOKEN for the enclave
919+
* @use_flc: write LEHASH MSRs prior to calling EINIT, and restore them after
919920
*
920921
* Retries a few times in order to perform EINIT operation on an enclave
921922
* because there could be potentially an interrupt storm.
@@ -926,9 +927,10 @@ static int sgx_einit(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,
926927
* SGX error code
927928
*/
928929
int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,
929-
struct sgx_einittoken *token)
930+
struct sgx_einittoken *token, bool use_flc)
930931
{
931932
int ret;
933+
int tmp;
932934
int i;
933935
int j;
934936

@@ -943,8 +945,23 @@ int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,
943945

944946
for (i = 0; i < SGX_EINIT_SLEEP_COUNT; i++) {
945947
for (j = 0; j < SGX_EINIT_SPIN_COUNT; j++) {
948+
preempt_disable();
949+
950+
if (use_flc) {
951+
wrmsrl_safe(MSR_IA32_SGXLEPUBKEYHASH0, ((u64*)token->payload.mrsigner)[0]);
952+
wrmsrl_safe(MSR_IA32_SGXLEPUBKEYHASH1, ((u64*)token->payload.mrsigner)[1]);
953+
wrmsrl_safe(MSR_IA32_SGXLEPUBKEYHASH2, ((u64*)token->payload.mrsigner)[2]);
954+
wrmsrl_safe(MSR_IA32_SGXLEPUBKEYHASH3, ((u64*)token->payload.mrsigner)[3]);
955+
}
956+
946957
ret = sgx_einit(encl, sigstruct, token);
947958

959+
if (use_flc) {
960+
sgx_reset_pubkey_hash(&tmp);
961+
}
962+
963+
preempt_enable();
964+
948965
if (ret == SGX_UNMASKED_EVENT)
949966
continue;
950967
else

sgx_ioctl.c

+54-9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include <linux/slab.h>
7373
#include <linux/hashtable.h>
7474
#include <linux/shmem_fs.h>
75+
#include <crypto/hash.h>
7576

7677
int sgx_get_encl(unsigned long addr, struct sgx_encl **encl)
7778
{
@@ -202,6 +203,31 @@ static long sgx_ioc_enclave_add_page(struct file *filep, unsigned int cmd,
202203
return ret;
203204
}
204205

206+
static int __sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus,
207+
void *hash)
208+
{
209+
SHASH_DESC_ON_STACK(shash, tfm);
210+
211+
shash->tfm = tfm;
212+
213+
return crypto_shash_digest(shash, modulus, SGX_MODULUS_SIZE, hash);
214+
}
215+
216+
static int sgx_get_key_hash(const void *modulus, void *hash)
217+
{
218+
struct crypto_shash *tfm;
219+
int ret;
220+
221+
tfm = crypto_alloc_shash("sha256", 0, CRYPTO_ALG_ASYNC);
222+
if (IS_ERR(tfm))
223+
return PTR_ERR(tfm);
224+
225+
ret = __sgx_get_key_hash(tfm, modulus, hash);
226+
227+
crypto_free_shash(tfm);
228+
return ret;
229+
}
230+
205231
/**
206232
* sgx_ioc_enclave_init - handler for %SGX_IOC_ENCLAVE_INIT
207233
*
@@ -218,16 +244,28 @@ static long sgx_ioc_enclave_add_page(struct file *filep, unsigned int cmd,
218244
static long sgx_ioc_enclave_init(struct file *filep, unsigned int cmd,
219245
unsigned long arg)
220246
{
221-
struct sgx_enclave_init *initp = (struct sgx_enclave_init *)arg;
222-
unsigned long sigstructp = (unsigned long)initp->sigstruct;
223-
unsigned long einittokenp = (unsigned long)initp->einittoken;
224-
unsigned long encl_id = initp->addr;
247+
unsigned long sigstructp;
248+
unsigned long einittokenp;
249+
unsigned long encl_id;
250+
struct sgx_enclave_init *initp;
251+
struct sgx_enclave_init_no_token *initntp;
225252
struct sgx_sigstruct *sigstruct;
226253
struct sgx_einittoken *einittoken;
227254
struct sgx_encl *encl;
228255
struct page *initp_page;
229256
int ret;
230257

258+
if (cmd == SGX_IOC_ENCLAVE_INIT_NO_TOKEN) {
259+
initntp = (struct sgx_enclave_init_no_token *)arg;
260+
sigstructp = (unsigned long)initntp->sigstruct;
261+
encl_id = initntp->addr;
262+
} else {
263+
initp = (struct sgx_enclave_init *)arg;
264+
sigstructp = (unsigned long)initp->sigstruct;
265+
einittokenp = (unsigned long)initp->einittoken;
266+
encl_id = initp->addr;
267+
}
268+
231269
initp_page = alloc_page(GFP_HIGHUSER);
232270
if (!initp_page)
233271
return -ENOMEM;
@@ -241,16 +279,22 @@ static long sgx_ioc_enclave_init(struct file *filep, unsigned int cmd,
241279
if (ret)
242280
goto out;
243281

244-
ret = copy_from_user(einittoken, (void __user *)einittokenp,
245-
sizeof(*einittoken));
246-
if (ret)
247-
goto out;
282+
if (cmd != SGX_IOC_ENCLAVE_INIT_NO_TOKEN) {
283+
ret = copy_from_user(einittoken, (void __user *)einittokenp,
284+
sizeof(*einittoken));
285+
if (ret)
286+
goto out;
287+
} else {
288+
ret = sgx_get_key_hash(sigstruct->modulus, einittoken->payload.mrsigner);
289+
if (ret)
290+
goto out;
291+
}
248292

249293
ret = sgx_get_encl(encl_id, &encl);
250294
if (ret)
251295
goto out;
252296

253-
ret = sgx_encl_init(encl, sigstruct, einittoken);
297+
ret = sgx_encl_init(encl, sigstruct, einittoken, cmd == SGX_IOC_ENCLAVE_INIT_NO_TOKEN);
254298

255299
kref_put(&encl->refcount, sgx_encl_release);
256300

@@ -394,6 +438,7 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
394438
handler = sgx_ioc_enclave_add_page;
395439
break;
396440
case SGX_IOC_ENCLAVE_INIT:
441+
case SGX_IOC_ENCLAVE_INIT_NO_TOKEN:
397442
handler = sgx_ioc_enclave_init;
398443
break;
399444
case SGX_IOC_ENCLAVE_EMODPR:

sgx_main.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ u64 sgx_xfrm_mask = 0x3;
105105
u32 sgx_misc_reserved;
106106
u32 sgx_xsave_size_tbl[64];
107107
bool sgx_has_sgx2;
108+
bool sgx_dev_alt_name;
108109

109110
static int sgx_mmap(struct file *file, struct vm_area_struct *vma)
110111
{
@@ -168,6 +169,13 @@ static struct miscdevice sgx_dev = {
168169
.mode = 0666,
169170
};
170171

172+
static struct miscdevice sgx_dev_alt = {
173+
.minor = MISC_DYNAMIC_MINOR,
174+
.name = "sgx",
175+
.fops = &sgx_fops,
176+
.mode = 0666,
177+
};
178+
171179
static int sgx_pm_suspend(struct device *dev)
172180
{
173181
struct sgx_tgid_ctx *ctx;
@@ -184,7 +192,7 @@ static int sgx_pm_suspend(struct device *dev)
184192
return 0;
185193
}
186194

187-
static void sgx_reset_pubkey_hash(void *failed)
195+
void sgx_reset_pubkey_hash(void *failed)
188196
{
189197
if (wrmsrl_safe(MSR_IA32_SGXLEPUBKEYHASH0, 0xa6053e051270b7acULL) ||
190198
wrmsrl_safe(MSR_IA32_SGXLEPUBKEYHASH1, 0x6cfbe8ba8b3b413dULL) ||
@@ -275,7 +283,7 @@ static int sgx_dev_init(struct device *parent)
275283
}
276284

277285
sgx_dev.parent = parent;
278-
ret = misc_register(&sgx_dev);
286+
ret = misc_register(sgx_dev_alt_name ? &sgx_dev_alt : &sgx_dev);
279287
if (ret) {
280288
pr_err("intel_sgx: misc_register() failed\n");
281289
goto out_workqueue;
@@ -408,3 +416,6 @@ module_init(init_sgx_module);
408416
module_exit(cleanup_sgx_module);
409417

410418
MODULE_LICENSE("Dual BSD/GPL");
419+
420+
module_param_named(devsgx, sgx_dev_alt_name, bool, 0444);
421+
MODULE_PARM_DESC(sgx_dev_alt_name, "Device name is sgx instead of isgx");

sgx_user.h

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
_IOW(SGX_MAGIC, 0x01, struct sgx_enclave_add_page)
7070
#define SGX_IOC_ENCLAVE_INIT \
7171
_IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init)
72+
#define SGX_IOC_ENCLAVE_INIT_NO_TOKEN \
73+
_IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init_no_token)
7274
#define SGX_IOC_ENCLAVE_EMODPR \
7375
_IOW(SGX_MAGIC, 0x09, struct sgx_modification_param)
7476
#define SGX_IOC_ENCLAVE_MKTCS \
@@ -147,6 +149,17 @@ struct sgx_enclave_init {
147149
__u64 einittoken;
148150
} __attribute__((__packed__));
149151

152+
/**
153+
* struct sgx_enclave_init - parameter structure for the
154+
* %SGX_IOC_ENCLAVE_INIT ioctl
155+
* @addr: address in the ELRANGE
156+
* @sigstruct: address for the page data
157+
*/
158+
struct sgx_enclave_init_no_token {
159+
__u64 addr;
160+
__u64 sigstruct;
161+
} __attribute__((__packed__));
162+
150163
/*
151164
* SGX2.0 definitions
152165
*/

0 commit comments

Comments
 (0)