1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Copyright (c) 2012, Cyril Hrubis <chrubis@suse.cz>
4 *
5 * This file is licensed under the GPL license. For the full content
6 * of this license, see the COPYING file at the top level of this
7 * source tree.
8 *
9 * MPR An implementation may permit accesses other than those specified by prot;
10 * however, if the Memory Protection option is supported, the implementation
11 * shall not permit a write to succeed where PROT_WRITE has not been set or
12 * shall not permit any access where PROT_NONE alone has been set.
13 * The implementation shall support at least the following values of prot:
14 * PROT_NONE, PROT_READ, PROT_WRITE, and the bitwise-inclusive OR of PROT_READ
15 * and PROT_WRITE.
16 *
17 * Test Steps:
18 *
19 * If the Memory Protection option is supported:
20 *
21 * 1. Spawn a child process.
22 * 2. The child process mmap a memory region setting prot as PROT_NONE.
23 * 3. Try to write to the mapped memory.
24 * 4. If the read will triger SIGSEGV, the PASS.
25 *
26 * Please refer to IEEE_1003.1-2001. 2.8.3.3 Memory Protection.
27 */
28
29 #include <sys/mman.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "posixtest.h"
42 #include "tempfile.h"
43
main(void)44 int main(void)
45 {
46 #ifdef _POSIX_MEMORY_PROTECTION
47 char tmpfname[PATH_MAX];
48 void *pa;
49 size_t size = 1024;
50 int fd;
51
52 pid_t child;
53 int status;
54 int sig_num;
55
56 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_6_3");
57 unlink(tmpfname);
58 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
59 if (fd == -1) {
60 printf("Error at open(): %s\n", strerror(errno));
61 return PTS_UNRESOLVED;
62 }
63 unlink(tmpfname);
64
65 child = fork();
66 switch (child) {
67 case 0:
68 pa = mmap(NULL, size, PROT_NONE, MAP_SHARED, fd, 0);
69 if (pa == MAP_FAILED) {
70 printf("Error at mmap: %s\n", strerror(errno));
71 return PTS_FAIL;
72 }
73
74 /* Write acess */
75 *(char *)pa = 'b';
76 return 0;
77 break;
78 case -1:
79 perror("fork()");
80 return PTS_UNRESOLVED;
81 break;
82 default:
83 break;
84 }
85
86 waitpid(child, &status, 0);
87 close(fd);
88
89 if (WIFSIGNALED(status)) {
90 sig_num = WTERMSIG(status);
91 printf("Child process terminated by signal %d\n", sig_num);
92 if (sig_num == SIGSEGV) {
93 printf("Got SIGSEGV when writing to the mapped memory, "
94 "setting PROT_NOTE\n" "Test PASSED\n");
95 return PTS_PASS;
96 }
97 }
98
99 if (WIFEXITED(status)) {
100 if (WEXITSTATUS(status) == 0) {
101 printf
102 ("Did not got SIGSEGV when writing to the mapped memory, "
103 "setting PROT_NOTE\n" "Test FAILED\n");
104 return PTS_FAIL;
105 }
106
107 }
108
109 printf("Test Unresolved\n");
110 return PTS_UNRESOLVED;
111 #else
112 printf("Test Usupported: _POSIX_MEMORY_PROTECTION not defined\n");
113 return PTS_UNSUPPORTED;
114 #endif
115 }
116