]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
fuzz: specify protocol with fuzz target name
authorPhilippe Antoine <contact@catenacyber.fr>
Mon, 29 Mar 2021 18:27:34 +0000 (20:27 +0200)
committerVictor Julien <vjulien@oisf.net>
Wed, 25 Jan 2023 19:26:37 +0000 (20:26 +0100)
cf https://redmine.openinfosecfoundation.org/issues/4125

This allows fuzz_applayerparser_parse to fuzz one specific
app-layer protocol based on the binary name, as is done
with the environment variable FUZZ_APPLAYER
That is if we rename/copy to fuzz_applayerparser_parse_smb,
it will fuzz only SMB protocol
This way, we can easily produce different fuzz targets for
each protocol in oss-fuzz

(cherry picked from commit e9b76a0e663ce5c2f8900f90a4f4fd7ed3436335)

src/tests/fuzz/fuzz_applayerparserparse.c

index 6d5a560cd7787e2f5b48e8bfad8dfdd0a20126e6..01886a6c977792e74dbdda0c24be99520129cb59 100644 (file)
@@ -16,6 +16,7 @@
 #define HEADER_LEN 6
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+int LLVMFuzzerInitialize(int *argc, char ***argv);
 
 AppLayerParserThreadCtx *alp_tctx = NULL;
 
@@ -35,6 +36,30 @@ const uint8_t separator[] = {0x01, 0xD5, 0xCA, 0x7A};
 SCInstance surifuzz;
 uint64_t forceLayer = 0;
 
+int LLVMFuzzerInitialize(int *argc, char ***argv)
+{
+    char *target_suffix = strrchr((*argv)[0], '_');
+    if (target_suffix != NULL) {
+        AppProto applayer = StringToAppProto(target_suffix + 1);
+        if (applayer != ALPROTO_UNKNOWN) {
+            forceLayer = applayer;
+            printf("Forcing %s=%" PRIu64 "\n", AppProtoToString(forceLayer), forceLayer);
+            return 0;
+        }
+    }
+    // else
+    const char *forceLayerStr = getenv("FUZZ_APPLAYER");
+    if (forceLayerStr) {
+        if (ByteExtractStringUint64(&forceLayer, 10, 0, forceLayerStr) < 0) {
+            forceLayer = 0;
+            printf("Invalid numeric value for FUZZ_APPLAYER environment variable");
+        } else {
+            printf("Forcing %s\n", AppProtoToString(forceLayer));
+        }
+    }
+    return 0;
+}
+
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 {
     Flow * f;
@@ -68,15 +93,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 
         PostConfLoadedSetup(&surifuzz);
         alp_tctx = AppLayerParserThreadCtxAlloc();
-        const char* forceLayerStr = getenv("FUZZ_APPLAYER");
-        if (forceLayerStr) {
-            if (ByteExtractStringUint64(&forceLayer, 10, 0, forceLayerStr) < 0) {
-                forceLayer = 0;
-                printf("Invalid numeric value for FUZZ_APPLAYER environment variable");
-            } else {
-                printf("Forcing %s\n", AppProtoToString(forceLayer));
-            }
-        }
     }
 
     if (data[0] >= ALPROTO_MAX) {