Skip to content
suit Documentation
Login

Documentation

// Fuzz target for the stamp reader: each input becomes the stamp file.

#include "platform.h"
#include "stamp.h"

#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

static char stamp_path[4096 + 8];

static void setup(void)
{
    const char *tmp = getenv("TMPDIR");
    char dir[4096];
    snprintf(dir, sizeof dir, "%s/suit-fuzz.XXXXXX", tmp != nullptr ? tmp : "/tmp");
    if (mkdtemp(dir) == nullptr) abort();
    setenv("SUIT_HOME", dir, 1);
    snprintf(stamp_path, sizeof stamp_path, "%s/stamp", dir);
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    if (stamp_path[0] == '\0') setup();
    int fd = open(stamp_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0 || (size > 0 && write(fd, data, size) != (ssize_t)size)) abort();
    close(fd);
    [[maybe_unused]] int rc = stamp_check(true);
    ensure(rc == EXIT_UNCHANGED || rc == EXIT_CHANGED || rc == EXIT_BAD_STAMP);
    return 0;
}