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