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 #define _XOPEN_SOURCE 600
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 #include "posixtest.h"
41
main(void)42 int main(void)
43 {
44 #ifdef _POSIX_MEMORY_PROTECTION
45 char tmpfname[256];
46 void *pa;
47 size_t size = 1024;
48 int fd;
49
50 pid_t child;
51 int status;
52 int sig_num;
53
54 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_6_1_%d", getpid());
55 unlink(tmpfname);
56 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
57 if (fd == -1) {
58 printf("Error at open(): %s\n", strerror(errno));
59 return PTS_UNRESOLVED;
60 }
61 unlink(tmpfname);
62
63 child = fork();
64
65 switch (child) {
66 case 0:
67 if (ftruncate(fd, size) == -1) {
68 printf("Error at ftruncate(): %s\n", strerror(errno));
69 return PTS_UNRESOLVED;
70 }
71
72 pa = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
73 if (pa == MAP_FAILED) {
74 printf("Error at mmap: %s\n", strerror(errno));
75 return PTS_FAIL;
76 }
77
78 *(char *)pa = 'b';
79 return 0;
80 break;
81 case -1:
82 printf("Error at fork(): %s\n", strerror(errno));
83 return PTS_UNRESOLVED;
84 break;
85 default:
86 break;
87 }
88
89 waitpid(child, &status, WUNTRACED);
90 close(fd);
91
92 if (WIFSIGNALED(status)) {
93 sig_num = WTERMSIG(status);
94 printf("Child process terminated by signal %d\n", sig_num);
95 if (sig_num == SIGSEGV) {
96 printf("Got SIGSEGV when writing to the mapped memory, "
97 "without setting PROT_WRITE\n" "Test PASSED\n");
98 return PTS_PASS;
99 }
100 }
101
102 if (WIFEXITED(status)) {
103 if (WEXITSTATUS(status) == 0) {
104 printf
105 ("Did not got SIGSEGV when writing to the mapped memory,"
106 " without setting PROT_WRITE\n" "Test FAILED\n");
107 return PTS_FAIL;
108 }
109 }
110
111 printf("Test Unresolved\n");
112 return PTS_UNRESOLVED;
113 #else
114 printf("Test Unsupported, _POSIX_MEMORY_PROTECTION not defined\n");
115 return PTS_UNSUPPORTED;
116 #endif
117 }
118