License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* This contains the io-permission bitmap code - written by obz, with changes
|
2008-01-30 20:33:10 +08:00
|
|
|
* by Linus. 32/64 bits code unification by Miguel Botón.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2006-01-12 04:17:48 +08:00
|
|
|
#include <linux/capability.h>
|
2019-08-20 08:17:48 +08:00
|
|
|
#include <linux/security.h>
|
2007-05-03 01:27:10 +08:00
|
|
|
#include <linux/syscalls.h>
|
2011-02-16 22:48:35 +08:00
|
|
|
#include <linux/bitmap.h>
|
2019-11-12 06:03:19 +08:00
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
2019-11-12 06:03:21 +08:00
|
|
|
#include <asm/io_bitmap.h>
|
2017-02-21 00:56:14 +08:00
|
|
|
#include <asm/desc.h>
|
2020-01-23 21:30:51 +08:00
|
|
|
#include <asm/syscalls.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-11-13 04:40:33 +08:00
|
|
|
#ifdef CONFIG_X86_IOPL_IOPERM
|
|
|
|
|
2019-11-12 06:03:22 +08:00
|
|
|
static atomic64_t io_bitmap_sequence;
|
|
|
|
|
2019-11-12 06:03:25 +08:00
|
|
|
void io_bitmap_share(struct task_struct *tsk)
|
2019-11-12 06:03:28 +08:00
|
|
|
{
|
|
|
|
/* Can be NULL when current->thread.iopl_emul == 3 */
|
|
|
|
if (current->thread.io_bitmap) {
|
|
|
|
/*
|
|
|
|
* Take a refcount on current's bitmap. It can be used by
|
|
|
|
* both tasks as long as none of them changes the bitmap.
|
|
|
|
*/
|
|
|
|
refcount_inc(¤t->thread.io_bitmap->refcnt);
|
|
|
|
tsk->thread.io_bitmap = current->thread.io_bitmap;
|
|
|
|
}
|
2019-11-12 06:03:25 +08:00
|
|
|
set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
|
|
|
|
}
|
|
|
|
|
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-25 00:27:39 +08:00
|
|
|
static void task_update_io_bitmap(struct task_struct *tsk)
|
2019-11-12 06:03:28 +08:00
|
|
|
{
|
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-25 00:27:39 +08:00
|
|
|
struct thread_struct *t = &tsk->thread;
|
2019-11-12 06:03:28 +08:00
|
|
|
|
|
|
|
if (t->iopl_emul == 3 || t->io_bitmap) {
|
|
|
|
/* TSS update is handled on exit to user space */
|
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-25 00:27:39 +08:00
|
|
|
set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
|
2019-11-12 06:03:28 +08:00
|
|
|
} else {
|
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-25 00:27:39 +08:00
|
|
|
clear_tsk_thread_flag(tsk, TIF_IO_BITMAP);
|
2019-11-12 06:03:28 +08:00
|
|
|
/* Invalidate TSS */
|
|
|
|
preempt_disable();
|
|
|
|
tss_update_io_bitmap();
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-25 00:27:39 +08:00
|
|
|
void io_bitmap_exit(struct task_struct *tsk)
|
2019-11-12 06:03:24 +08:00
|
|
|
{
|
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-25 00:27:39 +08:00
|
|
|
struct io_bitmap *iobm = tsk->thread.io_bitmap;
|
2019-11-12 06:03:24 +08:00
|
|
|
|
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-25 00:27:39 +08:00
|
|
|
tsk->thread.io_bitmap = NULL;
|
|
|
|
task_update_io_bitmap(tsk);
|
2019-11-12 06:03:25 +08:00
|
|
|
if (iobm && refcount_dec_and_test(&iobm->refcnt))
|
|
|
|
kfree(iobm);
|
2019-11-12 06:03:24 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-11-12 06:03:27 +08:00
|
|
|
* This changes the io permissions bitmap in the current task.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2018-03-11 18:34:38 +08:00
|
|
|
long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-01-04 18:59:32 +08:00
|
|
|
struct thread_struct *t = ¤t->thread;
|
2019-11-12 06:03:23 +08:00
|
|
|
unsigned int i, max_long;
|
2019-11-12 06:03:21 +08:00
|
|
|
struct io_bitmap *iobm;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
|
|
|
|
return -EINVAL;
|
2019-08-20 08:17:48 +08:00
|
|
|
if (turn_on && (!capable(CAP_SYS_RAWIO) ||
|
|
|
|
security_locked_down(LOCKDOWN_IOPORT)))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's the first ioperm() call in this thread's lifetime, set the
|
|
|
|
* IO bitmap up. ioperm() is much less timing critical than clone(),
|
|
|
|
* this is why we delay this operation until now:
|
|
|
|
*/
|
2019-11-12 06:03:21 +08:00
|
|
|
iobm = t->io_bitmap;
|
|
|
|
if (!iobm) {
|
2019-11-13 02:56:19 +08:00
|
|
|
/* No point to allocate a bitmap just to clear permissions */
|
|
|
|
if (!turn_on)
|
|
|
|
return 0;
|
2019-11-12 06:03:21 +08:00
|
|
|
iobm = kmalloc(sizeof(*iobm), GFP_KERNEL);
|
|
|
|
if (!iobm)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-11-12 06:03:21 +08:00
|
|
|
memset(iobm->bitmap, 0xff, sizeof(iobm->bitmap));
|
2019-11-12 06:03:25 +08:00
|
|
|
refcount_set(&iobm->refcnt, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the bitmap is not shared, then nothing can take a refcount as
|
|
|
|
* current can obviously not fork at the same time. If it's shared
|
|
|
|
* duplicate it and drop the refcount on the original one.
|
|
|
|
*/
|
|
|
|
if (refcount_read(&iobm->refcnt) > 1) {
|
|
|
|
iobm = kmemdup(iobm, sizeof(*iobm), GFP_KERNEL);
|
|
|
|
if (!iobm)
|
|
|
|
return -ENOMEM;
|
|
|
|
refcount_set(&iobm->refcnt, 1);
|
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-25 00:27:39 +08:00
|
|
|
io_bitmap_exit(current);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-11-12 06:03:25 +08:00
|
|
|
/*
|
|
|
|
* Store the bitmap pointer (might be the same if the task already
|
|
|
|
* head one). Must be done here so freeing the bitmap when all
|
|
|
|
* permissions are dropped has the pointer set up.
|
|
|
|
*/
|
|
|
|
t->io_bitmap = iobm;
|
|
|
|
/* Mark it active for context switching and exit to user mode */
|
|
|
|
set_thread_flag(TIF_IO_BITMAP);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-11-12 06:03:23 +08:00
|
|
|
* Update the tasks bitmap. The update of the TSS bitmap happens on
|
|
|
|
* exit to user mode. So this needs no protection.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2011-02-16 22:48:35 +08:00
|
|
|
if (turn_on)
|
2019-11-12 06:03:21 +08:00
|
|
|
bitmap_clear(iobm->bitmap, from, num);
|
2011-02-16 22:48:35 +08:00
|
|
|
else
|
2019-11-12 06:03:21 +08:00
|
|
|
bitmap_set(iobm->bitmap, from, num);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a (possibly new) maximum. This is simple and stupid,
|
|
|
|
* to keep it obviously correct:
|
|
|
|
*/
|
2019-11-12 06:03:24 +08:00
|
|
|
max_long = UINT_MAX;
|
2019-11-13 02:05:31 +08:00
|
|
|
for (i = 0; i < IO_BITMAP_LONGS; i++) {
|
2019-11-12 06:03:21 +08:00
|
|
|
if (iobm->bitmap[i] != ~0UL)
|
2005-04-17 06:20:36 +08:00
|
|
|
max_long = i;
|
2019-11-13 02:05:31 +08:00
|
|
|
}
|
2019-11-12 06:03:24 +08:00
|
|
|
/* All permissions dropped? */
|
|
|
|
if (max_long == UINT_MAX) {
|
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-25 00:27:39 +08:00
|
|
|
io_bitmap_exit(current);
|
2019-11-12 06:03:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-11-12 06:03:23 +08:00
|
|
|
iobm->max = (max_long + 1) * sizeof(unsigned long);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-11-13 02:05:31 +08:00
|
|
|
/*
|
2019-11-12 06:03:25 +08:00
|
|
|
* Update the sequence number to force a TSS update on return to
|
|
|
|
* user mode.
|
2019-11-13 02:05:31 +08:00
|
|
|
*/
|
2019-11-12 06:03:25 +08:00
|
|
|
iobm->sequence = atomic64_add_return(1, &io_bitmap_sequence);
|
2008-01-30 20:33:10 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:34:38 +08:00
|
|
|
SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on)
|
|
|
|
{
|
|
|
|
return ksys_ioperm(from, num, turn_on);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-11-12 06:03:27 +08:00
|
|
|
* The sys_iopl functionality depends on the level argument, which if
|
2019-11-12 06:03:29 +08:00
|
|
|
* granted for the task is used to enable access to all 65536 I/O ports.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2019-11-12 06:03:29 +08:00
|
|
|
* This does not use the IOPL mechanism provided by the CPU as that would
|
|
|
|
* also allow the user space task to use the CLI/STI instructions.
|
2019-11-12 06:03:27 +08:00
|
|
|
*
|
2019-11-12 06:03:29 +08:00
|
|
|
* Disabling interrupts in a user space task is dangerous as it might lock
|
|
|
|
* up the machine and the semantics vs. syscalls and exceptions is
|
|
|
|
* undefined.
|
2019-11-12 06:03:27 +08:00
|
|
|
*
|
2019-11-12 06:03:29 +08:00
|
|
|
* Setting IOPL to level 0-2 is disabling I/O permissions. Level 3
|
|
|
|
* 3 enables them.
|
2019-11-12 06:03:27 +08:00
|
|
|
*
|
|
|
|
* IOPL is strictly per thread and inherited on fork.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2012-11-20 11:00:52 +08:00
|
|
|
SYSCALL_DEFINE1(iopl, unsigned int, level)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-12-10 08:01:52 +08:00
|
|
|
struct thread_struct *t = ¤t->thread;
|
2019-11-12 06:03:28 +08:00
|
|
|
unsigned int old;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (level > 3)
|
|
|
|
return -EINVAL;
|
2019-11-12 06:03:28 +08:00
|
|
|
|
2019-11-12 06:03:29 +08:00
|
|
|
old = t->iopl_emul;
|
2019-11-12 06:03:28 +08:00
|
|
|
|
|
|
|
/* No point in going further if nothing changes */
|
|
|
|
if (level == old)
|
|
|
|
return 0;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Trying to gain more privileges? */
|
|
|
|
if (level > old) {
|
2019-08-20 08:17:48 +08:00
|
|
|
if (!capable(CAP_SYS_RAWIO) ||
|
|
|
|
security_locked_down(LOCKDOWN_IOPORT))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EPERM;
|
|
|
|
}
|
2019-11-12 06:03:28 +08:00
|
|
|
|
2019-11-12 06:03:29 +08:00
|
|
|
t->iopl_emul = level;
|
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-25 00:27:39 +08:00
|
|
|
task_update_io_bitmap(current);
|
2009-12-10 08:01:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-13 04:40:33 +08:00
|
|
|
|
|
|
|
#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
|