/src/pigeonhole/src/testsuite/fuzzsuite.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "fuzzer.h" |
5 | | #include "lib-signals.h" |
6 | | #include "ioloop.h" |
7 | | #include "env-util.h" |
8 | | #include "istream.h" |
9 | | #include "hostpid.h" |
10 | | #include "settings.h" |
11 | | |
12 | | #include "sieve.h" |
13 | | #include "sieve-extensions.h" |
14 | | #include "sieve-script.h" |
15 | | #include "sieve-storage.h" |
16 | | #include "sieve-binary.h" |
17 | | #include "sieve-result.h" |
18 | | #include "sieve-interpreter.h" |
19 | | |
20 | | #include "sieve-settings.h" |
21 | | |
22 | | #include "sieve-tool.h" |
23 | | |
24 | | #include "testsuite-common.h" |
25 | | #include "testsuite-log.h" |
26 | | #include "testsuite-settings.h" |
27 | | #include "testsuite-result.h" |
28 | | #include "testsuite-message.h" |
29 | | #include "testsuite-script.h" |
30 | | #include "testsuite-smtp.h" |
31 | | #include "testsuite-mailstore.h" |
32 | | |
33 | | #include <stdio.h> |
34 | | #include <unistd.h> |
35 | | #include <fcntl.h> |
36 | | #include <pwd.h> |
37 | | #include <sysexits.h> |
38 | | |
39 | | //#define FUZZSUITE_DEBUG |
40 | | |
41 | | const struct sieve_script_env *testsuite_scriptenv; |
42 | | |
43 | | /* |
44 | | * Configuration |
45 | | */ |
46 | | |
47 | | extern const struct setting_parser_info ext_duplicate_setting_parser_info; |
48 | | extern const struct setting_parser_info ext_editheader_header_setting_parser_info; |
49 | | extern const struct setting_parser_info ext_editheader_setting_parser_info; |
50 | | extern const struct setting_parser_info ext_extlists_list_setting_parser_info; |
51 | | extern const struct setting_parser_info ext_extlists_setting_parser_info; |
52 | | extern const struct setting_parser_info ext_include_setting_parser_info; |
53 | | extern const struct setting_parser_info ext_spamtest_setting_parser_info; |
54 | | extern const struct setting_parser_info ext_virustest_setting_parser_info; |
55 | | extern const struct setting_parser_info ext_subaddress_setting_parser_info; |
56 | | extern const struct setting_parser_info ext_vacation_setting_parser_info; |
57 | | extern const struct setting_parser_info ext_variables_setting_parser_info; |
58 | | extern const struct setting_parser_info ext_vnd_environment_setting_parser_info; |
59 | | extern const struct setting_parser_info ext_report_setting_parser_info; |
60 | | extern const struct setting_parser_info ntfy_mailto_setting_parser_info; |
61 | | extern const struct setting_parser_info sieve_dict_storage_setting_parser_info; |
62 | | extern const struct setting_parser_info sieve_file_storage_setting_parser_info; |
63 | | extern const struct setting_parser_info sieve_setting_parser_info; |
64 | | extern const struct setting_parser_info sieve_storage_setting_parser_info; |
65 | | |
66 | | static const struct setting_parser_info *set_infos[] = { |
67 | | &ext_duplicate_setting_parser_info, |
68 | | &ext_editheader_header_setting_parser_info, |
69 | | &ext_editheader_setting_parser_info, |
70 | | &ext_extlists_list_setting_parser_info, |
71 | | &ext_extlists_setting_parser_info, |
72 | | &ext_include_setting_parser_info, |
73 | | &ext_spamtest_setting_parser_info, |
74 | | &ext_subaddress_setting_parser_info, |
75 | | &ext_vacation_setting_parser_info, |
76 | | &ext_variables_setting_parser_info, |
77 | | &ext_virustest_setting_parser_info, |
78 | | &ext_vnd_environment_setting_parser_info, |
79 | | &ext_report_setting_parser_info, |
80 | | &ntfy_mailto_setting_parser_info, |
81 | | |
82 | | &sieve_file_storage_setting_parser_info, |
83 | | &sieve_dict_storage_setting_parser_info, |
84 | | |
85 | | &sieve_setting_parser_info, |
86 | | &sieve_storage_setting_parser_info, |
87 | | }; |
88 | | unsigned int set_infos_count = N_ELEMENTS(set_infos); |
89 | | |
90 | | /* |
91 | | * Testsuite execution |
92 | | */ |
93 | | |
94 | | static void |
95 | | fuzz_die(const siginfo_t *si ATTR_UNUSED, void *context ATTR_UNUSED) |
96 | 0 | { |
97 | 0 | printf("COMMAND LINE INTERRUPT\n"); |
98 | |
|
99 | 0 | testsuite_tmp_dir_deinit(); |
100 | | |
101 | | /* No delays, no fuzzer reports: be gone already */ |
102 | 0 | _exit(0); |
103 | 0 | } |
104 | | |
105 | | FUZZ_BEGIN_DATA(const unsigned char *data, size_t size) |
106 | | { |
107 | | static bool first_run = TRUE; |
108 | | struct sieve_instance *svinst; |
109 | | struct sieve_binary *sbin; |
110 | | const char *sieve_dir = ".", *error; |
111 | | unsigned int i; |
112 | | bool log_stdout = FALSE; |
113 | | int ret; |
114 | | |
115 | | if (first_run) { |
116 | | #ifndef FUZZSUITE_DEBUG |
117 | | testsuite_silent = (getenv("DEBUG") == NULL); |
118 | | #endif |
119 | | first_run = FALSE; |
120 | | } |
121 | | |
122 | | sieve_tool = sieve_tool_init_fuzzer("fuzzsuite"); |
123 | | |
124 | | /* Allow quick command line termination when developing. This is not |
125 | | expected in a normal fuzzer run. */ |
126 | | lib_signals_set_handler(SIGINT, 0, fuzz_die, NULL); |
127 | | |
128 | | #ifdef FUZZSUITE_DEBUG |
129 | | struct sieve_trace_config trace_config; |
130 | | |
131 | | i_zero(&trace_config); |
132 | | trace_config.level = SIEVE_TRLVL_MATCHING; |
133 | | |
134 | | log_stdout = TRUE; |
135 | | #endif |
136 | | |
137 | | // FIXME: very very ugly |
138 | | master_service_parse_option( |
139 | | master_service, 'o', |
140 | | "postmaster_address=postmaster@example.com"); |
141 | | master_service_parse_option(master_service, 'o', "mail_uid="); |
142 | | master_service_parse_option(master_service, 'o', "mail_gid="); |
143 | | master_service_parse_option(master_service, 'o', "default_internal_group="); |
144 | | |
145 | | /* Register settings manually */ |
146 | | for (i = 0; i < set_infos_count; i++) |
147 | | settings_info_register(set_infos[i]); |
148 | | |
149 | | /* Finish testsuite initialization */ |
150 | | svinst = sieve_tool_init_finish(sieve_tool, FALSE, FALSE); |
151 | | #ifdef FUZZSUITE_DEBUG |
152 | | event_set_forced_debug(sieve_get_event(svinst), TRUE); |
153 | | #endif |
154 | | testsuite_init(svinst, sieve_dir, sieve_tool_get_homedir(sieve_tool), |
155 | | log_stdout); |
156 | | |
157 | | if (!testsuite_silent) |
158 | | printf("Fuzz case:\n\n"); |
159 | | |
160 | | struct settings_instance *set_instance = |
161 | | settings_instance_find(svinst->event); |
162 | | |
163 | | /* Configure personal storage */ |
164 | | settings_override(set_instance, "sieve_script+", "included", |
165 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
166 | | settings_override(set_instance, |
167 | | "sieve_script/included/sieve_script_storage", |
168 | | "included", |
169 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
170 | | settings_override(set_instance, |
171 | | "sieve_script/included/sieve_script_type", |
172 | | SIEVE_STORAGE_TYPE_PERSONAL, |
173 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
174 | | settings_override(set_instance, |
175 | | "sieve_script/included/sieve_script_driver", |
176 | | "file", SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
177 | | settings_override(set_instance, |
178 | | "sieve_script/included/sieve_script_path", |
179 | | t_strdup_printf("%s/included", sieve_dir), |
180 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
181 | | |
182 | | /* Configure global storage */ |
183 | | settings_override(set_instance, "sieve_script+", "included-global", |
184 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
185 | | settings_override(set_instance, |
186 | | "sieve_script/included-global/sieve_script_storage", |
187 | | "included-global", |
188 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
189 | | settings_override(set_instance, |
190 | | "sieve_script/included-global/sieve_script_type", |
191 | | SIEVE_STORAGE_TYPE_GLOBAL, |
192 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
193 | | settings_override(set_instance, |
194 | | "sieve_script/included-global/sieve_script_driver", |
195 | | "file", SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
196 | | settings_override(set_instance, |
197 | | "sieve_script/included-global/sieve_script_path", |
198 | | t_strdup_printf("%s/included-global", sieve_dir), |
199 | | SETTINGS_OVERRIDE_TYPE_2ND_CLI_PARAM); |
200 | | |
201 | | /* Construct Sieve script */ |
202 | | struct sieve_script *script; |
203 | | struct istream *input; |
204 | | |
205 | | input = i_stream_create_from_data(data, size); |
206 | | script = sieve_data_script_create_from_input( |
207 | | svinst, SIEVE_SCRIPT_CAUSE_ANY, "fuzzsuite-main", input); |
208 | | |
209 | | /* Compile Sieve script */ |
210 | | if (sieve_compile_script(script, testsuite_log_main_ehandler, 0, |
211 | | &sbin, NULL) < 0) { |
212 | | testsuite_testcase_fail("failed to compile testcase script"); |
213 | | } else { |
214 | | #ifdef FUZZSUITE_DEBUG |
215 | | struct sieve_trace_log *trace_log = NULL; |
216 | | |
217 | | sieve_tool_dump_binary_to(sbin, "-", FALSE); |
218 | | (void)sieve_trace_log_create(svinst, NULL, &trace_log); |
219 | | #endif |
220 | | |
221 | | struct sieve_exec_status exec_status; |
222 | | struct sieve_script_env scriptenv; |
223 | | |
224 | | testsuite_mailstore_init(); |
225 | | testsuite_message_init(); |
226 | | |
227 | | if (sieve_script_env_init(&scriptenv, |
228 | | testsuite_mailstore_get_user(), |
229 | | &error) < 0) { |
230 | | i_fatal("Failed to initialize script execution: %s", |
231 | | error); |
232 | | } |
233 | | |
234 | | i_zero(&exec_status); |
235 | | |
236 | | scriptenv.default_mailbox = "INBOX"; |
237 | | scriptenv.smtp_start = testsuite_smtp_start; |
238 | | scriptenv.smtp_add_rcpt = testsuite_smtp_add_rcpt; |
239 | | scriptenv.smtp_send = testsuite_smtp_send; |
240 | | scriptenv.smtp_abort = testsuite_smtp_abort; |
241 | | scriptenv.smtp_finish = testsuite_smtp_finish; |
242 | | #ifdef FUZZSUITE_DEBUG |
243 | | scriptenv.trace_log = trace_log; |
244 | | scriptenv.trace_config = trace_config; |
245 | | #endif |
246 | | scriptenv.exec_status = &exec_status; |
247 | | |
248 | | testsuite_scriptenv = &scriptenv; |
249 | | |
250 | | testsuite_result_init(); |
251 | | |
252 | | /* Fuzz the test */ |
253 | | ret = testsuite_run(sbin, testsuite_log_main_ehandler); |
254 | | |
255 | | switch (ret) { |
256 | | case SIEVE_EXEC_OK: |
257 | | break; |
258 | | case SIEVE_EXEC_FAILURE: |
259 | | case SIEVE_EXEC_KEEP_FAILED: |
260 | | case SIEVE_EXEC_TEMP_FAILURE: |
261 | | testsuite_testcase_fail( |
262 | | "test script execution aborted due to error"); |
263 | | break; |
264 | | case SIEVE_EXEC_BIN_CORRUPT: |
265 | | i_panic("BUG: compiled test script binary is corrupt"); |
266 | | break; |
267 | | case SIEVE_EXEC_RESOURCE_LIMIT: |
268 | | i_panic("BUG: resource limit exceeded"); |
269 | | break; |
270 | | } |
271 | | |
272 | | sieve_close(&sbin); |
273 | | |
274 | | /* De-initialize message environment */ |
275 | | testsuite_result_deinit(); |
276 | | testsuite_message_deinit(); |
277 | | testsuite_mailstore_deinit(); |
278 | | |
279 | | #ifdef FUZZSUITE_DEBUG |
280 | | if (trace_log != NULL) |
281 | | sieve_trace_log_free(&trace_log); |
282 | | #endif |
283 | | |
284 | | testsuite_scriptenv = NULL; |
285 | | } |
286 | | |
287 | | sieve_script_unref(&script); |
288 | | i_stream_destroy(&input); |
289 | | |
290 | | /* De-initialize testsuite */ |
291 | | testsuite_deinit(); |
292 | | |
293 | | sieve_tool_deinit(&sieve_tool); |
294 | | |
295 | | testsuite_testcase_result(FALSE); |
296 | | } |
297 | | FUZZ_END |