2019-11-11 22:03:21 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _ASM_X86_IOBITMAP_H
|
|
|
|
#define _ASM_X86_IOBITMAP_H
|
|
|
|
|
2019-11-11 22:03:25 +00:00
|
|
|
#include <linux/refcount.h>
|
2019-11-11 22:03:21 +00:00
|
|
|
#include <asm/processor.h>
|
|
|
|
|
|
|
|
struct io_bitmap {
|
2019-11-11 22:03:22 +00:00
|
|
|
u64 sequence;
|
2019-11-11 22:03:25 +00:00
|
|
|
refcount_t refcnt;
|
2019-11-11 22:03:21 +00:00
|
|
|
/* The maximum number of bytes to copy so all zero bits are covered */
|
|
|
|
unsigned int max;
|
|
|
|
unsigned long bitmap[IO_BITMAP_LONGS];
|
|
|
|
};
|
|
|
|
|
2019-11-11 22:03:25 +00:00
|
|
|
struct task_struct;
|
|
|
|
|
2019-11-12 20:40:33 +00:00
|
|
|
#ifdef CONFIG_X86_IOPL_IOPERM
|
2019-11-11 22:03:25 +00:00
|
|
|
void io_bitmap_share(struct task_struct *tsk);
|
x86/ioperm: Prevent a memory leak when fork fails
In the copy_process() routine called by _do_fork(), failure to allocate
a PID (or further along in the function) will trigger an invocation to
exit_thread(). This is done to clean up from an earlier call to
copy_thread_tls(). Naturally, the child task is passed into exit_thread(),
however during the process, io_bitmap_exit() nullifies the parent's
io_bitmap rather than the child's.
As copy_thread_tls() has been called ahead of the failure, the reference
count on the calling thread's io_bitmap is incremented as we would expect.
However, io_bitmap_exit() doesn't accept any arguments, and thus assumes
it should trash the current thread's io_bitmap reference rather than the
child's. This is pretty sneaky in practice, because in all instances but
this one, exit_thread() is called with respect to the current task and
everything works out.
A determined attacker can issue an appropriate ioctl (i.e. KDENABIO) to
get a bitmap allocated, and force a clone3() syscall to fail by passing
in a zeroed clone_args structure. The kernel handles the erroneous struct
and the buggy code path is followed, and even though the parent's reference
to the io_bitmap is trashed, the child still holds a reference and thus
the structure will never be freed.
Fix this by tweaking io_bitmap_exit() and its subroutines to accept a
task_struct argument which to operate on.
Fixes: ea5f1cd7ab49 ("x86/ioperm: Remove bitmap if all permissions dropped")
Signed-off-by: Jay Lang <jaytlang@mit.edu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable#@vger.kernel.org
Link: https://lkml.kernel.org/r/20200524162742.253727-1-jaytlang@mit.edu
2020-05-24 16:27:39 +00:00
|
|
|
void io_bitmap_exit(struct task_struct *tsk);
|
2019-11-11 22:03:24 +00:00
|
|
|
|
2020-07-17 23:53:55 +00:00
|
|
|
static inline void native_tss_invalidate_io_bitmap(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Invalidate the I/O bitmap by moving io_bitmap_base outside the
|
|
|
|
* TSS limit so any subsequent I/O access from user space will
|
|
|
|
* trigger a #GP.
|
|
|
|
*
|
|
|
|
* This is correct even when VMEXIT rewrites the TSS limit
|
|
|
|
* to 0x67 as the only requirement is that the base points
|
|
|
|
* outside the limit.
|
|
|
|
*/
|
|
|
|
this_cpu_write(cpu_tss_rw.x86_tss.io_bitmap_base,
|
|
|
|
IO_BITMAP_OFFSET_INVALID);
|
|
|
|
}
|
|
|
|
|
2020-02-18 15:47:12 +00:00
|
|
|
void native_tss_update_io_bitmap(void);
|
|
|
|
|
|
|
|
#ifdef CONFIG_PARAVIRT_XXL
|
|
|
|
#include <asm/paravirt.h>
|
|
|
|
#else
|
|
|
|
#define tss_update_io_bitmap native_tss_update_io_bitmap
|
2020-07-17 23:53:55 +00:00
|
|
|
#define tss_invalidate_io_bitmap native_tss_invalidate_io_bitmap
|
2020-02-18 15:47:12 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-12 20:40:33 +00:00
|
|
|
#else
|
|
|
|
static inline void io_bitmap_share(struct task_struct *tsk) { }
|
x86/ioperm: Prevent a memory leak when fork fails
In the copy_process() routine called by _do_fork(), failure to allocate
a PID (or further along in the function) will trigger an invocation to
exit_thread(). This is done to clean up from an earlier call to
copy_thread_tls(). Naturally, the child task is passed into exit_thread(),
however during the process, io_bitmap_exit() nullifies the parent's
io_bitmap rather than the child's.
As copy_thread_tls() has been called ahead of the failure, the reference
count on the calling thread's io_bitmap is incremented as we would expect.
However, io_bitmap_exit() doesn't accept any arguments, and thus assumes
it should trash the current thread's io_bitmap reference rather than the
child's. This is pretty sneaky in practice, because in all instances but
this one, exit_thread() is called with respect to the current task and
everything works out.
A determined attacker can issue an appropriate ioctl (i.e. KDENABIO) to
get a bitmap allocated, and force a clone3() syscall to fail by passing
in a zeroed clone_args structure. The kernel handles the erroneous struct
and the buggy code path is followed, and even though the parent's reference
to the io_bitmap is trashed, the child still holds a reference and thus
the structure will never be freed.
Fix this by tweaking io_bitmap_exit() and its subroutines to accept a
task_struct argument which to operate on.
Fixes: ea5f1cd7ab49 ("x86/ioperm: Remove bitmap if all permissions dropped")
Signed-off-by: Jay Lang <jaytlang@mit.edu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable#@vger.kernel.org
Link: https://lkml.kernel.org/r/20200524162742.253727-1-jaytlang@mit.edu
2020-05-24 16:27:39 +00:00
|
|
|
static inline void io_bitmap_exit(struct task_struct *tsk) { }
|
2019-11-12 20:40:33 +00:00
|
|
|
static inline void tss_update_io_bitmap(void) { }
|
|
|
|
#endif
|
2019-11-11 22:03:23 +00:00
|
|
|
|
2019-11-11 22:03:21 +00:00
|
|
|
#endif
|