Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / tests / fuzz / crypt2_load_ondisk_fuzz.cc
1 /*
2  * cryptsetup LUKS1, FileVault, BitLocker fuzz target
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 extern "C" {
20 #define FILESIZE (16777216)
21 #include "src/cryptsetup.h"
22 #include <err.h>
23 #include "luks1/luks.h"
24 #include "crypto_backend/crypto_backend.h"
25 #include "FuzzerInterface.h"
26
27 void empty_log(int level, const char *msg, void *usrptr) {}
28
29 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
30         int fd, r;
31         struct crypt_device *cd = NULL;
32         char name[] = "/tmp/test-script-fuzz.XXXXXX";
33
34         fd = mkostemp(name, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC);
35         if (fd == -1)
36                 err(EXIT_FAILURE, "mkostemp() failed");
37
38         /* enlarge header */
39         if (ftruncate(fd, FILESIZE) == -1)
40                 goto out;
41
42         if (write_buffer(fd, data, size) != (ssize_t) size)
43                 goto out;
44
45         crypt_set_log_callback(NULL, empty_log, NULL);
46
47         if (crypt_init(&cd, name) == 0) {
48                 r = crypt_load(cd, CRYPT_LUKS1, NULL);
49                 if (r == 0)
50                         goto out;
51
52                 r = crypt_load(cd, CRYPT_FVAULT2, NULL);
53                 if (r == 0)
54                         goto out;
55
56                 (void) crypt_load(cd, CRYPT_BITLK, NULL);
57         }
58 out:
59         crypt_free(cd);
60         close(fd);
61         unlink(name);
62         return 0;
63 }
64 }