NEWS: Add entry for CVE-2021-27645
[glibc.git] / rt / tst-shm.c
blob19c5b35438cc47cb81079750d3628f4b436565a8
1 /* Test program for POSIX shm_* functions.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <error.h>
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
34 /* We want to see output immediately. */
35 #define STDOUT_UNBUFFERED
37 static char shm_test_name[sizeof "/glibc-shm-test-" + sizeof (pid_t) * 3];
38 static char shm_escape_name[sizeof "/../escaped-" + sizeof (pid_t) * 3];
40 static void
41 init_shm_test_names (void)
43 snprintf (shm_test_name, sizeof (shm_test_name), "/glibc-shm-test-%u",
44 getpid ());
45 snprintf (shm_escape_name, sizeof (shm_escape_name), "/../escaped-%u",
46 getpid ());
49 static void
50 worker (int write_now)
52 struct timespec ts;
53 struct stat64 st;
54 int i;
56 int fd = shm_open (shm_test_name, O_RDWR, 0600);
58 if (fd == -1)
59 error (EXIT_FAILURE, 0, "failed to open shared memory object: shm_open");
61 char *mem;
63 if (fd == -1)
64 exit (fd);
66 if (fstat64 (fd, &st) == -1)
67 error (EXIT_FAILURE, 0, "stat failed");
68 if (st.st_size != 4000)
69 error (EXIT_FAILURE, 0, "size incorrect");
71 mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
72 if (mem == MAP_FAILED)
73 error (EXIT_FAILURE, 0, "mmap failed");
75 ts.tv_sec = 0;
76 ts.tv_nsec = 500000000;
78 if (write_now)
79 for (i = 0; i <= 4; ++i)
80 mem[i] = i;
81 else
82 /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
83 while (1)
85 for (i = 0; i <= 4; ++i)
86 if (mem[i] != i)
87 break;
89 if (i > 4)
90 /* OK, that's done. */
91 break;
93 nanosleep (&ts, NULL);
96 if (!write_now)
97 for (i = 0; i <= 4; ++i)
98 mem[i] = 4 + i;
99 else
100 /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
101 while (1)
103 for (i = 0; i <= 4; ++i)
104 if (mem[i] != 4 + i)
105 break;
107 if (i > 4)
108 /* OK, that's done. */
109 break;
111 nanosleep (&ts, NULL);
114 if (munmap (mem, 4000) == -1)
115 error (EXIT_FAILURE, errno, "munmap");
117 close (fd);
119 exit (0);
123 static int
124 do_test (void)
126 int fd;
127 pid_t pid1;
128 pid_t pid2;
129 int status1;
130 int status2;
131 struct stat64 st;
133 init_shm_test_names ();
135 fd = shm_open (shm_escape_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
136 if (fd != -1)
138 perror ("read file outside of SHMDIR directory");
139 return 1;
143 /* Create the shared memory object. */
144 fd = shm_open (shm_test_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
145 if (fd == -1)
147 /* If shm_open is unimplemented we skip the test. */
148 if (errno == ENOSYS)
150 perror ("shm_open unimplemented. Test skipped.");
151 return 0;
153 else
154 error (EXIT_FAILURE, 0, "failed to create shared memory object: shm_open");
157 /* Size the object. We make it 4000 bytes long. */
158 if (ftruncate (fd, 4000) == -1)
160 /* This failed. Must be a bug in the implementation of the
161 shared memory itself. */
162 perror ("failed to size of shared memory object: ftruncate");
163 close (fd);
164 shm_unlink (shm_test_name);
165 return 0;
168 if (fstat64 (fd, &st) == -1)
170 shm_unlink (shm_test_name);
171 error (EXIT_FAILURE, 0, "initial stat failed");
173 if (st.st_size != 4000)
175 shm_unlink (shm_test_name);
176 error (EXIT_FAILURE, 0, "initial size not correct");
179 /* Spawn to processes which will do the work. */
180 pid1 = fork ();
181 if (pid1 == 0)
182 worker (0);
183 else if (pid1 == -1)
185 /* Couldn't create a second process. */
186 perror ("fork");
187 close (fd);
188 shm_unlink (shm_test_name);
189 return 0;
192 pid2 = fork ();
193 if (pid2 == 0)
194 worker (1);
195 else if (pid2 == -1)
197 /* Couldn't create a second process. */
198 int ignore;
199 perror ("fork");
200 kill (pid1, SIGTERM);
201 waitpid (pid1, &ignore, 0);
202 close (fd);
203 shm_unlink (shm_test_name);
204 return 0;
207 /* Wait until the two processes are finished. */
208 waitpid (pid1, &status1, 0);
209 waitpid (pid2, &status2, 0);
211 /* Now we can unlink the shared object. */
212 shm_unlink (shm_test_name);
214 return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
215 || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
218 static void
219 cleanup_handler (void)
221 shm_unlink (shm_test_name);
224 #define CLEANUP_HANDLER cleanup_handler
226 #include <support/test-driver.c>