Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / tests / fuzz / crypt2_load_proto_plain_json_fuzz.cc
1 /*
2  * cryptsetup LUKS2 custom mutator fuzz target
3  *
4  * Copyright (C) 2022-2023 Daniel Zatovic <daniel.zatovic@gmail.com>
5  * Copyright (C) 2022-2023 Red Hat, Inc. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "LUKS2_plain_JSON.pb.h"
23 #include "plain_json_proto_to_luks2_converter.h"
24 #include "libfuzzer/libfuzzer_macro.h"
25 #include "FuzzerInterface.h"
26
27 extern "C" {
28 #include <libcryptsetup.h>
29 #include <err.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 }
33
34 DEFINE_PROTO_FUZZER(const json_proto::LUKS2_both_headers &headers) {
35   struct crypt_device *cd = NULL;
36   char name[] = "/tmp/test-proto-fuzz.XXXXXX";
37   int fd = mkostemp(name, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
38
39   if (fd < 0)
40     err(EXIT_FAILURE, "mkostemp() failed");
41
42   json_proto::LUKS2ProtoConverter converter;
43   converter.convert(headers, fd);
44
45   if (crypt_init(&cd, name) == 0)
46     (void)crypt_load(cd, CRYPT_LUKS2, NULL);
47   crypt_free(cd);
48
49   close(fd);
50   unlink(name);
51 }