Skip to content

Commit d765b6f

Browse files
haproxy: fix false positives (#15306)
Signed-off-by: David Korczynski <david@adalogics.com>
1 parent 49f2933 commit d765b6f

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

projects/haproxy/fuzz_cfg_parser.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@
1818

1919
#include <haproxy/cfgparse.h>
2020
#include <haproxy/chunk.h>
21+
#include <haproxy/global.h>
2122

2223
#include <stdint.h>
2324
#include <stdio.h>
2425
#include <stdlib.h>
2526
#include <unistd.h>
2627

27-
/* trash is a global scratch buffer used throughout haproxy (e.g. in
28-
* make_arg_list). Normal startup initialises it via init_trash_buffers(),
29-
* but the fuzzer bypasses the full init sequence, so we allocate it once
30-
* here to avoid a NULL-pointer dereference.
31-
*/
32-
extern THREAD_LOCAL struct buffer trash;
33-
3428
#define FUZZ_TRASH_SIZE 65536
3529

3630
static int trash_initialized = 0;
3731

3832
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33+
/* One-time init: use init_trash_buffers() to properly initialize all trash
34+
* buffers (trash, trash_buf1, trash_buf2 and their large/small variants).
35+
* This mirrors haproxy's alloc_early_trash + alloc_trash_buffers_per_thread
36+
* startup sequence. */
3937
if (!trash_initialized) {
40-
chunk_init(&trash, malloc(FUZZ_TRASH_SIZE), FUZZ_TRASH_SIZE);
41-
if (!trash.area)
38+
global.tune.bufsize = FUZZ_TRASH_SIZE;
39+
global.tune.bufsize_large = FUZZ_TRASH_SIZE * 2;
40+
global.tune.bufsize_small = 1024;
41+
if (!init_trash_buffers(1))
42+
return 0;
43+
if (!init_trash_buffers(0))
4244
return 0;
4345
trash_initialized = 1;
4446
}

projects/haproxy/fuzz_h1_htx.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <haproxy/http-hdr.h>
4141
#include <haproxy/global.h>
4242
#include <haproxy/buf.h>
43+
#include <haproxy/chunk.h>
4344

4445
#include <stdint.h>
4546
#include <string.h>
@@ -49,14 +50,8 @@
4950
#define HTX_BUF_SIZE 65536
5051
#define TRASH_BUF_SIZE 65536
5152

52-
/* trash is a global scratch buffer used in h1_parse_msg_hdrs (via
53-
* b_slow_realign_ofs). Normal startup initialises it, but the fuzzer
54-
* bypasses the full init sequence. */
55-
extern THREAD_LOCAL struct buffer trash;
56-
5753
static int fuzz_initialized = 0;
5854
static char *htx_area = NULL;
59-
static char *trash_area = NULL;
6055

6156
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
6257
struct h1m h1m;
@@ -71,14 +66,25 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
7166
if (size < 2)
7267
return 0;
7368

74-
/* One-time init */
69+
/* One-time init: replicate the trash buffer setup that haproxy normally
70+
* performs at startup (alloc_early_trash + alloc_trash_buffers_per_thread).
71+
* init_trash_buffers() is the public entry point that creates the pool and
72+
* allocates the two alternating trash buffers (trash_buf1/trash_buf2) plus
73+
* the scratch buffer (trash) used throughout haproxy. The first call with
74+
* first=1 mirrors alloc_early_trash(); the second with first=0 also creates
75+
* the large and small trash pools and their per-thread buffers. */
7576
if (!fuzz_initialized) {
7677
htx_area = malloc(HTX_BUF_SIZE);
77-
trash_area = malloc(TRASH_BUF_SIZE);
78-
if (!htx_area || !trash_area)
78+
if (!htx_area)
7979
return 0;
80-
chunk_init(&trash, trash_area, TRASH_BUF_SIZE);
80+
global.tune.bufsize = TRASH_BUF_SIZE;
81+
global.tune.bufsize_large = TRASH_BUF_SIZE * 2;
82+
global.tune.bufsize_small = 1024;
8183
global.tune.max_http_hdr = MAX_HDR_NUM;
84+
if (!init_trash_buffers(1))
85+
return 0;
86+
if (!init_trash_buffers(0))
87+
return 0;
8288
fuzz_initialized = 1;
8389
}
8490

0 commit comments

Comments
 (0)