xref: /linux/mm/rodata_test.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 
1086f54bb7SLeon Romanovsky #include <linux/rodata_test.h>
112959a5f7SJinbum Park #include <linux/uaccess.h>
12*679d7f69SXiu Jianfeng #include <linux/mm.h>
132959a5f7SJinbum Park #include <asm/sections.h>
142959a5f7SJinbum Park 
15a872eb21SChristophe Leroy static const int rodata_test_data = 0xC3;
162959a5f7SJinbum Park 
rodata_test(void)172959a5f7SJinbum Park void rodata_test(void)
182959a5f7SJinbum Park {
192959a5f7SJinbum Park 	int zero = 0;
202959a5f7SJinbum Park 
212959a5f7SJinbum Park 	/* test 1: read the value */
222959a5f7SJinbum Park 	/* If this test fails, some previous testrun has clobbered the state */
232959a5f7SJinbum Park 	if (!rodata_test_data) {
24056b9d8aSKees Cook 		pr_err("test 1 fails (start data)\n");
252959a5f7SJinbum Park 		return;
262959a5f7SJinbum Park 	}
272959a5f7SJinbum Park 
282959a5f7SJinbum Park 	/* test 2: write to the variable; this should fault */
29fe557319SChristoph Hellwig 	if (!copy_to_kernel_nofault((void *)&rodata_test_data,
302959a5f7SJinbum Park 				(void *)&zero, sizeof(zero))) {
31056b9d8aSKees Cook 		pr_err("test data was not read only\n");
322959a5f7SJinbum Park 		return;
332959a5f7SJinbum Park 	}
342959a5f7SJinbum Park 
352959a5f7SJinbum Park 	/* test 3: check the value hasn't changed */
362959a5f7SJinbum Park 	if (rodata_test_data == zero) {
37056b9d8aSKees Cook 		pr_err("test data was changed\n");
382959a5f7SJinbum Park 		return;
392959a5f7SJinbum Park 	}
402959a5f7SJinbum Park 
412959a5f7SJinbum Park 	/* test 4: check if the rodata section is PAGE_SIZE aligned */
42*679d7f69SXiu Jianfeng 	if (!PAGE_ALIGNED(__start_rodata)) {
43056b9d8aSKees Cook 		pr_err("start of .rodata is not page size aligned\n");
442959a5f7SJinbum Park 		return;
452959a5f7SJinbum Park 	}
46*679d7f69SXiu Jianfeng 	if (!PAGE_ALIGNED(__end_rodata)) {
47056b9d8aSKees Cook 		pr_err("end of .rodata is not page size aligned\n");
482959a5f7SJinbum Park 		return;
492959a5f7SJinbum Park 	}
502959a5f7SJinbum Park 
51056b9d8aSKees Cook 	pr_info("all tests were successful\n");
522959a5f7SJinbum Park }
53