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
|
2016-06-27 13:17:25 +08:00
|
|
|
/*
|
|
|
|
* This is for all the tests related to logic bugs (e.g. bad dereferences,
|
|
|
|
* bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
|
|
|
|
* lockups) along with other things that don't fit well into existing LKDTM
|
|
|
|
* test source files.
|
|
|
|
*/
|
|
|
|
#include "lkdtm.h"
|
2016-08-18 05:42:12 +08:00
|
|
|
#include <linux/list.h>
|
2016-07-16 07:04:39 +08:00
|
|
|
#include <linux/sched.h>
|
2017-03-25 01:51:25 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2017-08-05 04:04:21 +08:00
|
|
|
#include <linux/sched/task_stack.h>
|
2017-03-25 01:51:25 +08:00
|
|
|
#include <linux/uaccess.h>
|
2020-04-07 11:12:34 +08:00
|
|
|
#include <linux/slab.h>
|
2016-06-27 13:17:25 +08:00
|
|
|
|
2020-06-26 04:37:04 +08:00
|
|
|
#if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
|
2019-11-25 13:18:04 +08:00
|
|
|
#include <asm/desc.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-18 05:42:12 +08:00
|
|
|
struct lkdtm_list {
|
|
|
|
struct list_head node;
|
|
|
|
};
|
|
|
|
|
2016-06-27 13:17:25 +08:00
|
|
|
/*
|
|
|
|
* Make sure our attempts to over run the kernel stack doesn't trigger
|
|
|
|
* a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
|
|
|
|
* recurse past the end of THREAD_SIZE by default.
|
|
|
|
*/
|
|
|
|
#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
|
2019-08-28 01:36:19 +08:00
|
|
|
#define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
|
2016-06-27 13:17:25 +08:00
|
|
|
#else
|
2022-07-22 05:57:06 +08:00
|
|
|
#define REC_STACK_SIZE (THREAD_SIZE / 8UL)
|
2016-06-27 13:17:25 +08:00
|
|
|
#endif
|
|
|
|
#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
|
|
|
|
|
|
|
|
static int recur_count = REC_NUM_DEFAULT;
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(lock_me_up);
|
|
|
|
|
2019-04-06 23:59:19 +08:00
|
|
|
/*
|
|
|
|
* Make sure compiler does not optimize this function or stack frame away:
|
|
|
|
* - function marked noinline
|
|
|
|
* - stack variables are marked volatile
|
2021-10-07 16:12:35 +08:00
|
|
|
* - stack variables are written (memset()) and read (buf[..] passed as arg)
|
|
|
|
* - function may have external effects (memzero_explicit())
|
|
|
|
* - no tail recursion possible
|
|
|
|
*/
|
2019-04-06 23:59:19 +08:00
|
|
|
static int noinline recursive_loop(int remaining)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
2019-04-06 23:59:19 +08:00
|
|
|
volatile char buf[REC_STACK_SIZE];
|
2021-10-07 16:12:35 +08:00
|
|
|
volatile int ret;
|
2016-06-27 13:17:25 +08:00
|
|
|
|
2019-04-06 23:59:19 +08:00
|
|
|
memset((void *)buf, remaining & 0xFF, sizeof(buf));
|
2016-06-27 13:17:25 +08:00
|
|
|
if (!remaining)
|
2021-10-07 16:12:35 +08:00
|
|
|
ret = 0;
|
2016-06-27 13:17:25 +08:00
|
|
|
else
|
2021-10-07 16:12:35 +08:00
|
|
|
ret = recursive_loop((int)buf[remaining % sizeof(buf)] - 1);
|
|
|
|
memzero_explicit((void *)buf, sizeof(buf));
|
|
|
|
return ret;
|
2016-06-27 13:17:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the depth is negative, use the default, otherwise keep parameter. */
|
|
|
|
void __init lkdtm_bugs_init(int *recur_param)
|
|
|
|
{
|
|
|
|
if (*recur_param < 0)
|
|
|
|
*recur_param = recur_count;
|
|
|
|
else
|
|
|
|
recur_count = *recur_param;
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_PANIC(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
panic("dumptest");
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_BUG(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2017-11-18 07:27:17 +08:00
|
|
|
static int warn_counter;
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_WARNING(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
2019-08-20 01:24:52 +08:00
|
|
|
WARN_ON(++warn_counter);
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_WARNING_MESSAGE(void)
|
2019-08-20 01:24:52 +08:00
|
|
|
{
|
|
|
|
WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
|
2016-06-27 13:17:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_EXCEPTION(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
2017-04-15 05:15:09 +08:00
|
|
|
*((volatile int *) 0) = 0;
|
2016-06-27 13:17:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_LOOP(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_EXHAUST_STACK(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
2019-08-28 01:36:19 +08:00
|
|
|
pr_info("Calling function with %lu frame size to depth %d ...\n",
|
2019-04-06 23:59:19 +08:00
|
|
|
REC_STACK_SIZE, recur_count);
|
|
|
|
recursive_loop(recur_count);
|
|
|
|
pr_info("FAIL: survived without exhausting stack?!\n");
|
2016-06-27 13:17:25 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:56:44 +08:00
|
|
|
static noinline void __lkdtm_CORRUPT_STACK(void *stack)
|
|
|
|
{
|
2017-08-05 05:34:40 +08:00
|
|
|
memset(stack, '\xff', 64);
|
2017-01-11 22:56:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-05 05:34:40 +08:00
|
|
|
/* This should trip the stack canary, not corrupt the return address. */
|
2022-03-04 08:31:16 +08:00
|
|
|
static noinline void lkdtm_CORRUPT_STACK(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
/* Use default char array length that triggers stack protection. */
|
2017-08-05 05:34:40 +08:00
|
|
|
char data[8] __aligned(sizeof(void *));
|
|
|
|
|
2020-06-26 04:37:01 +08:00
|
|
|
pr_info("Corrupting stack containing char array ...\n");
|
|
|
|
__lkdtm_CORRUPT_STACK((void *)&data);
|
2017-08-05 05:34:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as above but will only get a canary with -fstack-protector-strong */
|
2022-03-04 08:31:16 +08:00
|
|
|
static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
|
2017-08-05 05:34:40 +08:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
unsigned short shorts[4];
|
|
|
|
unsigned long *ptr;
|
|
|
|
} data __aligned(sizeof(void *));
|
|
|
|
|
2020-06-26 04:37:01 +08:00
|
|
|
pr_info("Corrupting stack containing union ...\n");
|
|
|
|
__lkdtm_CORRUPT_STACK((void *)&data);
|
2016-06-27 13:17:25 +08:00
|
|
|
}
|
|
|
|
|
2021-04-02 07:23:47 +08:00
|
|
|
static pid_t stack_pid;
|
|
|
|
static unsigned long stack_addr;
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_REPORT_STACK(void)
|
2021-04-02 07:23:47 +08:00
|
|
|
{
|
|
|
|
volatile uintptr_t magic;
|
|
|
|
pid_t pid = task_pid_nr(current);
|
|
|
|
|
|
|
|
if (pid != stack_pid) {
|
|
|
|
pr_info("Starting stack offset tracking for pid %d\n", pid);
|
|
|
|
stack_pid = pid;
|
|
|
|
stack_addr = (uintptr_t)&magic;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
|
|
|
|
}
|
|
|
|
|
2021-10-23 06:38:26 +08:00
|
|
|
static pid_t stack_canary_pid;
|
|
|
|
static unsigned long stack_canary;
|
|
|
|
static unsigned long stack_canary_offset;
|
|
|
|
|
|
|
|
static noinline void __lkdtm_REPORT_STACK_CANARY(void *stack)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
pid_t pid = task_pid_nr(current);
|
|
|
|
unsigned long *canary = (unsigned long *)stack;
|
|
|
|
unsigned long current_offset = 0, init_offset = 0;
|
|
|
|
|
|
|
|
/* Do our best to find the canary in a 16 word window ... */
|
|
|
|
for (i = 1; i < 16; i++) {
|
|
|
|
canary = (unsigned long *)stack + i;
|
|
|
|
#ifdef CONFIG_STACKPROTECTOR
|
|
|
|
if (*canary == current->stack_canary)
|
|
|
|
current_offset = i;
|
|
|
|
if (*canary == init_task.stack_canary)
|
|
|
|
init_offset = i;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_offset == 0) {
|
|
|
|
/*
|
|
|
|
* If the canary doesn't match what's in the task_struct,
|
|
|
|
* we're either using a global canary or the stack frame
|
|
|
|
* layout changed.
|
|
|
|
*/
|
|
|
|
if (init_offset != 0) {
|
|
|
|
pr_err("FAIL: global stack canary found at offset %ld (canary for pid %d matches init_task's)!\n",
|
|
|
|
init_offset, pid);
|
|
|
|
} else {
|
|
|
|
pr_warn("FAIL: did not correctly locate stack canary :(\n");
|
|
|
|
pr_expected_config(CONFIG_STACKPROTECTOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (init_offset != 0) {
|
|
|
|
pr_warn("WARNING: found both current and init_task canaries nearby?!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
canary = (unsigned long *)stack + current_offset;
|
|
|
|
if (stack_canary_pid == 0) {
|
|
|
|
stack_canary = *canary;
|
|
|
|
stack_canary_pid = pid;
|
|
|
|
stack_canary_offset = current_offset;
|
|
|
|
pr_info("Recorded stack canary for pid %d at offset %ld\n",
|
|
|
|
stack_canary_pid, stack_canary_offset);
|
|
|
|
} else if (pid == stack_canary_pid) {
|
|
|
|
pr_warn("ERROR: saw pid %d again -- please use a new pid\n", pid);
|
|
|
|
} else {
|
|
|
|
if (current_offset != stack_canary_offset) {
|
|
|
|
pr_warn("ERROR: canary offset changed from %ld to %ld!?\n",
|
|
|
|
stack_canary_offset, current_offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*canary == stack_canary) {
|
|
|
|
pr_warn("FAIL: canary identical for pid %d and pid %d at offset %ld!\n",
|
|
|
|
stack_canary_pid, pid, current_offset);
|
|
|
|
} else {
|
|
|
|
pr_info("ok: stack canaries differ between pid %d and pid %d at offset %ld.\n",
|
|
|
|
stack_canary_pid, pid, current_offset);
|
|
|
|
/* Reset the test. */
|
|
|
|
stack_canary_pid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_REPORT_STACK_CANARY(void)
|
2021-10-23 06:38:26 +08:00
|
|
|
{
|
|
|
|
/* Use default char array length that triggers stack protection. */
|
|
|
|
char data[8] __aligned(sizeof(void *)) = { };
|
|
|
|
|
|
|
|
__lkdtm_REPORT_STACK_CANARY((void *)&data);
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
|
|
|
|
u32 *p;
|
|
|
|
u32 val = 0x12345678;
|
|
|
|
|
|
|
|
p = (u32 *)(data + 1);
|
|
|
|
if (*p == 0)
|
|
|
|
val = 0x87654321;
|
|
|
|
*p = val;
|
2021-06-24 04:39:31 +08:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
|
|
|
|
pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
|
2016-06-27 13:17:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_SOFTLOCKUP(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
for (;;)
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_HARDLOCKUP(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
local_irq_disable();
|
|
|
|
for (;;)
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_SPINLOCKUP(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
/* Must be called twice to trigger. */
|
|
|
|
spin_lock(&lock_me_up);
|
|
|
|
/* Let sparse know we intended to exit holding the lock. */
|
|
|
|
__release(&lock_me_up);
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_HUNG_TASK(void)
|
2016-06-27 13:17:25 +08:00
|
|
|
{
|
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
|
|
|
schedule();
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:12:34 +08:00
|
|
|
volatile unsigned int huge = INT_MAX - 2;
|
|
|
|
volatile unsigned int ignored;
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_OVERFLOW_SIGNED(void)
|
2020-04-07 11:12:34 +08:00
|
|
|
{
|
|
|
|
int value;
|
|
|
|
|
|
|
|
value = huge;
|
|
|
|
pr_info("Normal signed addition ...\n");
|
|
|
|
value += 1;
|
|
|
|
ignored = value;
|
|
|
|
|
|
|
|
pr_info("Overflowing signed addition ...\n");
|
|
|
|
value += 4;
|
|
|
|
ignored = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_OVERFLOW_UNSIGNED(void)
|
2020-04-07 11:12:34 +08:00
|
|
|
{
|
|
|
|
unsigned int value;
|
|
|
|
|
|
|
|
value = huge;
|
|
|
|
pr_info("Normal unsigned addition ...\n");
|
|
|
|
value += 1;
|
|
|
|
ignored = value;
|
|
|
|
|
|
|
|
pr_info("Overflowing unsigned addition ...\n");
|
|
|
|
value += 4;
|
|
|
|
ignored = value;
|
|
|
|
}
|
|
|
|
|
2020-04-02 02:28:55 +08:00
|
|
|
/* Intentionally using old-style flex array definition of 1 byte. */
|
2020-04-07 11:12:34 +08:00
|
|
|
struct array_bounds_flex_array {
|
|
|
|
int one;
|
|
|
|
int two;
|
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct array_bounds {
|
|
|
|
int one;
|
|
|
|
int two;
|
|
|
|
char data[8];
|
|
|
|
int three;
|
|
|
|
};
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_ARRAY_BOUNDS(void)
|
2020-04-07 11:12:34 +08:00
|
|
|
{
|
|
|
|
struct array_bounds_flex_array *not_checked;
|
|
|
|
struct array_bounds *checked;
|
|
|
|
volatile int i;
|
|
|
|
|
|
|
|
not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
|
|
|
|
checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
|
2022-01-20 17:29:36 +08:00
|
|
|
if (!not_checked || !checked) {
|
|
|
|
kfree(not_checked);
|
|
|
|
kfree(checked);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-07 11:12:34 +08:00
|
|
|
|
|
|
|
pr_info("Array access within bounds ...\n");
|
|
|
|
/* For both, touch all bytes in the actual member size. */
|
|
|
|
for (i = 0; i < sizeof(checked->data); i++)
|
|
|
|
checked->data[i] = 'A';
|
|
|
|
/*
|
|
|
|
* For the uninstrumented flex array member, also touch 1 byte
|
|
|
|
* beyond to verify it is correctly uninstrumented.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sizeof(not_checked->data) + 1; i++)
|
|
|
|
not_checked->data[i] = 'A';
|
|
|
|
|
|
|
|
pr_info("Array access beyond bounds ...\n");
|
|
|
|
for (i = 0; i < sizeof(checked->data) + 1; i++)
|
|
|
|
checked->data[i] = 'B';
|
|
|
|
|
|
|
|
kfree(not_checked);
|
|
|
|
kfree(checked);
|
2020-06-26 04:37:01 +08:00
|
|
|
pr_err("FAIL: survived array bounds overflow!\n");
|
2022-04-12 03:13:39 +08:00
|
|
|
if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
|
|
|
|
pr_expected_config(CONFIG_UBSAN_TRAP);
|
|
|
|
else
|
|
|
|
pr_expected_config(CONFIG_UBSAN_BOUNDS);
|
2020-04-07 11:12:34 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_CORRUPT_LIST_ADD(void)
|
2016-08-18 05:42:12 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Initially, an empty list via LIST_HEAD:
|
|
|
|
* test_head.next = &test_head
|
|
|
|
* test_head.prev = &test_head
|
|
|
|
*/
|
|
|
|
LIST_HEAD(test_head);
|
|
|
|
struct lkdtm_list good, bad;
|
|
|
|
void *target[2] = { };
|
|
|
|
void *redirection = ⌖
|
|
|
|
|
|
|
|
pr_info("attempting good list addition\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adding to the list performs these actions:
|
|
|
|
* test_head.next->prev = &good.node
|
|
|
|
* good.node.next = test_head.next
|
|
|
|
* good.node.prev = test_head
|
|
|
|
* test_head.next = good.node
|
|
|
|
*/
|
|
|
|
list_add(&good.node, &test_head);
|
|
|
|
|
|
|
|
pr_info("attempting corrupted list addition\n");
|
|
|
|
/*
|
|
|
|
* In simulating this "write what where" primitive, the "what" is
|
|
|
|
* the address of &bad.node, and the "where" is the address held
|
|
|
|
* by "redirection".
|
|
|
|
*/
|
|
|
|
test_head.next = redirection;
|
|
|
|
list_add(&bad.node, &test_head);
|
|
|
|
|
|
|
|
if (target[0] == NULL && target[1] == NULL)
|
|
|
|
pr_err("Overwrite did not happen, but no BUG?!\n");
|
2021-06-24 04:39:34 +08:00
|
|
|
else {
|
2016-08-18 05:42:12 +08:00
|
|
|
pr_err("list_add() corruption not detected!\n");
|
2021-06-24 04:39:34 +08:00
|
|
|
pr_expected_config(CONFIG_DEBUG_LIST);
|
|
|
|
}
|
2016-08-18 05:42:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_CORRUPT_LIST_DEL(void)
|
2016-08-18 05:42:12 +08:00
|
|
|
{
|
|
|
|
LIST_HEAD(test_head);
|
|
|
|
struct lkdtm_list item;
|
|
|
|
void *target[2] = { };
|
|
|
|
void *redirection = ⌖
|
|
|
|
|
|
|
|
list_add(&item.node, &test_head);
|
|
|
|
|
|
|
|
pr_info("attempting good list removal\n");
|
|
|
|
list_del(&item.node);
|
|
|
|
|
|
|
|
pr_info("attempting corrupted list removal\n");
|
|
|
|
list_add(&item.node, &test_head);
|
|
|
|
|
|
|
|
/* As with the list_add() test above, this corrupts "next". */
|
|
|
|
item.node.next = redirection;
|
|
|
|
list_del(&item.node);
|
|
|
|
|
|
|
|
if (target[0] == NULL && target[1] == NULL)
|
|
|
|
pr_err("Overwrite did not happen, but no BUG?!\n");
|
2021-06-24 04:39:34 +08:00
|
|
|
else {
|
2016-08-18 05:42:12 +08:00
|
|
|
pr_err("list_del() corruption not detected!\n");
|
2021-06-24 04:39:34 +08:00
|
|
|
pr_expected_config(CONFIG_DEBUG_LIST);
|
|
|
|
}
|
2016-08-18 05:42:12 +08:00
|
|
|
}
|
2017-03-25 01:51:25 +08:00
|
|
|
|
2017-08-05 04:04:21 +08:00
|
|
|
/* Test that VMAP_STACK is actually allocating with a leading guard page */
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_STACK_GUARD_PAGE_LEADING(void)
|
2017-08-05 04:04:21 +08:00
|
|
|
{
|
|
|
|
const unsigned char *stack = task_stack_page(current);
|
|
|
|
const unsigned char *ptr = stack - 1;
|
|
|
|
volatile unsigned char byte;
|
|
|
|
|
|
|
|
pr_info("attempting bad read from page below current stack\n");
|
|
|
|
|
|
|
|
byte = *ptr;
|
|
|
|
|
2020-06-26 21:05:19 +08:00
|
|
|
pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
|
2017-08-05 04:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test that VMAP_STACK is actually allocating with a trailing guard page */
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
|
2017-08-05 04:04:21 +08:00
|
|
|
{
|
|
|
|
const unsigned char *stack = task_stack_page(current);
|
|
|
|
const unsigned char *ptr = stack + THREAD_SIZE;
|
|
|
|
volatile unsigned char byte;
|
|
|
|
|
|
|
|
pr_info("attempting bad read from page above current stack\n");
|
|
|
|
|
|
|
|
byte = *ptr;
|
|
|
|
|
2020-06-26 21:05:19 +08:00
|
|
|
pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
|
2017-08-05 04:04:21 +08:00
|
|
|
}
|
2019-06-23 04:18:23 +08:00
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_UNSET_SMEP(void)
|
2019-06-23 04:18:23 +08:00
|
|
|
{
|
2019-12-13 08:35:22 +08:00
|
|
|
#if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
|
2019-06-23 04:18:23 +08:00
|
|
|
#define MOV_CR4_DEPTH 64
|
|
|
|
void (*direct_write_cr4)(unsigned long val);
|
|
|
|
unsigned char *insn;
|
|
|
|
unsigned long cr4;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
cr4 = native_read_cr4();
|
|
|
|
|
|
|
|
if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
|
|
|
|
pr_err("FAIL: SMEP not in use\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cr4 &= ~(X86_CR4_SMEP);
|
|
|
|
|
|
|
|
pr_info("trying to clear SMEP normally\n");
|
|
|
|
native_write_cr4(cr4);
|
|
|
|
if (cr4 == native_read_cr4()) {
|
|
|
|
pr_err("FAIL: pinning SMEP failed!\n");
|
|
|
|
cr4 |= X86_CR4_SMEP;
|
|
|
|
pr_info("restoring SMEP\n");
|
|
|
|
native_write_cr4(cr4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pr_info("ok: SMEP did not get cleared\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To test the post-write pinning verification we need to call
|
|
|
|
* directly into the middle of native_write_cr4() where the
|
|
|
|
* cr4 write happens, skipping any pinning. This searches for
|
|
|
|
* the cr4 writing instruction.
|
|
|
|
*/
|
|
|
|
insn = (unsigned char *)native_write_cr4;
|
|
|
|
for (i = 0; i < MOV_CR4_DEPTH; i++) {
|
|
|
|
/* mov %rdi, %cr4 */
|
|
|
|
if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
|
|
|
|
break;
|
|
|
|
/* mov %rdi,%rax; mov %rax, %cr4 */
|
|
|
|
if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
|
|
|
|
insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
|
|
|
|
insn[i+4] == 0x22 && insn[i+5] == 0xe0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= MOV_CR4_DEPTH) {
|
|
|
|
pr_info("ok: cannot locate cr4 writing call gadget\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
direct_write_cr4 = (void *)(insn + i);
|
|
|
|
|
|
|
|
pr_info("trying to clear SMEP with call gadget\n");
|
|
|
|
direct_write_cr4(cr4);
|
|
|
|
if (native_read_cr4() & X86_CR4_SMEP) {
|
|
|
|
pr_info("ok: SMEP removal was reverted\n");
|
|
|
|
} else {
|
|
|
|
pr_err("FAIL: cleared SMEP not detected!\n");
|
|
|
|
cr4 |= X86_CR4_SMEP;
|
|
|
|
pr_info("restoring SMEP\n");
|
|
|
|
native_write_cr4(cr4);
|
|
|
|
}
|
|
|
|
#else
|
2020-01-03 04:29:17 +08:00
|
|
|
pr_err("XFAIL: this test is x86_64-only\n");
|
2019-06-23 04:18:23 +08:00
|
|
|
#endif
|
|
|
|
}
|
2019-11-25 13:18:04 +08:00
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static void lkdtm_DOUBLE_FAULT(void)
|
2019-11-25 13:18:04 +08:00
|
|
|
{
|
2020-06-26 04:37:04 +08:00
|
|
|
#if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
|
2019-11-25 13:18:04 +08:00
|
|
|
/*
|
|
|
|
* Trigger #DF by setting the stack limit to zero. This clobbers
|
|
|
|
* a GDT TLS slot, which is okay because the current task will die
|
|
|
|
* anyway due to the double fault.
|
|
|
|
*/
|
|
|
|
struct desc_struct d = {
|
|
|
|
.type = 3, /* expand-up, writable, accessed data */
|
|
|
|
.p = 1, /* present */
|
|
|
|
.d = 1, /* 32-bit */
|
|
|
|
.g = 0, /* limit in bytes */
|
|
|
|
.s = 1, /* not system */
|
|
|
|
};
|
|
|
|
|
|
|
|
local_irq_disable();
|
|
|
|
write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
|
|
|
|
GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put our zero-limit segment in SS and then trigger a fault. The
|
|
|
|
* 4-byte access to (%esp) will fault with #SS, and the attempt to
|
|
|
|
* deliver the fault will recursively cause #SS and result in #DF.
|
|
|
|
* This whole process happens while NMIs and MCEs are blocked by the
|
|
|
|
* MOV SS window. This is nice because an NMI with an invalid SS
|
|
|
|
* would also double-fault, resulting in the NMI or MCE being lost.
|
|
|
|
*/
|
|
|
|
asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
|
|
|
|
"r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
|
|
|
|
|
2020-01-03 04:29:17 +08:00
|
|
|
pr_err("FAIL: tried to double fault but didn't die\n");
|
|
|
|
#else
|
|
|
|
pr_err("XFAIL: this test is ia32-only\n");
|
2019-11-25 13:18:04 +08:00
|
|
|
#endif
|
2020-01-03 04:29:17 +08:00
|
|
|
}
|
2020-03-13 17:05:04 +08:00
|
|
|
|
2020-06-26 04:37:04 +08:00
|
|
|
#ifdef CONFIG_ARM64
|
2020-03-13 17:05:04 +08:00
|
|
|
static noinline void change_pac_parameters(void)
|
|
|
|
{
|
2021-06-13 17:26:31 +08:00
|
|
|
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
|
2020-06-26 04:37:04 +08:00
|
|
|
/* Reset the keys of current task */
|
|
|
|
ptrauth_thread_init_kernel(current);
|
|
|
|
ptrauth_thread_switch_kernel(current);
|
|
|
|
}
|
2020-03-13 17:05:04 +08:00
|
|
|
}
|
2020-06-26 04:37:04 +08:00
|
|
|
#endif
|
2020-03-13 17:05:04 +08:00
|
|
|
|
2022-03-04 08:31:16 +08:00
|
|
|
static noinline void lkdtm_CORRUPT_PAC(void)
|
2020-03-13 17:05:04 +08:00
|
|
|
{
|
2020-06-26 04:37:04 +08:00
|
|
|
#ifdef CONFIG_ARM64
|
|
|
|
#define CORRUPT_PAC_ITERATE 10
|
2020-03-13 17:05:04 +08:00
|
|
|
int i;
|
|
|
|
|
2021-06-13 17:26:31 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
|
|
|
|
pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
|
2020-06-26 04:37:04 +08:00
|
|
|
|
2020-03-13 17:05:04 +08:00
|
|
|
if (!system_supports_address_auth()) {
|
2020-06-26 04:37:04 +08:00
|
|
|
pr_err("FAIL: CPU lacks pointer authentication feature\n");
|
2020-03-13 17:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-26 04:37:04 +08:00
|
|
|
pr_info("changing PAC parameters to force function return failure...\n");
|
2020-03-13 17:05:04 +08:00
|
|
|
/*
|
2020-06-26 04:37:04 +08:00
|
|
|
* PAC is a hash value computed from input keys, return address and
|
2020-03-13 17:05:04 +08:00
|
|
|
* stack pointer. As pac has fewer bits so there is a chance of
|
|
|
|
* collision, so iterate few times to reduce the collision probability.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
|
|
|
|
change_pac_parameters();
|
|
|
|
|
2020-06-26 04:37:04 +08:00
|
|
|
pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
|
|
|
|
#else
|
|
|
|
pr_err("XFAIL: this test is arm64-only\n");
|
2020-03-13 17:05:04 +08:00
|
|
|
#endif
|
2020-06-26 04:37:04 +08:00
|
|
|
}
|
2022-03-04 08:31:16 +08:00
|
|
|
|
|
|
|
static struct crashtype crashtypes[] = {
|
|
|
|
CRASHTYPE(PANIC),
|
|
|
|
CRASHTYPE(BUG),
|
|
|
|
CRASHTYPE(WARNING),
|
|
|
|
CRASHTYPE(WARNING_MESSAGE),
|
|
|
|
CRASHTYPE(EXCEPTION),
|
|
|
|
CRASHTYPE(LOOP),
|
|
|
|
CRASHTYPE(EXHAUST_STACK),
|
|
|
|
CRASHTYPE(CORRUPT_STACK),
|
|
|
|
CRASHTYPE(CORRUPT_STACK_STRONG),
|
|
|
|
CRASHTYPE(REPORT_STACK),
|
|
|
|
CRASHTYPE(REPORT_STACK_CANARY),
|
|
|
|
CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
|
|
|
|
CRASHTYPE(SOFTLOCKUP),
|
|
|
|
CRASHTYPE(HARDLOCKUP),
|
|
|
|
CRASHTYPE(SPINLOCKUP),
|
|
|
|
CRASHTYPE(HUNG_TASK),
|
|
|
|
CRASHTYPE(OVERFLOW_SIGNED),
|
|
|
|
CRASHTYPE(OVERFLOW_UNSIGNED),
|
|
|
|
CRASHTYPE(ARRAY_BOUNDS),
|
|
|
|
CRASHTYPE(CORRUPT_LIST_ADD),
|
|
|
|
CRASHTYPE(CORRUPT_LIST_DEL),
|
|
|
|
CRASHTYPE(STACK_GUARD_PAGE_LEADING),
|
|
|
|
CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
|
|
|
|
CRASHTYPE(UNSET_SMEP),
|
|
|
|
CRASHTYPE(DOUBLE_FAULT),
|
|
|
|
CRASHTYPE(CORRUPT_PAC),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct crashtype_category bugs_crashtypes = {
|
|
|
|
.crashtypes = crashtypes,
|
|
|
|
.len = ARRAY_SIZE(crashtypes),
|
|
|
|
};
|