1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2025 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions /*
30*bbb1b6f9SApple OSS Distributions * rvi_doublefree.c
31*bbb1b6f9SApple OSS Distributions *
32*bbb1b6f9SApple OSS Distributions * Test for mbuf_pullup() double-free vulnerability in
33*bbb1b6f9SApple OSS Distributions * rvi_ctl_send -> rvi_bpf_tap
34*bbb1b6f9SApple OSS Distributions *
35*bbb1b6f9SApple OSS Distributions * This test verifies that the Remote VIF (RVI) control interface
36*bbb1b6f9SApple OSS Distributions * properly handles malformed pktap headers without triggering
37*bbb1b6f9SApple OSS Distributions * a double-free condition.
38*bbb1b6f9SApple OSS Distributions */
39*bbb1b6f9SApple OSS Distributions
40*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
41*bbb1b6f9SApple OSS Distributions
42*bbb1b6f9SApple OSS Distributions #include <stdint.h>
43*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
44*bbb1b6f9SApple OSS Distributions #include <string.h>
45*bbb1b6f9SApple OSS Distributions #include <unistd.h>
46*bbb1b6f9SApple OSS Distributions #include <fcntl.h>
47*bbb1b6f9SApple OSS Distributions #include <errno.h>
48*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
49*bbb1b6f9SApple OSS Distributions #include <sys/sys_domain.h>
50*bbb1b6f9SApple OSS Distributions #include <sys/kern_control.h>
51*bbb1b6f9SApple OSS Distributions #include <sys/ioctl.h>
52*bbb1b6f9SApple OSS Distributions #include <net/if.h>
53*bbb1b6f9SApple OSS Distributions #include <net/bpf.h>
54*bbb1b6f9SApple OSS Distributions #include <pcap/dlt.h>
55*bbb1b6f9SApple OSS Distributions
56*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
57*bbb1b6f9SApple OSS Distributions T_META_NAMESPACE("xnu.net"),
58*bbb1b6f9SApple OSS Distributions T_META_ASROOT(true),
59*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
60*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"),
61*bbb1b6f9SApple OSS Distributions T_META_CHECK_LEAKS(false),
62*bbb1b6f9SApple OSS Distributions T_META_ENABLED(false)
63*bbb1b6f9SApple OSS Distributions );
64*bbb1b6f9SApple OSS Distributions
65*bbb1b6f9SApple OSS Distributions /* ------ minimal pktap header -------- */
66*bbb1b6f9SApple OSS Distributions struct pktap_header {
67*bbb1b6f9SApple OSS Distributions uint32_t pth_length;
68*bbb1b6f9SApple OSS Distributions uint32_t pth_type_next;
69*bbb1b6f9SApple OSS Distributions uint32_t pth_dlt;
70*bbb1b6f9SApple OSS Distributions char pth_ifname[64];
71*bbb1b6f9SApple OSS Distributions uint32_t pth_flags;
72*bbb1b6f9SApple OSS Distributions uint32_t pth_protocol_family;
73*bbb1b6f9SApple OSS Distributions uint32_t pth_frame_pre_length;
74*bbb1b6f9SApple OSS Distributions uint32_t pth_frame_post_length;
75*bbb1b6f9SApple OSS Distributions };
76*bbb1b6f9SApple OSS Distributions /* -------------------------------------------------------------- */
77*bbb1b6f9SApple OSS Distributions
78*bbb1b6f9SApple OSS Distributions /* - Remote-VIF control constants (snippets from remote_vif.h) -- */
79*bbb1b6f9SApple OSS Distributions #define RVI_CTL_NAME "com.apple.net.rvi_control"
80*bbb1b6f9SApple OSS Distributions #define RVI_COMMAND_GET_INTERFACE 0x20 /* getsockopt -> rviN */
81*bbb1b6f9SApple OSS Distributions
82*bbb1b6f9SApple OSS Distributions static int
open_rvi_socket(void)83*bbb1b6f9SApple OSS Distributions open_rvi_socket(void)
84*bbb1b6f9SApple OSS Distributions {
85*bbb1b6f9SApple OSS Distributions /* open PF_SYSTEM / SYSPROTO_CONTROL and auto-create a fresh rviN */
86*bbb1b6f9SApple OSS Distributions int fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
87*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd, "socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)");
88*bbb1b6f9SApple OSS Distributions
89*bbb1b6f9SApple OSS Distributions struct ctl_info ci = {0};
90*bbb1b6f9SApple OSS Distributions strlcpy(ci.ctl_name, RVI_CTL_NAME, sizeof(ci.ctl_name));
91*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, CTLIOCGINFO, &ci), "ioctl(CTLIOCGINFO)");
92*bbb1b6f9SApple OSS Distributions
93*bbb1b6f9SApple OSS Distributions struct sockaddr_ctl sc = {
94*bbb1b6f9SApple OSS Distributions .sc_len = sizeof(sc),
95*bbb1b6f9SApple OSS Distributions .sc_family = AF_SYSTEM,
96*bbb1b6f9SApple OSS Distributions .ss_sysaddr = AF_SYS_CONTROL,
97*bbb1b6f9SApple OSS Distributions .sc_id = ci.ctl_id,
98*bbb1b6f9SApple OSS Distributions .sc_unit = 0 /* allocate new rviN */
99*bbb1b6f9SApple OSS Distributions };
100*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(connect(fd, (struct sockaddr *)&sc, sizeof(sc)),
101*bbb1b6f9SApple OSS Distributions "connect to RVI control socket");
102*bbb1b6f9SApple OSS Distributions
103*bbb1b6f9SApple OSS Distributions return fd;
104*bbb1b6f9SApple OSS Distributions }
105*bbb1b6f9SApple OSS Distributions
106*bbb1b6f9SApple OSS Distributions static void
get_rvi_ifname(int kctl_fd,char ifname[IFNAMSIZ])107*bbb1b6f9SApple OSS Distributions get_rvi_ifname(int kctl_fd, char ifname[IFNAMSIZ])
108*bbb1b6f9SApple OSS Distributions {
109*bbb1b6f9SApple OSS Distributions /* find out what interface was created (rviN) */
110*bbb1b6f9SApple OSS Distributions socklen_t len = IFNAMSIZ;
111*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(kctl_fd, SYSPROTO_CONTROL,
112*bbb1b6f9SApple OSS Distributions RVI_COMMAND_GET_INTERFACE, ifname, &len),
113*bbb1b6f9SApple OSS Distributions "getsockopt(RVI_COMMAND_GET_INTERFACE)");
114*bbb1b6f9SApple OSS Distributions }
115*bbb1b6f9SApple OSS Distributions
116*bbb1b6f9SApple OSS Distributions static int
bump_raw_count(const char * ifname)117*bbb1b6f9SApple OSS Distributions bump_raw_count(const char *ifname)
118*bbb1b6f9SApple OSS Distributions {
119*bbb1b6f9SApple OSS Distributions /* open /dev/bpf, attach DLT_RAW -> _raw_count++ in rvi_set_bpf_tap */
120*bbb1b6f9SApple OSS Distributions int bpf_fd = open("/dev/bpf1", O_RDONLY);
121*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bpf_fd, "open(/dev/bpf1)");
122*bbb1b6f9SApple OSS Distributions
123*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(bpf_fd, BIOCSETIF, (void *)ifname),
124*bbb1b6f9SApple OSS Distributions "ioctl(BIOCSETIF, %s)", ifname);
125*bbb1b6f9SApple OSS Distributions
126*bbb1b6f9SApple OSS Distributions u_int dlt = DLT_RAW;
127*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(bpf_fd, BIOCSDLT, &dlt),
128*bbb1b6f9SApple OSS Distributions "ioctl(BIOCSDLT, DLT_RAW)");
129*bbb1b6f9SApple OSS Distributions
130*bbb1b6f9SApple OSS Distributions /*
131*bbb1b6f9SApple OSS Distributions * CRITICAL: Do NOT close the BPF file descriptor here.
132*bbb1b6f9SApple OSS Distributions * The vulnerability requires the BPF descriptor to remain open
133*bbb1b6f9SApple OSS Distributions * to maintain _raw_count > 0 for the rvi_bpf_tap code path.
134*bbb1b6f9SApple OSS Distributions */
135*bbb1b6f9SApple OSS Distributions return bpf_fd; /* Return FD for cleanup later */
136*bbb1b6f9SApple OSS Distributions }
137*bbb1b6f9SApple OSS Distributions
138*bbb1b6f9SApple OSS Distributions T_DECL(rvi_doublefree_test,
139*bbb1b6f9SApple OSS Distributions "Test RVI control interface resilience against malformed pktap headers",
140*bbb1b6f9SApple OSS Distributions T_META_ASROOT(true))
141*bbb1b6f9SApple OSS Distributions {
142*bbb1b6f9SApple OSS Distributions char ifname[IFNAMSIZ] = {0};
143*bbb1b6f9SApple OSS Distributions int rvifd = -1;
144*bbb1b6f9SApple OSS Distributions int bpf_fd = -1;
145*bbb1b6f9SApple OSS Distributions
146*bbb1b6f9SApple OSS Distributions rvifd = open_rvi_socket();
147*bbb1b6f9SApple OSS Distributions get_rvi_ifname(rvifd, ifname);
148*bbb1b6f9SApple OSS Distributions T_LOG("Created RVI interface: %s", ifname);
149*bbb1b6f9SApple OSS Distributions
150*bbb1b6f9SApple OSS Distributions /* Increase _raw_count to trigger rvi_bpf_tap code path */
151*bbb1b6f9SApple OSS Distributions /* CRITICAL: Keep BPF FD open to maintain vulnerability conditions */
152*bbb1b6f9SApple OSS Distributions bpf_fd = bump_raw_count(ifname);
153*bbb1b6f9SApple OSS Distributions
154*bbb1b6f9SApple OSS Distributions /* ------------------- craft the malicious mbuf --------------------- */
155*bbb1b6f9SApple OSS Distributions size_t hdr_len = 256; /* > sizeof(pktap_header) */
156*bbb1b6f9SApple OSS Distributions char *hdr = calloc(1, hdr_len); /* zero-initialised */
157*bbb1b6f9SApple OSS Distributions T_ASSERT_NOTNULL(hdr, "allocate pktap header buffer");
158*bbb1b6f9SApple OSS Distributions
159*bbb1b6f9SApple OSS Distributions /* set pth_protocol_family (offset 40) = AF_INET */
160*bbb1b6f9SApple OSS Distributions *(uint32_t *)(hdr + 40) = AF_INET;
161*bbb1b6f9SApple OSS Distributions
162*bbb1b6f9SApple OSS Distributions /* set pth_frame_pre_length (offset 44) = huge value -> pullup fails */
163*bbb1b6f9SApple OSS Distributions *(uint32_t *)(hdr + 44) = 0x10000000;
164*bbb1b6f9SApple OSS Distributions
165*bbb1b6f9SApple OSS Distributions const char payload[] = "A"; /* 1-byte dummy "IP packet" */
166*bbb1b6f9SApple OSS Distributions size_t plen = hdr_len + sizeof(payload);
167*bbb1b6f9SApple OSS Distributions
168*bbb1b6f9SApple OSS Distributions char *buf = malloc(plen);
169*bbb1b6f9SApple OSS Distributions T_ASSERT_NOTNULL(buf, "allocate send buffer");
170*bbb1b6f9SApple OSS Distributions
171*bbb1b6f9SApple OSS Distributions memcpy(buf, hdr, hdr_len);
172*bbb1b6f9SApple OSS Distributions memcpy(buf + hdr_len, payload, sizeof(payload));
173*bbb1b6f9SApple OSS Distributions
174*bbb1b6f9SApple OSS Distributions /*
175*bbb1b6f9SApple OSS Distributions * This send operation should not cause a kernel panic or double-free.
176*bbb1b6f9SApple OSS Distributions * The kernel should gracefully handle the malformed pktap header.
177*bbb1b6f9SApple OSS Distributions */
178*bbb1b6f9SApple OSS Distributions ssize_t sent = send(rvifd, buf, plen, 0);
179*bbb1b6f9SApple OSS Distributions
180*bbb1b6f9SApple OSS Distributions /*
181*bbb1b6f9SApple OSS Distributions * We expect this to either succeed (if the vulnerability is fixed)
182*bbb1b6f9SApple OSS Distributions * or fail gracefully (without a panic). The key is that we shouldn't
183*bbb1b6f9SApple OSS Distributions * crash the system.
184*bbb1b6f9SApple OSS Distributions */
185*bbb1b6f9SApple OSS Distributions if (sent < 0) {
186*bbb1b6f9SApple OSS Distributions T_LOG("send() failed as expected: %s", strerror(errno));
187*bbb1b6f9SApple OSS Distributions } else {
188*bbb1b6f9SApple OSS Distributions T_LOG("send() succeeded, sent %zd bytes", sent);
189*bbb1b6f9SApple OSS Distributions }
190*bbb1b6f9SApple OSS Distributions
191*bbb1b6f9SApple OSS Distributions /* If we reach this point, the kernel didn't panic - test passes */
192*bbb1b6f9SApple OSS Distributions T_PASS("RVI interface handled malformed pktap header without crashing");
193*bbb1b6f9SApple OSS Distributions
194*bbb1b6f9SApple OSS Distributions /* Clean up resources */
195*bbb1b6f9SApple OSS Distributions free(hdr);
196*bbb1b6f9SApple OSS Distributions free(buf);
197*bbb1b6f9SApple OSS Distributions if (bpf_fd >= 0) {
198*bbb1b6f9SApple OSS Distributions close(bpf_fd);
199*bbb1b6f9SApple OSS Distributions }
200*bbb1b6f9SApple OSS Distributions if (rvifd >= 0) {
201*bbb1b6f9SApple OSS Distributions close(rvifd);
202*bbb1b6f9SApple OSS Distributions }
203*bbb1b6f9SApple OSS Distributions }
204