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