Home | History | Annotate | Download | only in mmap
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Copyright (c) 2012, Cyril Hrubis <chrubis (at) 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 read 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 
     43 int main(void)
     44 {
     45 #ifdef _POSIX_MEMORY_PROTECTION
     46 	char tmpfname[256];
     47 	ssize_t size = 1024;
     48 	char data[size];
     49 	void *pa;
     50 	int fd;
     51 
     52 	pid_t child;
     53 	int status;
     54 	int sig_num;
     55 
     56 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_6_2_%d", getpid());
     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 		memset(data, 'a', size);
     69 		if (write(fd, data, size) != size) {
     70 			printf("Error at write(): %s\n", strerror(errno));
     71 			return PTS_UNRESOLVED;
     72 		}
     73 
     74 		pa = mmap(NULL, size, PROT_NONE, MAP_SHARED, fd, 0);
     75 		if (pa == MAP_FAILED) {
     76 			printf("Error at mmap: %s\n", strerror(errno));
     77 			return PTS_FAIL;
     78 		}
     79 
     80 		/* Read acess */
     81 		if (*(char *)pa != 'a') {
     82 			printf("Test Fail: The file is not mapped correctly\n");
     83 			return PTS_FAIL;
     84 		}
     85 		return 0;
     86 		break;
     87 	case -1:
     88 		printf("Error at fork(): %s\n", strerror(errno));
     89 		return PTS_UNRESOLVED;
     90 		break;
     91 	default:
     92 		break;
     93 	}
     94 
     95 	waitpid(child, &status, 0);
     96 	close(fd);
     97 
     98 	if (WIFSIGNALED(status)) {
     99 		sig_num = WTERMSIG(status);
    100 		printf("Child process terminated by signal %d\n", sig_num);
    101 		if (sig_num == SIGSEGV) {
    102 			printf("Got SIGSEGV when reading the mapped memory, "
    103 			       "setting PROT_NOTE\n" "Test PASSED\n");
    104 			return PTS_PASS;
    105 		}
    106 	}
    107 
    108 	if (WIFEXITED(status)) {
    109 		if (WEXITSTATUS(status) == 0) {
    110 			printf
    111 			    ("Did not got SIGSEGV when reading the mapped memory, "
    112 			     "setting PROT_NOTE\n" "Test FAILED\n");
    113 			return PTS_FAIL;
    114 		}
    115 
    116 	}
    117 
    118 	printf("Test Unresolved\n");
    119 	return PTS_UNRESOLVED;
    120 #else
    121 	printf("Test Usupported: _POSIX_MEMORY_PROTECTION not defined\n");
    122 	return PTS_UNSUPPORTED;
    123 #endif
    124 }
    125