xref: /xnu-8796.121.2/tests/IP6_EXTHDR_CHECK_61873584.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <errno.h>
2*c54f35caSApple OSS Distributions #include <stdio.h>
3*c54f35caSApple OSS Distributions #include <string.h>
4*c54f35caSApple OSS Distributions #include <stdlib.h>
5*c54f35caSApple OSS Distributions #include <unistd.h>
6*c54f35caSApple OSS Distributions #include <arpa/inet.h>
7*c54f35caSApple OSS Distributions #include <net/if_var.h>
8*c54f35caSApple OSS Distributions #include <netinet/ip6.h>
9*c54f35caSApple OSS Distributions #include <darwintest.h>
10*c54f35caSApple OSS Distributions 
11*c54f35caSApple OSS Distributions struct packet1 {
12*c54f35caSApple OSS Distributions 	struct ip6_hbh hbh;
13*c54f35caSApple OSS Distributions 	struct ip6_opt hbh_opt;
14*c54f35caSApple OSS Distributions 	uint8_t hbh_pad[4];
15*c54f35caSApple OSS Distributions 	struct ip6_frag frag;
16*c54f35caSApple OSS Distributions 	struct ip6_dest dest;
17*c54f35caSApple OSS Distributions 	struct ip6_opt dest_opt;
18*c54f35caSApple OSS Distributions 	uint8_t dest_pad[4];
19*c54f35caSApple OSS Distributions };
20*c54f35caSApple OSS Distributions 
21*c54f35caSApple OSS Distributions struct packet2 {
22*c54f35caSApple OSS Distributions 	struct ip6_hbh hbh;
23*c54f35caSApple OSS Distributions 	struct ip6_opt hbh_opt;
24*c54f35caSApple OSS Distributions 	uint8_t hbh_pad[4];
25*c54f35caSApple OSS Distributions 	struct ip6_frag frag;
26*c54f35caSApple OSS Distributions 	struct ip6_opt dest_opt;
27*c54f35caSApple OSS Distributions 	uint8_t dest_pad[6];
28*c54f35caSApple OSS Distributions 	uint8_t payload[16];
29*c54f35caSApple OSS Distributions };
30*c54f35caSApple OSS Distributions 
31*c54f35caSApple OSS Distributions T_DECL(IP6_EXTHDR_CHECK_ICMPV6_61873584, "ICMPv6 test for IP6_EXTHDR_CHECK stale mbuf pointer vulnerability", T_META("as_root", "true"))
32*c54f35caSApple OSS Distributions {
33*c54f35caSApple OSS Distributions 	struct sockaddr_in6 daddr;
34*c54f35caSApple OSS Distributions 	struct packet1 packet1;
35*c54f35caSApple OSS Distributions 	struct packet2 packet2;
36*c54f35caSApple OSS Distributions 	int s, id, res;
37*c54f35caSApple OSS Distributions 
38*c54f35caSApple OSS Distributions 	srand(time(NULL));
39*c54f35caSApple OSS Distributions 	id = rand();
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
42*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_RAW, IPPROTO_HOPOPTS), NULL);
43*c54f35caSApple OSS Distributions 	T_SETUPEND;
44*c54f35caSApple OSS Distributions 
45*c54f35caSApple OSS Distributions 	memset(&daddr, 0, sizeof(daddr));
46*c54f35caSApple OSS Distributions 	daddr.sin6_family = AF_INET6;
47*c54f35caSApple OSS Distributions 	daddr.sin6_port = 0;
48*c54f35caSApple OSS Distributions 	inet_pton(AF_INET6, "::1", &daddr.sin6_addr);
49*c54f35caSApple OSS Distributions 
50*c54f35caSApple OSS Distributions 	memset(&packet1, 'A', sizeof(struct packet1));
51*c54f35caSApple OSS Distributions 	packet1.hbh.ip6h_nxt = IPPROTO_FRAGMENT;
52*c54f35caSApple OSS Distributions 	packet1.hbh.ip6h_len = 0;
53*c54f35caSApple OSS Distributions 	packet1.hbh_opt.ip6o_type = IP6OPT_PADN;
54*c54f35caSApple OSS Distributions 	packet1.hbh_opt.ip6o_len = 4;
55*c54f35caSApple OSS Distributions 	packet1.frag.ip6f_nxt = IPPROTO_DSTOPTS;
56*c54f35caSApple OSS Distributions 	packet1.frag.ip6f_reserved = 0;
57*c54f35caSApple OSS Distributions 	packet1.frag.ip6f_offlg = htons(0) | IP6F_MORE_FRAG;
58*c54f35caSApple OSS Distributions 	packet1.frag.ip6f_ident = id;
59*c54f35caSApple OSS Distributions 	// Use IPPROTO_RAW for "assertion failed: m->m_flags & M_PKTHDR" panic
60*c54f35caSApple OSS Distributions 	// Use IPPROTO_ICMPV6 for "m_free: freeing an already freed mbuf" panic
61*c54f35caSApple OSS Distributions 	packet1.dest.ip6d_nxt = IPPROTO_RAW;
62*c54f35caSApple OSS Distributions 	packet1.dest.ip6d_len = 1;
63*c54f35caSApple OSS Distributions 	packet1.dest_opt.ip6o_type = IP6OPT_PADN;
64*c54f35caSApple OSS Distributions 	packet1.dest_opt.ip6o_len = 4;
65*c54f35caSApple OSS Distributions 
66*c54f35caSApple OSS Distributions 	memset(&packet2, 'B', sizeof(struct packet2));
67*c54f35caSApple OSS Distributions 	packet2.hbh.ip6h_nxt = IPPROTO_FRAGMENT;
68*c54f35caSApple OSS Distributions 	packet2.hbh.ip6h_len = 0;
69*c54f35caSApple OSS Distributions 	packet2.hbh_opt.ip6o_type = IP6OPT_PADN;
70*c54f35caSApple OSS Distributions 	packet2.hbh_opt.ip6o_len = 4;
71*c54f35caSApple OSS Distributions 	packet2.frag.ip6f_nxt = IPPROTO_DSTOPTS;
72*c54f35caSApple OSS Distributions 	packet2.frag.ip6f_reserved = 0;
73*c54f35caSApple OSS Distributions 	packet2.frag.ip6f_offlg = htons(8);
74*c54f35caSApple OSS Distributions 	packet2.frag.ip6f_ident = id;
75*c54f35caSApple OSS Distributions 	packet2.dest_opt.ip6o_type = IP6OPT_PADN;
76*c54f35caSApple OSS Distributions 	packet2.dest_opt.ip6o_len = 6;
77*c54f35caSApple OSS Distributions 
78*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(res = sendto(s, (char *)&packet1, sizeof(packet1), 0,
79*c54f35caSApple OSS Distributions 	    (struct sockaddr *)&daddr, (socklen_t)sizeof(daddr)), NULL);
80*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(res = sendto(s, (char *)&packet2, sizeof(packet2), 0,
81*c54f35caSApple OSS Distributions 	    (struct sockaddr *)&daddr, (socklen_t)sizeof(daddr)), NULL);
82*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(res = close(s), NULL);
83*c54f35caSApple OSS Distributions }
84