1 /*-
2  * Copyright (c) 2006 Bruce M. Simpson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Regression test for uiomove in kernel; specifically for PR kern/38495.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 
37 #include <stdlib.h>
38 #include <signal.h>
39 #include <setjmp.h>
40 #include <string.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <unistd.h>
44 
45 static char socket_path[] = "tmp.XXXXXX";
46 
47 static jmp_buf myjmpbuf;
48 
49 static void handle_sigalrm(int signo __unused)
50 {
51 	longjmp(myjmpbuf, 1);
52 }
53 
54 int
55 main(void)
56 {
57 	struct sockaddr_un un;
58 	pid_t pid;
59 	int s;
60 
61 	if (mkstemp(socket_path) == -1)
62 		err(1, "mkstemp");
63 	s = socket(PF_LOCAL, SOCK_DGRAM, 0);
64 	if (s == -1)
65 		errx(-1, "socket");
66 	memset(&un, 0, sizeof(un));
67 	un.sun_family = AF_LOCAL;
68 	unlink(socket_path);
69 	strcpy(un.sun_path, socket_path);
70 	if (bind(s, (struct sockaddr *)&un, sizeof(un)) == -1)
71 		errx(-1, "bind");
72 	pid = fork();
73 	if (pid == -1)
74 		errx(-1, "fork");
75 	if (pid == 0) {
76 		int conn;
77 		char buf[] = "AAAAAAAAA";
78 
79 		close(s);
80 		conn = socket(AF_LOCAL, SOCK_DGRAM, 0);
81 		if (conn == -1)
82 			errx(-1,"socket");
83 		if (sendto(conn, buf, sizeof(buf), 0, (struct sockaddr *)&un,
84 		    sizeof(un)) != sizeof(buf))
85 			errx(-1,"sendto");
86 		close(conn);
87 		_exit(0);
88 	}
89 
90 	sleep(5);
91 
92 	/* Make sure the data is there when we try to receive it. */
93 	if (recvfrom(s, (void *)-1, 1, 0, NULL, NULL) != -1)
94 		errx(-1,"recvfrom succeeded when failure expected");
95 
96 	(void)signal(SIGALRM, handle_sigalrm);
97 	if (setjmp(myjmpbuf) == 0) {
98 		/*
99 	 	 * This recvfrom will panic an unpatched system, and block
100 		 * a patched one.
101 		 */
102 		alarm(5);
103 		(void)recvfrom(s, (void *)-1, 1, 0, NULL, NULL);
104 	}
105 
106 	/* We should reach here via longjmp() and all should be well. */
107 
108 	return (0);
109 }
110