xref: /linux/mm/rodata_test.c (revision fe557319aa06c23cffc9346000f119547e0f289a)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22959a5f7SJinbum Park /*
32959a5f7SJinbum Park  * rodata_test.c: functional test for mark_rodata_ro function
42959a5f7SJinbum Park  *
52959a5f7SJinbum Park  * (C) Copyright 2008 Intel Corporation
62959a5f7SJinbum Park  * Author: Arjan van de Ven <arjan@linux.intel.com>
72959a5f7SJinbum Park  */
8056b9d8aSKees Cook #define pr_fmt(fmt) "rodata_test: " fmt
9056b9d8aSKees Cook 
102959a5f7SJinbum Park #include <linux/uaccess.h>
112959a5f7SJinbum Park #include <asm/sections.h>
122959a5f7SJinbum Park 
13a872eb21SChristophe Leroy static const int rodata_test_data = 0xC3;
142959a5f7SJinbum Park 
152959a5f7SJinbum Park void rodata_test(void)
162959a5f7SJinbum Park {
172959a5f7SJinbum Park 	unsigned long start, end;
182959a5f7SJinbum Park 	int zero = 0;
192959a5f7SJinbum Park 
202959a5f7SJinbum Park 	/* test 1: read the value */
212959a5f7SJinbum Park 	/* If this test fails, some previous testrun has clobbered the state */
222959a5f7SJinbum Park 	if (!rodata_test_data) {
23056b9d8aSKees Cook 		pr_err("test 1 fails (start data)\n");
242959a5f7SJinbum Park 		return;
252959a5f7SJinbum Park 	}
262959a5f7SJinbum Park 
272959a5f7SJinbum Park 	/* test 2: write to the variable; this should fault */
28*fe557319SChristoph Hellwig 	if (!copy_to_kernel_nofault((void *)&rodata_test_data,
292959a5f7SJinbum Park 				(void *)&zero, sizeof(zero))) {
30056b9d8aSKees Cook 		pr_err("test data was not read only\n");
312959a5f7SJinbum Park 		return;
322959a5f7SJinbum Park 	}
332959a5f7SJinbum Park 
342959a5f7SJinbum Park 	/* test 3: check the value hasn't changed */
352959a5f7SJinbum Park 	if (rodata_test_data == zero) {
36056b9d8aSKees Cook 		pr_err("test data was changed\n");
372959a5f7SJinbum Park 		return;
382959a5f7SJinbum Park 	}
392959a5f7SJinbum Park 
402959a5f7SJinbum Park 	/* test 4: check if the rodata section is PAGE_SIZE aligned */
412959a5f7SJinbum Park 	start = (unsigned long)__start_rodata;
422959a5f7SJinbum Park 	end = (unsigned long)__end_rodata;
432959a5f7SJinbum Park 	if (start & (PAGE_SIZE - 1)) {
44056b9d8aSKees Cook 		pr_err("start of .rodata is not page size aligned\n");
452959a5f7SJinbum Park 		return;
462959a5f7SJinbum Park 	}
472959a5f7SJinbum Park 	if (end & (PAGE_SIZE - 1)) {
48056b9d8aSKees Cook 		pr_err("end of .rodata is not page size aligned\n");
492959a5f7SJinbum Park 		return;
502959a5f7SJinbum Park 	}
512959a5f7SJinbum Park 
52056b9d8aSKees Cook 	pr_info("all tests were successful\n");
532959a5f7SJinbum Park }
54