blob: 2613371945b7ec6f8f1ce24ad378ba539f4d9bc0 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Jinbum Park2959a5f2017-02-27 14:30:22 -08002/*
3 * rodata_test.c: functional test for mark_rodata_ro function
4 *
5 * (C) Copyright 2008 Intel Corporation
6 * Author: Arjan van de Ven <arjan@linux.intel.com>
Jinbum Park2959a5f2017-02-27 14:30:22 -08007 */
Kees Cook056b9d82017-05-03 14:53:32 -07008#define pr_fmt(fmt) "rodata_test: " fmt
9
Leon Romanovsky86f54bb2020-08-20 17:42:08 -070010#include <linux/rodata_test.h>
Jinbum Park2959a5f2017-02-27 14:30:22 -080011#include <linux/uaccess.h>
12#include <asm/sections.h>
13
Christophe Leroya872eb22017-10-03 16:15:16 -070014static const int rodata_test_data = 0xC3;
Jinbum Park2959a5f2017-02-27 14:30:22 -080015
16void rodata_test(void)
17{
18 unsigned long start, end;
19 int zero = 0;
20
21 /* test 1: read the value */
22 /* If this test fails, some previous testrun has clobbered the state */
23 if (!rodata_test_data) {
Kees Cook056b9d82017-05-03 14:53:32 -070024 pr_err("test 1 fails (start data)\n");
Jinbum Park2959a5f2017-02-27 14:30:22 -080025 return;
26 }
27
28 /* test 2: write to the variable; this should fault */
Christoph Hellwigfe557312020-06-17 09:37:53 +020029 if (!copy_to_kernel_nofault((void *)&rodata_test_data,
Kees Cook056b9d82017-05-03 14:53:32 -070030 (void *)&zero, sizeof(zero))) {
31 pr_err("test data was not read only\n");
Jinbum Park2959a5f2017-02-27 14:30:22 -080032 return;
33 }
34
35 /* test 3: check the value hasn't changed */
36 if (rodata_test_data == zero) {
Kees Cook056b9d82017-05-03 14:53:32 -070037 pr_err("test data was changed\n");
Jinbum Park2959a5f2017-02-27 14:30:22 -080038 return;
39 }
40
41 /* test 4: check if the rodata section is PAGE_SIZE aligned */
42 start = (unsigned long)__start_rodata;
43 end = (unsigned long)__end_rodata;
44 if (start & (PAGE_SIZE - 1)) {
Kees Cook056b9d82017-05-03 14:53:32 -070045 pr_err("start of .rodata is not page size aligned\n");
Jinbum Park2959a5f2017-02-27 14:30:22 -080046 return;
47 }
48 if (end & (PAGE_SIZE - 1)) {
Kees Cook056b9d82017-05-03 14:53:32 -070049 pr_err("end of .rodata is not page size aligned\n");
Jinbum Park2959a5f2017-02-27 14:30:22 -080050 return;
51 }
52
Kees Cook056b9d82017-05-03 14:53:32 -070053 pr_info("all tests were successful\n");
Jinbum Park2959a5f2017-02-27 14:30:22 -080054}