From a35e324d9245835abb07166910ffc9ec9d690038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20R=C3=BChsen?= Date: Sat, 5 Jan 2019 21:10:04 +0100 Subject: [PATCH] Add libidn2_register_fuzzer and corpora --- fuzz/Makefile.am | 4 +- fuzz/libidn2_register_fuzzer.c | 68 ++++++++++++++++++++++++++++++++++ lib/context.c | 24 ++++++++---- 3 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 fuzz/libidn2_register_fuzzer.c diff --git a/fuzz/Makefile.am b/fuzz/Makefile.am index bc55d30..6f6d7c8 100644 --- a/fuzz/Makefile.am +++ b/fuzz/Makefile.am @@ -6,12 +6,14 @@ LDADD = ../lib/libidn2.la ../gl/libgnu.la $(LTLIBUNISTRING) IDN_TESTS = \ libidn2_to_ascii_8z_fuzzer$(EXEEXT) \ - libidn2_to_unicode_8z8z_fuzzer$(EXEEXT) + libidn2_to_unicode_8z8z_fuzzer$(EXEEXT) \ + libidn2_register_fuzzer$(EXEEXT) check_PROGRAMS = $(IDN_TESTS) libidn2_to_ascii_8z_fuzzer_SOURCES = libidn2_to_ascii_8z_fuzzer.c main.c fuzzer.h libidn2_to_unicode_8z8z_fuzzer_SOURCES = libidn2_to_unicode_8z8z_fuzzer.c main.c fuzzer.h +libidn2_register_fuzzer_SOURCES = libidn2_register_fuzzer.c main.c fuzzer.h dist-hook: find . -name '*.options' -exec cp -v '{}' $(distdir) ';' diff --git a/fuzz/libidn2_register_fuzzer.c b/fuzz/libidn2_register_fuzzer.c new file mode 100644 index 0000000..7164a93 --- /dev/null +++ b/fuzz/libidn2_register_fuzzer.c @@ -0,0 +1,68 @@ +/* + * Copyright(c) 2019 Tim Ruehsen + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This file is part of libidn2. + */ + +#include + +#include /* assert */ +#include /* malloc, free */ +#include /* memcpy */ + +#include "idn2.h" +#include "fuzzer.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + char *ulabel, *alabel; + char *out; + + if (size > 1024) + return 0; + + ulabel = (char *) malloc(size + 1); + assert(ulabel != NULL); + + /* 0 terminate */ + memcpy(ulabel, data, size); + ulabel[size] = 0; + + if (idn2_register_ul(ulabel, NULL, &out, 0) == IDNA_SUCCESS) + idn2_free(out); + + free(ulabel); + + alabel = (char *) malloc(size + 4 + 1); + assert(alabel != NULL); + + /* 0 terminate */ + memcpy(alabel, "xn--", 4); + memcpy(alabel + 4, data, size); + alabel[size] = 0; + + if (idn2_register_ul(NULL, alabel, &out, 0) == IDNA_SUCCESS) + idn2_free(out); + + free(alabel); + + return 0; +} diff --git a/lib/context.c b/lib/context.c index 991ec9f..1ee9ba3 100644 --- a/lib/context.c +++ b/lib/context.c @@ -29,11 +29,8 @@ #include #include "idn2.h" - #include "tables.h" - #include /* uc_combining_class, UC_CCC_VR */ - #include "context.h" int @@ -115,6 +112,17 @@ _idn2_contextj_rule (const uint32_t * label, size_t llen, size_t pos) return IDN2_CONTEXTJ_NO_RULE; } +static inline const char * +_uc_script_name (ucs4_t uc) +{ + const uc_script_t *ucs = uc_script(uc); + + if (!ucs) + return ""; + + return ucs->name; +} + int _idn2_contexto_rule (const uint32_t * label, size_t llen, size_t pos) { @@ -140,7 +148,7 @@ _idn2_contexto_rule (const uint32_t * label, size_t llen, size_t pos) /* GREEK LOWER NUMERAL SIGN (KERAIA) */ if (pos == llen - 1) return IDN2_CONTEXTO; - if (strcmp (uc_script (label[pos + 1])->name, "Greek") == 0) + if (strcmp (_uc_script_name (label[pos + 1]), "Greek") == 0) return IDN2_OK; return IDN2_CONTEXTO; break; @@ -151,7 +159,7 @@ _idn2_contexto_rule (const uint32_t * label, size_t llen, size_t pos) /* HEBREW PUNCTUATION GERSHAYIM */ if (pos == 0) return IDN2_CONTEXTO; - if (strcmp (uc_script (label[pos - 1])->name, "Hebrew") == 0) + if (strcmp (_uc_script_name (label[pos - 1]), "Hebrew") == 0) return IDN2_OK; return IDN2_CONTEXTO; break; @@ -202,9 +210,9 @@ _idn2_contexto_rule (const uint32_t * label, size_t llen, size_t pos) bool script_ok = false; for (i = 0; !script_ok && i < llen; i++) - if (strcmp (uc_script (label[i])->name, "Hiragana") == 0 - || strcmp (uc_script (label[i])->name, "Katakana") == 0 - || strcmp (uc_script (label[i])->name, "Han") == 0) + if (strcmp (_uc_script_name (label[i]), "Hiragana") == 0 + || strcmp (_uc_script_name (label[i]), "Katakana") == 0 + || strcmp (_uc_script_name (label[i]), "Han") == 0) script_ok = true; if (script_ok) -- 2.19.1