Re: [intel-sgx-kernel-dev] [PATCH v5 10/11] intel_sgx: glue code for in-kernel LE

From: Sean Christopherson
Date: Tue Nov 14 2017 - 13:20:45 EST


On Mon, 2017-11-13 at 21:45 +0200, Jarkko Sakkinen wrote:
> --- a/drivers/platform/x86/intel_sgx/sgx_main.c
> +++ b/drivers/platform/x86/intel_sgx/sgx_main.c
> @@ -88,6 +88,37 @@ u64 sgx_encl_size_max_64;
> Âu64 sgx_xfrm_mask = 0x3;
> Âu32 sgx_misc_reserved;
> Âu32 sgx_xsave_size_tbl[64];
> +bool sgx_unlocked_msrs;
> +u64 sgx_le_pubkeyhash[4];
> +
> +static DECLARE_RWSEM(sgx_file_sem);
> +
> +static int sgx_open(struct inode *inode, struct file *file)
> +{
> + int ret;
> +
> + down_read(&sgx_file_sem);
> +
> + ret = sgx_le_start(&sgx_le_ctx);
> + if (ret) {
> + up_read(&sgx_file_sem);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int sgx_release(struct inode *inode, struct file *file)
> +{
> + up_read(&sgx_file_sem);
> +
> + if (down_write_trylock(&sgx_file_sem)) {
> + sgx_le_stop(&sgx_le_ctx);
> + up_write(&sgx_file_sem);
> + }
> +
> + return 0;
> +}

This semaphore approach is broken due to the LE process using an anon inode for
/dev/sgx, which results in sgx_release being called without an accompanying call
to sgx_open. ÂThis causes deadlocks due to a semaphore underrun.

https://lists.01.org/pipermail/intel-sgx-kernel-dev/2017-November/000901.html

[ÂÂ242.659272] INFO: task lsdt:9425 blocked for more than 120 seconds.
[ÂÂ242.659783]ÂÂÂÂÂÂÂNot tainted 4.14.0-rc4+ #18
[ÂÂ242.660063] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables thisÂ
[ÂÂ242.660558] lsdtÂÂÂÂÂÂÂÂÂÂÂÂDÂÂÂÂ0ÂÂ9425ÂÂÂÂÂÂ1 0x00000004
[ÂÂ242.660559] Call Trace:
[ÂÂ242.660564]ÂÂ__schedule+0x3c2/0x8b0
[ÂÂ242.660567]ÂÂschedule+0x36/0x80
[ÂÂ242.660568]ÂÂrwsem_down_read_failed+0x10a/0x170
[ÂÂ242.660569]ÂÂcall_rwsem_down_read_failed+0x18/0x30
[ÂÂ242.660570]ÂÂ? call_rwsem_down_read_failed+0x18/0x30
[ÂÂ242.660571]ÂÂdown_read+0x20/0x40
[ÂÂ242.660572]ÂÂsgx_open+0x19/0x40 [intel_sgx]
[ÂÂ242.660574]ÂÂchrdev_open+0xbf/0x1b0
[ÂÂ242.660576]ÂÂdo_dentry_open+0x1f8/0x300
[ÂÂ242.660577]ÂÂ? cdev_put+0x30/0x30
[ÂÂ242.660578]ÂÂvfs_open+0x4f/0x70
[ÂÂ242.660579]ÂÂpath_openat+0x2ae/0x13a0
[ÂÂ242.660581]ÂÂ? mem_cgroup_uncharge_swap+0x60/0x90
[ÂÂ242.660582]ÂÂdo_filp_open+0x99/0x110
[ÂÂ242.660583]ÂÂ? __check_object_size+0xfc/0x1a0
[ÂÂ242.660585]ÂÂ? __alloc_fd+0xb0/0x170
[ÂÂ242.660586]ÂÂdo_sys_open+0x124/0x210
[ÂÂ242.660587]ÂÂ? do_sys_open+0x124/0x210
[ÂÂ242.660588]ÂÂSyS_open+0x1e/0x20
[ÂÂ242.660589]ÂÂentry_SYSCALL_64_fastpath+0x1e/0xa9
[ÂÂ242.660590] RIP: 0033:0x7f426cf9ec7d
[ÂÂ242.660591] RSP: 002b:00007f426b31ea60 EFLAGS: 00000293 ORIG_RAX:Â
[ÂÂ242.660592] RAX: ffffffffffffffda RBX: 000000c4200ba000 RCX: 00007f426cf9ec7d
[ÂÂ242.660592] RDX: 0000000000000000 RSI: 0000000000000002 RDI: 000000000068cca7
[ÂÂ242.660593] RBP: 00007f426b31ec10 R08: 0000000000f6bc30 R09: 0000000000000000
[ÂÂ242.660593] R10: 00007f4264000078 R11: 0000000000000293 R12: 0000000000000001
[ÂÂ242.660594] R13: 0000000000000000 R14: 00007f426d31b13d R15: 00007f42640008c0

> Â#ifdef CONFIG_COMPAT
> Âlong sgx_compat_ioctl(struct file *filep, unsigned int cmd, unsigned long
> arg)
> @@ -141,8 +172,10 @@ static unsigned long sgx_get_unmapped_area(struct file
> *file,
> Â return addr;
> Â}
> Â
> -static const struct file_operations sgx_fops = {
> +const struct file_operations sgx_fops = {
> Â .owner = THIS_MODULE,
> + .open = sgx_open,
> + .release = sgx_release,
> Â .unlocked_ioctl = sgx_ioctl,
> Â#ifdef CONFIG_COMPAT
> Â .compat_ioctl = sgx_compat_ioctl,