Message ID | 20200915112842.897265-16-jarkko.sakkinen@linux.intel.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Intel SGX foundations | expand |
On Tue, Sep 15, 2020 at 02:28:33PM +0300, Jarkko Sakkinen wrote: > @@ -181,5 +192,12 @@ int __init sgx_drv_init(void) > return ret; > } > > + ret = misc_register(&sgx_dev_provision); > + if (ret) { > + pr_err("Creating /dev/sgx/provision failed with %d.\n", ret); > + misc_deregister(&sgx_dev_enclave); The comment over misc_deregister() says: * Unregister a miscellaneous device that was previously * successfully registered with misc_register(). but this is not a successful registration here, in the if (ret) case... > + return ret; > + } > + > return 0; > } > diff --git a/arch/x86/kernel/cpu/sgx/driver.h b/arch/x86/kernel/cpu/sgx/driver.h > index e4063923115b..72747d01c046 100644 > --- a/arch/x86/kernel/cpu/sgx/driver.h > +++ b/arch/x86/kernel/cpu/sgx/driver.h > @@ -23,6 +23,8 @@ extern u64 sgx_attributes_reserved_mask; > extern u64 sgx_xfrm_reserved_mask; > extern u32 sgx_xsave_size_tbl[64]; > > +extern const struct file_operations sgx_provision_fops; > + > long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg); > > int sgx_drv_init(void); > diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c > index de2ed4f35ffb..4227bca7b477 100644 > --- a/arch/x86/kernel/cpu/sgx/ioctl.c > +++ b/arch/x86/kernel/cpu/sgx/ioctl.c > @@ -673,6 +673,50 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg) > return ret; > } > > +/** > + * sgx_ioc_enclave_set_attribute - handler for %SGX_IOC_ENCLAVE_PROVISION > + * @filep: open file to /dev/sgx ^^^^^^ Can you guess what my comment to that would be...? > + * @arg: userspace pointer to a struct sgx_enclave_provision instance > + * > + * Mark the enclave as being allowed to access a restricted attribute bit. > + * The requested attribute is specified via the attribute_fd field in the > + * provided struct sgx_enclave_provision. The attribute_fd must be a > + * handle to an SGX attribute file, e.g. "/dev/sgx/provision". > + * > + * Failure to explicitly request access to a restricted attribute will cause > + * sgx_ioc_enclave_init() to fail. Currently, the only restricted attribute > + * is access to the PROVISION_KEY. > + * > + * Note, access to the EINITTOKEN_KEY is disallowed entirely. > + * > + * Return: 0 on success, -errno otherwise > + */ > +static long sgx_ioc_enclave_provision(struct sgx_encl *encl, > + void __user *arg) No need for the line break: both function args can fit on the same line. ...
On Mon, Sep 21, 2020 at 08:07:28PM +0200, Borislav Petkov wrote: > On Tue, Sep 15, 2020 at 02:28:33PM +0300, Jarkko Sakkinen wrote: > > @@ -181,5 +192,12 @@ int __init sgx_drv_init(void) > > return ret; > > } > > > > + ret = misc_register(&sgx_dev_provision); > > + if (ret) { > > + pr_err("Creating /dev/sgx/provision failed with %d.\n", ret); > > + misc_deregister(&sgx_dev_enclave); > > The comment over misc_deregister() says: > > * Unregister a miscellaneous device that was previously > * successfully registered with misc_register(). > > but this is not a successful registration here, in the if (ret) case... 'sgx_dev_enclave' is successfully register whenever that happens. Am I missing something here? > > + * sgx_ioc_enclave_set_attribute - handler for %SGX_IOC_ENCLAVE_PROVISION > > + * @filep: open file to /dev/sgx > ^^^^^^ > > Can you guess what my comment to that would be...? There is also another incosistency that I fixed: the first line should have 'sgx_ioc_enclave_provision'. > > +static long sgx_ioc_enclave_provision(struct sgx_encl *encl, > > + void __user *arg) > > No need for the line break: both function args can fit on the same line. Fixed this one too, thanks. > > ... > > -- > Regards/Gruss, > Boris. > > https://people.kernel.org/tglx/notes-about-netiquette /Jarkko
On Mon, Sep 21, 2020 at 10:27:06PM +0300, Jarkko Sakkinen wrote: > 'sgx_dev_enclave' is successfully register whenever that happens. Am I > missing something here? No, I was missing the fact that you have *two* misc devices and you're deregistering the first one in the error path of the second. Forget what I said. ;-\
On Mon, Sep 21, 2020 at 09:41:56PM +0200, Borislav Petkov wrote: > On Mon, Sep 21, 2020 at 10:27:06PM +0300, Jarkko Sakkinen wrote: > > 'sgx_dev_enclave' is successfully register whenever that happens. Am I > > missing something here? > > No, I was missing the fact that you have *two* misc devices and you're > deregistering the first one in the error path of the second. > > Forget what I said. ;-\ Ok, great. BTW, probably would make sense to rewrite the short summary as: "x86/sgx: Add SGX_IOC_ENCLAVE_PROVISION" for the sake of consistency with the three precursoring patches. > -- > Regards/Gruss, > Boris. > > https://people.kernel.org/tglx/notes-about-netiquette /Jarkko
On Tue, Sep 22, 2020 at 12:26:38AM +0300, Jarkko Sakkinen wrote: > BTW, probably would make sense to rewrite the short summary as: > > "x86/sgx: Add SGX_IOC_ENCLAVE_PROVISION" > > for the sake of consistency with the three precursoring patches. Ok. Thx.
diff --git a/arch/x86/include/uapi/asm/sgx.h b/arch/x86/include/uapi/asm/sgx.h index 7729730d8580..d0916fb9629e 100644 --- a/arch/x86/include/uapi/asm/sgx.h +++ b/arch/x86/include/uapi/asm/sgx.h @@ -25,6 +25,8 @@ enum sgx_page_flags { _IOWR(SGX_MAGIC, 0x01, struct sgx_enclave_add_pages) #define SGX_IOC_ENCLAVE_INIT \ _IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init) +#define SGX_IOC_ENCLAVE_PROVISION \ + _IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision) /** * struct sgx_enclave_create - parameter structure for the @@ -61,4 +63,13 @@ struct sgx_enclave_init { __u64 sigstruct; }; +/** + * struct sgx_enclave_provision - parameter structure for the + * %SGX_IOC_ENCLAVE_PROVISION ioctl + * @attribute_fd: file handle of the attribute file in the securityfs + */ +struct sgx_enclave_provision { + __u64 attribute_fd; +}; + #endif /* _UAPI_ASM_X86_SGX_H */ diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c index 7bdb49dfcca6..d01b28f7ce4a 100644 --- a/arch/x86/kernel/cpu/sgx/driver.c +++ b/arch/x86/kernel/cpu/sgx/driver.c @@ -134,6 +134,10 @@ static const struct file_operations sgx_encl_fops = { .get_unmapped_area = sgx_get_unmapped_area, }; +const struct file_operations sgx_provision_fops = { + .owner = THIS_MODULE, +}; + static struct miscdevice sgx_dev_enclave = { .minor = MISC_DYNAMIC_MINOR, .name = "enclave", @@ -141,6 +145,13 @@ static struct miscdevice sgx_dev_enclave = { .fops = &sgx_encl_fops, }; +static struct miscdevice sgx_dev_provision = { + .minor = MISC_DYNAMIC_MINOR, + .name = "provision", + .nodename = "sgx/provision", + .fops = &sgx_provision_fops, +}; + int __init sgx_drv_init(void) { unsigned int eax, ebx, ecx, edx; @@ -181,5 +192,12 @@ int __init sgx_drv_init(void) return ret; } + ret = misc_register(&sgx_dev_provision); + if (ret) { + pr_err("Creating /dev/sgx/provision failed with %d.\n", ret); + misc_deregister(&sgx_dev_enclave); + return ret; + } + return 0; } diff --git a/arch/x86/kernel/cpu/sgx/driver.h b/arch/x86/kernel/cpu/sgx/driver.h index e4063923115b..72747d01c046 100644 --- a/arch/x86/kernel/cpu/sgx/driver.h +++ b/arch/x86/kernel/cpu/sgx/driver.h @@ -23,6 +23,8 @@ extern u64 sgx_attributes_reserved_mask; extern u64 sgx_xfrm_reserved_mask; extern u32 sgx_xsave_size_tbl[64]; +extern const struct file_operations sgx_provision_fops; + long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg); int sgx_drv_init(void); diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c index de2ed4f35ffb..4227bca7b477 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -673,6 +673,50 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg) return ret; } +/** + * sgx_ioc_enclave_set_attribute - handler for %SGX_IOC_ENCLAVE_PROVISION + * @filep: open file to /dev/sgx + * @arg: userspace pointer to a struct sgx_enclave_provision instance + * + * Mark the enclave as being allowed to access a restricted attribute bit. + * The requested attribute is specified via the attribute_fd field in the + * provided struct sgx_enclave_provision. The attribute_fd must be a + * handle to an SGX attribute file, e.g. "/dev/sgx/provision". + * + * Failure to explicitly request access to a restricted attribute will cause + * sgx_ioc_enclave_init() to fail. Currently, the only restricted attribute + * is access to the PROVISION_KEY. + * + * Note, access to the EINITTOKEN_KEY is disallowed entirely. + * + * Return: 0 on success, -errno otherwise + */ +static long sgx_ioc_enclave_provision(struct sgx_encl *encl, + void __user *arg) +{ + struct sgx_enclave_provision params; + struct file *attribute_file; + int ret; + + if (copy_from_user(¶ms, arg, sizeof(params))) + return -EFAULT; + + attribute_file = fget(params.attribute_fd); + if (!attribute_file) + return -EINVAL; + + if (attribute_file->f_op != &sgx_provision_fops) { + ret = -EINVAL; + goto out; + } + + encl->attributes |= SGX_ATTR_PROVISIONKEY; + ret = 0; + +out: + fput(attribute_file); + return ret; +} long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { @@ -698,6 +742,9 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) case SGX_IOC_ENCLAVE_INIT: ret = sgx_ioc_enclave_init(encl, (void __user *)arg); break; + case SGX_IOC_ENCLAVE_PROVISION: + ret = sgx_ioc_enclave_provision(encl, (void __user *)arg); + break; default: ret = -ENOIOCTLCMD; break;