Skip to content

Commit f5ec79f

Browse files
a1xndrhuth
authored andcommitted
fuzz: Expect the cmdline in a freeable GString
In the initial FuzzTarget, get_init_cmdline returned a char *. With this API, we had no guarantee about where the string came from. For example, i440fx-qtest-reboot-fuzz simply returned a pointer to a string literal, while the QOS-based targets build the arguments out in a GString an return the gchar *str pointer. Since we did not try to free the cmdline, we have a leak for any targets that do not simply return string literals. Clean up this mess by forcing fuzz-targets to return a GString, that we can free. Signed-off-by: Alexander Bulekov <[email protected]> Message-Id: <[email protected]> Reviewed-by: Darren Kenny <[email protected]> Signed-off-by: Thomas Huth <[email protected]>
1 parent 15c51f7 commit f5ec79f

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

tests/qtest/fuzz/fuzz.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,15 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp)
199199
}
200200

201201
/* Run QEMU's softmmu main with the fuzz-target dependent arguments */
202-
const char *init_cmdline = fuzz_target->get_init_cmdline(fuzz_target);
203-
init_cmdline = g_strdup_printf("%s -qtest /dev/null -qtest-log %s",
204-
init_cmdline,
205-
getenv("QTEST_LOG") ? "/dev/fd/2"
206-
: "/dev/null");
207-
202+
GString *cmd_line = fuzz_target->get_init_cmdline(fuzz_target);
203+
g_string_append_printf(cmd_line,
204+
" -qtest /dev/null -qtest-log %s",
205+
getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null");
208206

209207
/* Split the runcmd into an argv and argc */
210208
wordexp_t result;
211-
wordexp(init_cmdline, &result, 0);
209+
wordexp(cmd_line->str, &result, 0);
210+
g_string_free(cmd_line, true);
212211

213212
qemu_init(result.we_wordc, result.we_wordv, NULL);
214213

tests/qtest/fuzz/fuzz.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ typedef struct FuzzTarget {
5050

5151

5252
/*
53-
* returns the arg-list that is passed to qemu/softmmu init()
54-
* Cannot be NULL
53+
* Returns the arguments that are passed to qemu/softmmu init(). Freed by
54+
* the caller.
5555
*/
56-
const char* (*get_init_cmdline)(struct FuzzTarget *);
56+
GString *(*get_init_cmdline)(struct FuzzTarget *);
5757

5858
/*
5959
* will run once, prior to running qemu/softmmu init.

tests/qtest/fuzz/i440fx_fuzz.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ static void i440fx_fuzz_qos_fork(QTestState *s,
158158

159159
static const char *i440fx_qtest_argv = TARGET_NAME " -machine accel=qtest"
160160
" -m 0 -display none";
161-
static const char *i440fx_argv(FuzzTarget *t)
161+
static GString *i440fx_argv(FuzzTarget *t)
162162
{
163-
return i440fx_qtest_argv;
163+
return g_string_new(i440fx_qtest_argv);
164164
}
165165

166166
static void fork_init(void)

tests/qtest/fuzz/qos_fuzz.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void *qos_allocate_objects(QTestState *qts, QGuestAllocator **p_alloc)
6666
return allocate_objects(qts, current_path + 1, p_alloc);
6767
}
6868

69-
static const char *qos_build_main_args(void)
69+
static GString *qos_build_main_args(void)
7070
{
7171
char **path = fuzz_path_vec;
7272
QOSGraphNode *test_node;
@@ -88,7 +88,7 @@ static const char *qos_build_main_args(void)
8888
/* Prepend the arguments that we need */
8989
g_string_prepend(cmd_line,
9090
TARGET_NAME " -display none -machine accel=qtest -m 64 ");
91-
return cmd_line->str;
91+
return cmd_line;
9292
}
9393

9494
/*
@@ -189,7 +189,7 @@ static void walk_path(QOSGraphNode *orig_path, int len)
189189
g_free(path_str);
190190
}
191191

192-
static const char *qos_get_cmdline(FuzzTarget *t)
192+
static GString *qos_get_cmdline(FuzzTarget *t)
193193
{
194194
/*
195195
* Set a global variable that we use to identify the qos_path for our

0 commit comments

Comments
 (0)