forked from google/syzkaller
/
generated.go
8832 lines (7982 loc) · 254 KB
/
generated.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// AUTOGENERATED FILE FROM executor/*.h
package csource
var commonHeader = `
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#if GOOS_freebsd || GOOS_test && HOSTGOOS_freebsd
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if SYZ_TRACE
#include <errno.h>
#endif
#if SYZ_EXECUTOR && !GOOS_linux
#include <unistd.h>
NORETURN void doexit(int status)
{
_exit(status);
for (;;) {
}
}
#endif
#if SYZ_EXECUTOR || SYZ_MULTI_PROC || SYZ_REPEAT && SYZ_CGROUPS || \
SYZ_NET_DEVICES || __NR_syz_mount_image || __NR_syz_read_part_table || \
__NR_syz_usb_connect || __NR_syz_usb_connect_ath9k || \
(GOOS_freebsd || GOOS_openbsd || GOOS_netbsd) && SYZ_NET_INJECTION
static unsigned long long procid;
#endif
#if !GOOS_fuchsia && !GOOS_windows
#if SYZ_EXECUTOR || SYZ_HANDLE_SEGV
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#if GOOS_linux
#include <sys/syscall.h>
#endif
static __thread int skip_segv;
static __thread jmp_buf segv_env;
#if GOOS_akaros
#include <parlib/parlib.h>
static void recover(void)
{
_longjmp(segv_env, 1);
}
#endif
static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
uintptr_t addr = (uintptr_t)info->si_addr;
const uintptr_t prog_start = 1 << 20;
const uintptr_t prog_end = 100 << 20;
if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) && (addr < prog_start || addr > prog_end)) {
debug("SIGSEGV on %p, skipping\n", (void*)addr);
#if GOOS_akaros
struct user_context* uctx = (struct user_context*)ctx;
uctx->tf.hw_tf.tf_rip = (long)(void*)recover;
return;
#else
_longjmp(segv_env, 1);
#endif
}
debug("SIGSEGV on %p, exiting\n", (void*)addr);
doexit(sig);
}
static void install_segv_handler(void)
{
struct sigaction sa;
#if GOOS_linux
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
#endif
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = segv_handler;
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#define NONFAILING(...) \
{ \
__atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
if (_setjmp(segv_env) == 0) { \
__VA_ARGS__; \
} \
__atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
}
#endif
#endif
#if !GOOS_linux
#if (SYZ_EXECUTOR || SYZ_REPEAT) && SYZ_EXECUTOR_USES_FORK_SERVER
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
static void kill_and_wait(int pid, int* status)
{
kill(pid, SIGKILL);
while (waitpid(-1, status, 0) != pid) {
}
}
#endif
#endif
#if !GOOS_windows
#if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER || \
__NR_syz_usb_connect || __NR_syz_usb_connect_ath9k
static void sleep_ms(uint64 ms)
{
usleep(ms * 1000);
}
#endif
#if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER || \
SYZ_LEAK
#include <time.h>
static uint64 current_time_ms(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
fail("clock_gettime failed");
return (uint64)ts.tv_sec * 1000 + (uint64)ts.tv_nsec / 1000000;
}
#endif
#if SYZ_EXECUTOR || SYZ_SANDBOX_ANDROID || SYZ_USE_TMP_DIR
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
static void use_temporary_dir(void)
{
#if SYZ_SANDBOX_ANDROID
char tmpdir_template[] = "/data/data/syzkaller/syzkaller.XXXXXX";
#elif GOOS_fuchsia
char tmpdir_template[] = "/tmp/syzkaller.XXXXXX";
#else
char tmpdir_template[] = "./syzkaller.XXXXXX";
#endif
char* tmpdir = mkdtemp(tmpdir_template);
if (!tmpdir)
fail("failed to mkdtemp");
if (chmod(tmpdir, 0777))
fail("failed to chmod");
if (chdir(tmpdir))
fail("failed to chdir");
}
#endif
#endif
#if GOOS_akaros || GOOS_netbsd || GOOS_freebsd || GOOS_openbsd || GOOS_test
#if (SYZ_EXECUTOR || SYZ_REPEAT) && SYZ_EXECUTOR_USES_FORK_SERVER && (SYZ_EXECUTOR || SYZ_USE_TMP_DIR)
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
static void remove_dir(const char* dir)
{
DIR* dp;
struct dirent* ep;
dp = opendir(dir);
if (dp == NULL)
exitf("opendir(%s) failed", dir);
while ((ep = readdir(dp))) {
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
continue;
char filename[FILENAME_MAX];
snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
struct stat st;
if (lstat(filename, &st))
exitf("lstat(%s) failed", filename);
if (S_ISDIR(st.st_mode)) {
remove_dir(filename);
continue;
}
if (unlink(filename))
exitf("unlink(%s) failed", filename);
}
closedir(dp);
if (rmdir(dir))
exitf("rmdir(%s) failed", dir);
}
#endif
#endif
#if !GOOS_linux
#if SYZ_EXECUTOR
static int inject_fault(int nth)
{
return 0;
}
#endif
#if SYZ_EXECUTOR
static int fault_injected(int fail_fd)
{
return 0;
}
#endif
#endif
#if !GOOS_windows
#if SYZ_EXECUTOR || SYZ_THREADED
#include <errno.h>
#include <pthread.h>
static void thread_start(void* (*fn)(void*), void* arg)
{
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 128 << 10);
int i;
for (i = 0; i < 100; i++) {
if (pthread_create(&th, &attr, fn, arg) == 0) {
pthread_attr_destroy(&attr);
return;
}
if (errno == EAGAIN) {
usleep(50);
continue;
}
break;
}
exitf("pthread_create failed");
}
#endif
#endif
#if GOOS_freebsd || GOOS_netbsd || GOOS_openbsd || GOOS_akaros || GOOS_test
#if SYZ_EXECUTOR || SYZ_THREADED
#include <pthread.h>
#include <time.h>
typedef struct {
pthread_mutex_t mu;
pthread_cond_t cv;
int state;
} event_t;
static void event_init(event_t* ev)
{
if (pthread_mutex_init(&ev->mu, 0))
exitf("pthread_mutex_init failed");
if (pthread_cond_init(&ev->cv, 0))
exitf("pthread_cond_init failed");
ev->state = 0;
}
static void event_reset(event_t* ev)
{
ev->state = 0;
}
static void event_set(event_t* ev)
{
pthread_mutex_lock(&ev->mu);
if (ev->state)
fail("event already set");
ev->state = 1;
pthread_mutex_unlock(&ev->mu);
pthread_cond_broadcast(&ev->cv);
}
static void event_wait(event_t* ev)
{
pthread_mutex_lock(&ev->mu);
while (!ev->state)
pthread_cond_wait(&ev->cv, &ev->mu);
pthread_mutex_unlock(&ev->mu);
}
static int event_isset(event_t* ev)
{
pthread_mutex_lock(&ev->mu);
int res = ev->state;
pthread_mutex_unlock(&ev->mu);
return res;
}
static int event_timedwait(event_t* ev, uint64 timeout)
{
uint64 start = current_time_ms();
uint64 now = start;
pthread_mutex_lock(&ev->mu);
for (;;) {
if (ev->state)
break;
uint64 remain = timeout - (now - start);
struct timespec ts;
ts.tv_sec = remain / 1000;
ts.tv_nsec = (remain % 1000) * 1000 * 1000;
pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
now = current_time_ms();
if (now - start > timeout)
break;
}
int res = ev->state;
pthread_mutex_unlock(&ev->mu);
return res;
}
#endif
#endif
#if SYZ_EXECUTOR || SYZ_USE_BITMASKS
#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \
*(type*)(addr) = htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \
(((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))
#endif
#if SYZ_EXECUTOR || SYZ_USE_CHECKSUMS
struct csum_inet {
uint32 acc;
};
static void csum_inet_init(struct csum_inet* csum)
{
csum->acc = 0;
}
static void csum_inet_update(struct csum_inet* csum, const uint8* data, size_t length)
{
if (length == 0)
return;
size_t i;
for (i = 0; i < length - 1; i += 2)
csum->acc += *(uint16*)&data[i];
if (length & 1)
csum->acc += (uint16)data[length - 1];
while (csum->acc > 0xffff)
csum->acc = (csum->acc & 0xffff) + (csum->acc >> 16);
}
static uint16 csum_inet_digest(struct csum_inet* csum)
{
return ~csum->acc;
}
#endif
#if GOOS_akaros
#include <ros/syscall.h>
#include <stdlib.h>
#include <unistd.h>
#if SYZ_EXECUTOR || SYZ_SANDBOX_NONE
static void loop();
static int do_sandbox_none(void)
{
loop();
return 0;
}
#endif
#if SYZ_EXECUTOR || SYZ_REPEAT
static void execute_one();
const char* program_name;
void child()
{
#if SYZ_EXECUTOR || SYZ_HANDLE_SEGV
install_segv_handler();
#endif
#if SYZ_EXECUTOR
receive_execute();
close(kInPipeFd);
#endif
execute_one();
doexit(0);
}
#endif
#elif GOOS_freebsd || GOOS_netbsd || GOOS_openbsd
#include <unistd.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <sys/syscall.h>
#if GOOS_netbsd
#if SYZ_EXECUTOR || __NR_syz_usb_connect
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include <dev/usb/vhci.h>
#include <fcntl.h>
#include <sys/ioctl.h>
/* -------------------------------------------------------------------------- */
/*
* Redefinitions to match the linux types used in common_usb.h.
*/
struct usb_endpoint_descriptor {
uint8 bLength;
uint8 bDescriptorType;
uint8 bEndpointAddress;
uint8 bmAttributes;
uint16 wMaxPacketSize;
uint8 bInterval;
uint8 bRefresh;
uint8 bSynchAddress;
} __attribute__((packed));
struct usb_device_descriptor {
uint8 bLength;
uint8 bDescriptorType;
uint16 bcdUSB;
uint8 bDeviceClass;
uint8 bDeviceSubClass;
uint8 bDeviceProtocol;
uint8 bMaxPacketSize0;
uint16 idVendor;
uint16 idProduct;
uint16 bcdDevice;
uint8 iManufacturer;
uint8 iProduct;
uint8 iSerialNumber;
uint8 bNumConfigurations;
} __attribute__((packed));
struct usb_config_descriptor {
uint8 bLength;
uint8 bDescriptorType;
uint16 wTotalLength;
uint8 bNumInterfaces;
uint8 bConfigurationValue;
uint8 iConfiguration;
uint8 bmAttributes;
uint8 bMaxPower;
} __attribute__((packed));
struct usb_interface_descriptor {
uint8 bLength;
uint8 bDescriptorType;
uint8 bInterfaceNumber;
uint8 bAlternateSetting;
uint8 bNumEndpoints;
uint8 bInterfaceClass;
uint8 bInterfaceSubClass;
uint8 bInterfaceProtocol;
uint8 iInterface;
} __attribute__((packed));
struct usb_ctrlrequest {
uint8 bRequestType;
uint8 bRequest;
uint16 wValue;
uint16 wIndex;
uint16 wLength;
} __attribute__((packed));
struct usb_qualifier_descriptor {
uint8 bLength;
uint8 bDescriptorType;
uint16 bcdUSB;
uint8 bDeviceClass;
uint8 bDeviceSubClass;
uint8 bDeviceProtocol;
uint8 bMaxPacketSize0;
uint8 bNumConfigurations;
uint8 bRESERVED;
} __attribute__((packed));
#define USB_TYPE_MASK (0x03 << 5)
#define USB_TYPE_STANDARD (0x00 << 5)
#define USB_TYPE_CLASS (0x01 << 5)
#define USB_TYPE_VENDOR (0x02 << 5)
#define USB_TYPE_RESERVED (0x03 << 5)
#define USB_DT_DEVICE 0x01
#define USB_DT_CONFIG 0x02
#define USB_DT_STRING 0x03
#define USB_DT_INTERFACE 0x04
#define USB_DT_ENDPOINT 0x05
#define USB_DT_DEVICE_QUALIFIER 0x06
#define USB_DT_OTHER_SPEED_CONFIG 0x07
#define USB_DT_INTERFACE_POWER 0x08
#define USB_DT_OTG 0x09
#define USB_DT_DEBUG 0x0a
#define USB_DT_INTERFACE_ASSOCIATION 0x0b
#define USB_DT_SECURITY 0x0c
#define USB_DT_KEY 0x0d
#define USB_DT_ENCRYPTION_TYPE 0x0e
#define USB_DT_BOS 0x0f
#define USB_DT_DEVICE_CAPABILITY 0x10
#define USB_DT_WIRELESS_ENDPOINT_COMP 0x11
#define USB_DT_WIRE_ADAPTER 0x21
#define USB_DT_RPIPE 0x22
#define USB_DT_CS_RADIO_CONTROL 0x23
#define USB_DT_PIPE_USAGE 0x24
#define USB_DT_SS_ENDPOINT_COMP 0x30
#define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31
#define USB_REQ_GET_STATUS 0x00
#define USB_REQ_CLEAR_FEATURE 0x01
#define USB_REQ_SET_FEATURE 0x03
#define USB_REQ_SET_ADDRESS 0x05
#define USB_REQ_GET_DESCRIPTOR 0x06
#define USB_REQ_SET_DESCRIPTOR 0x07
#define USB_REQ_GET_CONFIGURATION 0x08
#define USB_REQ_SET_CONFIGURATION 0x09
#define USB_REQ_GET_INTERFACE 0x0A
#define USB_REQ_SET_INTERFACE 0x0B
#define USB_REQ_SYNCH_FRAME 0x0C
#define USB_REQ_SET_SEL 0x30
#define USB_REQ_SET_ISOCH_DELAY 0x31
#define USB_REQ_SET_ENCRYPTION 0x0D
#define USB_REQ_GET_ENCRYPTION 0x0E
#define USB_REQ_RPIPE_ABORT 0x0E
#define USB_REQ_SET_HANDSHAKE 0x0F
#define USB_REQ_RPIPE_RESET 0x0F
#define USB_REQ_GET_HANDSHAKE 0x10
#define USB_REQ_SET_CONNECTION 0x11
#define USB_REQ_SET_SECURITY_DATA 0x12
#define USB_REQ_GET_SECURITY_DATA 0x13
#define USB_REQ_SET_WUSB_DATA 0x14
#define USB_REQ_LOOPBACK_DATA_WRITE 0x15
#define USB_REQ_LOOPBACK_DATA_READ 0x16
#define USB_REQ_SET_INTERFACE_DS 0x17
#define USB_REQ_GET_PARTNER_PDO 20
#define USB_REQ_GET_BATTERY_STATUS 21
#define USB_REQ_SET_PDO 22
#define USB_REQ_GET_VDM 23
#define USB_REQ_SEND_VDM 24
#define USB_MAX_IFACE_NUM 4
#define USB_MAX_EP_NUM 32
#define USB_MAX_FDS 6
struct usb_endpoint_index {
struct usb_endpoint_descriptor desc;
int handle;
};
struct usb_iface_index {
struct usb_interface_descriptor* iface;
uint8 bInterfaceNumber;
uint8 bAlternateSetting;
uint8 bInterfaceClass;
struct usb_endpoint_index eps[USB_MAX_EP_NUM];
int eps_num;
};
struct usb_device_index {
struct usb_device_descriptor* dev;
struct usb_config_descriptor* config;
uint8 bDeviceClass;
uint8 bMaxPower;
int config_length;
struct usb_iface_index ifaces[USB_MAX_IFACE_NUM];
int ifaces_num;
int iface_cur;
};
struct usb_info {
int fd;
struct usb_device_index index;
};
static struct usb_info usb_devices[USB_MAX_FDS];
static int usb_devices_num;
static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index)
{
if (length < sizeof(*index->dev) + sizeof(*index->config))
return false;
memset(index, 0, sizeof(*index));
index->dev = (struct usb_device_descriptor*)buffer;
index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev));
index->bDeviceClass = index->dev->bDeviceClass;
index->bMaxPower = index->config->bMaxPower;
index->config_length = length - sizeof(*index->dev);
index->iface_cur = -1;
size_t offset = 0;
while (true) {
if (offset + 1 >= length)
break;
uint8 desc_length = buffer[offset];
uint8 desc_type = buffer[offset + 1];
if (desc_length <= 2)
break;
if (offset + desc_length > length)
break;
if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) {
struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset);
debug("parse_usb_descriptor: found interface #%u (%d, %d) at %p\n",
index->ifaces_num, iface->bInterfaceNumber, iface->bAlternateSetting, iface);
index->ifaces[index->ifaces_num].iface = iface;
index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber;
index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting;
index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass;
index->ifaces_num++;
}
if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) {
struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1];
debug("parse_usb_descriptor: found endpoint #%u at %p\n", iface->eps_num, buffer + offset);
if (iface->eps_num < USB_MAX_EP_NUM) {
memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc));
iface->eps_num++;
}
}
offset += desc_length;
}
return true;
}
static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len)
{
int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED);
if (i >= USB_MAX_FDS)
return NULL;
int rv = 0;
NONFAILING(rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index));
if (!rv)
return NULL;
__atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE);
return &usb_devices[i].index;
}
static struct usb_device_index* lookup_usb_index(int fd)
{
int i;
for (i = 0; i < USB_MAX_FDS; i++) {
if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) {
return &usb_devices[i].index;
}
}
return NULL;
}
#if USB_DEBUG
#include <linux/hid.h>
#include <linux/usb/audio.h>
#include <linux/usb/cdc.h>
#include <linux/usb/ch11.h>
#include <linux/usb/ch9.h>
#define USBLP_REQ_GET_ID 0x00
#define USBLP_REQ_GET_STATUS 0x01
#define USBLP_REQ_RESET 0x02
const char* usb_class_to_string(unsigned value)
{
switch (value) {
case USB_CLASS_PER_INTERFACE:
return "USB_CLASS_PER_INTERFACE";
case USB_CLASS_AUDIO:
return "USB_CLASS_AUDIO";
case USB_CLASS_COMM:
return "USB_CLASS_COMM";
case USB_CLASS_HID:
return "USB_CLASS_HID";
case USB_CLASS_PHYSICAL:
return "USB_CLASS_PHYSICAL";
case USB_CLASS_STILL_IMAGE:
return "USB_CLASS_STILL_IMAGE";
case USB_CLASS_PRINTER:
return "USB_CLASS_PRINTER";
case USB_CLASS_MASS_STORAGE:
return "USB_CLASS_MASS_STORAGE";
case USB_CLASS_HUB:
return "USB_CLASS_HUB";
case USB_CLASS_CDC_DATA:
return "USB_CLASS_CDC_DATA";
case USB_CLASS_CSCID:
return "USB_CLASS_CSCID";
case USB_CLASS_CONTENT_SEC:
return "USB_CLASS_CONTENT_SEC";
case USB_CLASS_VIDEO:
return "USB_CLASS_VIDEO";
case USB_CLASS_WIRELESS_CONTROLLER:
return "USB_CLASS_WIRELESS_CONTROLLER";
case USB_CLASS_MISC:
return "USB_CLASS_MISC";
case USB_CLASS_APP_SPEC:
return "USB_CLASS_APP_SPEC";
case USB_CLASS_VENDOR_SPEC:
return "USB_CLASS_VENDOR_SPEC";
}
return "unknown";
}
static void analyze_usb_device(struct usb_device_index* index)
{
debug("analyze_usb_device: idVendor = %04x\n", (unsigned)index->dev->idVendor);
debug("analyze_usb_device: idProduct = %04x\n", (unsigned)index->dev->idProduct);
debug("analyze_usb_device: bDeviceClass = %x (%s)\n", (unsigned)index->dev->bDeviceClass,
usb_class_to_string(index->dev->bDeviceClass));
debug("analyze_usb_device: bDeviceSubClass = %x\n", (unsigned)index->dev->bDeviceSubClass);
debug("analyze_usb_device: bDeviceProtocol = %x\n", (unsigned)index->dev->bDeviceProtocol);
for (int i = 0; i < index->ifaces_num; i++) {
struct usb_interface_descriptor* iface = index->ifaces[i].iface;
debug("analyze_usb_device: interface #%d:\n", i);
debug("analyze_usb_device: bInterfaceClass = %x (%s)\n", (unsigned)iface->bInterfaceClass,
usb_class_to_string(iface->bInterfaceClass));
debug("analyze_usb_device: bInterfaceSubClass = %x\n", (unsigned)iface->bInterfaceSubClass);
debug("analyze_usb_device: bInterfaceProtocol = %x\n", (unsigned)iface->bInterfaceProtocol);
}
}
static bool analyze_control_request_standard(struct usb_device_index* index, struct usb_ctrlrequest* ctrl)
{
uint8 bDeviceClass = index->bDeviceClass;
uint8 bInterfaceClass = index->ifaces[index->iface_cur].bInterfaceClass;
if (bDeviceClass == USB_CLASS_HID || bInterfaceClass == USB_CLASS_HID) {
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
debug("analyze_control_request: req = USB_REQ_GET_DESCRIPTOR\n");
switch (ctrl->wValue >> 8) {
case HID_DT_HID:
debug("analyze_control_request: desc = HID_DT_HID\n");
return true;
case HID_DT_REPORT:
debug("analyze_control_request: desc = HID_DT_REPORT\n");
return true;
case HID_DT_PHYSICAL:
debug("analyze_control_request: desc = HID_DT_PHYSICAL\n");
return false;
}
}
}
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
debug("analyze_control_request: req = USB_REQ_GET_DESCRIPTOR\n");
switch (ctrl->wValue >> 8) {
case USB_DT_DEVICE:
debug("analyze_control_request: desc = USB_DT_DEVICE\n");
return true;
case USB_DT_CONFIG:
debug("analyze_control_request: desc = USB_DT_CONFIG, index = %d\n", (int)(ctrl->wValue & 0xff));
return true;
case USB_DT_STRING:
debug("analyze_control_request: desc = USB_DT_STRING\n");
return true;
case USB_DT_INTERFACE:
debug("analyze_control_request: desc = USB_DT_INTERFACE\n");
break;
case USB_DT_ENDPOINT:
debug("analyze_control_request: desc = USB_DT_ENDPOINT\n");
break;
case USB_DT_DEVICE_QUALIFIER:
debug("analyze_control_request: desc = USB_DT_DEVICE_QUALIFIER\n");
return true;
case USB_DT_OTHER_SPEED_CONFIG:
debug("analyze_control_request: desc = USB_DT_OTHER_SPEED_CONFIG\n");
break;
case USB_DT_INTERFACE_POWER:
debug("analyze_control_request: desc = USB_DT_INTERFACE_POWER\n");
break;
case USB_DT_OTG:
debug("analyze_control_request: desc = USB_DT_OTG\n");
break;
case USB_DT_DEBUG:
debug("analyze_control_request: desc = USB_DT_DEBUG\n");
break;
case USB_DT_INTERFACE_ASSOCIATION:
debug("analyze_control_request: desc = USB_DT_INTERFACE_ASSOCIATION\n");
break;
case USB_DT_SECURITY:
debug("analyze_control_request: desc = USB_DT_SECURITY\n");
break;
case USB_DT_KEY:
debug("analyze_control_request: desc = USB_DT_KEY\n");
break;
case USB_DT_ENCRYPTION_TYPE:
debug("analyze_control_request: desc = USB_DT_ENCRYPTION_TYPE\n");
break;
case USB_DT_BOS:
debug("analyze_control_request: desc = USB_DT_BOS\n");
return true;
case USB_DT_DEVICE_CAPABILITY:
debug("analyze_control_request: desc = USB_DT_DEVICE_CAPABILITY\n");
break;
case USB_DT_WIRELESS_ENDPOINT_COMP:
debug("analyze_control_request: desc = USB_DT_WIRELESS_ENDPOINT_COMP\n");
break;
case USB_DT_WIRE_ADAPTER:
debug("analyze_control_request: desc = USB_DT_WIRE_ADAPTER\n");
break;
case USB_DT_RPIPE:
debug("analyze_control_request: desc = USB_DT_RPIPE\n");
break;
case USB_DT_CS_RADIO_CONTROL:
debug("analyze_control_request: desc = USB_DT_CS_RADIO_CONTROL\n");
break;
case USB_DT_PIPE_USAGE:
debug("analyze_control_request: desc = USB_DT_PIPE_USAGE\n");
break;
case USB_DT_SS_ENDPOINT_COMP:
debug("analyze_control_request: desc = USB_DT_SS_ENDPOINT_COMP\n");
break;
case USB_DT_SSP_ISOC_ENDPOINT_COMP:
debug("analyze_control_request: desc = USB_DT_SSP_ISOC_ENDPOINT_COMP\n");
break;
default:
debug("analyze_control_request: desc = unknown = 0x%x\n", (int)(ctrl->wValue >> 8));
break;
}
break;
case USB_REQ_GET_STATUS:
debug("analyze_control_request: req = USB_REQ_GET_STATUS\n");
break;
case USB_REQ_CLEAR_FEATURE:
debug("analyze_control_request: req = USB_REQ_CLEAR_FEATURE\n");
break;
case USB_REQ_SET_FEATURE:
debug("analyze_control_request: req = USB_REQ_SET_FEATURE\n");
break;
case USB_REQ_GET_CONFIGURATION:
debug("analyze_control_request: req = USB_REQ_GET_CONFIGURATION\n");
return true;
case USB_REQ_SET_CONFIGURATION:
debug("analyze_control_request: req = USB_REQ_SET_CONFIGURATION\n");
break;
case USB_REQ_GET_INTERFACE:
debug("analyze_control_request: req = USB_REQ_GET_INTERFACE\n");
return true;
case USB_REQ_SET_INTERFACE:
debug("analyze_control_request: req = USB_REQ_SET_INTERFACE\n");
break;
default:
debug("analyze_control_request: req = unknown = 0x%x\n", (int)ctrl->bRequest);
break;
}
return false;
}
static bool analyze_control_request_class(struct usb_device_index* index, struct usb_ctrlrequest* ctrl)
{
uint8 bDeviceClass = index->bDeviceClass;
uint8 bInterfaceClass = index->ifaces[index->iface_cur].bInterfaceClass;
if (bDeviceClass == USB_CLASS_HID || bInterfaceClass == USB_CLASS_HID) {
switch (ctrl->bRequest) {
case HID_REQ_GET_REPORT:
debug("analyze_control_request: req = HID_REQ_GET_REPORT\n");
return true;
case HID_REQ_GET_IDLE:
debug("analyze_control_request: req = HID_REQ_GET_IDLE\n");
break;
case HID_REQ_GET_PROTOCOL:
debug("analyze_control_request: req = HID_REQ_GET_PROTOCOL\n");
return true;
case HID_REQ_SET_REPORT:
debug("analyze_control_request: req = HID_REQ_SET_REPORT\n");
break;
case HID_REQ_SET_IDLE:
debug("analyze_control_request: req = HID_REQ_SET_IDLE\n");
break;
case HID_REQ_SET_PROTOCOL:
debug("analyze_control_request: req = HID_REQ_SET_PROTOCOL\n");
break;
}
}
if (bDeviceClass == USB_CLASS_AUDIO || bInterfaceClass == USB_CLASS_AUDIO) {
switch (ctrl->bRequest) {
case UAC_SET_CUR:
debug("analyze_control_request: req = UAC_SET_CUR\n");
break;
case UAC_GET_CUR:
debug("analyze_control_request: req = UAC_GET_CUR\n");
return true;
case UAC_SET_MIN:
debug("analyze_control_request: req = UAC_SET_MIN\n");
break;
case UAC_GET_MIN:
debug("analyze_control_request: req = UAC_GET_MIN\n");
return true;
case UAC_SET_MAX:
debug("analyze_control_request: req = UAC_SET_MAX\n");
break;
case UAC_GET_MAX:
debug("analyze_control_request: req = UAC_GET_MAX\n");
return true;
case UAC_SET_RES:
debug("analyze_control_request: req = UAC_SET_RES\n");
break;
case UAC_GET_RES:
debug("analyze_control_request: req = UAC_GET_RES\n");
return true;
case UAC_SET_MEM:
debug("analyze_control_request: req = UAC_SET_MEM\n");
break;
case UAC_GET_MEM:
debug("analyze_control_request: req = UAC_GET_MEM\n");
return true;
}
}
if (bDeviceClass == USB_CLASS_PRINTER || bInterfaceClass == USB_CLASS_PRINTER) {
switch (ctrl->bRequest) {
case USBLP_REQ_GET_ID:
debug("analyze_control_request: req = USBLP_REQ_GET_ID\n");
return true;
case USBLP_REQ_GET_STATUS:
debug("analyze_control_request: req = USBLP_REQ_GET_STATUS\n");
return true;
case USBLP_REQ_RESET:
debug("analyze_control_request: req = USBLP_REQ_RESET\n");
break;
}
}
if (bDeviceClass == USB_CLASS_HUB || bInterfaceClass == USB_CLASS_HUB) {
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (ctrl->wValue >> 8) {
case USB_DT_HUB:
debug("analyze_control_request: desc = USB_DT_HUB\n");
return true;
case USB_DT_SS_HUB:
debug("analyze_control_request: desc = USB_DT_SS_HUB\n");
return true;
}
case USB_REQ_GET_STATUS:
debug("analyze_control_request: req = USB_REQ_GET_STATUS\n");
return true;
case HUB_SET_DEPTH:
debug("analyze_control_request: req = HUB_SET_DEPTH\n");
break;
}
}
if (bInterfaceClass == USB_CLASS_COMM) {
switch (ctrl->bRequest) {
case USB_CDC_SEND_ENCAPSULATED_COMMAND:
debug("analyze_control_request: req = USB_CDC_SEND_ENCAPSULATED_COMMAND\n");
break;
case USB_CDC_GET_ENCAPSULATED_RESPONSE:
debug("analyze_control_request: req = USB_CDC_GET_ENCAPSULATED_RESPONSE\n");
break;
case USB_CDC_REQ_SET_LINE_CODING:
debug("analyze_control_request: req = USB_CDC_REQ_SET_LINE_CODING\n");
break;
case USB_CDC_REQ_GET_LINE_CODING:
debug("analyze_control_request: req = USB_CDC_REQ_GET_LINE_CODING\n");
break;
case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
debug("analyze_control_request: req = USB_CDC_REQ_SET_CONTROL_LINE_STATE\n");
break;
case USB_CDC_REQ_SEND_BREAK:
debug("analyze_control_request: req = USB_CDC_REQ_SEND_BREAK\n");
break;
case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
debug("analyze_control_request: req = USB_CDC_SET_ETHERNET_MULTICAST_FILTERS\n");
break;
case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
debug("analyze_control_request: req = USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER\n");
break;
case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
debug("analyze_control_request: req = USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER\n");
break;
case USB_CDC_SET_ETHERNET_PACKET_FILTER: