From d42895daebbb86e540c0c6f8fbc6fe79cd4055b7 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Mon, 16 Nov 2026 10:22:28 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/corpus targets keyvalue handling Signed-off-by: Arthur Chan --- .../config/xmlsec_keyvalue_fuzzer.dict | 22 ++ .../config/xmlsec_keyvalue_fuzzer.options | 3 + .../corpus/xmlsec_keyvalue/aes-keyvalue.xml | 5 + .../corpus/xmlsec_keyvalue/dsa-keyvalue.xml | 5 + .../corpus/xmlsec_keyvalue/ec-keyvalue.xml | 8 + .../corpus/xmlsec_keyvalue/hmac-keyvalue.xml | 5 + .../corpus/xmlsec_keyvalue/rsa-keyvalue.xml | 8 + apps/oss-fuzz/xmlsec_keyvalue_target.c | 236 ++++++++++++++++++ 8 files changed, 292 insertions(+) create mode 100644 apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.dict create mode 100644 apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.options create mode 100644 apps/oss-fuzz/corpus/xmlsec_keyvalue/aes-keyvalue.xml create mode 100644 apps/oss-fuzz/corpus/xmlsec_keyvalue/dsa-keyvalue.xml create mode 100644 apps/oss-fuzz/corpus/xmlsec_keyvalue/ec-keyvalue.xml create mode 100644 apps/oss-fuzz/corpus/xmlsec_keyvalue/hmac-keyvalue.xml create mode 100644 apps/oss-fuzz/corpus/xmlsec_keyvalue/rsa-keyvalue.xml create mode 100644 apps/oss-fuzz/xmlsec_keyvalue_target.c diff --git a/apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.dict b/apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.dict new file mode 100644 index 000000000..cee878c66 --- /dev/null +++ b/apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.dict @@ -0,0 +1,22 @@ +# KeyValue element and namespace tokens from include/xmlsec/strings.h and +# src/strings.c. keyinfo_fuzzer never requests a write, and its corpus has no +# KeyValue children, so these names are mutation-resistant gates. +"http://www.w3.org/2000/09/xmldsig#" +"http://www.w3.org/2009/xmldsig11#" +"http://www.aleksey.com/xmlsec/2002" +"http://www.w3.org/2001/04/xmlenc#" +"KeyInfo" +"KeyValue" +"RSAKeyValue" +"DSAKeyValue" +"ECKeyValue" +"DHKeyValue" +"AESKeyValue" +"HMACKeyValue" +"DESKeyValue" +"Modulus" +"Exponent" +"NamedCurve" +"PublicKey" +"Seed" +"PgenCounter" diff --git a/apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.options b/apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.options new file mode 100644 index 000000000..56749d979 --- /dev/null +++ b/apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.options @@ -0,0 +1,3 @@ +[libfuzzer] +dict = xmlsec_keyvalue_fuzzer.dict +max_len = 65536 diff --git a/apps/oss-fuzz/corpus/xmlsec_keyvalue/aes-keyvalue.xml b/apps/oss-fuzz/corpus/xmlsec_keyvalue/aes-keyvalue.xml new file mode 100644 index 000000000..64a7be14c --- /dev/null +++ b/apps/oss-fuzz/corpus/xmlsec_keyvalue/aes-keyvalue.xml @@ -0,0 +1,5 @@ + + + MDEyMzQ1Njc4OUFCQ0RFRg== + + diff --git a/apps/oss-fuzz/corpus/xmlsec_keyvalue/dsa-keyvalue.xml b/apps/oss-fuzz/corpus/xmlsec_keyvalue/dsa-keyvalue.xml new file mode 100644 index 000000000..71f19efce --- /dev/null +++ b/apps/oss-fuzz/corpus/xmlsec_keyvalue/dsa-keyvalue.xml @@ -0,0 +1,5 @@ + + +

5llqtXo2G1g61J8WFsP00U9cl5z7emzkxqTre4C8pV/qdT8YRkbxrvHK7Bc7BXdx7abcP46842DcEZ07u7esCzlV9jziQTWsij9d6l8Nra9RRMqz37wDx0hUPRfmWHeEVFHpQkgUlL1lai/Ujs36fbiInXWoR3fWQn6+vjqP5S8=

vSPQz7ak24nBo5gwSMfEXkvEblc=CUyqAoEkRHpKrbmVnA8t3vkSEM8Gk3qHmTNunxVdmBVaitqPnf6D7SXxLRQNvuJ8+BAI70U/+iu5EUknE+QzvH2bPP9B9pXl9lNBV88wzjCC+M3kQD2+Bjps+NdJkTjhrJMygi8KPGCDbmfSqf+F1+nBIpDIgKzWxbhUMHd9VfI=JLZHChtL9OjhYpD2tVpN4+Urp9P0R/1jmkQA3vYycSf6GDtGDyESjCuWl2sH4EwIt3yJkhN4CPwGgrrkq8H1S+U7X76nTEaobAmElnBrBeF6uatRwwmCvVm4DCKs1XJNeN1QlsbLikf9aJguzjj8unzyYZRdjMcgjhLtVjeYAC0=ATfG1k4XSpDlWczkblMTpUkvazbCh6NH0ImOzLyjXlhRpihqtb/HBaCGWs3JzAiafW75tk57/9+vj6drV/vl44l14K5piP7VnDSADNCkBq9gCN/wM3u8w7cb8gpgTqC68I2yHzVVh4umjOrrgg==
+
+
diff --git a/apps/oss-fuzz/corpus/xmlsec_keyvalue/ec-keyvalue.xml b/apps/oss-fuzz/corpus/xmlsec_keyvalue/ec-keyvalue.xml new file mode 100644 index 000000000..6b031c14b --- /dev/null +++ b/apps/oss-fuzz/corpus/xmlsec_keyvalue/ec-keyvalue.xml @@ -0,0 +1,8 @@ + + + + + BP4TDUMXanrPs0QmI8bGK0l7x3mhNCt11nlv3+6F6uWpSZjtZuPHnb+OCwBCcPHKeK1j24raHXg8Il5Xwzx2YNw= + + + diff --git a/apps/oss-fuzz/corpus/xmlsec_keyvalue/hmac-keyvalue.xml b/apps/oss-fuzz/corpus/xmlsec_keyvalue/hmac-keyvalue.xml new file mode 100644 index 000000000..34b1564a5 --- /dev/null +++ b/apps/oss-fuzz/corpus/xmlsec_keyvalue/hmac-keyvalue.xml @@ -0,0 +1,5 @@ + + + MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY= + + diff --git a/apps/oss-fuzz/corpus/xmlsec_keyvalue/rsa-keyvalue.xml b/apps/oss-fuzz/corpus/xmlsec_keyvalue/rsa-keyvalue.xml new file mode 100644 index 000000000..bc57139e3 --- /dev/null +++ b/apps/oss-fuzz/corpus/xmlsec_keyvalue/rsa-keyvalue.xml @@ -0,0 +1,8 @@ + + + + 3iVnhDXlgGiWvV2f21bCP4NAeTkwouWvN9K94SNeV01xzuvPg2GRb+ozF0/YbQ8jj7UDeuIgcLoBrC/jSMtp7gJp6zj3oHhX97NZVv5SBUmBOJRbB8efy3apTvTvWlzJQhO4WVXBRDqmA0dGHPRRuMB6l125wy+WBMWxO6BzUooe9m0OpQXjKokJKFcl9Zd4ht3EqXW8cuHgiyrtYzXTcO63W9+J6dFOi1DTYhKMkK53jMsModlleEUdqUHgTbxK0TBSYMa2sB1rGfcVq+QanOlsRHXAXiZM/BE6uPqlFq+g5osSZbHfH2qC/0G/wKKlWzIx0YPjQSBq/MbroodKLQ== + AQAB + + + diff --git a/apps/oss-fuzz/xmlsec_keyvalue_target.c b/apps/oss-fuzz/xmlsec_keyvalue_target.c new file mode 100644 index 000000000..94ddd44e6 --- /dev/null +++ b/apps/oss-fuzz/xmlsec_keyvalue_target.c @@ -0,0 +1,236 @@ +/* + * xmlsec KeyValue read/write fuzz target. + * + * Threat model: a KeyInfo/KeyValue element is attacker-controlled in any XML + * Signature or XML Encryption document. xmlsec_keyinfo_fuzzer only calls + * xmlSecKeyInfoNodeRead (unknown or AES key id) and never writes, so the + * KeyValue writers in src/keysdata_helpers.c stay dark. This target reads the + * untrusted KeyInfo and writes any key it obtains back through + * xmlSecKeyInfoNodeWrite. It also generates a small symmetric key and writes + * that, which reaches the binary KeyValue writers without a successful parse. + * + * Expected bugs: memory-safety in base64 decode, bignum import and XML + * serialization of key material. External fetches stay disabled. + */ +#include +#include +#include + +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static int g_initialized = 0; +static int g_init_failed = 0; +static xmlSecKeysMngrPtr g_mngr = NULL; + +static void ignore_error(void* ctx, const char* msg, ...) { + (void)ctx; + (void)msg; +} + +static void ignore_xmlsec_error(const char* file, int line, const char* func, + const char* errorObject, const char* errorSubject, + int reason, const char* msg) { + (void)file; + (void)line; + (void)func; + (void)errorObject; + (void)errorSubject; + (void)reason; + (void)msg; +} + +static int do_init(void) { + xmlInitParser(); + if (xmlSecInit() < 0) { + return -1; + } + if (xmlSecCheckVersion() != 1) { + xmlSecShutdown(); + return -1; + } + if (xmlSecOpenSSLAppInit(NULL) < 0) { + xmlSecShutdown(); + return -1; + } + if (xmlSecOpenSSLInit() < 0) { + xmlSecOpenSSLShutdown(); + xmlSecOpenSSLAppShutdown(); + xmlSecShutdown(); + return -1; + } + xmlSetGenericErrorFunc(NULL, &ignore_error); + xmlSecErrorsSetCallback(&ignore_xmlsec_error); + + g_mngr = xmlSecKeysMngrCreate(); + if (g_mngr == NULL) { + goto fail; + } + if (xmlSecOpenSSLAppDefaultKeysMngrInit(g_mngr) < 0) { + xmlSecKeysMngrDestroy(g_mngr); + g_mngr = NULL; + goto fail; + } + return 0; + +fail: + xmlSecOpenSSLShutdown(); + xmlSecOpenSSLAppShutdown(); + xmlSecShutdown(); + return -1; +} +static void write_key(xmlSecKeyPtr key) { + xmlSecKeyInfoCtxPtr ctx; + xmlDocPtr doc; + xmlNodePtr node; + + if (key == NULL || xmlSecKeyGetValue(key) == NULL) { + return; + } + doc = xmlNewDoc(BAD_CAST "1.0"); + if (doc == NULL) { + return; + } + node = xmlNewDocNode(doc, NULL, xmlSecNodeKeyInfo, NULL); + if (node == NULL) { + xmlFreeDoc(doc); + return; + } + xmlDocSetRootElement(doc, node); + xmlSetNs(node, xmlNewNs(node, xmlSecDSigNs, NULL)); + + ctx = xmlSecKeyInfoCtxCreate(g_mngr); + if (ctx == NULL) { + xmlFreeDoc(doc); + return; + } + ctx->mode = xmlSecKeyInfoModeWrite; + ctx->keyReq.keyId = xmlSecKeyDataIdUnknown; + ctx->keyReq.keyType = xmlSecKeyDataTypeAny; + ctx->keyReq.keyUsage = xmlSecKeyUsageAny; + (void)xmlSecKeyInfoNodeWrite(node, key, ctx); + xmlSecKeyInfoCtxDestroy(ctx); + xmlFreeDoc(doc); +} + +static void read_keyinfo(xmlNodePtr node, xmlSecKeyDataId keyId) { + xmlSecKeyPtr key; + xmlSecKeyInfoCtxPtr ctx; + + key = xmlSecKeyCreate(); + if (key == NULL) { + return; + } + ctx = xmlSecKeyInfoCtxCreate(g_mngr); + if (ctx == NULL) { + xmlSecKeyDestroy(key); + return; + } + ctx->mode = xmlSecKeyInfoModeRead; + ctx->keyReq.keyId = keyId; + ctx->keyReq.keyType = xmlSecKeyDataTypeAny; + ctx->keyReq.keyUsage = xmlSecKeyUsageAny; + ctx->retrievalMethodCtx.enabledUris = + xmlSecTransformUriTypeEmpty | xmlSecTransformUriTypeSameDocument; + (void)xmlSecKeyInfoNodeRead(node, key, ctx); + xmlSecKeyInfoCtxDestroy(ctx); + write_key(key); + xmlSecKeyDestroy(key); +} + +static void write_generated_symmetric(uint8_t selector) { + xmlSecKeyDataId id; + xmlSecSize sizeBits; + xmlSecKeyPtr key; + + /* Ids are function-call macros, not compile-time constants. */ + switch (selector % 4) { + case 0: id = xmlSecOpenSSLKeyDataAesId; sizeBits = 128; break; + case 1: id = xmlSecOpenSSLKeyDataAesId; sizeBits = 256; break; + case 2: id = xmlSecOpenSSLKeyDataHmacId; sizeBits = 256; break; + default: id = xmlSecOpenSSLKeyDataHmacId; sizeBits = 160; break; + } + key = xmlSecKeyGenerate(id, sizeBits, xmlSecKeyDataTypeSymmetric); + if (key == NULL) { + return; + } + write_key(key); + xmlSecKeyDestroy(key); +} + +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + xmlDocPtr doc = NULL; + xmlNodePtr root = NULL; + xmlNodePtr node = NULL; + xmlSecKeyDataId ids[8]; + size_t nids = 0; + size_t i; + + if (!g_initialized) { + g_init_failed = (do_init() < 0); + g_initialized = 1; + } + /* Bound the document so one input cannot stall the process. */ + if (g_init_failed || size < 2 || size > 65536) { + return 0; + } + + write_generated_symmetric(data[0]); + + /* Parse the whole buffer so corpus files are valid KeyInfo documents. + data[0] still selects the generated symmetric key. */ + doc = xmlReadMemory((const char*)data, (int)size, "fuzz.xml", NULL, + xmlSecParserGetDefaultOptions() | XML_PARSE_NONET); + if (doc == NULL) { + return 0; + } + root = xmlDocGetRootElement(doc); + if (root == NULL) { + xmlFreeDoc(doc); + return 0; + } + if (xmlSecCheckNodeName(root, xmlSecNodeKeyInfo, xmlSecDSigNs)) { + node = root; + } else { + node = xmlSecFindNode(root, xmlSecNodeKeyInfo, xmlSecDSigNs); + } + if (node == NULL) { + xmlFreeDoc(doc); + return 0; + } + + ids[nids++] = xmlSecKeyDataIdUnknown; + ids[nids++] = xmlSecOpenSSLKeyDataRsaId; +#ifndef XMLSEC_NO_DSA + ids[nids++] = xmlSecOpenSSLKeyDataDsaId; +#endif +#ifndef XMLSEC_NO_EC + ids[nids++] = xmlSecOpenSSLKeyDataEcId; +#endif +#ifndef XMLSEC_NO_DH + ids[nids++] = xmlSecOpenSSLKeyDataDhId; +#endif + ids[nids++] = xmlSecOpenSSLKeyDataAesId; + ids[nids++] = xmlSecOpenSSLKeyDataHmacId; + for (i = 0; i < nids; i++) { + read_keyinfo(node, ids[i]); + } + xmlFreeDoc(doc); + return 0; +} +