xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mprotect/mprotect05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2023 Oracle and/or its affiliates. All Rights Reserved.
4  * Author: Liam R. Howlett <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Testcase to check the mprotect(2) system call split and merge.
11  *
12  * https://bugzilla.kernel.org/show_bug.cgi?id=217061
13  *
14  */
15 
16 #include "tst_test.h"
17 
18 #define TEST_FILE "mprotect05-testfile"
19 
20 static int fd;
21 static char *addr = MAP_FAILED;
22 static unsigned long pagesize;
23 static unsigned long fullsize;
24 
setup(void)25 static void setup(void)
26 {
27 	pagesize = getpagesize();
28 	fullsize = 5 * pagesize;
29 }
30 
run(void)31 static void run(void)
32 {
33 	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0777);
34 	addr = SAFE_MMAP(0, fullsize, PROT_READ, MAP_SHARED, fd, 0);
35 
36 	if (mprotect(addr + pagesize, pagesize, PROT_EXEC))
37 		tst_res(TFAIL | TERRNO, "mprotect failed to exec");
38 
39 	if (mprotect(addr + 3 * pagesize, pagesize, PROT_WRITE))
40 		tst_res(TFAIL | TERRNO, "mprotect failed to write");
41 
42 	if (mprotect(addr + pagesize, pagesize * 4, PROT_READ))
43 		tst_res(TFAIL | TERRNO, "mprotect failed to read");
44 
45 	SAFE_MUNMAP(addr, fullsize);
46 	SAFE_CLOSE(fd);
47 	addr = MAP_FAILED;
48 	SAFE_UNLINK(TEST_FILE);
49 	tst_res(TPASS, "test passed");
50 }
51 
cleanup(void)52 static void cleanup(void)
53 {
54 	if (addr != MAP_FAILED) {
55 		SAFE_MUNMAP(addr, fullsize);
56 		SAFE_CLOSE(fd);
57 	}
58 }
59 
60 static struct tst_test test = {
61 	.test_all = run,
62 	.setup = setup,
63 	.cleanup = cleanup,
64 	.needs_tmpdir  = 1,
65 	.tags = (const struct tst_tag[]) {
66 		{"linux-git", "2fcd07b7ccd5"},
67 		{}
68 	},
69 };
70