etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

mock_io.c (1924B)

      1 /*
      2  * Syscall intercepts for the linux_usbfs I/O path.
      3  * Linked via -Wl,--wrap=open,--wrap=read,--wrap=close,--wrap=ioctl,--wrap=write
      4  *
      5  * Compiled into fuzz_usbfs but never called by its direct-injection path.
      6  * Available for a future harness that exercises initialize_device().
      7  */
      8 
      9 #include <stddef.h>
     10 #include <stdint.h>
     11 #include <string.h>
     12 #include <stdarg.h>
     13 #include <sys/types.h>
     14 #include <errno.h>
     15 
     16 #define FUZZ_BUF_MAX (1 << 16)
     17 #define FAKE_FD 42
     18 
     19 extern uint8_t g_fuzz_buf[FUZZ_BUF_MAX];
     20 extern size_t  g_fuzz_len;
     21 extern size_t  g_fuzz_offset;
     22 
     23 int __wrap_open(const char *path, int flags, ...)
     24 {
     25     (void)flags;
     26     if (path && __builtin_strstr(path, "/dev/bus/usb"))
     27         return FAKE_FD;
     28     errno = ENOENT;
     29     return -1;
     30 }
     31 
     32 ssize_t __wrap_read(int fd, void *buf, size_t len)
     33 {
     34     extern ssize_t __real_read(int, void *, size_t);
     35     if (fd != FAKE_FD)
     36         return __real_read(fd, buf, len);
     37     if (g_fuzz_offset >= g_fuzz_len)
     38         return 0;
     39     size_t avail = g_fuzz_len - g_fuzz_offset;
     40     size_t n = len < avail ? len : avail;
     41     memcpy(buf, g_fuzz_buf + g_fuzz_offset, n);
     42     g_fuzz_offset += n;
     43     return (ssize_t)n;
     44 }
     45 
     46 int __wrap_ioctl(int fd, unsigned long request, ...)
     47 {
     48     (void)fd;
     49     /* GET_CAPABILITIES → return all flags */
     50     if ((request & 0xFF) == 26) {
     51         va_list ap; va_start(ap, request);
     52         uint32_t *c = va_arg(ap, uint32_t *); va_end(ap);
     53         if (c) *c = 0x1F;
     54         return 0;
     55     }
     56     /* GET_SPEED → SuperSpeed Plus */
     57     if ((request & 0xFF) == 31) return 6;
     58     return 0;
     59 }
     60 
     61 int __wrap_close(int fd)
     62 {
     63     extern int __real_close(int);
     64     return fd == FAKE_FD ? 0 : __real_close(fd);
     65 }
     66 
     67 ssize_t __wrap_write(int fd, const void *buf, size_t len)
     68 {
     69     extern ssize_t __real_write(int, const void *, size_t);
     70     if (fd == 2 || fd == 198 || fd == 199)
     71         return __real_write(fd, buf, len);
     72     (void)buf;
     73     return (ssize_t)len;
     74 }