x86/ioperm: Extend IOPL config to control ioperm() as well
The following commit has been merged into the x86/iopl branch of tip:
Commit-ID: 111e7b15cf10f6e973ccf537c70c66a5de539060 Gitweb: https://git.kernel.org/tip/111e7b15cf10f6e973ccf537c70c66a5de539060 Author: Thomas Gleixner <[email protected]> AuthorDate: Tue, 12 Nov 2019 21:40:33 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Sat, 16 Nov 2019 11:24:06 +01:00
x86/ioperm: Extend IOPL config to control ioperm() as well
If iopl() is disabled, then providing ioperm() does not make much sense.
Rename the config option and disable/enable both syscalls with it. Guard the code with #ifdefs where appropriate.
--- arch/x86/Kconfig | 7 +++++-- arch/x86/include/asm/io_bitmap.h | 6 ++++++ arch/x86/include/asm/processor.h | 9 ++++++++- arch/x86/include/asm/thread_info.h | 7 ++++++- arch/x86/kernel/cpu/common.c | 26 +++++++++++++++++--------- arch/x86/kernel/ioport.c | 26 +++++++++++++++++++------- arch/x86/kernel/process.c | 4 ++++ 7 files changed, 65 insertions(+), 20 deletions(-)
arch/x86/Kconfig +5 -2
@@ -1254,10 +1254,13 @@ config X86_VSYSCALL_EMULATION Disabling this option saves about 7K of kernel size and possibly 4K of additional runtime pagetable memory. -config X86_IOPL_EMULATION - bool "IOPL Emulation" +config X86_IOPL_IOPERM + bool "IOPERM and IOPL Emulation" default y ---help--- + This enables the ioperm() and iopl() syscalls which are necessary + for legacy applications. + Legacy IOPL support is an overbroad mechanism which allows user space aside of accessing all 65536 I/O ports also to disable interrupts. To gain this access the caller needs CAP_SYS_RAWIO
arch/x86/include/asm/io_bitmap.h +6 -0
@@ -15,9 +15,15 @@ struct io_bitmap {struct task_struct ; +#ifdef CONFIG_X86_IOPL_IOPERM void io_bitmap_share (struct task_struct *tsk);void io_bitmap_exit (void );void tss_update_io_bitmap (void ); +#else +static inline void io_bitmap_share (struct task_struct *tsk) { } +static inline void io_bitmap_exit (void ) { } +static inline void tss_update_io_bitmap (void ) { } +#endif #endif
arch/x86/include/asm/processor.h +8 -1
@@ -340,13 +340,18 @@ struct x86_hw_tss { (offsetof(struct tss_struct, io_bitmap.mapall) - \ offsetof(struct tss_struct, x86_tss)) +#ifdef CONFIG_X86_IOPL_IOPERM /* * sizeof(unsigned long) coming from an extra "long" at the end of the * iobitmap. The limit is inclusive, i.e. the last valid byte. */ -#define __KERNEL_TSS_LIMIT \ +# define __KERNEL_TSS_LIMIT \ (IO_BITMAP_OFFSET_VALID_ALL + IO_BITMAP_BYTES + \ sizeof(unsigned long) - 1) +#else +# define __KERNEL_TSS_LIMIT \ + (offsetof(struct tss_struct, x86_tss) + sizeof(struct x86_hw_tss) - 1) +#endif /* Base offset outside of TSS_LIMIT so unpriviledged IO causes #GP */ #define IO_BITMAP_OFFSET_INVALID (__KERNEL_TSS_LIMIT + 1)
@@ -398,7 +403,9 @@ struct tss_struct {*/ struct x86_hw_tss x86_tss; +#ifdef CONFIG_X86_IOPL_IOPERM struct x86_io_bitmap io_bitmap; +#endif }__aligned(PAGE_SIZE) ; DECLARE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw);
arch/x86/include/asm/thread_info.h +6 -1
@@ -156,8 +156,13 @@ struct thread_info {# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE)#endif -#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY | \ +#ifdef CONFIG_X86_IOPL_IOPERM +# define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY | \ _TIF_IO_BITMAP) +#else +# define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY) +#endif +#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)#define STACK_WARN (THREAD_SIZE/8)
arch/x86/kernel/cpu/common.c +17 -9
@@ -1804,6 +1804,22 @@ static inline void gdt_setup_doublefault_tss(int cpu) }#endif /* !CONFIG_X86_64 */ +static inline void tss_setup_io_bitmap (struct tss_struct *tss) +{ + tss->x86_tss.io_bitmap_base =IO_BITMAP_OFFSET_INVALID ; + +#ifdef CONFIG_X86_IOPL_IOPERM + tss->io_bitmap.prev_max =0 ; + tss->io_bitmap.prev_sequence =0 ; +memset (tss->io_bitmap.bitmap,0xff ,sizeof (tss->io_bitmap.bitmap)); +/* +* Invalidate the extra array entry past the end of the all +* permission bitmap as required by the hardware. +*/ + tss->io_bitmap.mapall[IO_BITMAP_LONGS ] = ~0UL ; +#endif +} +/* * cpu_init() initializes state that is per-CPU. Some data is already * initialized (naturally) in the bootstrap process, such as the GDT
@@ -1860,15 +1876,7 @@ void cpu_init(void)/* Initialize the TSS. */ tss_setup_ist (tss); - tss->x86_tss.io_bitmap_base =IO_BITMAP_OFFSET_INVALID ; - tss->io_bitmap.prev_max =0 ; - tss->io_bitmap.prev_sequence =0 ; -memset (tss->io_bitmap.bitmap,0xff ,sizeof (tss->io_bitmap.bitmap)); -/* -* Invalidate the extra array entry past the end of the all -* permission bitmap as required by the hardware. -*/ - tss->io_bitmap.mapall[IO_BITMAP_LONGS ] = ~0UL ; +tss_setup_io_bitmap (tss);set_tss_desc (cpu, &get_cpu_entry_area (cpu)->tss.x86_tss);load_TR_desc ();
arch/x86/kernel/ioport.c +19 -7
@@ -14,6 +14,8 @@#include <asm/io_bitmap.h> #include <asm/desc.h> +#ifdef CONFIG_X86_IOPL_IOPERM +static atomic64_t io_bitmap_sequence;void io_bitmap_share (struct task_struct *tsk)
@@ -172,13 +174,6 @@ SYSCALL_DEFINE1(iopl, unsigned int, level)struct thread_struct *t = ¤t->thread;unsigned int old; -/* -* Careful: the IOPL bits in regs->flags are undefined under Xen PV -* and changing them has no effect. -*/ -if (IS_ENABLED (CONFIG_X86_IOPL_NONE )) -return -ENOSYS ; -if (level >3 )return -EINVAL ;
@@ -200,3 +195,20 @@ SYSCALL_DEFINE1(iopl, unsigned int, level)return 0 ; } + +#else /* CONFIG_X86_IOPL_IOPERM */ + +long ksys_ioperm (unsigned long from,unsigned long num,int turn_on) +{ +return -ENOSYS ; +} +SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on) +{ +return -ENOSYS ; +} + +SYSCALL_DEFINE1(iopl, unsigned int, level) +{ +return -ENOSYS ; +} +#endif
arch/x86/kernel/process.c +4 -0
@@ -322,6 +322,7 @@ void arch_setup_new_exec(void) } } +#ifdef CONFIG_X86_IOPL_IOPERM static inline void tss_invalidate_io_bitmap (struct tss_struct *tss) {/*
@@ -409,6 +410,9 @@ void tss_update_io_bitmap(void) tss_invalidate_io_bitmap(tss); } } +#else /* CONFIG_X86_IOPL_IOPERM */ +static inline void switch_to_bitmap (unsigned long tifp) { } +#endif #ifdef CONFIG_SMP
PATCH 1/1 x86/ioperm: Fix use of deprecated config option
From: Alexander Duyck <[email protected]>
In "x86/ioperm: Extend IOPL config to control ioperm()" the config option X86_IOPL_EMULATION was replaced with X86_IOPL_IOPERM. However it appears that there was at least one spot missed as tss_update_io_bitmap still had a reference to it contained in the code.
The result of this is that it exposed a NULL pointer dereference as seen below with a linux-next next-20191120 kernel:
[ 17.626562] BUG: kernel NULL pointer dereference, address: 0000000000000000 [ 17.626563] #PF: supervisor read access in kernel mode [ 17.626564] #PF: error_code(0x0000) - not-present page [ 17.626564] PGD 0 P4D 0 [ 17.626566] Oops: 0000 [#1] SMP PTI [ 17.626568] CPU: 5 PID: 1542 Comm: ovs-vswitchd Tainted: G W 5.4.0-rc8-next-20191120 #125 [ 17.626568] Hardware name: ASUSTeK COMPUTER INC. Z10PE-D8 WS/Z10PE-D8 WS, BIOS 3903 09/11/2018 [ 17.626570] RIP: 0010:tss_update_io_bitmap+0x4e/0x180 [ 17.626572] Code: 10 31 c0 65 48 03 1d 69 54 5d 6d 65 48 8b 04 25 40 8c 01 00 48 8b 10 f7 c2 00 00 40 00 0f 84 8c 00 00 00 4c 8b a0 c0 22 00 00 <49> 8b 04 24 48 39 43 68 74 2e 8b 53 70 41 39 54 24 0c 48 8d 7b 78 [ 17.626572] RSP: 0018:ffffb8888a0ebf08 EFLAGS: 00010006 [ 17.626573] RAX: ffff8a429811a680 RBX: ffff8a4c3f946000 RCX: 0000000000000011 [ 17.626574] RDX: 0000000000400080 RSI: 0000000000400080 RDI: 0000000000000000 [ 17.626575] RBP: ffffb8888a0ebf30 R08: 00007ffffb5d7ce0 R09: 0000000000000000 [ 17.626576] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000 [ 17.626576] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 [ 17.626578] FS: 00007f68a9635c40(0000) GS:ffff8a4c3f940000(0000) knlGS:0000000000000000 [ 17.626578] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 17.626579] CR2: 0000000000000000 CR3: 000000103572a001 CR4: 00000000001606e0 [ 17.626580] Call Trace: [ 17.626582] ? syscall_slow_exit_work+0x39/0xdb [ 17.626584] do_syscall_64+0x1a5/0x200 [ 17.626586] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 17.626587] RIP: 0033:0x7f68a7aff797 [ 17.626588] Code: 73 01 c3 48 8b 0d d9 86 2c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 b8 ac 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d a9 86 2c 00 f7 d8 64 89 01 48 [ 17.626588] RSP: 002b:00007ffffb5d8218 EFLAGS: 00000202 ORIG_RAX: 00000000000000ac [ 17.626589] RAX: 0000000000000000 RBX: 0000000000000014 RCX: 00007f68a7aff797 [ 17.626590] RDX: 00007ffffb5d8370 RSI: 00007ffffb5d8358 RDI: 0000000000000003 [ 17.626590] RBP: 00000000000000e6 R08: 0000000000000000 R09: 0000000000000000 [ 17.626591] R10: 00007ffffb5d7ce0 R11: 0000000000000202 R12: 0000564c9e3e2d80 [ 17.626592] R13: 00007ffffb5d8370 R14: 00007ffffb5d8358 R15: 0000000000000002 [ 17.626593] Modules linked in: iptable_filter sb_edac(+) acpi_cpufreq(-)
pcc_cpufreq(-) x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel snd_hda_codec_realtek snd_hda_codec_generic ledtrig_audio kvm snd_hda_intel snd_intel_dspcfg irqbypass snd_hda_codec snd_hda_core snd_hwdep crct10dif_pclmul eeepc_wmi crc32_pclmul snd_seq asus_wmi vfat snd_seq_device sparse_keymap rfkill snd_pcm ghash_clmulni_intel video wmi_bmof mxm_wmi fat aesni_intel snd_timer crypto_simd ixgbe snd cryptd mei_me mdio glue_helper pcspkr joydev dca i2c_i801 lpc_ich soundcore mei ipmi_si ipmi_devintf ipmi_msghandler wmi acpi_power_meter acpi_pad ip_tables xfs libcrc32c ast drm_vram_helper drm_ttm_helper ttm drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops drm ahci libahci crc32c_intel libata nvme nvme_core dm_mod
[ 17.626619] CR2: 0000000000000000 [ 17.626633] ---[ end trace e5d62f4aae005116 ]---
--- arch/x86/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/process.c +1 -2
@@ -382,8 +382,7 @@ void tss_update_io_bitmap(void)if (test_thread_flag (TIF_IO_BITMAP )) {struct thread_struct *t = ¤t->thread; -if (IS_ENABLED (CONFIG_X86_IOPL_EMULATION ) && - t->iopl_emul ==3 ) { +if (IS_ENABLED (CONFIG_X86_IOPL_IOPERM ) && t->iopl_emul ==3 ) { *base =IO_BITMAP_OFFSET_VALID_ALL ; }else {struct io_bitmap *iobm = t->io_bitmap;
Re: [x86/iopl PATCH] x86/ioperm: Fix use of deprecated config option
Quoted 4 lines · Alexander Duyck
From: Alexander Duyck <[email protected]>
Fixes 111e7b15cf10 ("x86/ioperm: Extend IOPL config to control ioperm() as well")Signed-off-by Alexander Duyck <[email protected]>
Duh, yes.
Quoted 20 lines
--- arch/x86/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/x86/kernel/process.c +1
-2@@ -382,8 +382,7 @@ void tss_update_io_bitmap(void)if (test_thread_flag (TIF_IO_BITMAP )) {struct thread_struct *t = ¤t->thread; -if (IS_ENABLED (CONFIG_X86_IOPL_EMULATION ) && - t->iopl_emul ==3 ) { +if (IS_ENABLED (CONFIG_X86_IOPL_IOPERM ) && t->iopl_emul ==3 ) { *base =IO_BITMAP_OFFSET_VALID_ALL ; }else {struct io_bitmap *iobm = t->io_bitmap;
[tip: x86/iopl] x86/ioperm: Fix use of deprecated config option
In reply to Alexander DuyckThe following commit has been merged into the x86/iopl branch of tip:
The following commit has been merged into the x86/iopl branch of tip:
Commit-ID: e3cb0c7102f04c83bf1a7cb1d052e92749310b46 Gitweb: https://git.kernel.org/tip/e3cb0c7102f04c83bf1a7cb1d052e92749310b46 Author: Alexander Duyck <[email protected]> AuthorDate: Wed, 20 Nov 2019 14:25:53 -08:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Nov 2019 23:40:05 +01:00
x86/ioperm: Fix use of deprecated config option
The commit
111e7b15cf10 ("x86/ioperm: Extend IOPL config to control ioperm() as well")
replaced X86_IOPL_EMULATION with X86_IOPL_IOPERM. However it appears that there was at least one spot missed as tss_update_io_bitmap() still had a reference to it contained in the code.
The result of this is that it exposed a NULL pointer dereference as seen below with a linux-next next-20191120 kernel:
BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] SMP PTI CPU: 5 PID: 1542 Comm: ovs-vswitchd Tainted: G W 5.4.0-rc8-next-20191120 #125 RIP: 0010:tss_update_io_bitmap+0x4e/0x180 Code: 10 31 c0 65 48 03 1d 69 54 5d 6d 65 48 8b 04 25 40 8c 01 00 48 8b 10 \ f7 c2 00 00 40 00 0f 84 8c 00 00 00 4c 8b a0 c0 22 00 00 <49> 8b 04 \ 24 48 39 43 68 74 2e 8b 53 70 41 39 54 24 0c 48 8d 7b 78 RSP: 0018:ffffb8888a0ebf08 EFLAGS: 00010006 RAX: ffff8a429811a680 RBX: ffff8a4c3f946000 RCX: 0000000000000011 RDX: 0000000000400080 RSI: 0000000000400080 RDI: 0000000000000000 RBP: ffffb8888a0ebf30 R08: 00007ffffb5d7ce0 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 FS: 00007f68a9635c40(0000) GS:ffff8a4c3f940000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000000 CR3: 000000103572a001 CR4: 00000000001606e0 Call Trace: ? syscall_slow_exit_work+0x39/0xdb do_syscall_64+0x1a5/0x200 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7f68a7aff797
--- arch/x86/kernel/process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/process.c +1 -2
@@ -382,8 +382,7 @@ void tss_update_io_bitmap(void)if (test_thread_flag (TIF_IO_BITMAP )) {struct thread_struct *t = ¤t->thread; -if (IS_ENABLED (CONFIG_X86_IOPL_EMULATION ) && - t->iopl_emul ==3 ) { +if (IS_ENABLED (CONFIG_X86_IOPL_IOPERM ) && t->iopl_emul ==3 ) { *base =IO_BITMAP_OFFSET_VALID_ALL ; }else {struct io_bitmap *iobm = t->io_bitmap;