1 #include <stdint.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <ftw.h>
7
8 #include "config.h"
9 #include "gpg.h"
10 #include "../common/types.h"
11 #include "../common/iobuf.h"
12 #include "keydb.h"
13 #include "keyedit.h"
14 #include "../common/util.h"
15 #include "main.h"
16 #include "call-dirmngr.h"
17 #include "trustdb.h"
18
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <sys/mount.h>
24
25 static bool initialized = false;
26 ctrl_t ctrlGlobal;
27 int fd;
28 char *filename;
29
30 //hack not to include gpg.c which has main function
31 int g10_errors_seen = 0;
32
33 void
g10_exit(int rc)34 g10_exit( int rc )
35 {
36 gcry_control (GCRYCTL_UPDATE_RANDOM_SEED_FILE);
37 gcry_control (GCRYCTL_TERM_SECMEM );
38 exit (rc);
39 }
40
41 static void
gpg_deinit_default_ctrl(ctrl_t ctrl)42 gpg_deinit_default_ctrl (ctrl_t ctrl)
43 {
44 #ifdef USE_TOFU
45 tofu_closedbs (ctrl);
46 #endif
47 gpg_dirmngr_deinit_session_data (ctrl);
48
49 keydb_release (ctrl->cached_getkey_kdb);
50 }
51
52 static void
my_gcry_logger(void * dummy,int level,const char * format,va_list arg_ptr)53 my_gcry_logger (void *dummy, int level, const char *format, va_list arg_ptr)
54 {
55 return;
56 }
57
unlink_cb(const char * fpath,const struct stat * sb,int typeflag)58 static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag)
59 {
60 if (typeflag == FTW_F){
61 unlink(fpath);
62 }
63 return 0;
64 }
65
rmrfdir(char * path)66 static void rmrfdir(char *path)
67 {
68 ftw(path, unlink_cb, 16);
69 if (rmdir(path) != 0) {
70 printf("failed rmdir, errno=%d\n", errno);
71 }
72 }
73
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)74 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
75
76 if (! initialized) {
77 ctrlGlobal = (ctrl_t) malloc(sizeof(*ctrlGlobal));
78 if (!ctrlGlobal) {
79 exit(1);
80 }
81 //deletes previous tmp dir and (re)create it as a ramfs
82 //system("umount /tmp/fuzzdirverify");
83 rmrfdir("/tmp/fuzzdirverify");
84 mkdir("/tmp/fuzzdirverify/", 0700);
85 //system("mount -t tmpfs -o size=64M tmpfs /tmp/fuzzdirverify");
86 filename=strdup("/tmp/fuzzdirverify/fuzz.gpg");
87 if (!filename) {
88 free(ctrlGlobal);
89 return 0;
90 }
91 fd = open("/tmp/fuzzdirverify/fuzz.gpg", O_RDWR | O_CREAT, 0600);
92 if (fd == -1) {
93 free(ctrlGlobal);
94 free(filename);
95 return 0;
96 }
97 gnupg_set_homedir("/tmp/fuzzdirverify/");
98 if (keydb_add_resource ("pubring" EXTSEP_S GPGEXT_GPG,
99 KEYDB_RESOURCE_FLAG_DEFAULT) != GPG_ERR_NO_ERROR) {
100 free(filename);
101 free(ctrlGlobal);
102 close(fd);
103 return 0;
104 }
105 if (setup_trustdb (1, NULL) != GPG_ERR_NO_ERROR) {
106 free(filename);
107 free(ctrlGlobal);
108 close(fd);
109 return 0;
110 }
111 //populate /tmp/fuzzdirverify/ as homedir ~/.gnupg
112 strlist_t sl = NULL;
113 public_key_list (ctrlGlobal, sl, 0, 0);
114 free_strlist(sl);
115 //no output for stderr
116 log_set_file("/dev/null");
117 gcry_set_log_handler (my_gcry_logger, NULL);
118 gnupg_initialize_compliance (GNUPG_MODULE_NAME_GPG);
119 initialized = true;
120 }
121
122 memset(ctrlGlobal, 0, sizeof(*ctrlGlobal));
123 ctrlGlobal->magic = SERVER_CONTROL_MAGIC;
124
125 if (ftruncate(fd, Size) == -1) {
126 return 0;
127 }
128 if (lseek (fd, 0, SEEK_SET) < 0) {
129 return 0;
130 }
131 if (write (fd, Data, Size) != Size) {
132 return 0;
133 }
134
135 verify_signatures(ctrlGlobal, 1, &filename);
136 gpg_deinit_default_ctrl (ctrlGlobal);
137 memset(ctrlGlobal, 0, sizeof(*ctrlGlobal));
138 ctrlGlobal->magic = SERVER_CONTROL_MAGIC;
139 verify_files(ctrlGlobal, 1, &filename);
140 gpg_deinit_default_ctrl (ctrlGlobal);
141
142 return 0;
143 }
144