blob: 7a7648258a06d8505612c497ecdf57fd1f419795 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +02002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +02004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00008 *
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +02009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000013 *
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
plars865695b2001-08-27 22:15:12 +000020 * DESCRIPTION
21 * Testcase to check the error conditions for mprotect(2)
22 *
23 * ALGORITHM
24 * test1:
25 * Invoke mprotect() with an address of 0. Check if error
plarsa42e4b92002-04-03 21:25:10 +000026 * is set to ENOMEM.
plars865695b2001-08-27 22:15:12 +000027 * test2:
28 * Invoke mprotect() with an address that is not a multiple
29 * of PAGESIZE. EINVAL
30 * test3:
31 * Mmap a file with only read permission (PROT_READ).
32 * Try to set write permission (PROT_WRITE) using mprotect(2).
33 * Check that error is set to EACCES.
34 *
plars865695b2001-08-27 22:15:12 +000035 * HISTORY
36 * 07/2001 Ported by Wayne Boyer
plarsa42e4b92002-04-03 21:25:10 +000037 * 03/2002 Paul Larson: case 1 should expect ENOMEM not EFAULT
plars865695b2001-08-27 22:15:12 +000038 */
39
40#include <fcntl.h>
41#include <errno.h>
42#include <sys/mman.h>
robbiewb2af0612002-04-03 22:18:59 +000043#include <stdlib.h>
robbiewab5d7fe2003-06-17 20:31:09 +000044#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000045#include "test.h"
plars865695b2001-08-27 22:15:12 +000046
subrata_modak56207ce2009-03-23 13:35:39 +000047char *TCID = "mprotect01";
plars865695b2001-08-27 22:15:12 +000048int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000049
Cyril Hrubisb9f92622013-06-04 17:20:04 +020050struct test_case {
51 void *addr;
subrata_modak56207ce2009-03-23 13:35:39 +000052 int len;
plars865695b2001-08-27 22:15:12 +000053 int prot;
subrata_modak56207ce2009-03-23 13:35:39 +000054 int error;
Cyril Hrubisb9f92622013-06-04 17:20:04 +020055 void (*setupfunc) (struct test_case *self);
56};
57
58static void cleanup(void);
59static void setup(void);
60static void setup1(struct test_case *self);
61static void setup2(struct test_case *self);
62static void setup3(struct test_case *self);
63
Cyril Hrubisb9f92622013-06-04 17:20:04 +020064static int fd;
65
66struct test_case TC[] = {
robbiewab5d7fe2003-06-17 20:31:09 +000067 /* Check for ENOMEM passing memory that cannot be accessed. */
Cyril Hrubisb9f92622013-06-04 17:20:04 +020068 {NULL, 0, PROT_READ, ENOMEM, setup1},
69
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020070 /*
71 * Check for EINVAL by passing a pointer which is not a
72 * multiple of PAGESIZE.
73 */
Cyril Hrubisb9f92622013-06-04 17:20:04 +020074 {NULL, 1024, PROT_READ, EINVAL, setup2},
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020075 /*
76 * Check for EACCES by trying to mark a section of memory
77 * which has been mmap'ed as read-only, as PROT_WRITE
78 */
Cyril Hrubisb9f92622013-06-04 17:20:04 +020079 {NULL, 0, PROT_WRITE, EACCES, setup3}
plars865695b2001-08-27 22:15:12 +000080};
81
plars74948ad2002-11-14 16:16:14 +000082int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000083{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020084 int lc;
plars865695b2001-08-27 22:15:12 +000085 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
plars865695b2001-08-27 22:15:12 +000087
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020088 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000090
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020091 setup();
plars865695b2001-08-27 22:15:12 +000092
plars865695b2001-08-27 22:15:12 +000093 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000095
plars865695b2001-08-27 22:15:12 +000096 for (i = 0; i < TST_TOTAL; i++) {
97
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020098 if (TC[i].setupfunc != NULL)
Cyril Hrubisb9f92622013-06-04 17:20:04 +020099 TC[i].setupfunc(&TC[i]);
plars865695b2001-08-27 22:15:12 +0000100
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200101 TEST(mprotect(TC[i].addr, TC[i].len, TC[i].prot));
plars865695b2001-08-27 22:15:12 +0000102
subrata_modak56207ce2009-03-23 13:35:39 +0000103 if (TEST_RETURN != -1) {
104 tst_resm(TFAIL, "call succeeded unexpectedly");
105 continue;
106 }
plars865695b2001-08-27 22:15:12 +0000107
subrata_modak56207ce2009-03-23 13:35:39 +0000108 if (TEST_ERRNO == TC[i].error) {
109 tst_resm(TPASS, "expected failure - "
110 "errno = %d : %s", TEST_ERRNO,
111 strerror(TEST_ERRNO));
112 } else {
113 tst_resm(TFAIL, "unexpected error - %d : %s - "
114 "expected %d", TEST_ERRNO,
115 strerror(TEST_ERRNO), TC[i].error);
plars865695b2001-08-27 22:15:12 +0000116 }
117 }
plars865695b2001-08-27 22:15:12 +0000118 }
subrata_modak56207ce2009-03-23 13:35:39 +0000119 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800120 tst_exit();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121}
plars865695b2001-08-27 22:15:12 +0000122
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200123static void setup1(struct test_case *self)
robbiewab5d7fe2003-06-17 20:31:09 +0000124{
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200125 self->len = getpagesize() + 1;
robbiewab5d7fe2003-06-17 20:31:09 +0000126}
127
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200128static void setup2(struct test_case *self)
plars865695b2001-08-27 22:15:12 +0000129{
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200130 self->addr = malloc(getpagesize());
robbiew91361372003-07-09 17:51:16 +0000131
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200132 if (self->addr == NULL)
plars865695b2001-08-27 22:15:12 +0000133 tst_brkm(TINFO, cleanup, "malloc failed");
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200134
135 /* Ensure addr2 is not page aligned */
136 self->addr++;
plars865695b2001-08-27 22:15:12 +0000137}
138
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200139static void setup3(struct test_case *self)
plars865695b2001-08-27 22:15:12 +0000140{
141 fd = open("/etc/passwd", O_RDONLY);
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200142 if (fd < 0)
plars865695b2001-08-27 22:15:12 +0000143 tst_brkm(TBROK, cleanup, "open failed");
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200144
145 self->len = getpagesize();
plars865695b2001-08-27 22:15:12 +0000146
147 /*
148 * mmap the PAGESIZE bytes as read only.
149 */
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200150 self->addr = mmap(0, self->len, PROT_READ, MAP_SHARED, fd, 0);
151 if (self->addr == MAP_FAILED)
plars865695b2001-08-27 22:15:12 +0000152 tst_brkm(TBROK, cleanup, "mmap failed");
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200153
plars865695b2001-08-27 22:15:12 +0000154}
155
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200156static void setup(void)
plars865695b2001-08-27 22:15:12 +0000157{
plars865695b2001-08-27 22:15:12 +0000158 tst_sig(FORK, DEF_HANDLER, cleanup);
159
plars865695b2001-08-27 22:15:12 +0000160 TEST_PAUSE;
161}
162
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200163static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000164{
Cyril Hrubisb9f92622013-06-04 17:20:04 +0200165 close(fd);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700166}