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 Memory Protection option is supported:
20 * 1. Spawn a child process.
21 * 2. The child process mmap a memory region setting prot as PROT_READ.
22 * 3. Try to write the mapped memory.
23 * 4. If the writing triger SIGSEGV, the PASS.
24 *
25 * Please refer to IEEE_1003.1-2001. 2.8.3.3 Memory Protection.
26 */
27
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "posixtest.h"
41 #include "tempfile.h"
42
main(void)43 int main(void)
44 {
45 #ifdef _POSIX_MEMORY_PROTECTION
46 char tmpfname[PATH_MAX];
47 void *pa;
48 size_t size = 1024;
49 int fd;
50
51 pid_t child;
52 int status;
53 int sig_num;
54
55 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_6_1");
56 unlink(tmpfname);
57 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
58 if (fd == -1) {
59 printf("Error at open(): %s\n", strerror(errno));
60 return PTS_UNRESOLVED;
61 }
62 unlink(tmpfname);
63
64 child = fork();
65
66 switch (child) {
67 case 0:
68 if (ftruncate(fd, size) == -1) {
69 printf("Error at ftruncate(): %s\n", strerror(errno));
70 return PTS_UNRESOLVED;
71 }
72
73 pa = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
74 if (pa == MAP_FAILED) {
75 printf("Error at mmap: %s\n", strerror(errno));
76 return PTS_FAIL;
77 }
78
79 *(char *)pa = 'b';
80 return 0;
81 break;
82 case -1:
83 printf("Error at fork(): %s\n", strerror(errno));
84 return PTS_UNRESOLVED;
85 break;
86 default:
87 break;
88 }
89
90 waitpid(child, &status, WUNTRACED);
91 close(fd);
92
93 if (WIFSIGNALED(status)) {
94 sig_num = WTERMSIG(status);
95 printf("Child process terminated by signal %d\n", sig_num);
96 if (sig_num == SIGSEGV) {
97 printf("Got SIGSEGV when writing to the mapped memory, "
98 "without setting PROT_WRITE\n" "Test PASSED\n");
99 return PTS_PASS;
100 }
101 }
102
103 if (WIFEXITED(status)) {
104 if (WEXITSTATUS(status) == 0) {
105 printf
106 ("Did not got SIGSEGV when writing to the mapped memory,"
107 " without setting PROT_WRITE\n" "Test FAILED\n");
108 return PTS_FAIL;
109 }
110 }
111
112 printf("Test Unresolved\n");
113 return PTS_UNRESOLVED;
114 #else
115 printf("Test Unsupported, _POSIX_MEMORY_PROTECTION not defined\n");
116 return PTS_UNSUPPORTED;
117 #endif
118 }
119