Path: blob/main/lib/iolog/regress/fuzz/fuzz_iolog_timing.c
1532 views
/*1* Copyright (c) 2021 Todd C. Miller <[email protected]>2*3* Permission to use, copy, modify, and distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES8* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF9* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR10* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES11* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN12* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF13* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.14*/1516#include <config.h>1718#include <sys/stat.h>19#include <stdio.h>20#include <stdlib.h>21#include <string.h>22#include <fcntl.h>23#include <unistd.h>24#if defined(HAVE_STDINT_H)25# include <stdint.h>26#elif defined(HAVE_INTTYPES_H)27# include <inttypes.h>28#endif29#ifdef HAVE_STDBOOL_H30# include <stdbool.h>31#else32# include <compat/stdbool.h>33#endif /* HAVE_STDBOOL_H */3435#include <sudo_compat.h>36#include <sudo_debug.h>37#include <sudo_eventlog.h>38#include <sudo_fatal.h>39#include <sudo_iolog.h>40#include <sudo_plugin.h>41#include <sudo_util.h>4243int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);4445static int46fuzz_conversation(int num_msgs, const struct sudo_conv_message msgs[],47struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)48{49int n;5051for (n = 0; n < num_msgs; n++) {52const struct sudo_conv_message *msg = &msgs[n];5354switch (msg->msg_type & 0xff) {55case SUDO_CONV_PROMPT_ECHO_ON:56case SUDO_CONV_PROMPT_MASK:57case SUDO_CONV_PROMPT_ECHO_OFF:58/* input not supported */59return -1;60case SUDO_CONV_ERROR_MSG:61case SUDO_CONV_INFO_MSG:62/* no output for fuzzers */63break;64default:65return -1;66}67}68return 0;69}7071int72LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)73{74struct iolog_file iolog_file = { true };75struct timing_closure closure;76char logdir[] = "/tmp/timing.XXXXXX";77int dfd = -1, fd = -1;7879initprogname("fuzz_iolog_timing");80if (getenv("SUDO_FUZZ_VERBOSE") == NULL)81sudo_warn_set_conversation(fuzz_conversation);8283/* I/O logs consist of multiple files in a directory. */84if (mkdtemp(logdir) == NULL)85return 0;8687/* Create a timing file from the supplied data. */88dfd = open(logdir, O_RDONLY|O_DIRECTORY);89if (dfd == -1)90goto cleanup;9192fd = openat(dfd, "timing", O_WRONLY|O_CREAT|O_EXCL, S_IRWXU);93if (fd == -1)94goto cleanup;9596if (write(fd, data, size) != (ssize_t)size)97goto cleanup;98close(fd);99fd = -1;100101/* Open the timing file we wrote and try to parse it. */102if (!iolog_open(&iolog_file, dfd, IOFD_TIMING, "r"))103goto cleanup;104105memset(&closure, 0, sizeof(closure));106closure.decimal = ".";107for (;;) {108if (iolog_read_timing_record(&iolog_file, &closure) != 0)109break;110}111iolog_close(&iolog_file, NULL);112113cleanup:114if (dfd != -1) {115if (fd != -1)116close(fd);117unlinkat(dfd, "timing", 0);118close(dfd);119}120rmdir(logdir);121fflush(stdout);122123return 0;124}125126/* STUB */127bool128iolog_swapids(bool restore)129{130return false;131}132133134