| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2006, 2007 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * 2007 Rémi Denis-Courmont <rdenis#simphalempin:com> |
|---|
| 5 | * 2007 Clément Stenac <zorglub#diwi:org> |
|---|
| 6 | * 2007 Dominik Kuhlen <dominik.kuhlen#gmit-gmbh:de> |
|---|
| 7 | * All Rights Reserved |
|---|
| 8 | * |
|---|
| 9 | * $Id$ |
|---|
| 10 | * |
|---|
| 11 | * This program is free software. It comes without any warranty, to |
|---|
| 12 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 13 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 14 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 15 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | /* |
|---|
| 19 | * load-fd.c: loaded file descriptor functions |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include "config.h" |
|---|
| 23 | |
|---|
| 24 | /* Need this for RTLD_NEXT */ |
|---|
| 25 | #define _GNU_SOURCE |
|---|
| 26 | /* Use this to get lseek64() on glibc systems */ |
|---|
| 27 | #define _LARGEFILE64_SOURCE |
|---|
| 28 | /* Use this to get proper prototypes on HP-UX systems */ |
|---|
| 29 | #define _XOPEN_SOURCE_EXTENDED |
|---|
| 30 | #define _INCLUDE_POSIX_SOURCE |
|---|
| 31 | |
|---|
| 32 | #if defined HAVE_STDINT_H |
|---|
| 33 | # include <stdint.h> |
|---|
| 34 | #elif defined HAVE_INTTYPES_H |
|---|
| 35 | # include <inttypes.h> |
|---|
| 36 | #endif |
|---|
| 37 | #include <stdlib.h> |
|---|
| 38 | #include <string.h> |
|---|
| 39 | #include <stdio.h> |
|---|
| 40 | #include <errno.h> |
|---|
| 41 | |
|---|
| 42 | #if defined HAVE_WINSOCK2_H |
|---|
| 43 | # include <winsock2.h> |
|---|
| 44 | #endif |
|---|
| 45 | #include <sys/types.h> |
|---|
| 46 | #if defined HAVE_SYS_SOCKET_H |
|---|
| 47 | # include <sys/socket.h> |
|---|
| 48 | #endif |
|---|
| 49 | #if defined HAVE_NETINET_IN_H |
|---|
| 50 | # include <netinet/in.h> |
|---|
| 51 | #endif |
|---|
| 52 | #if defined HAVE_ARPA_INET_H |
|---|
| 53 | # include <arpa/inet.h> |
|---|
| 54 | #endif |
|---|
| 55 | #if defined HAVE_SYS_UIO_H |
|---|
| 56 | # include <sys/uio.h> |
|---|
| 57 | #endif |
|---|
| 58 | #if defined HAVE_UNISTD_H |
|---|
| 59 | # include <unistd.h> |
|---|
| 60 | #endif |
|---|
| 61 | #include <fcntl.h> |
|---|
| 62 | #include <stdarg.h> |
|---|
| 63 | #if defined HAVE_AIO_H |
|---|
| 64 | # include <aio.h> |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | #include "libzzuf.h" |
|---|
| 68 | #include "lib-load.h" |
|---|
| 69 | #include "debug.h" |
|---|
| 70 | #include "fuzz.h" |
|---|
| 71 | #include "fd.h" |
|---|
| 72 | |
|---|
| 73 | #if defined HAVE_SOCKLEN_T |
|---|
| 74 | # define SOCKLEN_T socklen_t |
|---|
| 75 | #else |
|---|
| 76 | # define SOCKLEN_T int |
|---|
| 77 | #endif |
|---|
| 78 | |
|---|
| 79 | /* Local prototypes */ |
|---|
| 80 | #if defined HAVE_READV || defined HAVE_RECVMSG |
|---|
| 81 | static void fuzz_iovec (int fd, const struct iovec *iov, ssize_t ret); |
|---|
| 82 | #endif |
|---|
| 83 | static void offset_check (int fd); |
|---|
| 84 | |
|---|
| 85 | /* Library functions that we divert */ |
|---|
| 86 | static int (*ORIG(open)) (const char *file, int oflag, ...); |
|---|
| 87 | #if defined HAVE_OPEN64 |
|---|
| 88 | static int (*ORIG(open64)) (const char *file, int oflag, ...); |
|---|
| 89 | #endif |
|---|
| 90 | #if defined HAVE___OPEN64 |
|---|
| 91 | static int (*ORIG(__open64))(const char *file, int oflag, ...); |
|---|
| 92 | #endif |
|---|
| 93 | #if defined HAVE_DUP |
|---|
| 94 | static int (*ORIG(dup)) (int oldfd); |
|---|
| 95 | #endif |
|---|
| 96 | #if defined HAVE_DUP2 |
|---|
| 97 | static int (*ORIG(dup2)) (int oldfd, int newfd); |
|---|
| 98 | #endif |
|---|
| 99 | #if defined HAVE_ACCEPT |
|---|
| 100 | static int (*ORIG(accept)) (int sockfd, struct sockaddr *addr, |
|---|
| 101 | SOCKLEN_T *addrlen); |
|---|
| 102 | #endif |
|---|
| 103 | #if defined HAVE_BIND |
|---|
| 104 | static int (*ORIG(bind)) (int sockfd, const struct sockaddr *my_addr, |
|---|
| 105 | SOCKLEN_T addrlen); |
|---|
| 106 | #endif |
|---|
| 107 | #if defined HAVE_CONNECT |
|---|
| 108 | static int (*ORIG(connect)) (int sockfd, const struct sockaddr *serv_addr, |
|---|
| 109 | SOCKLEN_T addrlen); |
|---|
| 110 | #endif |
|---|
| 111 | #if defined HAVE_SOCKET |
|---|
| 112 | static int (*ORIG(socket)) (int domain, int type, int protocol); |
|---|
| 113 | #endif |
|---|
| 114 | #if defined HAVE_RECV |
|---|
| 115 | static RECV_T (*ORIG(recv)) (int s, void *buf, size_t len, int flags); |
|---|
| 116 | #endif |
|---|
| 117 | #if defined HAVE_RECVFROM |
|---|
| 118 | static RECV_T (*ORIG(recvfrom))(int s, void *buf, size_t len, int flags, |
|---|
| 119 | struct sockaddr *from, SOCKLEN_T *fromlen); |
|---|
| 120 | #endif |
|---|
| 121 | #if defined HAVE_RECVMSG |
|---|
| 122 | static RECV_T (*ORIG(recvmsg)) (int s, struct msghdr *hdr, int flags); |
|---|
| 123 | #endif |
|---|
| 124 | #if defined READ_USES_SSIZE_T |
|---|
| 125 | static ssize_t (*ORIG(read)) (int fd, void *buf, size_t count); |
|---|
| 126 | #else |
|---|
| 127 | static int (*ORIG(read)) (int fd, void *buf, unsigned int count); |
|---|
| 128 | #endif |
|---|
| 129 | #if defined HAVE_READV |
|---|
| 130 | static ssize_t (*ORIG(readv)) (int fd, const struct iovec *iov, int count); |
|---|
| 131 | #endif |
|---|
| 132 | #if defined HAVE_PREAD |
|---|
| 133 | static ssize_t (*ORIG(pread)) (int fd, void *buf, size_t count, off_t offset); |
|---|
| 134 | #endif |
|---|
| 135 | #if defined HAVE_AIO_READ |
|---|
| 136 | static int (*ORIG(aio_read)) (struct aiocb *aiocbp); |
|---|
| 137 | static ssize_t (*ORIG(aio_return)) (struct aiocb *aiocbp); |
|---|
| 138 | #endif |
|---|
| 139 | static off_t (*ORIG(lseek)) (int fd, off_t offset, int whence); |
|---|
| 140 | #if defined HAVE_LSEEK64 |
|---|
| 141 | static off64_t (*ORIG(lseek64)) (int fd, off64_t offset, int whence); |
|---|
| 142 | #endif |
|---|
| 143 | #if defined HAVE___LSEEK64 |
|---|
| 144 | static off64_t (*ORIG(__lseek64)) (int fd, off64_t offset, int whence); |
|---|
| 145 | #endif |
|---|
| 146 | static int (*ORIG(close)) (int fd); |
|---|
| 147 | |
|---|
| 148 | #define OPEN(fn) \ |
|---|
| 149 | do \ |
|---|
| 150 | { \ |
|---|
| 151 | int mode = 0; \ |
|---|
| 152 | LOADSYM(fn); \ |
|---|
| 153 | if(oflag & O_CREAT) \ |
|---|
| 154 | { \ |
|---|
| 155 | va_list va; \ |
|---|
| 156 | va_start(va, oflag); \ |
|---|
| 157 | mode = va_arg(va, int); \ |
|---|
| 158 | va_end(va); \ |
|---|
| 159 | ret = ORIG(fn)(file, oflag, mode); \ |
|---|
| 160 | } \ |
|---|
| 161 | else \ |
|---|
| 162 | { \ |
|---|
| 163 | ret = ORIG(fn)(file, oflag); \ |
|---|
| 164 | } \ |
|---|
| 165 | if(!_zz_ready || _zz_islocked(-1)) \ |
|---|
| 166 | return ret; \ |
|---|
| 167 | if(ret >= 0 \ |
|---|
| 168 | && ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY) \ |
|---|
| 169 | && _zz_mustwatch(file)) \ |
|---|
| 170 | { \ |
|---|
| 171 | if(oflag & O_CREAT) \ |
|---|
| 172 | debug("%s(\"%s\", %i, %i) = %i", \ |
|---|
| 173 | __func__, file, oflag, mode, ret); \ |
|---|
| 174 | else \ |
|---|
| 175 | debug("%s(\"%s\", %i) = %i", __func__, file, oflag, ret); \ |
|---|
| 176 | _zz_register(ret); \ |
|---|
| 177 | } \ |
|---|
| 178 | } while(0) |
|---|
| 179 | |
|---|
| 180 | int NEW(open)(const char *file, int oflag, ...) |
|---|
| 181 | { |
|---|
| 182 | int ret; OPEN(open); return ret; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | #if defined HAVE_OPEN64 |
|---|
| 186 | int NEW(open64)(const char *file, int oflag, ...) |
|---|
| 187 | { |
|---|
| 188 | int ret; OPEN(open64); return ret; |
|---|
| 189 | } |
|---|
| 190 | #endif |
|---|
| 191 | |
|---|
| 192 | #if defined HAVE___OPEN64 |
|---|
| 193 | int NEW(__open64)(const char *file, int oflag, ...) |
|---|
| 194 | { |
|---|
| 195 | int ret; OPEN(__open64); return ret; |
|---|
| 196 | } |
|---|
| 197 | #endif |
|---|
| 198 | |
|---|
| 199 | #if defined HAVE_DUP |
|---|
| 200 | int NEW(dup)(int oldfd) |
|---|
| 201 | { |
|---|
| 202 | int ret; |
|---|
| 203 | |
|---|
| 204 | LOADSYM(dup); |
|---|
| 205 | ret = ORIG(dup)(oldfd); |
|---|
| 206 | if(!_zz_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) |
|---|
| 207 | || !_zz_isactive(oldfd)) |
|---|
| 208 | return ret; |
|---|
| 209 | |
|---|
| 210 | if(ret >= 0) |
|---|
| 211 | { |
|---|
| 212 | debug("%s(%i) = %i", __func__, oldfd, ret); |
|---|
| 213 | _zz_register(ret); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | return ret; |
|---|
| 217 | } |
|---|
| 218 | #endif |
|---|
| 219 | |
|---|
| 220 | #if defined HAVE_DUP2 |
|---|
| 221 | int NEW(dup2)(int oldfd, int newfd) |
|---|
| 222 | { |
|---|
| 223 | int ret; |
|---|
| 224 | |
|---|
| 225 | LOADSYM(dup2); |
|---|
| 226 | ret = ORIG(dup2)(oldfd, newfd); |
|---|
| 227 | if(!_zz_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) |
|---|
| 228 | || !_zz_isactive(oldfd)) |
|---|
| 229 | return ret; |
|---|
| 230 | |
|---|
| 231 | if(ret >= 0) |
|---|
| 232 | { |
|---|
| 233 | /* We must close newfd if it was open, but only if oldfd != newfd |
|---|
| 234 | * and if dup2() suceeded. */ |
|---|
| 235 | if(oldfd != newfd && _zz_iswatched(newfd) && _zz_isactive(newfd)) |
|---|
| 236 | _zz_unregister(newfd); |
|---|
| 237 | |
|---|
| 238 | debug("%s(%i, %i) = %i", __func__, oldfd, newfd, ret); |
|---|
| 239 | _zz_register(ret); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | return ret; |
|---|
| 243 | } |
|---|
| 244 | #endif |
|---|
| 245 | |
|---|
| 246 | #if defined HAVE_ACCEPT |
|---|
| 247 | int NEW(accept)(int sockfd, struct sockaddr *addr, SOCKLEN_T *addrlen) |
|---|
| 248 | { |
|---|
| 249 | int ret; |
|---|
| 250 | |
|---|
| 251 | LOADSYM(accept); |
|---|
| 252 | ret = ORIG(accept)(sockfd, addr, addrlen); |
|---|
| 253 | if(!_zz_ready || _zz_islocked(-1) || !_zz_network |
|---|
| 254 | || !_zz_iswatched(sockfd) || !_zz_isactive(sockfd)) |
|---|
| 255 | return ret; |
|---|
| 256 | |
|---|
| 257 | if(ret >= 0) |
|---|
| 258 | { |
|---|
| 259 | if(addrlen) |
|---|
| 260 | debug("%s(%i, %p, &%i) = %i", __func__, |
|---|
| 261 | sockfd, addr, (int)*addrlen, ret); |
|---|
| 262 | else |
|---|
| 263 | debug("%s(%i, %p, NULL) = %i", __func__, sockfd, addr, ret); |
|---|
| 264 | _zz_register(ret); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | return ret; |
|---|
| 268 | } |
|---|
| 269 | #endif |
|---|
| 270 | |
|---|
| 271 | #if defined AF_INET6 |
|---|
| 272 | # define case_AF_INET6 case AF_INET6: |
|---|
| 273 | #else |
|---|
| 274 | # define case_AF_INET6 |
|---|
| 275 | #endif |
|---|
| 276 | |
|---|
| 277 | #define CONNECTION(fn, addr) \ |
|---|
| 278 | do \ |
|---|
| 279 | { \ |
|---|
| 280 | LOADSYM(fn); \ |
|---|
| 281 | ret = ORIG(fn)(sockfd, addr, addrlen); \ |
|---|
| 282 | if(!_zz_ready || _zz_islocked(-1) || !_zz_network) \ |
|---|
| 283 | return ret; \ |
|---|
| 284 | if(ret >= 0) \ |
|---|
| 285 | { \ |
|---|
| 286 | struct sockaddr_in in; \ |
|---|
| 287 | long int port; \ |
|---|
| 288 | switch(addr->sa_family) \ |
|---|
| 289 | { \ |
|---|
| 290 | case AF_INET: \ |
|---|
| 291 | case_AF_INET6 \ |
|---|
| 292 | /* We need to copy rather than cast sockaddr* to sockaddr_in* \ |
|---|
| 293 | * because sockaddr_in* has actually _larger_ alignment on \ |
|---|
| 294 | * eg. Linux alpha. And we only need sin_port so we only copy \ |
|---|
| 295 | * this member. */ \ |
|---|
| 296 | memcpy(&in.sin_port, \ |
|---|
| 297 | (char const *)addr + ((char *)&in.sin_port - (char *)&in), \ |
|---|
| 298 | sizeof(in.sin_port)); \ |
|---|
| 299 | port = ntohs(in.sin_port); \ |
|---|
| 300 | if(_zz_portwatched(port)) \ |
|---|
| 301 | break; \ |
|---|
| 302 | /* Fall through */ \ |
|---|
| 303 | default: \ |
|---|
| 304 | _zz_unregister(sockfd); \ |
|---|
| 305 | return ret; \ |
|---|
| 306 | } \ |
|---|
| 307 | debug("%s(%i, %p, %i) = %i", __func__, \ |
|---|
| 308 | sockfd, addr, (int)addrlen, ret); \ |
|---|
| 309 | } \ |
|---|
| 310 | } while(0); |
|---|
| 311 | |
|---|
| 312 | #if defined HAVE_BIND |
|---|
| 313 | int NEW(bind)(int sockfd, const struct sockaddr *my_addr, SOCKLEN_T addrlen) |
|---|
| 314 | { |
|---|
| 315 | int ret; CONNECTION(bind, my_addr); return ret; |
|---|
| 316 | } |
|---|
| 317 | #endif |
|---|
| 318 | |
|---|
| 319 | #if defined HAVE_CONNECT |
|---|
| 320 | int NEW(connect)(int sockfd, const struct sockaddr *serv_addr, |
|---|
| 321 | SOCKLEN_T addrlen) |
|---|
| 322 | { |
|---|
| 323 | int ret; CONNECTION(connect, serv_addr); return ret; |
|---|
| 324 | } |
|---|
| 325 | #endif |
|---|
| 326 | |
|---|
| 327 | #if defined HAVE_SOCKET |
|---|
| 328 | int NEW(socket)(int domain, int type, int protocol) |
|---|
| 329 | { |
|---|
| 330 | int ret; |
|---|
| 331 | |
|---|
| 332 | LOADSYM(socket); |
|---|
| 333 | ret = ORIG(socket)(domain, type, protocol); |
|---|
| 334 | if(!_zz_ready || _zz_islocked(-1) || !_zz_network) |
|---|
| 335 | return ret; |
|---|
| 336 | |
|---|
| 337 | if(ret >= 0) |
|---|
| 338 | { |
|---|
| 339 | debug("%s(%i, %i, %i) = %i", __func__, domain, type, protocol, ret); |
|---|
| 340 | _zz_register(ret); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | return ret; |
|---|
| 344 | } |
|---|
| 345 | #endif |
|---|
| 346 | |
|---|
| 347 | #if defined HAVE_RECV |
|---|
| 348 | RECV_T NEW(recv)(int s, void *buf, size_t len, int flags) |
|---|
| 349 | { |
|---|
| 350 | int ret; |
|---|
| 351 | |
|---|
| 352 | LOADSYM(recv); |
|---|
| 353 | ret = ORIG(recv)(s, buf, len, flags); |
|---|
| 354 | if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s) || !_zz_isactive(s)) |
|---|
| 355 | return ret; |
|---|
| 356 | |
|---|
| 357 | if(ret > 0) |
|---|
| 358 | { |
|---|
| 359 | char *b = buf; |
|---|
| 360 | |
|---|
| 361 | _zz_fuzz(s, buf, ret); |
|---|
| 362 | _zz_addpos(s, ret); |
|---|
| 363 | |
|---|
| 364 | if(ret >= 4) |
|---|
| 365 | debug("%s(%i, %p, %li, 0x%x) = %i \"%c%c%c%c...", __func__, |
|---|
| 366 | s, buf, (long int)len, flags, ret, b[0], b[1], b[2], b[3]); |
|---|
| 367 | else |
|---|
| 368 | debug("%s(%i, %p, %li, 0x%x) = %i \"%c...", __func__, |
|---|
| 369 | s, buf, (long int)len, flags, ret, b[0]); |
|---|
| 370 | } |
|---|
| 371 | else |
|---|
| 372 | debug("%s(%i, %p, %li, 0x%x) = %i", __func__, |
|---|
| 373 | s, buf, (long int)len, flags, ret); |
|---|
| 374 | |
|---|
| 375 | return ret; |
|---|
| 376 | } |
|---|
| 377 | #endif |
|---|
| 378 | |
|---|
| 379 | #if defined HAVE_RECVFROM |
|---|
| 380 | RECV_T NEW(recvfrom)(int s, void *buf, size_t len, int flags, |
|---|
| 381 | struct sockaddr *from, SOCKLEN_T *fromlen) |
|---|
| 382 | { |
|---|
| 383 | int ret; |
|---|
| 384 | |
|---|
| 385 | LOADSYM(recvfrom); |
|---|
| 386 | ret = ORIG(recvfrom)(s, buf, len, flags, from, fromlen); |
|---|
| 387 | if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s) || !_zz_isactive(s)) |
|---|
| 388 | return ret; |
|---|
| 389 | |
|---|
| 390 | if(ret > 0) |
|---|
| 391 | { |
|---|
| 392 | char *b = buf; |
|---|
| 393 | |
|---|
| 394 | _zz_fuzz(s, buf, ret); |
|---|
| 395 | _zz_addpos(s, ret); |
|---|
| 396 | |
|---|
| 397 | if(ret >= 4) |
|---|
| 398 | debug("%s(%i, %p, %li, 0x%x, %p, &%i) = %i \"%c%c%c%c...", |
|---|
| 399 | __func__, s, buf, (long int)len, flags, from, (int)*fromlen, |
|---|
| 400 | ret, b[0], b[1], b[2], b[3]); |
|---|
| 401 | else |
|---|
| 402 | debug("%s(%i, %p, %li, 0x%x, %p, &%i) = %i \"%c...", |
|---|
| 403 | __func__, s, buf, (long int)len, flags, from, (int)*fromlen, |
|---|
| 404 | ret, b[0]); |
|---|
| 405 | } |
|---|
| 406 | else |
|---|
| 407 | debug("%s(%i, %p, %li, 0x%x, %p, %p) = %i", __func__, |
|---|
| 408 | s, buf, (long int)len, flags, from, fromlen, ret); |
|---|
| 409 | |
|---|
| 410 | return ret; |
|---|
| 411 | } |
|---|
| 412 | #endif |
|---|
| 413 | |
|---|
| 414 | #if defined HAVE_RECVMSG |
|---|
| 415 | RECV_T NEW(recvmsg)(int s, struct msghdr *hdr, int flags) |
|---|
| 416 | { |
|---|
| 417 | ssize_t ret; |
|---|
| 418 | |
|---|
| 419 | LOADSYM(recvmsg); |
|---|
| 420 | ret = ORIG(recvmsg)(s, hdr, flags); |
|---|
| 421 | if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s) || !_zz_isactive(s)) |
|---|
| 422 | return ret; |
|---|
| 423 | |
|---|
| 424 | fuzz_iovec(s, hdr->msg_iov, ret); |
|---|
| 425 | debug("%s(%i, %p, %x) = %li", __func__, s, hdr, flags, (long int)ret); |
|---|
| 426 | |
|---|
| 427 | return ret; |
|---|
| 428 | } |
|---|
| 429 | #endif |
|---|
| 430 | |
|---|
| 431 | #if defined READ_USES_SSIZE_T |
|---|
| 432 | ssize_t NEW(read)(int fd, void *buf, size_t count) |
|---|
| 433 | #else |
|---|
| 434 | int NEW(read)(int fd, void *buf, unsigned int count) |
|---|
| 435 | #endif |
|---|
| 436 | { |
|---|
| 437 | int ret; |
|---|
| 438 | |
|---|
| 439 | LOADSYM(read); |
|---|
| 440 | ret = ORIG(read)(fd, buf, count); |
|---|
| 441 | if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) |
|---|
| 442 | || !_zz_isactive(fd)) |
|---|
| 443 | return ret; |
|---|
| 444 | |
|---|
| 445 | if(ret > 0) |
|---|
| 446 | { |
|---|
| 447 | char *b = buf; |
|---|
| 448 | |
|---|
| 449 | _zz_fuzz(fd, buf, ret); |
|---|
| 450 | _zz_addpos(fd, ret); |
|---|
| 451 | |
|---|
| 452 | if(ret >= 4) |
|---|
| 453 | debug("%s(%i, %p, %li) = %i \"%c%c%c%c...", __func__, fd, buf, |
|---|
| 454 | (long int)count, ret, b[0], b[1], b[2], b[3]); |
|---|
| 455 | else |
|---|
| 456 | debug("%s(%i, %p, %li) = %i \"%c...", __func__, fd, buf, |
|---|
| 457 | (long int)count, ret, b[0]); |
|---|
| 458 | } |
|---|
| 459 | else |
|---|
| 460 | debug("%s(%i, %p, %li) = %i", __func__, fd, buf, (long int)count, ret); |
|---|
| 461 | |
|---|
| 462 | offset_check(fd); |
|---|
| 463 | return ret; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | #if defined HAVE_READV |
|---|
| 467 | ssize_t NEW(readv)(int fd, const struct iovec *iov, int count) |
|---|
| 468 | { |
|---|
| 469 | ssize_t ret; |
|---|
| 470 | |
|---|
| 471 | LOADSYM(readv); |
|---|
| 472 | ret = ORIG(readv)(fd, iov, count); |
|---|
| 473 | if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) |
|---|
| 474 | || !_zz_isactive(fd)) |
|---|
| 475 | return ret; |
|---|
| 476 | |
|---|
| 477 | fuzz_iovec(fd, iov, ret); |
|---|
| 478 | debug("%s(%i, %p, %i) = %li", __func__, fd, iov, count, (long int)ret); |
|---|
| 479 | |
|---|
| 480 | offset_check(fd); |
|---|
| 481 | return ret; |
|---|
| 482 | } |
|---|
| 483 | #endif |
|---|
| 484 | |
|---|
| 485 | #if defined HAVE_PREAD |
|---|
| 486 | ssize_t NEW(pread)(int fd, void *buf, size_t count, off_t offset) |
|---|
| 487 | { |
|---|
| 488 | int ret; |
|---|
| 489 | |
|---|
| 490 | LOADSYM(pread); |
|---|
| 491 | ret = ORIG(pread)(fd, buf, count, offset); |
|---|
| 492 | if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) |
|---|
| 493 | || !_zz_isactive(fd)) |
|---|
| 494 | return ret; |
|---|
| 495 | |
|---|
| 496 | if(ret > 0) |
|---|
| 497 | { |
|---|
| 498 | long int curoff = _zz_getpos(fd); |
|---|
| 499 | char *b = buf; |
|---|
| 500 | |
|---|
| 501 | _zz_setpos(fd, offset); |
|---|
| 502 | _zz_fuzz(fd, buf, ret); |
|---|
| 503 | _zz_setpos(fd, curoff); |
|---|
| 504 | |
|---|
| 505 | if(ret >= 4) |
|---|
| 506 | debug("%s(%i, %p, %li, %li) = %i \"%c%c%c%c...", __func__, fd, buf, |
|---|
| 507 | (long int)count, (long int)offset, ret, |
|---|
| 508 | b[0], b[1], b[2], b[3]); |
|---|
| 509 | else |
|---|
| 510 | debug("%s(%i, %p, %li, %li) = %i \"%c...", __func__, fd, buf, |
|---|
| 511 | (long int)count, (long int)offset, ret, b[0]); |
|---|
| 512 | } |
|---|
| 513 | else |
|---|
| 514 | debug("%s(%i, %p, %li, %li) = %i", __func__, fd, buf, |
|---|
| 515 | (long int)count, (long int)offset, ret); |
|---|
| 516 | |
|---|
| 517 | return ret; |
|---|
| 518 | } |
|---|
| 519 | #endif |
|---|
| 520 | |
|---|
| 521 | #define LSEEK(fn, off_t) \ |
|---|
| 522 | do \ |
|---|
| 523 | { \ |
|---|
| 524 | LOADSYM(fn); \ |
|---|
| 525 | ret = ORIG(fn)(fd, offset, whence); \ |
|---|
| 526 | if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \ |
|---|
| 527 | || !_zz_isactive(fd)) \ |
|---|
| 528 | return ret; \ |
|---|
| 529 | debug("%s(%i, %lli, %i) = %lli", __func__, fd, \ |
|---|
| 530 | (long long int)offset, whence, (long long int)ret); \ |
|---|
| 531 | if(ret != (off_t)-1) \ |
|---|
| 532 | _zz_setpos(fd, ret); \ |
|---|
| 533 | } while(0) |
|---|
| 534 | |
|---|
| 535 | off_t NEW(lseek)(int fd, off_t offset, int whence) |
|---|
| 536 | { |
|---|
| 537 | off_t ret; |
|---|
| 538 | LSEEK(lseek, off_t); |
|---|
| 539 | return ret; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | #if defined HAVE_LSEEK64 |
|---|
| 543 | off64_t NEW(lseek64)(int fd, off64_t offset, int whence) |
|---|
| 544 | { |
|---|
| 545 | off64_t ret; LSEEK(lseek64, off64_t); return ret; |
|---|
| 546 | } |
|---|
| 547 | #endif |
|---|
| 548 | |
|---|
| 549 | #if defined HAVE___LSEEK64 |
|---|
| 550 | off64_t NEW(__lseek64)(int fd, off64_t offset, int whence) |
|---|
| 551 | { |
|---|
| 552 | off64_t ret; LSEEK(__lseek64, off64_t); return ret; |
|---|
| 553 | } |
|---|
| 554 | #endif |
|---|
| 555 | |
|---|
| 556 | #if defined HAVE_AIO_READ |
|---|
| 557 | int NEW(aio_read)(struct aiocb *aiocbp) |
|---|
| 558 | { |
|---|
| 559 | int ret; |
|---|
| 560 | int fd = aiocbp->aio_fildes; |
|---|
| 561 | |
|---|
| 562 | LOADSYM(aio_read); |
|---|
| 563 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 564 | return ORIG(aio_read)(aiocbp); |
|---|
| 565 | |
|---|
| 566 | _zz_lock(fd); |
|---|
| 567 | ret = ORIG(aio_read)(aiocbp); |
|---|
| 568 | |
|---|
| 569 | debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %i", __func__, |
|---|
| 570 | fd, aiocbp->aio_lio_opcode, aiocbp->aio_reqprio, aiocbp->aio_buf, |
|---|
| 571 | (long int)aiocbp->aio_nbytes, (long int)aiocbp->aio_offset, ret); |
|---|
| 572 | |
|---|
| 573 | return ret; |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | ssize_t NEW(aio_return)(struct aiocb *aiocbp) |
|---|
| 577 | { |
|---|
| 578 | ssize_t ret; |
|---|
| 579 | int fd = aiocbp->aio_fildes; |
|---|
| 580 | |
|---|
| 581 | LOADSYM(aio_return); |
|---|
| 582 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 583 | return ORIG(aio_return)(aiocbp); |
|---|
| 584 | |
|---|
| 585 | ret = ORIG(aio_return)(aiocbp); |
|---|
| 586 | _zz_unlock(fd); |
|---|
| 587 | |
|---|
| 588 | /* FIXME: make sure we’re actually *reading* */ |
|---|
| 589 | if(ret > 0) |
|---|
| 590 | { |
|---|
| 591 | _zz_setpos(fd, aiocbp->aio_offset); |
|---|
| 592 | _zz_fuzz(fd, aiocbp->aio_buf, ret); |
|---|
| 593 | _zz_addpos(fd, ret); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %li", __func__, |
|---|
| 597 | fd, aiocbp->aio_lio_opcode, aiocbp->aio_reqprio, aiocbp->aio_buf, |
|---|
| 598 | (long int)aiocbp->aio_nbytes, (long int)aiocbp->aio_offset, |
|---|
| 599 | (long int)ret); |
|---|
| 600 | |
|---|
| 601 | return ret; |
|---|
| 602 | } |
|---|
| 603 | #endif |
|---|
| 604 | |
|---|
| 605 | int NEW(close)(int fd) |
|---|
| 606 | { |
|---|
| 607 | int ret; |
|---|
| 608 | |
|---|
| 609 | /* Hey, it’s our debug channel! Silently pretend we closed it. */ |
|---|
| 610 | if(fd == _zz_debugfd) |
|---|
| 611 | return 0; |
|---|
| 612 | |
|---|
| 613 | LOADSYM(close); |
|---|
| 614 | ret = ORIG(close)(fd); |
|---|
| 615 | if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) |
|---|
| 616 | return ret; |
|---|
| 617 | |
|---|
| 618 | debug("%s(%i) = %i", __func__, fd, ret); |
|---|
| 619 | _zz_unregister(fd); |
|---|
| 620 | |
|---|
| 621 | return ret; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | /* XXX: the following functions are local */ |
|---|
| 625 | |
|---|
| 626 | #if defined HAVE_READV || defined HAVE_RECVMSG |
|---|
| 627 | static void fuzz_iovec(int fd, const struct iovec *iov, ssize_t ret) |
|---|
| 628 | { |
|---|
| 629 | /* NOTE: We assume that iov countains at least <ret> bytes. */ |
|---|
| 630 | while(ret > 0) |
|---|
| 631 | { |
|---|
| 632 | void *b = iov->iov_base; |
|---|
| 633 | size_t len = iov->iov_len; |
|---|
| 634 | |
|---|
| 635 | if(len > (size_t)ret) |
|---|
| 636 | len = ret; |
|---|
| 637 | |
|---|
| 638 | _zz_fuzz(fd, b, len); |
|---|
| 639 | _zz_addpos(fd, len); |
|---|
| 640 | |
|---|
| 641 | iov++; |
|---|
| 642 | ret -= len; |
|---|
| 643 | } |
|---|
| 644 | } |
|---|
| 645 | #endif |
|---|
| 646 | |
|---|
| 647 | /* Sanity check, can be OK though (for instance with a character device) */ |
|---|
| 648 | static void offset_check(int fd) |
|---|
| 649 | { |
|---|
| 650 | int orig_errno = errno; |
|---|
| 651 | #if defined HAVE_LSEEK64 |
|---|
| 652 | off64_t ret; |
|---|
| 653 | LOADSYM(lseek64); |
|---|
| 654 | ret = ORIG(lseek64)(fd, 0, SEEK_CUR); |
|---|
| 655 | #else |
|---|
| 656 | off_t ret; |
|---|
| 657 | LOADSYM(lseek); |
|---|
| 658 | ret = ORIG(lseek)(fd, 0, SEEK_CUR); |
|---|
| 659 | #endif |
|---|
| 660 | if(ret != -1 && ret != _zz_getpos(fd)) |
|---|
| 661 | debug("warning: offset inconsistency"); |
|---|
| 662 | errno = orig_errno; |
|---|
| 663 | } |
|---|
| 664 | |
|---|