1 /* Verify that child of the vfork-calling function can't return when
2    shadow stack is in use.
3    Copyright (C) 2020-2021 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
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.
10 
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.
15 
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/>.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <x86intrin.h>
27 #include <support/test-driver.h>
28 #include <support/xsignal.h>
29 #include <support/xunistd.h>
30 
31 __attribute__ ((noclone, noinline))
32 static void
do_test_1(void)33 do_test_1 (void)
34 {
35   pid_t p1;
36   int fd[2];
37 
38   if (pipe (fd) == -1)
39     {
40       puts ("pipe failed");
41       _exit (EXIT_FAILURE);
42     }
43 
44   if ((p1 = vfork ()) == 0)
45     {
46       pid_t p = getpid ();
47       TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p)));
48       /* Child return should trigger SIGSEGV.  */
49       return;
50     }
51   else if (p1 == -1)
52     {
53       puts ("vfork failed");
54       _exit (EXIT_FAILURE);
55     }
56 
57   pid_t p2 = 0;
58   if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t)))
59       != sizeof (pid_t))
60     puts ("pipd read failed");
61   else
62     {
63       int r;
64       if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
65 	puts ("waitpid failed");
66       else if (r != 0)
67 	puts ("pip write in child failed");
68     }
69 
70   /* Parent exits immediately so that parent returns without triggering
71      SIGSEGV when shadow stack isn't in use.  */
72   _exit (EXIT_FAILURE);
73 }
74 
75 static int
do_test(void)76 do_test (void)
77 {
78   /* NB: This test should trigger SIGSEGV with shadow stack enabled.  */
79   if (_get_ssp () == 0)
80     return EXIT_UNSUPPORTED;
81   do_test_1 ();
82   /* Child exits immediately so that child returns without triggering
83      SIGSEGV when shadow stack isn't in use.  */
84   _exit (EXIT_FAILURE);
85 }
86 
87 #define EXPECTED_SIGNAL (_get_ssp () == 0 ? 0 : SIGSEGV)
88 #include <support/test-driver.c>
89