Coverage Report

Created: 2024-10-03 06:24

/src/SockFuzzer/fuzz/fakes/fake_impls.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
// Trivial implementations belong here. More substantial faked
30
// subsystems should live in their own file.
31
32
#include <stdbool.h>
33
#include <stdint.h>
34
#include <string.h>
35
36
#include <kern/assert.h>
37
#include <libkern/libkern.h>
38
39
#include "bsd/net/nwk_wq.h"
40
#include "bsd/sys/_types/_timeval.h"
41
#include "bsd/sys/conf.h"
42
#include "bsd/sys/kdebug_kernel.h"
43
#include "bsd/sys/kernel_types.h"
44
#include "bsd/sys/malloc.h"
45
#include "bsd/sys/resource.h"
46
#include "bsd/uuid/uuid.h"
47
48
extern void get_fuzzed_bytes(void* addr, size_t bytes);
49
extern bool get_fuzzed_bool(void);
50
51
int snprintf(char*, size_t, const char*, ...) __printflike(3, 4);
52
53
int maxfilesperproc = 10;
54
55
36
bool PE_parse_boot_argn(const char* arg_string, void* arg_ptr, int max_arg) {
56
36
  if (!strcmp(arg_string, "ifa_debug")) {
57
9
    *(int*)arg_ptr = 0;
58
9
    return true;
59
9
  }
60
61
27
  if (!strcmp(arg_string, "inaddr_nhash")) {
62
1
    *(uint32_t*)arg_ptr = 0;
63
1
    return true;
64
1
  }
65
66
26
  if (!strcmp(arg_string, "mcache_flags")) {
67
0
    *(uint32_t*)arg_ptr = 0;
68
0
    return true;
69
0
  }
70
71
26
  if (!strcmp(arg_string, "mbuf_debug")) {
72
1
    *(uint32_t*)arg_ptr = 0;
73
1
    return true;
74
1
  }
75
76
25
  if (!strcmp(arg_string, "mleak_sample_factor")) {
77
1
    *(uint32_t*)arg_ptr = 0;
78
1
    return true;
79
1
  }
80
81
  // Just return 0 by default.
82
24
  memset(arg_ptr, 0, max_arg);
83
84
  // assert(false);
85
24
  return false;
86
25
}
87
88
4
void* os_log_create() { return (void*)1; }
89
90
0
void pflog_packet() {}
91
92
// TODO(nedwill): return a real vfs context
93
1.48M
void* vfs_context_current() { return NULL; }
94
95
0
int csproc_get_platform_binary(void* p) { return 0; }
96
97
2.52M
void uuid_clear(uuid_t uu) { memset(uu, 0, sizeof(uuid_t)); }
98
99
2.57M
int uuid_is_null(const uuid_t uu) {
100
2.57M
  return !memcmp(uu, UUID_NULL, sizeof(uuid_t));
101
2.57M
}
102
103
0
int uuid_compare(const uuid_t uu1, const uuid_t uu2) {
104
0
  return memcmp(uu1, uu2, sizeof(uuid_t));
105
0
}
106
107
// TODO(nedwill): this shouldn't return the same value
108
// within the same fuzz session (use a counter and reset)
109
0
void uuid_generate_random(uuid_t out) {
110
0
  if (get_fuzzed_bool()) {
111
0
    memcpy(out, "0000000000000000", 16);
112
0
    return;
113
0
  }
114
0
  if (get_fuzzed_bool()) {
115
0
    memcpy(out, "1111111111111111", 16);
116
0
    return;
117
0
  }
118
0
  memcpy(out, "2222222222222222", 16);
119
0
}
120
121
1
void uuid_copy(uuid_t dst, const uuid_t src) {
122
1
  memcpy(dst, src, sizeof(uuid_t));
123
1
}
124
125
0
void uuid_unparse_upper(const uuid_t uu, uuid_string_t out) {
126
0
  snprintf(out, sizeof(uuid_string_t),
127
0
           "%c%c%c%c-"
128
0
           "%c%c-"
129
0
           "%c%c-"
130
0
           "%c%c-"
131
0
           "%c%c%c%c%c%c",
132
0
           uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9],
133
0
           uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
134
0
}
135
136
0
void uuid_unparse(const uuid_t uu, uuid_string_t out) {
137
0
  uuid_unparse_upper(uu, out);
138
0
}
139
140
extern void* kernproc;
141
142
1.58M
void* vfs_context_proc() { return kernproc; }
143
144
// TODO(Ned): better timekeeping here
145
1.23M
uint64_t mach_continuous_time(void) { return 0; }
146
147
// TODO: handle timer scheduling
148
0
void timeout() { assert(false); }
149
150
417k
void microtime(struct timeval* tvp) { memset(&tvp, 0, sizeof(tvp)); }
151
152
4.38M
void microuptime(struct timeval* tvp) { memset(&tvp, 0, sizeof(tvp)); }
153
154
0
int mac_socket_check_accepted() { return 0; }
155
156
6.51k
int mac_socket_check_setsockopt() { return 0; }
157
158
739
int mac_socket_check_bind() { return 0; }
159
160
697k
int mac_file_check_ioctl() { return 0; }
161
162
0
int deflateInit2_() { return 1; }
163
0
int inflateInit2_() { return 1; }
164
165
0
bool kauth_cred_issuser() { return true; }
166
167
205k
unsigned long RandomULong() {
168
  // returning 0 here would be a failure
169
205k
  return 1;
170
205k
}
171
172
// TODO: threading
173
6
int kernel_thread_start() { return 0; }
174
175
1
int cdevsw_add(int major, const struct cdevsw *cdevsw) {
176
1
  return 0;
177
1
}
178
179
2
void devfs_make_node() {}
180
181
1.51M
bool lck_mtx_try_lock() { return true; }
182
183
2
void kprintf() { return; }
184
185
3
void thread_deallocate() {}
186
187
// we are root
188
46.8k
int proc_suser() { return 0; }
189
190
107k
void _os_log_internal() {}
191
192
0
void hw_atomic_add() {}
193
194
0
void hw_atomic_sub() {}
195
196
1.61M
void lck_mtx_destroy() {}
197
198
186k
int mac_socket_check_ioctl() { return 0; }
199
200
727k
bool proc_is64bit() { return true; }
201
202
19
int priv_check_cred() { return 0; }
203
204
448k
bool lck_rw_try_lock_exclusive() { return true; }
205
206
void* malloc(size_t size);
207
void free(void* ptr);
208
209
// TODO(nedwill): fix this hack
210
__attribute__((visibility("default"))) bool real_copyout = true;
211
212
53.7k
int copyout(const void* kaddr, user_addr_t udaddr, size_t len) {
213
  // randomly fail
214
53.7k
  if (get_fuzzed_bool()) {
215
1.94k
    return 1;
216
1.94k
  }
217
218
51.7k
  if (!udaddr || udaddr == 1 || !real_copyout) {
219
2.92k
    void* buf = malloc(len);
220
2.92k
    memcpy(buf, kaddr, len);
221
2.92k
    free(buf);
222
2.92k
    return 0;
223
2.92k
  }
224
225
48.8k
  memcpy((void*)udaddr, kaddr, len);
226
48.8k
  return 0;
227
51.7k
}
228
229
1.07M
void* __MALLOC(size_t size, int type, int flags, vm_allocation_site_t* site) {
230
1.07M
  void* addr = NULL;
231
1.07M
  assert(type < M_LAST);
232
233
1.07M
  if (size == 0) {
234
0
    return NULL;
235
0
  }
236
237
1.07M
  addr = malloc(size);
238
1.07M
  if (!addr) {
239
0
    return NULL;
240
0
  }
241
242
1.07M
  if (flags & M_ZERO) {
243
774k
    bzero(addr, size);
244
774k
  }
245
246
1.07M
  return (addr);
247
1.07M
}
248
249
673k
void read_frandom(void* buffer, unsigned int numBytes) {
250
673k
  get_fuzzed_bytes(buffer, numBytes);
251
673k
}
252
253
0
void read_random(void* buffer, unsigned int numBytes) {
254
0
  get_fuzzed_bytes(buffer, numBytes);
255
0
}
256
257
0
int ml_get_max_cpus(void) { return 1; }
258
259
void clock_interval_to_deadline(uint32_t interval, uint32_t scale_factor,
260
2.96M
                                uint64_t* result) {
261
2.96M
  *result = 0;
262
2.96M
}
263
264
void clock_interval_to_absolutetime_interval(uint32_t interval,
265
                                             uint32_t scale_factor,
266
346k
                                             uint64_t* result) {
267
346k
  *result = 0;
268
346k
}
269
270
3
void* thread_call_allocate_with_options() { return (void*)1; }
271
272
346k
bool thread_call_enter_delayed_with_leeway() { return true; }
273
274
2.01M
void lck_rw_assert() {}
275
276
1
uint32_t IOMapperIOVMAlloc() { return 0; }
277
278
2.52M
int proc_uniqueid() { return 0; }
279
280
0
uint64_t mach_absolute_time() { return 0; }
281
282
2.59M
int proc_pid() { return 0; }
283
284
void proc_getexecutableuuid(void* p, unsigned char* uuidbuf,
285
2.52M
                            unsigned long size) {
286
2.52M
  memset(uuidbuf, 0, size);
287
2.52M
}
288
289
2.52M
void proc_pidoriginatoruuid(void* buffer, size_t size) {
290
2.52M
  memset(buffer, 0, size);
291
2.52M
}
292
293
2.52M
void* kauth_cred_proc_ref() { return (void*)1; }
294
295
3.37M
void* kauth_cred_get() { return (void*)1; }
296
297
0
void* proc_ucred() { return (void*)1; }
298
299
2.52M
int suser(void* arg1, void* arg2) {
300
2.52M
  (void)arg1;
301
2.52M
  (void)arg2;
302
2.52M
  return 0;
303
2.52M
}
304
305
4.69M
void lck_rw_lock_shared() {}
306
307
7.07M
void lck_rw_done() {}
308
309
2.43M
bool proc_get_effective_thread_policy() {
310
  // TODO: more options
311
2.43M
  return false;
312
2.43M
}
313
314
5.24M
void* current_proc() { return kernproc; }
315
316
74.9k
int proc_selfpid() { return 1; }
317
318
0
void tvtohz() {}
319
320
0
int kauth_cred_getuid() {
321
  // UUID: root
322
0
  return get_fuzzed_bool() ? 1 : 0;
323
0
}
324
325
0
char* proc_best_name() { return "kernproc"; }
326
327
6
void* proc_find() { return kernproc; }
328
329
74.9k
int mac_socket_check_create() { return 0; }
330
331
15.2k
int mac_socket_check_accept() { return 0; }
332
333
14.5k
void ovbcopy(const char* from, char* to, size_t nbytes) {
334
14.5k
  memmove(to, from, nbytes);
335
14.5k
}
336
337
int __attribute__((warn_unused_result))
338
1.30M
copyin(const user_addr_t uaddr, void *kaddr, size_t len) {
339
  // Address 1 means use fuzzed bytes, otherwise use real bytes.
340
  // NOTE: this does not support nested useraddr.
341
1.30M
  if (uaddr != 1) {
342
690k
    memcpy(kaddr, (void*)uaddr, len);
343
690k
    return 0;
344
690k
  }
345
346
613k
  if (get_fuzzed_bool()) {
347
29.0k
    return -1;
348
29.0k
  }
349
350
584k
  get_fuzzed_bytes(kaddr, len);
351
584k
  return 0;
352
613k
}
353
354
1
void SHA1Final() {}
355
356
1
void SHA1Init() {}
357
358
2
void SHA1Update() {}
359
360
2
void* thread_call_allocate_with_priority() { return (void*)1; }
361
362
9
void lck_grp_attr_free() {}
363
364
2
void lck_grp_free() {}
365
366
4.35M
void lck_rw_lock_exclusive() {}
367
368
1.52M
void timevaladd() {}
369
0
void timevalsub() {}
370
371
27.6k
void thread_call_enter_delayed() {}
372
373
0
void MD5Init() {}
374
0
void MD5Update() {}
375
0
void MD5Final(unsigned char* digest, void* ctx) {
376
0
  memset(digest, 0, 4);
377
0
}
378
379
6
void proc_rele() {}
380
3.18M
void wakeup(void* chan) {}
381
382
0
void lck_spin_lock() {}
383
0
void lck_spin_unlock() {}
384
2.43M
void kauth_cred_unref(void* cred) {}
385
2.43M
void lck_rw_unlock_exclusive() {}
386
387
554k
bool IS_64BIT_PROCESS() { return true; }
388
389
19.7k
int mac_socket_check_listen() { return 0; }
390
391
0
void kauth_cred_ref() {}
392
393
0
void in_stat_set_activity_bitmap() {}
394
395
6.16k
int mac_socket_check_getsockopt() { return 0; }
396
397
511k
int mac_pipe_check_ioctl() { return 0; }
398
399
0
int mac_pipe_check_write() { return 0; }
400
401
0
int mac_pipe_check_kqfilter() { return 0; }
402
403
4
int mac_pipe_label_init() { return 0; }
404
405
0
int mac_pipe_label_destroy() { return 0; }
406
407
0
int mac_pipe_check_read() { return 0; }
408
409
0
int mac_pipe_check_stat() { return 0; }
410
411
4
int mac_pipe_label_associate() { return 0; }
412
413
0
int kauth_getuid() { return 0; }
414
415
0
int kauth_getgid() { return 0; }
416
417
0
int mac_pipe_check_select() { return 0; }
418
419
0
void _aio_close() {}
420
0
void unlink1() {}
421
422
15.2k
int mac_socket_check_connect() { return 0; }
423
424
0
void ml_thread_policy() {}
425
426
1
void aes_encrypt_key128() {}
427
428
2
void OSBacktrace() {}
429
430
0
void lck_grp_attr_setdefault() {}
431
432
3.11k
void nanouptime() {}
433
434
3.11k
void wakeup_one() {}
435
436
551k
int lck_mtx_try_lock_spin() { return 1; }
437
438
408k
void absolutetime_to_nanoseconds(uint64_t in, uint64_t* out) { *out = 0; }
439
440
void free(void* ptr);
441
442
11.5k
void nwk_wq_enqueue(struct nwk_wq_entry* nwk_item) {
443
11.5k
  nwk_item->func(nwk_item->arg);
444
11.5k
  free(nwk_item);
445
11.5k
}
446
447
111k
int ppsratecheck() { return 1; }
448
449
0
bool ratecheck() { return true; }
450
451
0
void fulong() {}
452
0
void ubc_cs_blob_deallocate() {}
453
0
void proc_thread() {}
454
0
void munge_user32_stat64() {}
455
0
int mac_file_check_lock() { return 0; }
456
0
void vnode_setsize() {}
457
0
void vnode_setnocache() {}
458
0
void kauth_authorize_fileop() {}
459
0
void VNOP_FSYNC() {}
460
0
void tablefull() {}
461
0
void vnode_recycle() {}
462
0
void ipc_object_copyin() {}
463
0
int mac_file_check_inherit() { return 0; }
464
0
void vnode_vid() {}
465
0
void munge_user32_stat() {}
466
0
void VNOP_OFFTOBLK() {}
467
16
int mac_file_check_create() { return 0; }
468
0
void fileport_port_to_fileglob() {}
469
0
void VNOP_SETATTR() {}
470
0
void vfs_devblocksize() {}
471
0
int mac_file_check_library_validation() { return 0; }
472
0
void ubc_cs_blob_add() {}
473
0
void vn_getpath() {}
474
0
void ipc_port_release_send() {}
475
0
void proc_kqhashlock_grp() {}
476
0
void vn_path_package_check() {}
477
0
void VNOP_GETATTR() {}
478
0
void ubc_cs_blob_allocate() {}
479
0
void audit_sysclose() {}
480
0
void vnode_is_openevt() {}
481
0
void audit_arg_vnpath_withref() {}
482
0
int mac_file_check_fcntl() { return 0; }
483
0
void VNOP_ALLOCATE() {}
484
0
void fg_vn_data_free() {}
485
0
void VNOP_BLKTOOFF() {}
486
0
void vnode_islnk() {}
487
0
void VNOP_IOCTL() {}
488
0
int mac_vnode_check_truncate() { return 0; }
489
0
int mac_file_check_dup() { return 0; }
490
0
void ubc_cs_blob_get() {}
491
0
void audit_arg_vnpath() {}
492
0
void get_task_ipcspace() {}
493
0
void vn_rdwr() {}
494
6
int mac_file_label_destroy() { return 0; }
495
0
void fileport_alloc() {}
496
0
void vnode_getwithref() {}
497
16
int mac_file_label_associate() { return 0; }
498
0
void sulong() {}
499
0
void proc_lck_attr() {}
500
0
int mac_vnode_check_write() { return 0; }
501
0
void ipc_port_copyout_send() {}
502
0
void kauth_filesec_free() {}
503
0
void munge_user64_stat64() {}
504
0
void munge_user64_stat() {}
505
0
void VNOP_EXCHANGE() {}
506
0
void vnode_set_openevt() {}
507
0
void vn_stat_noauth() {}
508
0
void vnode_mount() {}
509
0
void open1() {}
510
0
void kauth_authorize_fileop_has_listeners() {}
511
161k
void fp_isguarded() {}
512
0
void audit_arg_fflags() {}
513
0
int mac_vnode_notify_truncate() { return 0; }
514
161k
void fp_guard_exception() {}
515
0
void vnode_clear_openevt() {}
516
0
void pshm_stat() {}
517
0
void proc_knhashlock_grp() {}
518
0
void VNOP_BLOCKMAP() {}
519
0
void vnode_clearnocache() {}
520
0
void VNOP_ADVLOCK() {}
521
0
void ubc_cs_blob_revalidate() {}
522
0
void guarded_fileproc_free() {}
523
0
void audit_arg_text() {}
524
0
void vnode_isnocache() {}
525
0
void mach_port_deallocate() {}
526
16
int mac_file_label_init() { return 0; }
527
0
void vn_pathconf() {}
528
0
void audit_arg_mode() {}
529
0
long boottime_sec() { return 0; }
530
0
void mac_socket_check_receive() {}
531
7.32k
void mac_socket_check_send() {}
532
533
0
void kernel_debug() {}
534
535
260
void lck_rw_unlock_shared() {}
536
0
kern_return_t kmem_alloc_contig() { assert(false); }
537
uint32_t ipc_control_port_options;
538
539
0
bool current_task_can_use_restricted_in_port() { return true; }
540
541
unsigned int
542
ml_wait_max_cpus(void)
543
1
{
544
1
  return 1;
545
1
}
546
547
int
548
fls(unsigned int mask)
549
7
{
550
7
  if (mask == 0) {
551
0
    return 0;
552
0
  }
553
554
7
  return (sizeof(mask) << 3) - __builtin_clz(mask);
555
7
}
556
557
2
int scnprintf(char *buf, size_t size, const char *fmt, ...) {
558
2
  return 0;
559
2
}
560
561
rlim_t
562
1.48M
proc_limitgetcur(proc_t p, int which, boolean_t to_lock_proc) {
563
1.48M
  if (which == RLIMIT_NOFILE) {
564
1.48M
    return 10;
565
1.48M
  }
566
0
  assert(false);
567
0
}
568
569
6
task_t proc_task() { return TASK_NULL; }
570
571
575k
vm_offset_t current_percpu_base(void) {
572
575k
  return 0;
573
575k
}
574
575
0
int proc_pidversion(proc_t p) {
576
0
  assert(false);
577
0
  return 0;
578
0
}
579
580
unsigned int kdebug_enable = 0;
581
6
void kernel_debug_string_early(const char *message) {
582
6
  printf("kernel_debug_string_early: %s\n", message);
583
6
}