Path: blob/main/crypto/krb5/src/tests/fuzzing/fuzz_krb.c
34878 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/fuzzing/krb.c - fuzzing harness for miscellaneous libkrb5 functions */2/*3* Copyright (C) 2024 by Arjun. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS20* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE21* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,22* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR24* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,26* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED28* OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031#include "autoconf.h"32#include <k5-int.h>3334#define kMinInputLength 235#define kMaxInputLength 5123637#define ANAME_SZ 4038#define INST_SZ 4039#define REALM_SZ 404041extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);4243static void44fuzz_deltat(char *data_in)45{46krb5_deltat result;47krb5_string_to_deltat(data_in, &result);48}4950static void51fuzz_host_string(char *data_in)52{53krb5_error_code ret;54char *host;55int port = -1;5657ret = k5_parse_host_string(data_in, 0, &host, &port);58if (!ret)59free(host);60}6162static void63fuzz_princ(krb5_context context, char *data_in)64{65krb5_error_code ret;66krb5_principal p;67char *princ;6869ret = krb5_parse_name(context, data_in, &p);70if (ret)71return;7273ret = krb5_unparse_name(context, p, &princ);74if (!ret)75free(princ);7677krb5_free_principal(context, p);78}7980static void81fuzz_principal_425(krb5_context context, char *data_in)82{83krb5_principal princ;84krb5_425_conv_principal(context, data_in, data_in, data_in, &princ);85krb5_free_principal(context, princ);86}8788static void89fuzz_principal_524(krb5_context context, char *data_in)90{91krb5_error_code ret;92krb5_principal princ = 0;93char aname[ANAME_SZ + 1], inst[INST_SZ + 1], realm[REALM_SZ + 1];9495aname[ANAME_SZ] = inst[INST_SZ] = realm[REALM_SZ] = 0;9697ret = krb5_parse_name(context, data_in, &princ);98if (ret)99return;100101krb5_524_conv_principal(context, princ, aname, inst, realm);102krb5_free_principal(context, princ);103}104105static void106fuzz_timestamp(char *data_in)107{108krb5_error_code ret;109krb5_timestamp timestamp;110111ret = krb5_string_to_timestamp(data_in, ×tamp);112if (!ret)113ts2tt(timestamp);114}115116/*117* data_in is going to be modified during parsing.118*/119static void120fuzz_enctype_list(char *data_in)121{122krb5_error_code ret;123krb5_context context;124krb5_enctype *ienc, zero = 0;125126ret = krb5_init_context(&context);127if (ret)128return;129130ret = krb5int_parse_enctype_list(context, "", data_in, &zero, &ienc);131if (!ret)132free(ienc);133134krb5_free_context(context);135}136137extern int138LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)139{140krb5_error_code ret;141krb5_context context = NULL;142char *data_in;143144if (size < kMinInputLength || size > kMaxInputLength)145return 0;146147ret = krb5_init_context(&context);148if (ret)149return 0;150151data_in = k5memdup0(data, size, &ret);152if (ret)153goto cleanup;154155fuzz_deltat(data_in);156fuzz_host_string(data_in);157fuzz_princ(context, data_in);158fuzz_principal_425(context, data_in);159fuzz_principal_524(context, data_in);160fuzz_timestamp(data_in);161fuzz_enctype_list(data_in);162163free(data_in);164165cleanup:166krb5_free_context(context);167168return 0;169}170171172