Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / tests / fuzz / crypt2_load_fuzz.cc
1 /*
2  * cryptsetup LUKS2 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 extern "C" {
23 #define FILESIZE (16777216)
24 #include "src/cryptsetup.h"
25 #include <err.h>
26 #include "luks2/luks2.h"
27 #include "crypto_backend/crypto_backend.h"
28 #include "FuzzerInterface.h"
29
30 static int calculate_checksum(const uint8_t* data, size_t size) {
31         struct crypt_hash *hd = NULL;
32         struct luks2_hdr_disk *hdr = NULL;
33         int hash_size;
34         uint64_t hdr_size1, hdr_size2;
35         int r = 0;
36
37         /* primary header */
38         if (sizeof(struct luks2_hdr_disk) > size)
39                 return 0;
40         hdr = CONST_CAST(struct luks2_hdr_disk *) data;
41
42         hdr_size1 = be64_to_cpu(hdr->hdr_size);
43         if (hdr_size1 > size)
44                 return 0;
45         memset(&hdr->csum, 0, LUKS2_CHECKSUM_L);
46         if ((r = crypt_hash_init(&hd, "sha256")))
47                 goto out;
48         if ((r = crypt_hash_write(hd, CONST_CAST(char*) data, hdr_size1)))
49                 goto out;
50         hash_size = crypt_hash_size("sha256");
51         if (hash_size <= 0) {
52                 r = 1;
53                 goto out;
54         }
55         if ((r = crypt_hash_final(hd, (char*)&hdr->csum, (size_t)hash_size)))
56                 goto out;
57         crypt_hash_destroy(hd);
58
59         /* secondary header */
60         if (hdr_size1 < sizeof(struct luks2_hdr_disk))
61                 hdr_size1 = sizeof(struct luks2_hdr_disk);
62
63         if (hdr_size1 + sizeof(struct luks2_hdr_disk) > size)
64                 return 0;
65         hdr = CONST_CAST(struct luks2_hdr_disk *) (data + hdr_size1);
66
67         hdr_size2 = be64_to_cpu(hdr->hdr_size);
68         if (hdr_size2 > size || (hdr_size1 + hdr_size2) > size)
69                 return 0;
70
71         memset(&hdr->csum, 0, LUKS2_CHECKSUM_L);
72         if ((r = crypt_hash_init(&hd, "sha256")))
73                 goto out;
74         if ((r = crypt_hash_write(hd, (char*) hdr, hdr_size2)))
75                 goto out;
76         if ((r = crypt_hash_final(hd, (char*)&hdr->csum, (size_t)hash_size)))
77                 goto out;
78
79 out:
80         if (hd)
81                 crypt_hash_destroy(hd);
82         return r;
83 }
84
85 int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
86         int fd;
87         struct crypt_device *cd = NULL;
88         char name[] = "/tmp/test-script-fuzz.XXXXXX";
89
90         if (calculate_checksum(data, size))
91                 return 0;
92
93         fd = mkostemp(name, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
94         if (fd == -1)
95                 err(EXIT_FAILURE, "mkostemp() failed");
96
97         /* enlarge header */
98         if (ftruncate(fd, FILESIZE) == -1)
99                 goto out;
100
101         if (write_buffer(fd, data, size) != (ssize_t)size)
102                 goto out;
103
104         if (crypt_init(&cd, name) == 0)
105                 (void)crypt_load(cd, CRYPT_LUKS2, NULL);
106         crypt_free(cd);
107 out:
108         close(fd);
109         unlink(name);
110         return 0;
111 }
112 }