public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 9b5f0d892028a63a6b37e8ca80a6bb3127636405 4288 bytes (raw)

  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
 
/* Fuzzing wrapper
 * Derived from libnbd fuzzing wrapper
 * Copyright (C) 2013-2022 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>

#include <libnbd.h>

static void passt (int s);
static void qemu (int fd, int s);

int
main (int argc, char *argv[])
{
  int fd;
  pid_t pid;
  int sv[2];

  if (argc == 2) {
    /* Open the test case before we fork so we know the file exists. */
    fd = open (argv[1], O_RDONLY);
    if (fd == -1) {
      fprintf (stderr, "fuzz-wrapper: ");
      perror (argv[1]);
      exit (EXIT_FAILURE);
    }
  }
  else {
    fprintf (stderr, "fuzz-wrapper testcase\n");
    exit (EXIT_FAILURE);
  }

  /* Create a connected socket. */
  if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
    perror ("fuzz-wrapper: socketpair");
    exit (EXIT_FAILURE);
  }

  /* Fork: The parent will be the passt process.  The child will be
   * the phony qemu.
   */
  pid = fork ();
  if (pid == -1) {
    perror ("fuzz-wrapper: fork");
    exit (EXIT_FAILURE);
  }

  if (pid > 0) {
    /* Parent: passt. */
    close (sv[1]);
    close (fd);

    passt (sv[0]);
  }

  /* Child: qemu. */
  close (sv[0]);

  qemu (fd, sv[1]);

  close (sv[1]);

  _exit (EXIT_SUCCESS);
}

/* This is the parent process running passt. */
static void
passt (int sock)
{
  char sock_str[32];

  snprintf (sock_str, sizeof sock_str, "%d", sock);
  /* XXX Assumes passt is compiled in the top directory: */
  execlp ("../passt", "passt", "-f", "-e", "-1", "--fd", sock_str, NULL);
  perror ("fuzz-wrapper: execlp");
  _exit (EXIT_FAILURE);
}

/* This is the child process acting like qemu. */
static void
qemu (int fd, int sock)
{
  struct pollfd pfds[1];
  char rbuf[512], wbuf[512];
  size_t wsize = 0;
  ssize_t r;

  for (;;) {
    pfds[0].fd = sock;
    pfds[0].events = POLLIN;
    if (wsize > 0 || fd >= 0) pfds[0].events |= POLLOUT;
    pfds[0].revents = 0;

    if (poll (pfds, 1, -1) == -1) {
      if (errno == EINTR)
        continue;
      perror ("fuzz-wrapper: poll");
      /* This is not an error. */
      return;
    }

    /* We can read from the passt socket.  Just throw away anything sent. */
    if ((pfds[0].revents & POLLIN) != 0) {
      r = read (sock, rbuf, sizeof rbuf);
      if (r == -1 && errno != EINTR) {
        perror ("fuzz-wrapper: read");
        return;
      }
      else if (r == 0)          /* end of input from the server */
        return;
    }

    /* We can write to the passt socket. */
    if ((pfds[0].revents & POLLOUT) != 0) {
      /* Write more data from the wbuf. */
      if (wsize > 0) {
      morewrite:
        r = write (sock, wbuf, wsize);
        if (r == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
          perror ("fuzz-wrapper: write");
          return;
        }
        else if (r > 0) {
          memmove (wbuf, &wbuf[r], wsize-r);
          wsize -= r;
        }
      }
      /* Write more data from the file. */
      else if (fd >= 0) {
        r = read (fd, wbuf, sizeof wbuf);
        if (r == -1) {
          perror ("fuzz-wrapper: read");
          _exit (EXIT_FAILURE);
        }
        else if (r == 0) {
          fd = -1;              /* ignore the file from now on */
          shutdown (sock, SHUT_WR);
        }
        else {
          wsize = r;
          goto morewrite;
        }
      }
    }
  } /* for (;;) */
}

debug log:

solving 9b5f0d8 ...
found 9b5f0d8 in https://archives.passt.top/passt-dev/20221117122614.1269214-6-rjones@redhat.com/

applying [1/1] https://archives.passt.top/passt-dev/20221117122614.1269214-6-rjones@redhat.com/
diff --git a/fuzzing/fuzz-wrapper.c b/fuzzing/fuzz-wrapper.c
new file mode 100644
index 0000000..9b5f0d8

Checking patch fuzzing/fuzz-wrapper.c...
Applied patch fuzzing/fuzz-wrapper.c cleanly.

index at:
100644 9b5f0d892028a63a6b37e8ca80a6bb3127636405	fuzzing/fuzz-wrapper.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).