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