• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define _XOPEN_SOURCE 600
30 #include <sys/mman.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "posixtest.h"
42 
main(void)43 int main(void)
44 {
45 #ifdef _POSIX_MEMORY_PROTECTION
46 	char tmpfname[256];
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 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_6_2_%d", getpid());
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 	switch (child) {
66 	case 0:
67 		pa = mmap(NULL, size, PROT_NONE, MAP_SHARED, fd, 0);
68 		if (pa == MAP_FAILED) {
69 			printf("Error at mmap: %s\n", strerror(errno));
70 			return PTS_FAIL;
71 		}
72 
73 		/* Write acess */
74 		*(char *)pa = 'b';
75 		return 0;
76 		break;
77 	case -1:
78 		perror("fork()");
79 		return PTS_UNRESOLVED;
80 		break;
81 	default:
82 		break;
83 	}
84 
85 	waitpid(child, &status, 0);
86 	close(fd);
87 
88 	if (WIFSIGNALED(status)) {
89 		sig_num = WTERMSIG(status);
90 		printf("Child process terminated by signal %d\n", sig_num);
91 		if (sig_num == SIGSEGV) {
92 			printf("Got SIGSEGV when writing to the mapped memory, "
93 			       "setting PROT_NOTE\n" "Test PASSED\n");
94 			return PTS_PASS;
95 		}
96 	}
97 
98 	if (WIFEXITED(status)) {
99 		if (WEXITSTATUS(status) == 0) {
100 			printf
101 			    ("Did not got SIGSEGV when writing to the mapped memory, "
102 			     "setting PROT_NOTE\n" "Test FAILED\n");
103 			return PTS_FAIL;
104 		}
105 
106 	}
107 
108 	printf("Test Unresolved\n");
109 	return PTS_UNRESOLVED;
110 #else
111 	printf("Test Usupported: _POSIX_MEMORY_PROTECTION not defined\n");
112 	return PTS_UNSUPPORTED;
113 #endif
114 }
115