Coverage Report

Created: 2025-07-18 07:19

/src/curl_fuzzer/fuzz_bufq.cc
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 2017 - 2022, Max Dymond, <cmeister2@gmail.com>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 ***************************************************************************/
22
23
#include <assert.h>
24
#include <stdlib.h>
25
#include <signal.h>
26
#include <string.h>
27
#include <sys/mman.h>
28
#include <unistd.h>
29
#include <curl/curl.h>
30
31
#include <fuzzer/FuzzedDataProvider.h>
32
#include "fuzz_bufq.h"
33
34
extern "C" {
35
#include "bufq.h"
36
}
37
38
/**
39
 * Allocate template buffer.  This buffer is precomputed for performance and
40
 * used as a cyclic pattern when reading and writing. It can be useful to
41
 * detect unexpected data shifting or corruption. The buffer is marked
42
 * read-only so it cannot be written by mistake.
43
 */
44
static unsigned char *allocate_template_buffer(void)
45
1
{
46
1
  size_t sz = FUZZ_MAX_RW_SIZE + 256;
47
1
  unsigned char *buf = (unsigned char *)mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
48
1
  assert(buf != (unsigned char *)-1);
49
50
  /* Fill in with a cyclic pattern of 0, 1, ..., 255, 0, ... */
51
1
  unsigned char next_byte = 0;
52
16.7M
  for (size_t i = 0; i < sz; i++) {
53
16.7M
    buf[i] = next_byte++;
54
16.7M
  }
55
56
1
  int err = mprotect(buf, sz, PROT_READ);
57
1
  assert(err == 0);
58
1
  return buf;
59
1
}
60
61
/*
62
 * Compute a pointer to a read-only buffer with our pattern, knowing that the
63
 * first byte to appear is next_byte.
64
 */
65
10.1M
static unsigned char *compute_buffer(unsigned char next_byte, unsigned char *buf) {
66
10.1M
  return buf + next_byte;
67
10.1M
}
68
69
struct writer_cb_ctx {
70
  bool verbose;
71
  unsigned char *template_buf;
72
  size_t read_len;
73
  unsigned char next_byte_read;
74
};
75
76
/**
77
 * Consume and verify up to read_len from a BUFQ via callback for Curl_bufq_pass.
78
 */
79
CURLcode bufq_writer_cb(void *writer_ctx,
80
                       const unsigned char *buf, size_t len,
81
                       size_t *pnwritten)
82
2.10M
{
83
2.10M
  struct writer_cb_ctx *ctx = (struct writer_cb_ctx *)writer_ctx;
84
85
2.10M
  *pnwritten = 0;
86
2.10M
  if (ctx->read_len <= 0)
87
4.79k
    return CURLE_AGAIN;
88
89
2.10M
  FV_PRINTF(ctx->verbose, "Writer CB: %zu space available, %zu pending\n", len, ctx->read_len);
90
91
2.10M
  *pnwritten = len > ctx->read_len ? ctx->read_len : len;
92
93
2.10M
  unsigned char *compare = compute_buffer(ctx->next_byte_read, ctx->template_buf);
94
2.10M
  assert(memcmp(buf, compare, *pnwritten) == 0);
95
2.10M
  ctx->next_byte_read += *pnwritten;
96
2.10M
  ctx->read_len -= *pnwritten;
97
98
2.10M
  return CURLE_OK;
99
2.10M
}
100
101
struct reader_cb_ctx {
102
  bool verbose;
103
  unsigned char *template_buf;
104
  size_t write_len;
105
  unsigned char next_byte_write;
106
};
107
108
/**
109
 * Write up to write_len to a BUFQ via callback for Curl_bufq_slurp/sipn.
110
 */
111
static CURLcode bufq_reader_cb(void *reader_ctx,
112
                              unsigned char *buf, size_t len,
113
                              size_t *pnread)
114
4.16M
{
115
4.16M
  struct reader_cb_ctx *ctx = (struct reader_cb_ctx *)reader_ctx;
116
117
4.16M
  *pnread = 0;
118
4.16M
  if (ctx->write_len <= 0)
119
6.67k
    return CURLE_AGAIN;
120
121
4.15M
  FV_PRINTF(ctx->verbose, "Reader CB: %zu space available, %zu pending\n", len, ctx->write_len);
122
123
4.15M
  *pnread = len > ctx->write_len ? ctx->write_len : len;
124
125
4.15M
  unsigned char *compare = compute_buffer(ctx->next_byte_write, ctx->template_buf);
126
4.15M
  memcpy(buf, compare, *pnread);
127
4.15M
  ctx->next_byte_write += *pnread;
128
4.15M
  ctx->write_len -= *pnread;
129
130
4.15M
  return CURLE_OK;
131
4.16M
}
132
133
/**
134
 * Function for handling the operations
135
 */
136
int fuzz_handle_bufq(FuzzedDataProvider *fuzz)
137
1.14k
{
138
1.14k
  static bool verbose = (getenv("FUZZ_VERBOSE") != NULL);
139
1.14k
  static unsigned char *template_buf = allocate_template_buffer();
140
141
1.14k
  struct bufq q;
142
1.14k
  struct bufc_pool pool;
143
144
  /* Prepare basic configuration values */
145
1.14k
  int max_chunks = fuzz->ConsumeIntegralInRange(1, FUZZ_MAX_CHUNKS_QTY);
146
1.14k
  int chunk_size = fuzz->ConsumeIntegralInRange(1, FUZZ_MAX_CHUNK_SIZE);
147
1.14k
  bool use_pool = fuzz->ConsumeBool();
148
1.14k
  bool no_spare = fuzz->ConsumeBool();
149
1.14k
  int max_spare = fuzz->ConsumeIntegralInRange(1, FUZZ_MAX_MAX_SPARE);
150
151
1.14k
  FV_PRINTF(verbose, "Begin fuzzing!\n");
152
153
1.14k
  if (use_pool) {
154
528
    FV_PRINTF(verbose, "Using pool init\n");
155
528
    Curl_bufcp_init(&pool, chunk_size, max_spare);
156
528
    Curl_bufq_initp(&q, &pool, max_chunks, no_spare ? BUFQ_OPT_NO_SPARES : BUFQ_OPT_NONE);
157
613
  } else {
158
613
    FV_PRINTF(verbose, "Using normal init\n");
159
613
    Curl_bufq_init(&q, chunk_size, max_chunks);
160
613
  }
161
162
1.14k
  size_t buffer_bytes = 0;
163
1.14k
  unsigned char next_byte_read = 0;
164
1.14k
  unsigned char next_byte_write = 0;
165
8.62M
  while (fuzz->remaining_bytes() > 0) {
166
8.62M
    CURLcode err = CURLE_OK;
167
8.62M
    uint32_t op_type = fuzz->ConsumeIntegralInRange(0, OP_TYPE_MAX);
168
169
8.62M
    assert(Curl_bufq_is_empty(&q) == !buffer_bytes);
170
8.62M
    assert(Curl_bufq_len(&q) == buffer_bytes);
171
172
8.62M
    switch (op_type) {
173
1.53M
      case OP_TYPE_RESET: {
174
1.53M
        FV_PRINTF(verbose, "OP: reset\n");
175
1.53M
        Curl_bufq_reset(&q);
176
1.53M
        buffer_bytes = 0;
177
1.53M
        next_byte_read = next_byte_write;
178
1.53M
        break;
179
0
      }
180
181
329k
      case OP_TYPE_PEEK: {
182
329k
        FV_PRINTF(verbose, "OP: peek\n");
183
329k
        const unsigned char *pbuf;
184
329k
        size_t plen;
185
329k
        bool avail = Curl_bufq_peek(&q, &pbuf, &plen);
186
329k
        if (avail) {
187
70.0k
          unsigned char *compare = compute_buffer(next_byte_read, template_buf);
188
70.0k
          assert(memcmp(pbuf, compare, plen) == 0);
189
259k
        } else {
190
259k
          FV_PRINTF(verbose, "OP: peek, error\n");
191
259k
        }
192
329k
        break;
193
329k
      }
194
195
329k
      case OP_TYPE_PEEK_AT: {
196
263k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
197
263k
        FV_PRINTF(verbose, "OP: peek at %zu\n", op_size);
198
263k
        const unsigned char *pbuf;
199
263k
        size_t plen;
200
263k
        bool avail = Curl_bufq_peek_at(&q, op_size, &pbuf, &plen);
201
263k
        if (avail) {
202
5.53k
          unsigned char *compare = compute_buffer(next_byte_read + op_size, template_buf);
203
5.53k
          assert(memcmp(pbuf, compare, plen) == 0);
204
258k
        } else {
205
258k
          FV_PRINTF(verbose, "OP: peek at, error\n");
206
258k
        }
207
263k
        break;
208
263k
      }
209
210
263k
      case OP_TYPE_READ: {
211
249k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
212
249k
        FV_PRINTF(verbose, "OP: read, size %zu\n", op_size);
213
249k
        unsigned char *buf = (unsigned char *)malloc(op_size * sizeof(*buf));
214
249k
        size_t read;
215
249k
        CURLcode result = Curl_bufq_read(&q, buf, op_size, &read);
216
249k
        if (!result) {
217
13.1k
          FV_PRINTF(verbose, "OP: read, success, read %zu, expect begins with %x\n", read, next_byte_read);
218
13.1k
          buffer_bytes -= read;
219
13.1k
          assert(buffer_bytes >= 0);
220
13.1k
          unsigned char *compare = compute_buffer(next_byte_read, template_buf);
221
13.1k
          next_byte_read += read;
222
13.1k
          assert(memcmp(buf, compare, read) == 0);
223
235k
        } else {
224
235k
          FV_PRINTF(verbose, "OP: read, error\n");
225
235k
        }
226
249k
        free(buf);
227
249k
        break;
228
249k
      }
229
230
911k
      case OP_TYPE_SLURP: {
231
911k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
232
911k
        FV_PRINTF(verbose, "OP: slurp, size %zd\n", op_size);
233
911k
        struct reader_cb_ctx ctx = { .verbose = verbose, .template_buf = template_buf, .write_len = op_size, .next_byte_write = next_byte_write };
234
911k
        size_t write;
235
911k
        CURLcode result = Curl_bufq_slurp(&q, bufq_reader_cb, &ctx, &write);
236
911k
        if (!result) {
237
21.8k
          FV_PRINTF(verbose, "OP: slurp, success, wrote %zu, expect begins with %x\n", write, ctx.next_byte_write);
238
21.8k
          buffer_bytes += write;
239
889k
        } else {
240
889k
          FV_PRINTF(verbose, "OP: slurp, error\n");
241
          /* in case of -1, it may still have wrote something, adjust for that */
242
889k
          buffer_bytes += (op_size - ctx.write_len);
243
889k
        }
244
911k
        assert(buffer_bytes <= chunk_size * max_chunks);
245
911k
        next_byte_write = ctx.next_byte_write;
246
911k
        break;
247
911k
      }
248
249
608k
      case OP_TYPE_SIPN: {
250
608k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
251
608k
        FV_PRINTF(verbose, "OP: sipn, size %zd\n", op_size);
252
608k
        struct reader_cb_ctx ctx = { .verbose = verbose, .template_buf = template_buf, .write_len = op_size, .next_byte_write = next_byte_write };
253
608k
        size_t write;
254
608k
        CURLcode result = Curl_bufq_sipn(&q, op_size, bufq_reader_cb, &ctx, &write);
255
608k
        if (!result) {
256
376k
          FV_PRINTF(verbose, "OP: sipn, success, wrote %zu, expect begins with %x\n", write, ctx.next_byte_write);
257
376k
          buffer_bytes += write;
258
376k
          assert(buffer_bytes <= chunk_size * max_chunks);
259
376k
          next_byte_write = ctx.next_byte_write;
260
376k
        } else {
261
231k
          FV_PRINTF(verbose, "OP: sipn, error\n");
262
231k
        }
263
608k
        break;
264
608k
      }
265
266
608k
      case OP_TYPE_PASS: {
267
584k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
268
584k
        FV_PRINTF(verbose, "OP: pass, size %zd\n", op_size);
269
584k
        struct writer_cb_ctx ctx = { .verbose = verbose, .template_buf = template_buf, .read_len = op_size, .next_byte_read = next_byte_read };
270
584k
        size_t nread;
271
584k
        CURLcode result = Curl_bufq_pass(&q, bufq_writer_cb, &ctx, &nread);
272
584k
        if (!result) {
273
583k
          FV_PRINTF(verbose, "OP: pass, success, read %zu, expect begins with %x\n", nread, ctx.next_byte_read);
274
583k
          buffer_bytes -= nread;
275
583k
        } else {
276
1.19k
          FV_PRINTF(verbose, "OP: pass, error\n");
277
          /* in case of -1, it may still have read something, adjust for that */
278
1.19k
          buffer_bytes -= (op_size - ctx.read_len);
279
1.19k
        }
280
584k
        assert(buffer_bytes >= 0);
281
584k
        next_byte_read = ctx.next_byte_read;
282
584k
        break;
283
584k
      }
284
285
352k
      case OP_TYPE_SKIP: {
286
352k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
287
352k
        FV_PRINTF(verbose, "OP: skip, size %zu\n", op_size);
288
352k
        Curl_bufq_skip(&q, op_size);
289
352k
        size_t old_buffer_bytes = buffer_bytes;
290
352k
        buffer_bytes = old_buffer_bytes > op_size ? old_buffer_bytes - op_size : 0;
291
352k
        next_byte_read += old_buffer_bytes > op_size ? op_size : old_buffer_bytes;
292
352k
        break;
293
584k
      }
294
295
3.79M
      case OP_TYPE_WRITE: {
296
3.79M
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
297
3.79M
        FV_PRINTF(verbose, "OP: write, size %zu, begins with %x\n", op_size, next_byte_write);
298
3.79M
        unsigned char *buf = compute_buffer(next_byte_write, template_buf);
299
3.79M
        size_t written;
300
3.79M
        CURLcode result = Curl_bufq_write(&q, buf, op_size, &written);
301
3.79M
        if (!result) {
302
3.70M
          FV_PRINTF(verbose, "OP: write, success, written %zu\n", written);
303
3.70M
          next_byte_write += written;
304
3.70M
          buffer_bytes += written;
305
3.70M
          assert(buffer_bytes <= chunk_size * max_chunks);
306
3.70M
        } else {
307
91.3k
          FV_PRINTF(verbose, "OP: write, error\n");
308
91.3k
        }
309
3.79M
        break;
310
3.79M
      }
311
312
3.79M
      default: {
313
        /* Should never happen */
314
0
        assert(false);
315
0
      }
316
8.62M
    }
317
8.62M
  }
318
319
1.14k
  Curl_bufq_free(&q);
320
1.14k
  if (use_pool)
321
528
  {
322
528
    Curl_bufcp_free(&pool);
323
528
  }
324
325
1.14k
  return 0;
326
1.14k
}
327
328
/**
329
 * Fuzzing entry point. This function is passed a buffer containing a test
330
 * case.  This test case should drive the cURL API into making a series of
331
 * BUFQ operations.
332
 */
333
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
334
1.14k
{
335
1.14k
  FuzzedDataProvider fuzzed_data(data, size);
336
337
  /* Ignore SIGPIPE errors. We'll handle the errors ourselves. */
338
1.14k
  signal(SIGPIPE, SIG_IGN);
339
340
  /* Run the operations */
341
1.14k
  fuzz_handle_bufq(&fuzzed_data);
342
343
  /* This function must always return 0. Non-zero codes are reserved. */
344
1.14k
  return 0;
345
1.14k
}