source: zzuf/trunk/src/lib-fd.c @ 3634

Last change on this file since 3634 was 3634, checked in by Sam Hocevar, 11 years ago

Use strcpy, not strcat.

  • Property svn:keywords set to Id
File size: 17.0 KB
Line 
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: lib-fd.c 3634 2009-08-06 20:40:35Z sam $
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
81static void fuzz_iovec   (int fd, const struct iovec *iov, ssize_t ret);
82#endif
83static void offset_check (int fd);
84
85/* Library functions that we divert */
86static int     (*ORIG(open))    (const char *file, int oflag, ...);
87#if defined HAVE_OPEN64
88static int     (*ORIG(open64))  (const char *file, int oflag, ...);
89#endif
90#if defined HAVE___OPEN64
91static int     (*ORIG(__open64))(const char *file, int oflag, ...);
92#endif
93#if defined HAVE_DUP
94static int     (*ORIG(dup))     (int oldfd);
95#endif
96#if defined HAVE_DUP2
97static int     (*ORIG(dup2))    (int oldfd, int newfd);
98#endif
99#if defined HAVE_ACCEPT
100static int     (*ORIG(accept))  (int sockfd, struct sockaddr *addr,
101                                 SOCKLEN_T *addrlen);
102#endif
103#if defined HAVE_BIND
104static int     (*ORIG(bind))    (int sockfd, const struct sockaddr *my_addr,
105                                 SOCKLEN_T addrlen);
106#endif
107#if defined HAVE_CONNECT
108static int     (*ORIG(connect)) (int sockfd, const struct sockaddr *serv_addr,
109                                 SOCKLEN_T addrlen);
110#endif
111#if defined HAVE_SOCKET
112static int     (*ORIG(socket))  (int domain, int type, int protocol);
113#endif
114#if defined HAVE_RECV
115static RECV_T  (*ORIG(recv))    (int s, void *buf, size_t len, int flags);
116#endif
117#if defined HAVE_RECVFROM
118static 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
122static RECV_T  (*ORIG(recvmsg)) (int s,  struct msghdr *hdr, int flags);
123#endif
124#if defined READ_USES_SSIZE_T
125static ssize_t (*ORIG(read))    (int fd, void *buf, size_t count);
126#else
127static int     (*ORIG(read))    (int fd, void *buf, unsigned int count);
128#endif
129#if defined HAVE_READV
130static ssize_t (*ORIG(readv))   (int fd, const struct iovec *iov, int count);
131#endif
132#if defined HAVE_PREAD
133static ssize_t (*ORIG(pread))   (int fd, void *buf, size_t count, off_t offset);
134#endif
135#if defined HAVE_AIO_READ
136static int     (*ORIG(aio_read))   (struct aiocb *aiocbp);
137static ssize_t (*ORIG(aio_return)) (struct aiocb *aiocbp);
138#endif
139static off_t   (*ORIG(lseek))   (int fd, off_t offset, int whence);
140#if defined HAVE_LSEEK64
141static off64_t (*ORIG(lseek64)) (int fd, off64_t offset, int whence);
142#endif
143#if defined HAVE___LSEEK64
144static off64_t (*ORIG(__lseek64)) (int fd, off64_t offset, int whence);
145#endif
146static 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
180int NEW(open)(const char *file, int oflag, ...)
181{
182    int ret; OPEN(open); return ret;
183}
184
185#if defined HAVE_OPEN64
186int NEW(open64)(const char *file, int oflag, ...)
187{
188    int ret; OPEN(open64); return ret;
189}
190#endif
191
192#if defined HAVE___OPEN64
193int NEW(__open64)(const char *file, int oflag, ...)
194{
195    int ret; OPEN(__open64); return ret;
196}
197#endif
198
199#if defined HAVE_DUP
200int 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
221int 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
247int 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
313int 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
320int 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
328int 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
348RECV_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
380RECV_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 tmp[128];
393        char *b = buf;
394
395        _zz_fuzz(s, buf, ret);
396        _zz_addpos(s, ret);
397
398        if (fromlen)
399            sprintf(tmp, "&%i", (int)*fromlen);
400        else
401            strcpy(tmp, "NULL");
402
403        if (ret >= 4)
404            debug("%s(%i, %p, %li, 0x%x, %p, %s) = %i \"%c%c%c%c...",
405                  __func__, s, buf, (long int)len, flags, from, tmp,
406                  ret, b[0], b[1], b[2], b[3]);
407        else
408            debug("%s(%i, %p, %li, 0x%x, %p, %s) = %i \"%c...",
409                  __func__, s, buf, (long int)len, flags, from, tmp,
410                  ret, b[0]);
411    }
412    else
413        debug("%s(%i, %p, %li, 0x%x, %p, %p) = %i", __func__,
414              s, buf, (long int)len, flags, from, fromlen, ret);
415
416    return ret;
417}
418#endif
419
420#if defined HAVE_RECVMSG
421RECV_T NEW(recvmsg)(int s, struct msghdr *hdr, int flags)
422{
423    ssize_t ret;
424
425    LOADSYM(recvmsg);
426    ret = ORIG(recvmsg)(s, hdr, flags);
427    if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s) || !_zz_isactive(s))
428        return ret;
429
430    fuzz_iovec(s, hdr->msg_iov, ret);
431    debug("%s(%i, %p, %x) = %li", __func__, s, hdr, flags, (long int)ret);
432
433    return ret;
434}
435#endif
436
437#if defined READ_USES_SSIZE_T
438ssize_t NEW(read)(int fd, void *buf, size_t count)
439#else
440int NEW(read)(int fd, void *buf, unsigned int count)
441#endif
442{
443    int ret;
444
445    LOADSYM(read);
446    ret = ORIG(read)(fd, buf, count);
447    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
448         || !_zz_isactive(fd))
449        return ret;
450
451    if(ret > 0)
452    {
453        char *b = buf;
454
455        _zz_fuzz(fd, buf, ret);
456        _zz_addpos(fd, ret);
457
458        if(ret >= 4)
459            debug("%s(%i, %p, %li) = %i \"%c%c%c%c...", __func__, fd, buf,
460                  (long int)count, ret, b[0], b[1], b[2], b[3]);
461        else
462            debug("%s(%i, %p, %li) = %i \"%c...", __func__, fd, buf,
463                  (long int)count, ret, b[0]);
464    }
465    else
466        debug("%s(%i, %p, %li) = %i", __func__, fd, buf, (long int)count, ret);
467
468    offset_check(fd);
469    return ret;
470}
471
472#if defined HAVE_READV
473ssize_t NEW(readv)(int fd, const struct iovec *iov, int count)
474{
475    ssize_t ret;
476
477    LOADSYM(readv);
478    ret = ORIG(readv)(fd, iov, count);
479    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
480         || !_zz_isactive(fd))
481        return ret;
482
483    fuzz_iovec(fd, iov, ret);
484    debug("%s(%i, %p, %i) = %li", __func__, fd, iov, count, (long int)ret);
485
486    offset_check(fd);
487    return ret;
488}
489#endif
490
491#if defined HAVE_PREAD
492ssize_t NEW(pread)(int fd, void *buf, size_t count, off_t offset)
493{
494    int ret;
495
496    LOADSYM(pread);
497    ret = ORIG(pread)(fd, buf, count, offset);
498    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
499         || !_zz_isactive(fd))
500        return ret;
501
502    if(ret > 0)
503    {
504        long int curoff = _zz_getpos(fd);
505        char *b = buf;
506
507        _zz_setpos(fd, offset);
508        _zz_fuzz(fd, buf, ret);
509        _zz_setpos(fd, curoff);
510
511        if(ret >= 4)
512            debug("%s(%i, %p, %li, %li) = %i \"%c%c%c%c...", __func__, fd, buf,
513                  (long int)count, (long int)offset, ret,
514                  b[0], b[1], b[2], b[3]);
515        else
516            debug("%s(%i, %p, %li, %li) = %i \"%c...", __func__, fd, buf,
517                  (long int)count, (long int)offset, ret, b[0]);
518    }
519    else
520        debug("%s(%i, %p, %li, %li) = %i", __func__, fd, buf,
521              (long int)count, (long int)offset, ret);
522
523    return ret;
524}
525#endif
526
527#define LSEEK(fn, off_t) \
528    do \
529    { \
530        LOADSYM(fn); \
531        ret = ORIG(fn)(fd, offset, whence); \
532        if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
533             || !_zz_isactive(fd)) \
534            return ret; \
535        debug("%s(%i, %lli, %i) = %lli", __func__, fd, \
536              (long long int)offset, whence, (long long int)ret); \
537        if(ret != (off_t)-1) \
538            _zz_setpos(fd, ret); \
539    } while(0)
540
541off_t NEW(lseek)(int fd, off_t offset, int whence)
542{
543    off_t ret;
544    LSEEK(lseek, off_t);
545    return ret;
546}
547
548#if defined HAVE_LSEEK64
549off64_t NEW(lseek64)(int fd, off64_t offset, int whence)
550{
551    off64_t ret; LSEEK(lseek64, off64_t); return ret;
552}
553#endif
554
555#if defined HAVE___LSEEK64
556off64_t NEW(__lseek64)(int fd, off64_t offset, int whence)
557{
558    off64_t ret; LSEEK(__lseek64, off64_t); return ret;
559}
560#endif
561
562#if defined HAVE_AIO_READ
563int NEW(aio_read)(struct aiocb *aiocbp)
564{
565    int ret;
566    int fd = aiocbp->aio_fildes;
567
568    LOADSYM(aio_read);
569    if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd))
570        return ORIG(aio_read)(aiocbp);
571
572    _zz_lock(fd);
573    ret = ORIG(aio_read)(aiocbp);
574
575    debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %i", __func__,
576          fd, aiocbp->aio_lio_opcode, aiocbp->aio_reqprio, aiocbp->aio_buf,
577          (long int)aiocbp->aio_nbytes, (long int)aiocbp->aio_offset, ret);
578
579    return ret;
580}
581
582ssize_t NEW(aio_return)(struct aiocb *aiocbp)
583{
584    ssize_t ret;
585    int fd = aiocbp->aio_fildes;
586
587    LOADSYM(aio_return);
588    if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd))
589        return ORIG(aio_return)(aiocbp);
590
591    ret = ORIG(aio_return)(aiocbp);
592    _zz_unlock(fd);
593
594    /* FIXME: make sure we’re actually *reading* */
595    if(ret > 0)
596    {
597        _zz_setpos(fd, aiocbp->aio_offset);
598        _zz_fuzz(fd, aiocbp->aio_buf, ret);
599        _zz_addpos(fd, ret);
600    }
601
602    debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %li", __func__,
603          fd, aiocbp->aio_lio_opcode, aiocbp->aio_reqprio, aiocbp->aio_buf,
604          (long int)aiocbp->aio_nbytes, (long int)aiocbp->aio_offset,
605          (long int)ret);
606
607    return ret;
608}
609#endif
610
611int NEW(close)(int fd)
612{
613    int ret;
614
615    /* Hey, it’s our debug channel! Silently pretend we closed it. */
616    if(fd == _zz_debugfd)
617        return 0;
618
619    LOADSYM(close);
620    ret = ORIG(close)(fd);
621    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
622        return ret;
623
624    debug("%s(%i) = %i", __func__, fd, ret);
625    _zz_unregister(fd);
626
627    return ret;
628}
629
630/* XXX: the following functions are local */
631
632#if defined HAVE_READV || defined HAVE_RECVMSG
633static void fuzz_iovec(int fd, const struct iovec *iov, ssize_t ret)
634{
635    /* NOTE: We assume that iov countains at least <ret> bytes. */
636    while(ret > 0)
637    {
638        void *b = iov->iov_base;
639        size_t len = iov->iov_len;
640
641        if(len > (size_t)ret)
642            len = ret;
643
644        _zz_fuzz(fd, b, len);
645        _zz_addpos(fd, len);
646
647        iov++;
648        ret -= len;
649    }
650}
651#endif
652
653/* Sanity check, can be OK though (for instance with a character device) */
654static void offset_check(int fd)
655{
656    int orig_errno = errno;
657#if defined HAVE_LSEEK64
658    off64_t ret;
659    LOADSYM(lseek64);
660    ret = ORIG(lseek64)(fd, 0, SEEK_CUR);
661#else
662    off_t ret;
663    LOADSYM(lseek);
664    ret = ORIG(lseek)(fd, 0, SEEK_CUR);
665#endif
666    if(ret != -1 && ret != _zz_getpos(fd))
667        debug("warning: offset inconsistency");
668    errno = orig_errno;
669}
670
Note: See TracBrowser for help on using the repository browser.