]> 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 <victor@inliniac.net>
Fri, 9 Apr 2021 09:33:08 +0000 (11:33 +0200)
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

src/tests/fuzz/fuzz_applayerparserparse.c

index 11498be535e2ef28d1259869262c5a0c2e87bb7a..3a10763abb008307924b8bcd6553011f50a4d220 100644 (file)
@@ -15,6 +15,7 @@
 #define HEADER_LEN 6
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+int LLVMFuzzerInitialize(int *argc, char ***argv);
 
 AppLayerParserThreadCtx *alp_tctx = NULL;
 
@@ -34,6 +35,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;
@@ -67,15 +92,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) {