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