dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
594167
From 0235f9ea3d221aba513f4b6215418bf554e02791 Mon Sep 17 00:00:00 2001
594167
From: Evgeny Vereshchagin <evvers@ya.ru>
594167
Date: Mon, 3 Jan 2022 12:31:07 +0000
594167
Subject: [PATCH] fuzz: no longer skip empty files
594167
594167
Empty files and empty strings seem to have triggered various
594167
issues in the past so it seems they shouldn't be ignore by the
594167
fuzzers just because fmemopen can't handle them.
594167
594167
Prompted by https://github.com/systemd/systemd/pull/21939#issuecomment-1003113669
594167
594167
(cherry picked from commit 5df66d7d68006615abb4c4d3b1ebad545af4dd72)
594167
Related: #2087652
594167
---
594167
 src/core/fuzz-unit-file.c         | 6 +-----
594167
 src/fuzz/fuzz-env-file.c          | 5 ++---
594167
 src/fuzz/fuzz-hostname-setup.c    | 6 +-----
594167
 src/fuzz/fuzz-json.c              | 6 +-----
594167
 src/fuzz/fuzz.h                   | 9 +++++++++
594167
 src/nspawn/fuzz-nspawn-oci.c      | 6 +-----
594167
 src/nspawn/fuzz-nspawn-settings.c | 6 +-----
594167
 7 files changed, 16 insertions(+), 28 deletions(-)
594167
594167
diff --git a/src/core/fuzz-unit-file.c b/src/core/fuzz-unit-file.c
594167
index aef29f4cf7..780dd3988d 100644
594167
--- a/src/core/fuzz-unit-file.c
594167
+++ b/src/core/fuzz-unit-file.c
594167
@@ -2,7 +2,6 @@
594167
 
594167
 #include "conf-parser.h"
594167
 #include "fd-util.h"
594167
-#include "fileio.h"
594167
 #include "fuzz.h"
594167
 #include "install.h"
594167
 #include "load-fragment.h"
594167
@@ -22,10 +21,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         const char *name;
594167
         long offset;
594167
 
594167
-        if (size == 0)
594167
-                return 0;
594167
-
594167
-        f = fmemopen_unlocked((char*) data, size, "re");
594167
+        f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
         if (read_line(f, LINE_MAX, &p) < 0)
594167
diff --git a/src/fuzz/fuzz-env-file.c b/src/fuzz/fuzz-env-file.c
594167
index e0dac260b0..3b3e625608 100644
594167
--- a/src/fuzz/fuzz-env-file.c
594167
+++ b/src/fuzz/fuzz-env-file.c
594167
@@ -4,7 +4,6 @@
594167
 
594167
 #include "alloc-util.h"
594167
 #include "env-file.h"
594167
-#include "fileio.h"
594167
 #include "fd-util.h"
594167
 #include "fuzz.h"
594167
 #include "strv.h"
594167
@@ -13,10 +12,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         _cleanup_fclose_ FILE *f = NULL;
594167
         _cleanup_strv_free_ char **rl = NULL, **rlp =  NULL;
594167
 
594167
-        if (size == 0 || size > 65535)
594167
+        if (size > 65535)
594167
                 return 0;
594167
 
594167
-        f = fmemopen_unlocked((char*) data, size, "re");
594167
+        f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
         /* We don't want to fill the logs with messages about parse errors.
594167
diff --git a/src/fuzz/fuzz-hostname-setup.c b/src/fuzz/fuzz-hostname-setup.c
594167
index b8d36da54a..d7c23eef12 100644
594167
--- a/src/fuzz/fuzz-hostname-setup.c
594167
+++ b/src/fuzz/fuzz-hostname-setup.c
594167
@@ -2,7 +2,6 @@
594167
 
594167
 #include "alloc-util.h"
594167
 #include "fd-util.h"
594167
-#include "fileio.h"
594167
 #include "fuzz.h"
594167
 #include "hostname-setup.h"
594167
 
594167
@@ -10,10 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         _cleanup_fclose_ FILE *f = NULL;
594167
         _cleanup_free_ char *ret = NULL;
594167
 
594167
-        if (size == 0)
594167
-                return 0;
594167
-
594167
-        f = fmemopen_unlocked((char*) data, size, "re");
594167
+        f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
         /* We don't want to fill the logs with messages about parse errors.
594167
diff --git a/src/fuzz/fuzz-json.c b/src/fuzz/fuzz-json.c
594167
index f9a0e818c4..ad7460c6fd 100644
594167
--- a/src/fuzz/fuzz-json.c
594167
+++ b/src/fuzz/fuzz-json.c
594167
@@ -1,7 +1,6 @@
594167
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
 
594167
 #include "alloc-util.h"
594167
-#include "fileio.h"
594167
 #include "fd-util.h"
594167
 #include "fuzz.h"
594167
 #include "json.h"
594167
@@ -12,10 +11,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         _cleanup_fclose_ FILE *f = NULL, *g = NULL;
594167
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
594167
 
594167
-        if (size == 0)
594167
-                return 0;
594167
-
594167
-        f = fmemopen_unlocked((char*) data, size, "re");
594167
+        f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
         if (json_parse_file(f, NULL, 0, &v, NULL, NULL) < 0)
594167
diff --git a/src/fuzz/fuzz.h b/src/fuzz/fuzz.h
594167
index 579b0eed73..d7cbb0bb16 100644
594167
--- a/src/fuzz/fuzz.h
594167
+++ b/src/fuzz/fuzz.h
594167
@@ -4,5 +4,14 @@
594167
 #include <stddef.h>
594167
 #include <stdint.h>
594167
 
594167
+#include "fileio.h"
594167
+
594167
 /* The entry point into the fuzzer */
594167
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
594167
+
594167
+static inline FILE* data_to_file(const uint8_t *data, size_t size) {
594167
+        if (size == 0)
594167
+                return fopen("/dev/null", "re");
594167
+        else
594167
+                return fmemopen_unlocked((char*) data, size, "re");
594167
+}
594167
diff --git a/src/nspawn/fuzz-nspawn-oci.c b/src/nspawn/fuzz-nspawn-oci.c
594167
index cfebf65c00..91f2a81dfc 100644
594167
--- a/src/nspawn/fuzz-nspawn-oci.c
594167
+++ b/src/nspawn/fuzz-nspawn-oci.c
594167
@@ -2,7 +2,6 @@
594167
 
594167
 #include "alloc-util.h"
594167
 #include "fd-util.h"
594167
-#include "fileio.h"
594167
 #include "fuzz.h"
594167
 #include "nspawn-oci.h"
594167
 
594167
@@ -10,10 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         _cleanup_fclose_ FILE *f = NULL;
594167
         _cleanup_(settings_freep) Settings *s = NULL;
594167
 
594167
-        if (size == 0)
594167
-                return 0;
594167
-
594167
-        f = fmemopen_unlocked((char*) data, size, "re");
594167
+        f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
         /* We don't want to fill the logs with messages about parse errors.
594167
diff --git a/src/nspawn/fuzz-nspawn-settings.c b/src/nspawn/fuzz-nspawn-settings.c
594167
index bd98ed26e8..6b91e1506e 100644
594167
--- a/src/nspawn/fuzz-nspawn-settings.c
594167
+++ b/src/nspawn/fuzz-nspawn-settings.c
594167
@@ -2,7 +2,6 @@
594167
 
594167
 #include "alloc-util.h"
594167
 #include "fd-util.h"
594167
-#include "fileio.h"
594167
 #include "fuzz.h"
594167
 #include "nspawn-settings.h"
594167
 
594167
@@ -10,10 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         _cleanup_fclose_ FILE *f = NULL;
594167
         _cleanup_(settings_freep) Settings *s = NULL;
594167
 
594167
-        if (size == 0)
594167
-                return 0;
594167
-
594167
-        f = fmemopen_unlocked((char*) data, size, "re");
594167
+        f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
         /* We don't want to fill the logs with messages about parse errors.