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