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 * The file descriptor fildes shall have been opened with read permission,
10 * regardless of the protection options specified. If PROT_WRITE is
11 * specified, the application shall ensure that it has opened the file
12 * descriptor fildes with write permission unless MAP_PRIVATE
13 * is specified in the flags parameter as described below.
14 *
15 * Test Steps:
16 * 1 Open a file with write only permition.
17 * 2. Mmap the file to a memory region setting prot as PROT_READ.
18 * 3. Get EACCES error when mmap().
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "posixtest.h"
32
main(void)33 int main(void)
34 {
35 char tmpfname[256];
36 void *pa;
37 size_t size = 1024;
38 int fd;
39
40 /* Create the tmp file */
41 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_6_6_%d", getpid());
42 unlink(tmpfname);
43 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
44 if (fd == -1) {
45 printf("Error at open(): %s\n", strerror(errno));
46 return PTS_UNRESOLVED;
47 }
48 if (ftruncate(fd, size) == -1) {
49 printf("Error at ftruncate(): %s\n", strerror(errno));
50 return PTS_UNRESOLVED;
51 }
52 close(fd);
53
54 /* Open write only */
55 fd = open(tmpfname, O_WRONLY, S_IRUSR | S_IWUSR);
56 if (fd == -1) {
57 printf("Error at open(): %s\n", strerror(errno));
58 return PTS_UNRESOLVED;
59 }
60 unlink(tmpfname);
61
62 /* prot can be set as whatever value */
63 pa = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
64 if (pa == MAP_FAILED && errno == EACCES) {
65 printf("EACCES on attempt to map writeonly file as "
66 "PROT_READ\n" "Test PASSED\n");
67 return PTS_PASS;
68 }
69
70 printf("Mapping writeonly file with PROT_READ have not "
71 "returned EACCES\n" "Test FAILED\n");
72 return PTS_FAIL;
73 }
74