xref: /xnu-8020.140.41/tests/route_output_stack_oflow_56033075.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions #include <errno.h>
2*27b03b36SApple OSS Distributions #include <stdio.h>
3*27b03b36SApple OSS Distributions #include <stdlib.h>
4*27b03b36SApple OSS Distributions #include <string.h>
5*27b03b36SApple OSS Distributions #include <strings.h>
6*27b03b36SApple OSS Distributions 
7*27b03b36SApple OSS Distributions #include <net/route.h>
8*27b03b36SApple OSS Distributions #include <sys/socket.h>
9*27b03b36SApple OSS Distributions #include <unistd.h>
10*27b03b36SApple OSS Distributions 
11*27b03b36SApple OSS Distributions #include <darwintest.h>
12*27b03b36SApple OSS Distributions 
13*27b03b36SApple OSS Distributions #define ROUNDUP32(n) (((n) + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1))
14*27b03b36SApple OSS Distributions 
15*27b03b36SApple OSS Distributions T_DECL(route_output_stack_oflow_56033075, "Stack overflow via ma_copy through route_output")
16*27b03b36SApple OSS Distributions {
17*27b03b36SApple OSS Distributions 	int s;
18*27b03b36SApple OSS Distributions 	uint8_t buf[
19*27b03b36SApple OSS Distributions 		sizeof(struct rt_msghdr) +
20*27b03b36SApple OSS Distributions 		ROUNDUP32(sizeof(struct sockaddr_storage) + 1) + /* RTAX_DST */
21*27b03b36SApple OSS Distributions 		ROUNDUP32(sizeof(struct sockaddr_storage) + 1) + /* RTAX_GATEWAY */
22*27b03b36SApple OSS Distributions 		ROUNDUP32(sizeof(struct sockaddr_storage) + 1)   /* RTAX_NETMASK */
23*27b03b36SApple OSS Distributions 	];
24*27b03b36SApple OSS Distributions 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
25*27b03b36SApple OSS Distributions 	struct sockaddr *sa;
26*27b03b36SApple OSS Distributions 	size_t len;
27*27b03b36SApple OSS Distributions 
28*27b03b36SApple OSS Distributions 	bzero(buf, sizeof(buf));
29*27b03b36SApple OSS Distributions 	rtm->rtm_type = RTM_GET;
30*27b03b36SApple OSS Distributions 	rtm->rtm_version = RTM_VERSION;
31*27b03b36SApple OSS Distributions 	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
32*27b03b36SApple OSS Distributions 	len = sizeof(struct rt_msghdr);
33*27b03b36SApple OSS Distributions 
34*27b03b36SApple OSS Distributions 	/* RTAX_DST: */
35*27b03b36SApple OSS Distributions 	sa = (struct sockaddr *)(rtm + 1);
36*27b03b36SApple OSS Distributions 	sa->sa_family = AF_INET6;
37*27b03b36SApple OSS Distributions 	sa->sa_len = sizeof(struct sockaddr_storage) + 1;
38*27b03b36SApple OSS Distributions 	memset(&sa->sa_data[0], 0xff, sa->sa_len);
39*27b03b36SApple OSS Distributions 	len += ROUNDUP32(sa->sa_len);
40*27b03b36SApple OSS Distributions 
41*27b03b36SApple OSS Distributions 	/* RTAX_GATEWAY: */
42*27b03b36SApple OSS Distributions 	sa = (struct sockaddr *)((void *)buf + len);
43*27b03b36SApple OSS Distributions 	sa->sa_family = AF_INET6;
44*27b03b36SApple OSS Distributions 	sa->sa_len = sizeof(struct sockaddr_storage) + 1;
45*27b03b36SApple OSS Distributions 	memset(&sa->sa_data[0], 0xff, sa->sa_len);
46*27b03b36SApple OSS Distributions 	len += ROUNDUP32(sa->sa_len);
47*27b03b36SApple OSS Distributions 
48*27b03b36SApple OSS Distributions 	/* RTAX_NETMASK: */
49*27b03b36SApple OSS Distributions 	sa = (struct sockaddr *)((void *)buf + len);
50*27b03b36SApple OSS Distributions 	sa->sa_family = AF_INET6;
51*27b03b36SApple OSS Distributions 	sa->sa_len = sizeof(struct sockaddr_storage) + 1;
52*27b03b36SApple OSS Distributions 	memset(&sa->sa_data[0], 0x41, sa->sa_len);
53*27b03b36SApple OSS Distributions 	len += ROUNDUP32(sa->sa_len);
54*27b03b36SApple OSS Distributions 
55*27b03b36SApple OSS Distributions 	T_SETUPBEGIN;
56*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(PF_ROUTE, SOCK_RAW, PF_ROUTE), NULL);
57*27b03b36SApple OSS Distributions 	T_SETUPEND;
58*27b03b36SApple OSS Distributions 
59*27b03b36SApple OSS Distributions 	/* check we get EINVAL for > sizeof(struct sockaddr_storage): */
60*27b03b36SApple OSS Distributions 	rtm->rtm_msglen = len;
61*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(-1, send(s, buf, len, 0), NULL);
62*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(EINVAL, errno, NULL);
63*27b03b36SApple OSS Distributions 
64*27b03b36SApple OSS Distributions 	/* now check the ok case: */
65*27b03b36SApple OSS Distributions 	len = sizeof(struct rt_msghdr);
66*27b03b36SApple OSS Distributions 
67*27b03b36SApple OSS Distributions 	/* RTAX_DST: */
68*27b03b36SApple OSS Distributions 	sa = (struct sockaddr *)(rtm + 1);
69*27b03b36SApple OSS Distributions 	sa->sa_family = AF_INET6;
70*27b03b36SApple OSS Distributions 	sa->sa_len = sizeof(struct sockaddr_storage);
71*27b03b36SApple OSS Distributions 	len += ROUNDUP32(sa->sa_len);
72*27b03b36SApple OSS Distributions 
73*27b03b36SApple OSS Distributions 	/* RTAX_GATEWAY: */
74*27b03b36SApple OSS Distributions 	sa = (struct sockaddr *)((void *)buf + len);
75*27b03b36SApple OSS Distributions 	sa->sa_family = AF_INET6;
76*27b03b36SApple OSS Distributions 	sa->sa_len = sizeof(struct sockaddr_storage);
77*27b03b36SApple OSS Distributions 	len += ROUNDUP32(sa->sa_len);
78*27b03b36SApple OSS Distributions 
79*27b03b36SApple OSS Distributions 	/* RTAX_NETMASK: */
80*27b03b36SApple OSS Distributions 	sa = (struct sockaddr *)((void *)buf + len);
81*27b03b36SApple OSS Distributions 	sa->sa_family = AF_INET6;
82*27b03b36SApple OSS Distributions 	sa->sa_len = sizeof(struct sockaddr_storage);
83*27b03b36SApple OSS Distributions 	len += ROUNDUP32(sa->sa_len);
84*27b03b36SApple OSS Distributions 
85*27b03b36SApple OSS Distributions 	rtm->rtm_msglen = len;
86*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(-1, send(s, buf, len, 0), NULL);
87*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(ESRCH, errno, NULL);
88*27b03b36SApple OSS Distributions }
89