/src/SockFuzzer/fuzz/api/backend.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2021 Google LLC |
3 | | * |
4 | | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | | * |
6 | | * This file contains Original Code and/or Modifications of Original Code |
7 | | * as defined in and that are subject to the Apple Public Source License |
8 | | * Version 2.0 (the 'License'). You may not use this file except in |
9 | | * compliance with the License. The rights granted to you under the License |
10 | | * may not be used to create, or enable the creation or redistribution of, |
11 | | * unlawful or unlicensed copies of an Apple operating system, or to |
12 | | * circumvent, violate, or enable the circumvention or violation of, any |
13 | | * terms of an Apple operating system software license agreement. |
14 | | * |
15 | | * Please obtain a copy of the License at |
16 | | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | | * |
18 | | * The Original Code and all software distributed under the License are |
19 | | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
20 | | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
22 | | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | | * Please see the License for the specific language governing rights and |
24 | | * limitations under the License. |
25 | | * |
26 | | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | | */ |
28 | | |
29 | | #include <sys/proc_internal.h> |
30 | | #include <sys/filedesc.h> |
31 | | |
32 | | #include "bsd/sys/_types/_size_t.h" |
33 | | #include "bsd/sys/kpi_mbuf.h" |
34 | | #include "bsd/sys/kpi_socket.h" |
35 | | #include "bsd/sys/malloc.h" |
36 | | #include "bsd/sys/protosw.h" |
37 | | #include "bsd/sys/resourcevar.h" |
38 | | |
39 | | extern ifnet_t lo_ifp; |
40 | | |
41 | | void kernel_startup_bootstrap(); |
42 | | void inpcb_timeout(void*, void*); |
43 | | void key_timehandler(void); |
44 | | void frag_timeout(void); |
45 | | void nd6_slowtimo(void); |
46 | | void nd6_timeout(void); |
47 | | void in6_tmpaddrtimer(void); |
48 | | void mp_timeout(void); |
49 | | void igmp_timeout(void); |
50 | | void tcp_fuzzer_reset(void); |
51 | | void in6_rtqtimo(void*); |
52 | | void frag6_timeout(); |
53 | | void mld_timeout(); |
54 | | bool ioctl_wrapper(unsigned long com); |
55 | | void ip_input(mbuf_t m); |
56 | | void ip6_input(mbuf_t m); |
57 | | struct mbuf* mbuf_create(const uint8_t* data, size_t size, bool is_header, |
58 | | bool force_ext, int m_type, int pktflags); |
59 | | void mcache_init(void); |
60 | | void mbinit(void); |
61 | | void eventhandler_init(void); |
62 | | void dlil_init(void); |
63 | | void socketinit(void); |
64 | | void domaininit(void); |
65 | | void domain_timeout(void*); |
66 | | void loopattach(void); |
67 | | void ether_family_init(void); |
68 | | void tcp_cc_init(void); |
69 | | void net_init_run(void); |
70 | | errno_t necp_init(void); |
71 | | void in_rtqtimo(void* targ); |
72 | | void* nstat_idle_check(void* p0, void* p1); |
73 | | |
74 | | extern int dlil_verbose; |
75 | | |
76 | | struct proc proc0; |
77 | | struct filedesc filedesc0; |
78 | | struct plimit plimit0; |
79 | | proc_t kernproc; |
80 | | int cmask = CMASK; |
81 | | |
82 | 1 | __attribute__((visibility("default"))) bool init_proc(void) { |
83 | 1 | kernproc = &proc0; |
84 | 1 | kernproc->p_fd = &filedesc0; |
85 | | // Permitting 10 open files should be more than enough |
86 | | // without blowing up execution time. If you change this |
87 | | // number you probably want to change the fd enum in the |
88 | | // protobuf file. |
89 | 1 | plimit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 10; |
90 | 1 | plimit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = 10; |
91 | 1 | kernproc->p_limit = &plimit0; |
92 | 1 | filedesc0.fd_cmask = cmask; |
93 | 1 | filedesc0.fd_knlistsize = -1; |
94 | 1 | filedesc0.fd_knlist = NULL; |
95 | 1 | filedesc0.fd_knhash = NULL; |
96 | 1 | filedesc0.fd_knhashmask = 0; |
97 | | // Increase sb_max |
98 | 1 | sb_max = 8192*1024 * 4; |
99 | 1 | dlil_verbose = 0; |
100 | 1 | return true; |
101 | 1 | } |
102 | | |
103 | | // TODO: expose these clears to the net protobuf |
104 | 408k | __attribute__((visibility("default"))) void clear_all() { |
105 | 408k | inpcb_timeout(NULL, NULL); |
106 | 408k | key_timehandler(); |
107 | 408k | frag_timeout(); |
108 | 408k | nd6_slowtimo(); |
109 | 408k | nd6_timeout(); |
110 | 408k | in6_tmpaddrtimer(); |
111 | 408k | mp_timeout(); |
112 | 408k | igmp_timeout(); |
113 | 408k | tcp_fuzzer_reset(); |
114 | 408k | frag6_timeout(); |
115 | 408k | mld_timeout(); |
116 | | |
117 | | // this adds work to the work queue |
118 | 408k | in6_rtqtimo(NULL); |
119 | 408k | in_rtqtimo(NULL); |
120 | | |
121 | | // TODO(nedwill): nd6_dad_timer |
122 | 408k | nstat_idle_check(NULL, NULL); |
123 | 408k | domain_timeout(NULL); |
124 | 408k | } |
125 | | |
126 | 936k | #define MT_DATA 1 |
127 | | |
128 | | __attribute__((visibility("default"))) struct mbuf* get_mbuf_data( |
129 | 936k | const char* data, size_t size, int pktflags) { |
130 | 936k | struct mbuf* mbuf_data = |
131 | 936k | mbuf_create((const uint8_t*)data, size, true, false, MT_DATA, pktflags); |
132 | | |
133 | | // TODO(nedwill): consider using a non-loopback interface |
134 | | // This indicates where the packet came from. |
135 | 936k | mbuf_pkthdr_setrcvif((mbuf_t)mbuf_data, lo_ifp); |
136 | 936k | return mbuf_data; |
137 | 936k | } |
138 | | |
139 | | extern unsigned long ioctls[]; |
140 | | extern int num_ioctls; |
141 | | |
142 | 1 | __attribute__((visibility("default"))) bool initialize_network() { |
143 | 1 | kernel_startup_bootstrap(); |
144 | 1 | kernel_startup_initialize_upto(STARTUP_SUB_EARLY_BOOT); |
145 | 1 | mcache_init(); |
146 | 1 | mbinit(); |
147 | 1 | eventhandler_init(); |
148 | 1 | dlil_init(); |
149 | 1 | socketinit(); |
150 | 1 | domaininit(); |
151 | 1 | loopattach(); |
152 | 1 | ether_family_init(); |
153 | 1 | tcp_cc_init(); |
154 | 1 | net_init_run(); |
155 | 1 | int res = necp_init(); |
156 | 1 | assert(!res); |
157 | 1 | return true; |
158 | 1 | } |
159 | | |
160 | 470k | __attribute__((visibility("default"))) void ip_input_wrapper(void* m) { |
161 | 470k | ip_input((mbuf_t)m); |
162 | 470k | } |
163 | | |
164 | 465k | __attribute__((visibility("default"))) void ip6_input_wrapper(void* m) { |
165 | 465k | ip6_input((mbuf_t)m); |
166 | 465k | } |