Coverage Report

Created: 2025-10-10 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/fuzz_bufq.cc
Line
Count
Source
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
11.9M
static unsigned char *compute_buffer(unsigned char next_byte, unsigned char *buf) {
66
11.9M
  return buf + next_byte;
67
11.9M
}
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
3.03M
{
83
3.03M
  struct writer_cb_ctx *ctx = (struct writer_cb_ctx *)writer_ctx;
84
85
3.03M
  *pnwritten = 0;
86
3.03M
  if (ctx->read_len <= 0)
87
5.89k
    return CURLE_AGAIN;
88
89
3.03M
  FV_PRINTF(ctx->verbose, "Writer CB: %zu space available, %zu pending\n", len, ctx->read_len);
90
91
3.03M
  *pnwritten = len > ctx->read_len ? ctx->read_len : len;
92
93
3.03M
  unsigned char *compare = compute_buffer(ctx->next_byte_read, ctx->template_buf);
94
3.03M
  assert(memcmp(buf, compare, *pnwritten) == 0);
95
3.03M
  ctx->next_byte_read += *pnwritten;
96
3.03M
  ctx->read_len -= *pnwritten;
97
98
3.03M
  return CURLE_OK;
99
3.03M
}
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.78M
{
115
4.78M
  struct reader_cb_ctx *ctx = (struct reader_cb_ctx *)reader_ctx;
116
117
4.78M
  *pnread = 0;
118
4.78M
  if (ctx->write_len <= 0)
119
7.76k
    return CURLE_AGAIN;
120
121
4.78M
  FV_PRINTF(ctx->verbose, "Reader CB: %zu space available, %zu pending\n", len, ctx->write_len);
122
123
4.78M
  *pnread = len > ctx->write_len ? ctx->write_len : len;
124
125
4.78M
  unsigned char *compare = compute_buffer(ctx->next_byte_write, ctx->template_buf);
126
4.78M
  memcpy(buf, compare, *pnread);
127
4.78M
  ctx->next_byte_write += *pnread;
128
4.78M
  ctx->write_len -= *pnread;
129
130
4.78M
  return CURLE_OK;
131
4.78M
}
132
133
/**
134
 * Function for handling the operations
135
 */
136
int fuzz_handle_bufq(FuzzedDataProvider *fuzz)
137
1.11k
{
138
1.11k
  static bool verbose = (getenv("FUZZ_VERBOSE") != NULL);
139
1.11k
  static unsigned char *template_buf = allocate_template_buffer();
140
141
1.11k
  struct bufq q;
142
1.11k
  struct bufc_pool pool;
143
144
  /* Prepare basic configuration values */
145
1.11k
  int max_chunks = fuzz->ConsumeIntegralInRange(1, FUZZ_MAX_CHUNKS_QTY);
146
1.11k
  int chunk_size = fuzz->ConsumeIntegralInRange(1, FUZZ_MAX_CHUNK_SIZE);
147
1.11k
  bool use_pool = fuzz->ConsumeBool();
148
1.11k
  bool no_spare = fuzz->ConsumeBool();
149
1.11k
  int max_spare = fuzz->ConsumeIntegralInRange(1, FUZZ_MAX_MAX_SPARE);
150
151
1.11k
  FV_PRINTF(verbose, "Begin fuzzing!\n");
152
153
1.11k
  if (use_pool) {
154
516
    FV_PRINTF(verbose, "Using pool init\n");
155
516
    Curl_bufcp_init(&pool, chunk_size, max_spare);
156
516
    Curl_bufq_initp(&q, &pool, max_chunks, no_spare ? BUFQ_OPT_NO_SPARES : BUFQ_OPT_NONE);
157
595
  } else {
158
595
    FV_PRINTF(verbose, "Using normal init\n");
159
595
    Curl_bufq_init(&q, chunk_size, max_chunks);
160
595
  }
161
162
1.11k
  size_t buffer_bytes = 0;
163
1.11k
  unsigned char next_byte_read = 0;
164
1.11k
  unsigned char next_byte_write = 0;
165
10.1M
  while (fuzz->remaining_bytes() > 0) {
166
10.1M
    CURLcode err = CURLE_OK;
167
10.1M
    uint32_t op_type = fuzz->ConsumeIntegralInRange(0, OP_TYPE_MAX);
168
169
10.1M
    assert(Curl_bufq_is_empty(&q) == !buffer_bytes);
170
10.1M
    assert(Curl_bufq_len(&q) == buffer_bytes);
171
172
10.1M
    switch (op_type) {
173
2.51M
      case OP_TYPE_RESET: {
174
2.51M
        FV_PRINTF(verbose, "OP: reset\n");
175
2.51M
        Curl_bufq_reset(&q);
176
2.51M
        buffer_bytes = 0;
177
2.51M
        next_byte_read = next_byte_write;
178
2.51M
        break;
179
0
      }
180
181
489k
      case OP_TYPE_PEEK: {
182
489k
        FV_PRINTF(verbose, "OP: peek\n");
183
489k
        const unsigned char *pbuf;
184
489k
        size_t plen;
185
489k
        bool avail = Curl_bufq_peek(&q, &pbuf, &plen);
186
489k
        if (avail) {
187
78.4k
          unsigned char *compare = compute_buffer(next_byte_read, template_buf);
188
78.4k
          assert(memcmp(pbuf, compare, plen) == 0);
189
411k
        } else {
190
411k
          FV_PRINTF(verbose, "OP: peek, error\n");
191
411k
        }
192
489k
        break;
193
489k
      }
194
195
489k
      case OP_TYPE_PEEK_AT: {
196
228k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
197
228k
        FV_PRINTF(verbose, "OP: peek at %zu\n", op_size);
198
228k
        const unsigned char *pbuf;
199
228k
        size_t plen;
200
228k
        bool avail = Curl_bufq_peek_at(&q, op_size, &pbuf, &plen);
201
228k
        if (avail) {
202
4.08k
          unsigned char *compare = compute_buffer(next_byte_read + op_size, template_buf);
203
4.08k
          assert(memcmp(pbuf, compare, plen) == 0);
204
224k
        } else {
205
224k
          FV_PRINTF(verbose, "OP: peek at, error\n");
206
224k
        }
207
228k
        break;
208
228k
      }
209
210
244k
      case OP_TYPE_READ: {
211
244k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
212
244k
        FV_PRINTF(verbose, "OP: read, size %zu\n", op_size);
213
244k
        unsigned char *buf = (unsigned char *)malloc(op_size * sizeof(*buf));
214
244k
        size_t read;
215
244k
        CURLcode result = Curl_bufq_read(&q, buf, op_size, &read);
216
244k
        if (!result) {
217
13.4k
          FV_PRINTF(verbose, "OP: read, success, read %zu, expect begins with %x\n", read, next_byte_read);
218
13.4k
          buffer_bytes -= read;
219
13.4k
          assert(buffer_bytes >= 0);
220
13.4k
          unsigned char *compare = compute_buffer(next_byte_read, template_buf);
221
13.4k
          next_byte_read += read;
222
13.4k
          assert(memcmp(buf, compare, read) == 0);
223
230k
        } else {
224
230k
          FV_PRINTF(verbose, "OP: read, error\n");
225
230k
        }
226
244k
        free(buf);
227
244k
        break;
228
244k
      }
229
230
353k
      case OP_TYPE_SLURP: {
231
353k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
232
353k
        FV_PRINTF(verbose, "OP: slurp, size %zd\n", op_size);
233
353k
        struct reader_cb_ctx ctx = { .verbose = verbose, .template_buf = template_buf, .write_len = op_size, .next_byte_write = next_byte_write };
234
353k
        size_t write;
235
353k
        CURLcode result = Curl_bufq_slurp(&q, bufq_reader_cb, &ctx, &write);
236
353k
        if (!result) {
237
14.5k
          FV_PRINTF(verbose, "OP: slurp, success, wrote %zu, expect begins with %x\n", write, ctx.next_byte_write);
238
14.5k
          buffer_bytes += write;
239
338k
        } else {
240
338k
          FV_PRINTF(verbose, "OP: slurp, error\n");
241
          /* in case of -1, it may still have wrote something, adjust for that */
242
338k
          buffer_bytes += (op_size - ctx.write_len);
243
338k
        }
244
353k
        assert(buffer_bytes <= chunk_size * max_chunks);
245
353k
        next_byte_write = ctx.next_byte_write;
246
353k
        break;
247
353k
      }
248
249
895k
      case OP_TYPE_SIPN: {
250
895k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
251
895k
        FV_PRINTF(verbose, "OP: sipn, size %zd\n", op_size);
252
895k
        struct reader_cb_ctx ctx = { .verbose = verbose, .template_buf = template_buf, .write_len = op_size, .next_byte_write = next_byte_write };
253
895k
        size_t write;
254
895k
        CURLcode result = Curl_bufq_sipn(&q, op_size, bufq_reader_cb, &ctx, &write);
255
895k
        if (!result) {
256
464k
          FV_PRINTF(verbose, "OP: sipn, success, wrote %zu, expect begins with %x\n", write, ctx.next_byte_write);
257
464k
          buffer_bytes += write;
258
464k
          assert(buffer_bytes <= chunk_size * max_chunks);
259
464k
          next_byte_write = ctx.next_byte_write;
260
464k
        } else {
261
430k
          FV_PRINTF(verbose, "OP: sipn, error\n");
262
430k
        }
263
895k
        break;
264
895k
      }
265
266
895k
      case OP_TYPE_PASS: {
267
637k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
268
637k
        FV_PRINTF(verbose, "OP: pass, size %zd\n", op_size);
269
637k
        struct writer_cb_ctx ctx = { .verbose = verbose, .template_buf = template_buf, .read_len = op_size, .next_byte_read = next_byte_read };
270
637k
        size_t nread;
271
637k
        CURLcode result = Curl_bufq_pass(&q, bufq_writer_cb, &ctx, &nread);
272
637k
        if (!result) {
273
636k
          FV_PRINTF(verbose, "OP: pass, success, read %zu, expect begins with %x\n", nread, ctx.next_byte_read);
274
636k
          buffer_bytes -= nread;
275
636k
        } else {
276
1.25k
          FV_PRINTF(verbose, "OP: pass, error\n");
277
          /* in case of -1, it may still have read something, adjust for that */
278
1.25k
          buffer_bytes -= (op_size - ctx.read_len);
279
1.25k
        }
280
637k
        assert(buffer_bytes >= 0);
281
637k
        next_byte_read = ctx.next_byte_read;
282
637k
        break;
283
637k
      }
284
285
767k
      case OP_TYPE_SKIP: {
286
767k
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
287
767k
        FV_PRINTF(verbose, "OP: skip, size %zu\n", op_size);
288
767k
        Curl_bufq_skip(&q, op_size);
289
767k
        size_t old_buffer_bytes = buffer_bytes;
290
767k
        buffer_bytes = old_buffer_bytes > op_size ? old_buffer_bytes - op_size : 0;
291
767k
        next_byte_read += old_buffer_bytes > op_size ? op_size : old_buffer_bytes;
292
767k
        break;
293
637k
      }
294
295
4.02M
      case OP_TYPE_WRITE: {
296
4.02M
        size_t op_size = fuzz->ConsumeIntegralInRange(0, FUZZ_MAX_RW_SIZE);
297
4.02M
        FV_PRINTF(verbose, "OP: write, size %zu, begins with %x\n", op_size, next_byte_write);
298
4.02M
        unsigned char *buf = compute_buffer(next_byte_write, template_buf);
299
4.02M
        size_t written;
300
4.02M
        CURLcode result = Curl_bufq_write(&q, buf, op_size, &written);
301
4.02M
        if (!result) {
302
3.96M
          FV_PRINTF(verbose, "OP: write, success, written %zu\n", written);
303
3.96M
          next_byte_write += written;
304
3.96M
          buffer_bytes += written;
305
3.96M
          assert(buffer_bytes <= chunk_size * max_chunks);
306
3.96M
        } else {
307
65.0k
          FV_PRINTF(verbose, "OP: write, error\n");
308
65.0k
        }
309
4.02M
        break;
310
4.02M
      }
311
312
4.02M
      default: {
313
        /* Should never happen */
314
0
        assert(false);
315
0
      }
316
10.1M
    }
317
10.1M
  }
318
319
1.11k
  Curl_bufq_free(&q);
320
1.11k
  if (use_pool)
321
516
  {
322
516
    Curl_bufcp_free(&pool);
323
516
  }
324
325
1.11k
  return 0;
326
1.11k
}
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.11k
{
335
1.11k
  FuzzedDataProvider fuzzed_data(data, size);
336
337
  /* Ignore SIGPIPE errors. We'll handle the errors ourselves. */
338
1.11k
  signal(SIGPIPE, SIG_IGN);
339
340
  /* Run the operations */
341
1.11k
  fuzz_handle_bufq(&fuzzed_data);
342
343
  /* This function must always return 0. Non-zero codes are reserved. */
344
1.11k
  return 0;
345
1.11k
}