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