mirror of
https://github.com/torvalds/linux.git
synced 2026-01-12 00:42:35 +08:00
Patch series "Fix mm/rodata_test", v2.
Make sure that the test actually reads the read-only memory location.
Verify that the variable contains the expected value rather than any
non-zero value.
This patch (of 2):
The C compiler may optimize away the memory read of a const variable if
its value is known at compile time.
In particular, GCC14 with -O2 generates no code at all for test 1, and it
generates the following x86_64 instructions for test 3:
cmpl $195, 4(%rsp)
je .L14
That is, it replaces the read of rodata_test_data with an immediate value
and compares it to the value of the local variable "zero".
Use READ_ONCE() to undo any such compiler optimizations and enforce a
memory read.
Link: https://lkml.kernel.org/r/cover.1732016064.git.ptesarik@suse.com
Link: https://lkml.kernel.org/r/2a66dee010151b25cb143efb39091ef7530aa00a.1732016064.git.ptesarik@suse.com
Fixes: 2959a5f726 ("mm: add arch-independent testcases for RODATA")
Signed-off-by: Petr Tesarik <ptesarik@suse.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Cc: Jinbum Park <jinb.park7@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
53 lines
1.3 KiB
C
53 lines
1.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* rodata_test.c: functional test for mark_rodata_ro function
|
|
*
|
|
* (C) Copyright 2008 Intel Corporation
|
|
* Author: Arjan van de Ven <arjan@linux.intel.com>
|
|
*/
|
|
#define pr_fmt(fmt) "rodata_test: " fmt
|
|
|
|
#include <linux/rodata_test.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/mm.h>
|
|
#include <asm/sections.h>
|
|
|
|
static const int rodata_test_data = 0xC3;
|
|
|
|
void rodata_test(void)
|
|
{
|
|
int zero = 0;
|
|
|
|
/* test 1: read the value */
|
|
/* If this test fails, some previous testrun has clobbered the state */
|
|
if (!READ_ONCE(rodata_test_data)) {
|
|
pr_err("test 1 fails (start data)\n");
|
|
return;
|
|
}
|
|
|
|
/* test 2: write to the variable; this should fault */
|
|
if (!copy_to_kernel_nofault((void *)&rodata_test_data,
|
|
(void *)&zero, sizeof(zero))) {
|
|
pr_err("test data was not read only\n");
|
|
return;
|
|
}
|
|
|
|
/* test 3: check the value hasn't changed */
|
|
if (READ_ONCE(rodata_test_data) == zero) {
|
|
pr_err("test data was changed\n");
|
|
return;
|
|
}
|
|
|
|
/* test 4: check if the rodata section is PAGE_SIZE aligned */
|
|
if (!PAGE_ALIGNED(__start_rodata)) {
|
|
pr_err("start of .rodata is not page size aligned\n");
|
|
return;
|
|
}
|
|
if (!PAGE_ALIGNED(__end_rodata)) {
|
|
pr_err("end of .rodata is not page size aligned\n");
|
|
return;
|
|
}
|
|
|
|
pr_info("all tests were successful\n");
|
|
}
|