// https://syzkaller.appspot.com/bug?id=9fbcadab8fc17a120a08beac176c0ace5c04ef56 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Copyright 2026 syzkaller project authors. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // IMPORTANT: Do not copy the macros or definitions below directly into your reproducer. // Instead, add the following line to your reproducer: // #include "race_toolkit.h" // --- Race Condition Toolkit --- // Macros and snippets for CPU pinning, memory barriers, and userfaultfd. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Unbuffered I/O: Ensure logs are written immediately. #define SETUP_UNBUFFERED_IO() setvbuf(stdout, NULL, _IONBF, 0) // CPU Pinning: Pin the current thread to a specific CPU core. #define PIN_TO_CPU(cpu) \ do { \ cpu_set_t mask; \ CPU_ZERO(&mask); \ CPU_SET(cpu, &mask); \ if (sched_setaffinity(0, sizeof(mask), &mask) == -1) { \ perror("sched_setaffinity"); \ } \ } while (0) // Memory Barrier: Ensure memory ordering. #define MB() __atomic_thread_fence(__ATOMIC_SEQ_CST) // Spin-wait Barrier: Wait until a memory location has a specific value. // Best for tight race windows (low latency, no context switches). #define WAIT_ON(addr, val) \ do { \ while (__atomic_load_n(addr, __ATOMIC_ACQUIRE) != (val)) \ ; \ } while (0) // Signal: Set a memory location to a specific value to release a WAIT_ON. #define SIGNAL(addr, val) __atomic_store_n(addr, val, __ATOMIC_RELEASE) // --- Timing Primitives --- // Robust timing loops in VM environments (using CLOCK_MONOTONIC to avoid time(NULL) jumps). static inline double timer_elapsed_sec(struct timespec* start) { struct timespec now; if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) { perror("clock_gettime(CLOCK_MONOTONIC) elapsed"); exit(1); } return (double)(now.tv_sec - start->tv_sec) + (double)(now.tv_nsec - start->tv_nsec) / 1e9; } // Initialize a monotonic timer variable. #define TIMER_START(t) \ struct timespec t; \ if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) { \ perror("clock_gettime(CLOCK_MONOTONIC) start"); \ exit(1); \ } // Check if the elapsed time since 't' is less than 'sec' seconds. #define TIMER_NOT_EXPIRED(t, sec) (timer_elapsed_sec(&(t)) < (double)(sec)) // Futex-based Event: Shared with syzkaller executor. // Best for general synchronization or longer waits to save CPU. typedef struct { int state; } event_t; static void event_init(event_t* ev) { ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) { fprintf(stderr, "event already set\n"); exit(1); } __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE); syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000); } static void event_wait(event_t* ev) { while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0); } // userfaultfd setup: Register a memory range for page fault handling. static int setup_uffd(void* addr, size_t len) { int uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (uffd == -1) return -1; struct uffdio_api api = {.api = UFFD_API, .features = 0}; if (ioctl(uffd, UFFDIO_API, &api) == -1) { close(uffd); return -1; } struct uffdio_register reg = { .range = {.start = (uintptr_t)addr, .len = len}, .mode = UFFDIO_REGISTER_MODE_MISSING}; if (ioctl(uffd, UFFDIO_REGISTER, ®) == -1) { close(uffd); return -1; } return uffd; } // --- Guidance on Usage --- // 1. Use WAIT_ON/SIGNAL for tight race conditions to avoid scheduling overhead. // 2. Use event_t (futexes) for general coordination or when waiting for longer periods. // 3. Always use PIN_TO_CPU to increase race probability on multi-core systems. // 4. Use setup_uffd to register a memory range for page fault handling. This allows you to // pause a thread accessing that memory until you handle the fault, creating a reliable // and controllable race window. // 5. Call SETUP_UNBUFFERED_IO() at the start of main() to ensure that logs are printed // immediately. This is essential for understanding the exact interleaving of events // when debugging race conditions. // 6. For timing-based loops (e.g., running a race for 10 seconds), do NOT use time(NULL) // or loops relying on real-time clocks, as VM clocks are highly unreliable and can fail or drift. // Instead, use the robust monotonic timing primitives TIMER_START and TIMER_NOT_EXPIRED: // TIMER_START(start); // while (TIMER_NOT_EXPIRED(start, 10.0)) { // // Your race logic here // } #ifndef AF_BLUETOOTH #define AF_BLUETOOTH 31 #endif #ifndef BTPROTO_HCI #define BTPROTO_HCI 1 #endif #ifndef BTPROTO_SCO #define BTPROTO_SCO 2 #endif #ifndef SOCK_SEQPACKET #define SOCK_SEQPACKET 5 #endif #ifndef HCIDEVUP #define HCIDEVUP _IOW('H', 201, int) #endif #ifndef HCIDEVDOWN #define HCIDEVDOWN _IOW('H', 202, int) #endif #ifndef HCIGETDEVINFO #define HCIGETDEVINFO _IOR('H', 211, int) #endif #define NUM_CONN_THREADS 8 typedef struct { uint8_t b[6]; } __attribute__((packed)) bdaddr_t; struct sockaddr_sco { sa_family_t sco_family; bdaddr_t sco_bdaddr; }; struct hci_dev_stats { uint32_t err_rx; uint32_t err_tx; uint32_t cmd_tx; uint32_t evt_rx; uint32_t acl_tx; uint32_t acl_rx; uint32_t sco_tx; uint32_t sco_rx; uint32_t byte_rx; uint32_t byte_tx; }; struct hci_dev_info { uint16_t dev_id; char name[8]; bdaddr_t bdaddr; uint32_t flags; uint8_t type; uint8_t features[8]; uint32_t pkt_type; uint32_t link_policy; uint32_t link_mode; uint16_t acl_mtu; uint16_t acl_pkts; uint16_t sco_mtu; uint16_t sco_pkts; struct hci_dev_stats stat; }; static int vhci_fd = -1; static int hci_dev_id = -1; static volatile int running = 1; static void* vhci_responder_thread(void* arg) { (void)arg; uint8_t buf[1024]; while (__atomic_load_n(&running, __ATOMIC_RELAXED)) { struct pollfd pfd = { .fd = vhci_fd, .events = POLLIN }; int pr = poll(&pfd, 1, 100); if (pr <= 0) continue; ssize_t n = read(vhci_fd, buf, sizeof(buf)); if (n <= 0) continue; /* HCI Command Packet format: [0x01, opcode_lo, opcode_hi, plen, payload...] */ if (buf[0] == 0x01 && n >= 4) { uint16_t opcode = (uint16_t)buf[1] | ((uint16_t)buf[2] << 8); switch (opcode) { case 0x0c03: { /* HCI_OP_RESET */ uint8_t resp[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c0d: { /* HCI_OP_READ_STORED_LINK_KEY (5 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x08, 0x01, 0x0d, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c12: { /* HCI_OP_DELETE_STORED_LINK_KEY (3 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x06, 0x01, 0x12, 0x0c, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c14: { /* HCI_OP_READ_LOCAL_NAME (249 bytes payload) */ uint8_t resp[255]; memset(resp, 0, sizeof(resp)); resp[0] = 0x04; resp[1] = 0x0e; resp[2] = 252; resp[3] = 0x01; resp[4] = 0x14; resp[5] = 0x0c; resp[6] = 0x00; /* status */ strncpy((char*)&resp[7], "vhci", 248); (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c1b: { /* HCI_OP_READ_PAGE_SCAN_ACTIVITY (5 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x08, 0x01, 0x1b, 0x0c, 0x00, 0x00, 0x08, 0x12, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c23: { /* HCI_OP_READ_CLASS_OF_DEV (4 bytes payload: status + 3 dev_class) */ uint8_t resp[] = { 0x04, 0x0e, 0x07, 0x01, 0x23, 0x0c, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c25: { /* HCI_OP_READ_VOICE_SETTING (3 bytes payload: status + 2 voice_setting) */ uint8_t resp[] = { 0x04, 0x0e, 0x06, 0x01, 0x25, 0x0c, 0x00, 0x60, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c38: { /* HCI_OP_READ_NUM_SUPPORTED_IAC (2 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x05, 0x01, 0x38, 0x0c, 0x00, 0x01 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c46: { /* HCI_OP_READ_PAGE_SCAN_TYPE (2 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x05, 0x01, 0x46, 0x0c, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c58: { /* HCI_OP_READ_INQ_RSP_TX_POWER (2 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x05, 0x01, 0x58, 0x0c, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c5b: { /* HCI_OP_READ_DEF_ERR_DATA_REPORTING (2 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x05, 0x01, 0x5b, 0x0c, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x0c7b: { /* HCI_OP_READ_AUTH_PAYLOAD_TO (5 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x08, 0x01, 0x7b, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x080e: { /* HCI_OP_READ_DEF_LINK_POLICY (3 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x06, 0x01, 0x0e, 0x08, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1001: { /* HCI_OP_READ_LOCAL_VERSION (9 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1002: { /* HCI_OP_READ_LOCAL_COMMANDS (65 bytes payload: status + 64 zeroes) */ uint8_t resp[71]; memset(resp, 0, sizeof(resp)); resp[0] = 0x04; resp[1] = 0x0e; resp[2] = 68; resp[3] = 0x01; resp[4] = 0x02; resp[5] = 0x10; resp[6] = 0x00; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1003: { /* HCI_OP_READ_LOCAL_FEATURES (BR/EDR + LMP_SCO; no LE) */ uint8_t resp[] = { 0x04, 0x0e, 0x0c, 0x01, 0x03, 0x10, 0x00, 0xff, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1004: { /* HCI_OP_READ_LOCAL_EXT_FEATURES (11 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x0e, 0x01, 0x04, 0x10, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1005: { /* HCI_OP_READ_BUFFER_SIZE (sco_max_pkt = 0 to trigger SCO link failure) */ uint8_t resp[] = { 0x04, 0x0e, 0x0b, 0x01, 0x05, 0x10, 0x00, 0x00, 0x04, 0x40, 0x0a, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1009: { /* HCI_OP_READ_BD_ADDR (7 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x0a, 0x01, 0x09, 0x10, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x11 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x100c: { /* HCI_OP_READ_LOCAL_PAIRING_OPTS (3 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x06, 0x01, 0x0c, 0x10, 0x00, 0x00, 0x10 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x1407: { /* HCI_OP_READ_CLOCK (9 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x0c, 0x01, 0x07, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x2002: { /* HCI_OP_LE_READ_BUFFER_SIZE (4 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x07, 0x01, 0x02, 0x20, 0x00, 0xfb, 0x00, 0x10 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x2003: { /* HCI_OP_LE_READ_LOCAL_FEATURES (9 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x0c, 0x01, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } case 0x201c: { /* HCI_OP_LE_READ_SUPPORTED_STATES (9 bytes payload) */ uint8_t resp[] = { 0x04, 0x0e, 0x0c, 0x01, 0x1c, 0x20, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; (void)write(vhci_fd, resp, sizeof(resp)); break; } default: { /* Generic Command Complete: 1-byte status = 0x00 */ uint8_t resp[] = { 0x04, 0x0e, 0x04, 0x01, buf[1], buf[2], 0x00 }; (void)write(vhci_fd, resp, sizeof(resp)); break; } } } } return NULL; } static int wait_for_hci_up(int dev_id, int timeout_ms) { int s = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (s < 0) { printf("[-] Failed to open BTPROTO_HCI socket for wait_for_hci_up: %s\n", strerror(errno)); return -1; } for (int elapsed = 0; elapsed < timeout_ms; elapsed += 50) { struct hci_dev_info di; memset(&di, 0, sizeof(di)); di.dev_id = dev_id; if (ioctl(s, HCIGETDEVINFO, &di) == 0) { if (di.flags & (1 << 0)) { /* (1 << HCI_UP) */ printf("[+] Device hci%d is UP (flags=0x%x).\n", dev_id, di.flags); close(s); return 0; } } /* Try bring-up; ignore -EALREADY / -EBUSY / -EINPROGRESS during startup */ (void)ioctl(s, HCIDEVUP, dev_id); usleep(50000); } close(s); return -1; } static void* thread_down_up(void* arg) { (void)arg; PIN_TO_CPU(0); int sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (sock < 0) { printf("[-] Failed to create BTPROTO_HCI socket in thread_down_up: %s\n", strerror(errno)); return NULL; } while (__atomic_load_n(&running, __ATOMIC_RELAXED)) { (void)ioctl(sock, HCIDEVUP, hci_dev_id); usleep(500); (void)ioctl(sock, HCIDEVDOWN, hci_dev_id); usleep(500); } close(sock); return NULL; } static void* thread_connect(void* arg) { long cpu = (long)arg; if (cpu >= 0) PIN_TO_CPU((int)cpu); struct sockaddr_sco src_addr; memset(&src_addr, 0, sizeof(src_addr)); src_addr.sco_family = AF_BLUETOOTH; src_addr.sco_bdaddr.b[0] = 0xaa; src_addr.sco_bdaddr.b[1] = 0xbb; src_addr.sco_bdaddr.b[2] = 0xcc; src_addr.sco_bdaddr.b[3] = 0xdd; src_addr.sco_bdaddr.b[4] = 0xee; src_addr.sco_bdaddr.b[5] = 0x11; struct sockaddr_sco dst_addr; memset(&dst_addr, 0, sizeof(dst_addr)); dst_addr.sco_family = AF_BLUETOOTH; dst_addr.sco_bdaddr.b[0] = 0x11; dst_addr.sco_bdaddr.b[1] = 0x22; dst_addr.sco_bdaddr.b[2] = 0x33; dst_addr.sco_bdaddr.b[3] = 0x44; dst_addr.sco_bdaddr.b[4] = 0x55; dst_addr.sco_bdaddr.b[5] = 0x66; uint8_t cnt = 0; while (__atomic_load_n(&running, __ATOMIC_RELAXED)) { int s = socket(AF_BLUETOOTH, SOCK_SEQPACKET | SOCK_NONBLOCK, BTPROTO_SCO); if (s < 0) { usleep(1000); continue; } (void)bind(s, (struct sockaddr*)&src_addr, sizeof(src_addr)); for (int i = 0; i < 20 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) { dst_addr.sco_bdaddr.b[5] = cnt++; int ret = connect(s, (struct sockaddr*)&dst_addr, sizeof(dst_addr)); if (ret == 0 || errno == EBADFD || errno == EISCONN) break; } close(s); } return NULL; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Starting reproducer for WARNING in hci_connect_sco / __queue_work...\n"); /* Step 1: Open /dev/vhci */ int res = open("/dev/vhci", O_RDWR); if (res < 0) { printf("[-] Failed to open /dev/vhci: %s\n", strerror(errno)); exit(1); } vhci_fd = res; printf("[+] open /dev/vhci successful.\n"); /* Step 2: Create HCI_PRIMARY virtual adapter */ const uint8_t create_dev[] = { 0xff, 0x00 }; ssize_t written = write(vhci_fd, create_dev, sizeof(create_dev)); if (written < 0) { printf("[-] Failed to write create_dev to /dev/vhci: %s\n", strerror(errno)); exit(1); } if ((size_t)written != sizeof(create_dev)) { printf("[-] Partial write to /dev/vhci: %zd\n", written); exit(1); } printf("[+] write create_dev to /dev/vhci successful.\n"); /* Step 3: Read back device ID */ uint8_t id_buf[4]; ssize_t nread = read(vhci_fd, id_buf, sizeof(id_buf)); if (nread < 0) { printf("[-] Failed to read device confirmation from /dev/vhci: %s\n", strerror(errno)); exit(1); } if ((size_t)nread != sizeof(id_buf)) { printf("[-] Unexpected response size from /dev/vhci: %zd\n", nread); exit(1); } hci_dev_id = (int)(id_buf[2] | (id_buf[3] << 8)); printf("[+] Created virtual Bluetooth adapter hci%d.\n", hci_dev_id); /* Step 4: Spawn VHCI controller emulation thread */ pthread_t th_resp; res = pthread_create(&th_resp, NULL, vhci_responder_thread, NULL); if (res != 0) { printf("[-] Failed to create vhci_responder_thread: %s\n", strerror(res)); exit(1); } printf("[+] vhci_responder_thread started successfully.\n"); /* Step 5: Wait for adapter to become UP */ res = wait_for_hci_up(hci_dev_id, 10000); if (res != 0) { printf("[-] Timed out waiting for hci%d to become UP: %s\n", hci_dev_id, strerror(errno)); exit(1); } printf("[+] hci%d is UP and fully operational.\n", hci_dev_id); /* Step 6: Spawn racer threads */ printf("[*] Spawning racer threads...\n"); pthread_t th_down; res = pthread_create(&th_down, NULL, thread_down_up, NULL); if (res != 0) { printf("[-] Failed to create thread_down_up: %s\n", strerror(res)); exit(1); } pthread_t th_conn[NUM_CONN_THREADS]; long cpus[NUM_CONN_THREADS] = { 1, 1, 1, 0, 1, 0, -1, -1 }; for (int i = 0; i < NUM_CONN_THREADS; i++) { res = pthread_create(&th_conn[i], NULL, thread_connect, (void*)cpus[i]); if (res != 0) { printf("[-] Failed to create thread_connect[%d]: %s\n", i, strerror(res)); exit(1); } } printf("[+] All %d racer threads spawned successfully.\n", NUM_CONN_THREADS); /* Step 7: Run race */ printf("[*] Racing HCIDEVDOWN/HCIDEVUP vs connect(BTPROTO_SCO)...\n"); TIMER_START(start); while (TIMER_NOT_EXPIRED(start, 22.0)) { usleep(200000); } /* Step 8: Clean shutdown */ printf("[*] Stopping worker threads...\n"); __atomic_store_n(&running, 0, __ATOMIC_RELAXED); pthread_join(th_down, NULL); for (int i = 0; i < NUM_CONN_THREADS; i++) { pthread_join(th_conn[i], NULL); } pthread_join(th_resp, NULL); printf("[+] Racer threads stopped.\n"); res = close(vhci_fd); if (res < 0) { printf("[-] Failed to close /dev/vhci: %s\n", strerror(errno)); } else { printf("[+] Closed /dev/vhci successfully.\n"); } /* Step 9: Wait for asynchronous kernel timeouts / work items to settle */ printf("[*] Waiting for asynchronous kernel events to settle...\n"); sleep(3); printf("[+] Reproducer finished.\n"); return 0; }