xref: /spdk/test/app/fuzz/iscsi_fuzz/iscsi_fuzz.c (revision e2916ec13731949a1e4ffefc148ac43bd7f1f11e)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2019 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 #include "spdk/event.h"
8 #include "spdk/util.h"
9 #include "spdk/string.h"
10 #include "spdk/likely.h"
11 #include "spdk/json.h"
12 #include "spdk/endian.h"
13 #include "spdk/bdev.h"
14 #include "spdk/notify.h"
15 #include "spdk/scsi.h"
16 #include "spdk_internal/mock.h"
17 #include "spdk/scsi_spec.h"
18 #include "iscsi/conn.h"
19 #include "iscsi/iscsi.c"
20 #include "scsi/scsi_internal.h"
21 #include "spdk/sock.h"
22 
23 #define GET_PDU_LOOP_COUNT	16
24 #define DEFAULT_RUNTIME		30	/* seconds */
25 #define MAX_RUNTIME_S		86400	/* 24 hours */
26 
27 /* Global run state */
28 uint64_t		g_runtime_ticks;
29 int			g_runtime;
30 int			g_num_active_threads;
31 bool			g_run = true;
32 bool			g_is_valid_opcode = true;
33 
34 SPDK_LOG_REGISTER_COMPONENT(iscsi)
35 
36 /* Global resources */
37 TAILQ_HEAD(, spdk_iscsi_pdu)		g_get_pdu_list;
38 TAILQ_HEAD(, fuzz_iscsi_dev_ctx)	g_dev_list = TAILQ_HEAD_INITIALIZER(g_dev_list);
39 struct spdk_poller			*g_app_completion_poller;
40 void					*g_valid_buffer;
41 unsigned int				g_random_seed;
42 char					*g_tgt_ip = "127.0.0.1";
43 char					*g_tgt_port = "3260";
44 /* TBD: Discovery login to get target information. We use fixed IQN for target for now. */
45 char					*g_tgt_name = "iqn.2016-06.io.spdk:disk1";
46 char					*g_init_name = "iqn.2016-06.io.spdk:fuzzinit";
47 
48 struct fuzz_iscsi_iov_ctx {
49 	struct iovec			iov_req;
50 	struct iovec			iov_data;
51 	struct iovec			iov_resp;
52 };
53 
54 struct fuzz_iscsi_io_ctx {
55 	struct fuzz_iscsi_iov_ctx		iov_ctx;
56 	union {
57 		struct iscsi_bhs		*bhs;
58 		struct iscsi_bhs_nop_out	*nop_out_req;
59 		struct iscsi_bhs_scsi_req	*scsi_req;
60 		struct iscsi_bhs_task_req	*task_req;
61 		struct iscsi_bhs_login_req	*login_req;
62 		struct iscsi_bhs_text_req	*text_req;
63 		struct iscsi_bhs_data_out	*data_out_req;
64 		struct iscsi_bhs_logout_req	*logout_req;
65 		struct iscsi_bhs_snack_req	*snack_req;
66 	} req;
67 };
68 
69 struct fuzz_iscsi_dev_ctx {
70 	struct spdk_iscsi_sess			sess;
71 	struct spdk_iscsi_conn			*conn;
72 	struct fuzz_iscsi_io_ctx		io_ctx;
73 
74 	struct spdk_thread			*thread;
75 	struct spdk_poller			*poller;
76 	unsigned int				random_seed, current_cmd_sn;
77 	uint64_t				num_sent_pdus;
78 	uint64_t				num_valid_pdus;
79 
80 	TAILQ_ENTRY(fuzz_iscsi_dev_ctx)		link;
81 };
82 
83 static void
fuzz_fill_random_bytes(char * character_repr,size_t len,unsigned int * rand_seed)84 fuzz_fill_random_bytes(char *character_repr, size_t len, unsigned int *rand_seed)
85 {
86 	size_t i;
87 
88 	for (i = 0; i < len; i++) {
89 		character_repr[i] = rand_r(rand_seed) % UINT8_MAX;
90 	}
91 }
92 
93 static char *
fuzz_get_value_base_64_buffer(void * item,size_t len)94 fuzz_get_value_base_64_buffer(void *item, size_t len)
95 {
96 	char *value_string;
97 	size_t total_size;
98 	int rc;
99 
100 	/* Null pointer */
101 	total_size = spdk_base64_get_encoded_strlen(len) + 1;
102 
103 	value_string = calloc(1, total_size);
104 	if (value_string == NULL) {
105 		return NULL;
106 	}
107 
108 	rc = spdk_base64_encode(value_string, item, len);
109 	if (rc < 0) {
110 		free(value_string);
111 		return NULL;
112 	}
113 
114 	return value_string;
115 }
116 
117 int
iscsi_chap_get_authinfo(struct iscsi_chap_auth * auth,const char * authuser,int ag_tag)118 iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authuser,
119 			int ag_tag)
120 {
121 	return 0;
122 }
123 
124 void
shutdown_iscsi_conns_done(void)125 shutdown_iscsi_conns_done(void)
126 {
127 	return;
128 }
129 
130 void
iscsi_put_pdu(struct spdk_iscsi_pdu * pdu)131 iscsi_put_pdu(struct spdk_iscsi_pdu *pdu)
132 {
133 	if (!pdu) {
134 		return;
135 	}
136 
137 	pdu->ref--;
138 	if (pdu->ref < 0) {
139 		pdu->ref = 0;
140 	}
141 
142 	if (pdu->ref == 0) {
143 		if (pdu->data) {
144 			free(pdu->data);
145 		}
146 		free(pdu);
147 	}
148 }
149 
150 struct spdk_iscsi_pdu *
iscsi_get_pdu(struct spdk_iscsi_conn * conn)151 iscsi_get_pdu(struct spdk_iscsi_conn *conn)
152 {
153 	struct spdk_iscsi_pdu *pdu;
154 
155 	pdu = calloc(1, sizeof(*pdu));
156 	if (!pdu) {
157 		return NULL;
158 	}
159 
160 	pdu->ref = 1;
161 	pdu->conn = conn;
162 
163 	return pdu;
164 }
165 
166 static void
iscsi_task_free(struct spdk_scsi_task * scsi_task)167 iscsi_task_free(struct spdk_scsi_task *scsi_task)
168 {
169 	struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task);
170 
171 	assert(task->parent == NULL);
172 
173 	iscsi_task_disassociate_pdu(task);
174 	assert(task->conn->pending_task_cnt > 0);
175 	task->conn->pending_task_cnt--;
176 	free(task);
177 }
178 
179 struct spdk_iscsi_task *
iscsi_task_get(struct spdk_iscsi_conn * conn,struct spdk_iscsi_task * parent,spdk_scsi_task_cpl cpl_fn)180 iscsi_task_get(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *parent,
181 	       spdk_scsi_task_cpl cpl_fn)
182 {
183 	struct spdk_iscsi_task *task;
184 
185 	/* iSCSI subtask is not necessary for now. */
186 	assert(parent == NULL);
187 
188 	task = calloc(1, sizeof(*task));
189 	if (!task) {
190 		printf("Unable to get task\n");
191 		abort();
192 	}
193 
194 	task->conn = conn;
195 	assert(conn->pending_task_cnt < UINT32_MAX);
196 	conn->pending_task_cnt++;
197 	spdk_scsi_task_construct(&task->scsi, cpl_fn, iscsi_task_free);
198 
199 	return task;
200 }
201 
202 static void
cleanup(void)203 cleanup(void)
204 {
205 	struct fuzz_iscsi_dev_ctx *dev_ctx, *tmp;
206 
207 	TAILQ_FOREACH_SAFE(dev_ctx, &g_dev_list, link, tmp) {
208 		printf("device %p stats: Sent %" PRIu64 " valid opcode PDUs, %" PRIu64 " invalid opcode PDUs.\n",
209 		       dev_ctx, dev_ctx->num_valid_pdus,
210 		       dev_ctx->num_sent_pdus - dev_ctx->num_valid_pdus);
211 		free(dev_ctx);
212 	}
213 
214 	spdk_free(g_valid_buffer);
215 }
216 
217 /* data dumping functions begin */
218 static int
dump_iscsi_cmd(void * ctx,const void * data,size_t size)219 dump_iscsi_cmd(void *ctx, const void *data, size_t size)
220 {
221 	fprintf(stderr, "%s\n", (const char *)data);
222 	return 0;
223 }
224 
225 static void
print_scsi_io_data(struct spdk_json_write_ctx * w,struct fuzz_iscsi_io_ctx * io_ctx)226 print_scsi_io_data(struct spdk_json_write_ctx *w, struct fuzz_iscsi_io_ctx *io_ctx)
227 {
228 	char *data_segment_len;
229 
230 	data_segment_len = fuzz_get_value_base_64_buffer((void *)io_ctx->req.bhs->data_segment_len,
231 			   sizeof(io_ctx->req.bhs->data_segment_len));
232 
233 	spdk_json_write_named_uint32(w, "opcode", io_ctx->req.bhs->opcode);
234 	spdk_json_write_named_uint32(w, "immediate", io_ctx->req.bhs->immediate);
235 	spdk_json_write_named_uint32(w, "reserved", io_ctx->req.bhs->reserved);
236 	spdk_json_write_named_uint32(w, "total_ahs_len", io_ctx->req.bhs->total_ahs_len);
237 	spdk_json_write_named_string(w, "data_segment_len", data_segment_len);
238 	spdk_json_write_named_uint32(w, "itt", io_ctx->req.bhs->itt);
239 	spdk_json_write_named_uint32(w, "exp_stat_sn", io_ctx->req.bhs->exp_stat_sn);
240 
241 	free(data_segment_len);
242 }
243 
244 static void
print_req_obj(struct fuzz_iscsi_dev_ctx * dev_ctx,struct fuzz_iscsi_io_ctx * io_ctx)245 print_req_obj(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
246 {
247 	struct spdk_json_write_ctx *w;
248 
249 	w = spdk_json_write_begin(dump_iscsi_cmd, NULL, SPDK_JSON_WRITE_FLAG_FORMATTED);
250 	spdk_json_write_named_object_begin(w, "bhs");
251 	print_scsi_io_data(w, io_ctx);
252 	spdk_json_write_object_end(w);
253 	spdk_json_write_end(w);
254 }
255 
256 /* data dumping functions end */
257 
258 /* dev initialization begin */
259 static int
fuzz_iscsi_dev_init(void)260 fuzz_iscsi_dev_init(void)
261 {
262 	struct fuzz_iscsi_dev_ctx *dev_ctx;
263 	int rc = 0;
264 
265 	dev_ctx = calloc(1, sizeof(*dev_ctx));
266 	if (dev_ctx == NULL) {
267 		return -ENOMEM;
268 	}
269 
270 	dev_ctx->thread = spdk_get_thread();
271 	if (dev_ctx->thread == NULL) {
272 		fprintf(stderr, "Unable to get a thread for a fuzz device.\n");
273 		rc = -EINVAL;
274 		goto error_out;
275 	}
276 
277 	dev_ctx->current_cmd_sn = 0;
278 
279 	TAILQ_INSERT_TAIL(&g_dev_list, dev_ctx, link);
280 	return 0;
281 
282 error_out:
283 	free(dev_ctx);
284 	return rc;
285 }
286 /* dev initialization end */
287 
288 /* build requests begin */
289 static void
prep_iscsi_pdu_bhs_opcode_cmd(struct fuzz_iscsi_dev_ctx * dev_ctx,struct fuzz_iscsi_io_ctx * io_ctx)290 prep_iscsi_pdu_bhs_opcode_cmd(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
291 {
292 	io_ctx->iov_ctx.iov_req.iov_len = sizeof(struct iscsi_bhs);
293 	fuzz_fill_random_bytes((char *)io_ctx->req.bhs, sizeof(struct iscsi_bhs),
294 			       &dev_ctx->random_seed);
295 }
296 /* build requests end */
297 
298 static int
iscsi_pdu_hdr_op_login_rsp(struct spdk_iscsi_conn * conn,struct spdk_iscsi_pdu * pdu)299 iscsi_pdu_hdr_op_login_rsp(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
300 {
301 	return 0;
302 }
303 
304 static int
iscsi_fuzz_pdu_hdr_handle(struct spdk_iscsi_conn * conn,struct spdk_iscsi_pdu * pdu)305 iscsi_fuzz_pdu_hdr_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
306 {
307 	int opcode;
308 	int rc = 0;
309 
310 	opcode = pdu->bhs.opcode;
311 	if (opcode == ISCSI_OP_LOGIN_RSP) {
312 		return iscsi_pdu_hdr_op_login_rsp(conn, pdu);
313 	}
314 
315 	switch (opcode) {
316 	case ISCSI_OP_LOGOUT_RSP:
317 		fprintf(stderr, "Received logout hdr_handle response opcode(0x26) from Target.\n");
318 		conn->is_logged_out = true;
319 		break;
320 	case ISCSI_OP_NOPIN:
321 	case ISCSI_OP_SCSI_RSP:
322 	case ISCSI_OP_TASK_RSP:
323 	case ISCSI_OP_TEXT_RSP:
324 	case ISCSI_OP_SCSI_DATAIN:
325 	case ISCSI_OP_R2T:
326 	case ISCSI_OP_ASYNC:
327 	case ISCSI_OP_VENDOR_3C:
328 	case ISCSI_OP_VENDOR_3D:
329 	case ISCSI_OP_VENDOR_3E:
330 		fprintf(stderr, "Received hdr_handle response opcode from Target is 0x%x.\n", pdu->bhs.opcode);
331 		break;
332 	case ISCSI_OP_REJECT:
333 		fprintf(stderr, "Received rejected hdr_handle response opcode(0x3f) from Target.\n");
334 		break;
335 	default:
336 		rc = -1;
337 		break;
338 	}
339 
340 	return rc;
341 }
342 
343 static int
iscsi_pdu_payload_op_login_rsp(struct spdk_iscsi_conn * conn,struct spdk_iscsi_pdu * pdu)344 iscsi_pdu_payload_op_login_rsp(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
345 {
346 	struct iscsi_bhs_login_rsp *rsph;
347 
348 	rsph = (struct iscsi_bhs_login_rsp *)&pdu->bhs;
349 	if (rsph == NULL) {
350 		return -1;
351 	}
352 
353 	assert(rsph->tsih != 0);
354 	assert(rsph->status_class == 0);
355 	assert(ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags));
356 	assert(!(rsph->flags & ISCSI_LOGIN_CONTINUE));
357 	assert((rsph->flags & ISCSI_LOGIN_NEXT_STAGE_MASK) == ISCSI_LOGIN_NEXT_STAGE_3);
358 
359 	/* We got the Login Final Response and move to Full-Feature Phase. */
360 	conn->full_feature = 1;
361 	return 0;
362 }
363 
364 static int
iscsi_fuzz_pdu_payload_handle(struct spdk_iscsi_conn * conn,struct spdk_iscsi_pdu * pdu)365 iscsi_fuzz_pdu_payload_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
366 {
367 	int opcode;
368 	int rc = 0;
369 
370 	opcode = pdu->bhs.opcode;
371 	fprintf(stderr, "Received payload_handle response opcode from Target is 0x%x.\n", opcode);
372 
373 	switch (opcode) {
374 	case ISCSI_OP_LOGIN_RSP:
375 		rc = iscsi_pdu_payload_op_login_rsp(conn, pdu);
376 		break;
377 	case ISCSI_OP_NOPIN:
378 	case ISCSI_OP_SCSI_RSP:
379 	case ISCSI_OP_TASK_RSP:
380 	case ISCSI_OP_TEXT_RSP:
381 	case ISCSI_OP_SCSI_DATAIN:
382 	case ISCSI_OP_R2T:
383 	case ISCSI_OP_ASYNC:
384 	case ISCSI_OP_VENDOR_3C:
385 	case ISCSI_OP_VENDOR_3D:
386 	case ISCSI_OP_VENDOR_3E:
387 	case ISCSI_OP_REJECT:
388 		break;
389 	default:
390 		rc = -1;
391 		break;
392 	}
393 
394 	return rc;
395 }
396 
397 static int
iscsi_fuzz_read_pdu(struct spdk_iscsi_conn * conn)398 iscsi_fuzz_read_pdu(struct spdk_iscsi_conn *conn)
399 {
400 	enum iscsi_pdu_recv_state prev_state;
401 	struct spdk_iscsi_pdu *pdu;
402 	uint32_t data_len;
403 	int rc;
404 
405 	do {
406 		prev_state = conn->pdu_recv_state;
407 		pdu = conn->pdu_in_progress;
408 
409 		switch (conn->pdu_recv_state) {
410 		case ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY:
411 			assert(conn->pdu_in_progress == NULL);
412 
413 			conn->pdu_in_progress = iscsi_get_pdu(conn);
414 			if (conn->pdu_in_progress == NULL) {
415 				return SPDK_ISCSI_CONNECTION_FATAL;
416 			}
417 			TAILQ_INSERT_TAIL(&g_get_pdu_list, conn->pdu_in_progress, tailq);
418 			conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR;
419 			break;
420 		case ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR:
421 			if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
422 				rc = iscsi_conn_read_data(conn,
423 							  ISCSI_BHS_LEN - pdu->bhs_valid_bytes,
424 							  (uint8_t *)&pdu->bhs + pdu->bhs_valid_bytes);
425 				if (rc < 0) {
426 					conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
427 					break;
428 				}
429 				pdu->bhs_valid_bytes += rc;
430 				if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
431 					return 0;
432 				}
433 			}
434 
435 			pdu->data_segment_len = ISCSI_ALIGN(DGET24(pdu->bhs.data_segment_len));
436 
437 			rc = iscsi_fuzz_pdu_hdr_handle(conn, pdu);
438 			if (rc < 0) {
439 				printf("Critical error is detected. Close the connection\n");
440 				conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
441 				break;
442 			}
443 
444 			if (conn->is_logged_out) {
445 				printf("pdu received after logout\n");
446 				conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
447 				break;
448 			}
449 
450 			conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD;
451 			break;
452 		case ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
453 			data_len = pdu->data_segment_len;
454 			if (data_len != 0 && pdu->data == NULL) {
455 				pdu->data = calloc(1, data_len);
456 				if (pdu->data == NULL) {
457 					return 0;
458 				}
459 			}
460 
461 			/* copy the actual data into local buffer */
462 			if (pdu->data_valid_bytes < data_len) {
463 				rc = iscsi_conn_read_data_segment(conn, pdu,
464 								  pdu->data_valid_bytes,
465 								  data_len - pdu->data_valid_bytes);
466 				if (rc < 0) {
467 					conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
468 					break;
469 				}
470 				pdu->data_valid_bytes += rc;
471 				if (pdu->data_valid_bytes < data_len) {
472 					return 0;
473 				}
474 			}
475 
476 			if (!pdu->is_rejected) {
477 				rc = iscsi_fuzz_pdu_payload_handle(conn, pdu);
478 			} else {
479 				rc = 0;
480 			}
481 			if (rc == 0) {
482 				spdk_trace_record(TRACE_ISCSI_TASK_EXECUTED, 0, 0, (uintptr_t)pdu);
483 				conn->pdu_in_progress = NULL;
484 				conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
485 				return 1;
486 			} else {
487 				conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
488 			}
489 			break;
490 		case ISCSI_PDU_RECV_STATE_ERROR:
491 			return SPDK_ISCSI_CONNECTION_FATAL;
492 		default:
493 			assert(false);
494 			printf("code should not come here\n");
495 			break;
496 		}
497 	} while (prev_state != conn->pdu_recv_state);
498 
499 	return 0;
500 }
501 
502 #define GET_PDU_LOOP_COUNT	16
503 
504 static int
fuzz_iscsi_handle_incoming_pdus(struct spdk_iscsi_conn * conn)505 fuzz_iscsi_handle_incoming_pdus(struct spdk_iscsi_conn *conn)
506 {
507 	int i, rc;
508 
509 	/* Read new PDUs from network */
510 	for (i = 0; i < GET_PDU_LOOP_COUNT; i++) {
511 		rc = iscsi_fuzz_read_pdu(conn);
512 		if (rc == 0) {
513 			break;
514 		} else if (rc < 0) {
515 			return rc;
516 		}
517 	}
518 
519 	return i;
520 }
521 
522 static void
fuzz_iscsi_send_login_request(struct fuzz_iscsi_dev_ctx * dev_ctx,uint8_t session_type)523 fuzz_iscsi_send_login_request(struct fuzz_iscsi_dev_ctx *dev_ctx, uint8_t session_type)
524 {
525 	struct fuzz_iscsi_io_ctx *io_ctx = NULL;
526 	struct spdk_iscsi_pdu *req_pdu;
527 	struct iscsi_bhs_login_req *login_req;
528 	struct spdk_iscsi_conn *conn = dev_ctx->conn;
529 
530 	req_pdu = iscsi_get_pdu(conn);
531 	req_pdu->writev_offset = 0;
532 	req_pdu->hdigest_valid_bytes = 0;
533 	req_pdu->ahs_valid_bytes = 0;
534 	req_pdu->data_buf_len = 8192;
535 	req_pdu->data = calloc(1, 8192);
536 	assert(req_pdu->data != NULL);
537 	req_pdu->data_segment_len = 0;
538 
539 	login_req = (struct iscsi_bhs_login_req *)&req_pdu->bhs;
540 	io_ctx = &dev_ctx->io_ctx;
541 	io_ctx->req.login_req = login_req;
542 	io_ctx->req.login_req->version_min = 0;
543 	/* a new session */
544 	io_ctx->req.login_req->tsih = 0;
545 
546 	req_pdu->bhs.opcode = ISCSI_OP_LOGIN;
547 	req_pdu->bhs.immediate = 1;
548 	req_pdu->bhs.reserved = 0;
549 	req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
550 	req_pdu->bhs.total_ahs_len = 0;
551 
552 	/* An initiator that chooses to operate without iSCSI security and with
553 	 * all the operational parameters taking the default values issues the
554 	 * Login with the T bit set to 1, the CSG set to
555 	 * LoginOperationalNegotiation, and the NSG set to FullFeaturePhase.
556 	 *
557 	 * Byte /	   0	   |	   1	   |	   2	   |	   3	   |
558 	 *	   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
559 	 *	   +---------------+---------------+---------------+---------------+
560 	 *	  0|.|1| 0x03	   |T|C|.|.|CSG|NSG| Version-max   | Version-min   |
561 	 */
562 	req_pdu->bhs.flags = ISCSI_LOGIN_TRANSIT | (ISCSI_OPERATIONAL_NEGOTIATION_PHASE << 2) |
563 			     ISCSI_FULL_FEATURE_PHASE;
564 
565 	req_pdu->data_segment_len = iscsi_append_text("InitiatorName", g_init_name,
566 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
567 	req_pdu->data_segment_len = iscsi_append_text("HeaderDigest", "None",
568 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
569 	req_pdu->data_segment_len = iscsi_append_text("DataDigest", "None",
570 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
571 	req_pdu->data_segment_len = iscsi_append_text("DefaultTime2Wait", "2",
572 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
573 	req_pdu->data_segment_len = iscsi_append_text("DefaultTime2Retain", "0",
574 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
575 	req_pdu->data_segment_len = iscsi_append_text("IFMarker", "No",
576 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
577 	req_pdu->data_segment_len = iscsi_append_text("OFMarker", "No",
578 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
579 	req_pdu->data_segment_len = iscsi_append_text("ErrorRecoveryLevel", "0",
580 				    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
581 
582 	if (session_type == SESSION_TYPE_DISCOVERY) {
583 		/* Discovery PDU */
584 		conn->sess->session_type = SESSION_TYPE_DISCOVERY;
585 		req_pdu->data_segment_len = iscsi_append_text("SessionType", "Discovery",
586 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
587 		req_pdu->data_segment_len = iscsi_append_text("MaxRecvDataSegmentLength", "32768",
588 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
589 	} else {
590 		/* Login PDU */
591 		conn->sess->session_type = SESSION_TYPE_NORMAL;
592 		req_pdu->data_segment_len = iscsi_append_text("SessionType", "Normal",
593 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
594 		req_pdu->data_segment_len = iscsi_append_text("TargetName", g_tgt_name,
595 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
596 		req_pdu->data_segment_len = iscsi_append_text("InitialR2T", "No",
597 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
598 		req_pdu->data_segment_len = iscsi_append_text("ImmediateData", "Yes",
599 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
600 		req_pdu->data_segment_len = iscsi_append_text("MaxBurstLength", "16776192",
601 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
602 		req_pdu->data_segment_len = iscsi_append_text("FirstBurstLength", "262144",
603 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
604 		req_pdu->data_segment_len = iscsi_append_text("MaxOutstandingR2T", "1",
605 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
606 		req_pdu->data_segment_len = iscsi_append_text("MaxConnections", "1",
607 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
608 		req_pdu->data_segment_len = iscsi_append_text("DataPDUInOrder", "Yes",
609 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
610 		req_pdu->data_segment_len = iscsi_append_text("DataSequenceInOrder", "Yes",
611 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
612 		req_pdu->data_segment_len = iscsi_append_text("MaxRecvDataSegmentLength", "262144",
613 					    req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
614 	}
615 
616 	DSET24(req_pdu->bhs.data_segment_len, req_pdu->data_segment_len);
617 	iscsi_conn_write_pdu(conn, req_pdu, iscsi_conn_pdu_generic_complete, NULL);
618 }
619 
620 static void
fuzz_iscsi_send_logout_request(struct fuzz_iscsi_dev_ctx * dev_ctx)621 fuzz_iscsi_send_logout_request(struct fuzz_iscsi_dev_ctx *dev_ctx)
622 {
623 	struct fuzz_iscsi_io_ctx *io_ctx = NULL;
624 	struct spdk_iscsi_pdu *req_pdu;
625 	struct iscsi_bhs_logout_req *logout_req;
626 	struct spdk_iscsi_conn *conn = dev_ctx->conn;
627 
628 	conn->is_logged_out = true;
629 
630 	req_pdu = iscsi_get_pdu(conn);
631 	req_pdu->writev_offset = 0;
632 	req_pdu->hdigest_valid_bytes = 0;
633 	req_pdu->ahs_valid_bytes = 0;
634 	req_pdu->data_buf_len = 0;
635 
636 	logout_req = (struct iscsi_bhs_logout_req *)&req_pdu->bhs;
637 	io_ctx = &dev_ctx->io_ctx;
638 	io_ctx->req.logout_req = logout_req;
639 
640 	req_pdu->bhs.opcode = ISCSI_OP_LOGOUT;
641 	req_pdu->bhs.immediate = 1;
642 	req_pdu->bhs.reserved = 0;
643 	req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
644 	req_pdu->bhs.total_ahs_len = 0;
645 	req_pdu->bhs.flags = 0;
646 
647 	DSET24(req_pdu->bhs.data_segment_len, 0);
648 	iscsi_conn_write_pdu(conn, req_pdu, iscsi_conn_pdu_generic_complete, conn);
649 }
650 
651 static void
iscsi_fuzz_conn_reset(struct spdk_iscsi_conn * conn,struct spdk_iscsi_sess * sess)652 iscsi_fuzz_conn_reset(struct spdk_iscsi_conn *conn, struct spdk_iscsi_sess *sess)
653 {
654 	conn->sess = sess;
655 	conn->data_in_cnt = 0;
656 	conn->params = NULL;
657 	conn->header_digest = true;
658 	conn->data_digest = false;
659 	conn->header_digest = 0;
660 	conn->MaxRecvDataSegmentLength = 8192;
661 	conn->full_feature = 0;
662 	conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
663 	conn->pdu_in_progress = NULL;
664 	conn->is_logged_out = 0;
665 }
666 
667 static void
iscsi_fuzz_sock_connect(struct spdk_iscsi_conn * conn)668 iscsi_fuzz_sock_connect(struct spdk_iscsi_conn *conn)
669 {
670 	const char *host = g_tgt_ip;
671 	const char *port = g_tgt_port;
672 	char saddr[INET6_ADDRSTRLEN], caddr[INET6_ADDRSTRLEN];
673 	uint16_t cport, sport;
674 	int rc = 0;
675 
676 	conn->sock = spdk_sock_connect(host, spdk_strtol(port, 10), NULL);
677 	if (conn->sock == NULL) {
678 		fprintf(stderr, "connect error(%d): %s\n", errno, spdk_strerror(errno));
679 		spdk_sock_close(&conn->sock);
680 		return;
681 	}
682 	fprintf(stderr, "\nConnecting to the server on %s:%s\n", host, port);
683 
684 	rc = spdk_sock_getaddr(conn->sock, saddr, sizeof(saddr), &sport, caddr, sizeof(caddr), &cport);
685 	if (rc < 0) {
686 		fprintf(stderr, "Cannot get connection addresses\n");
687 		spdk_sock_close(&conn->sock);
688 		return;
689 	}
690 
691 	fprintf(stderr, "Connection accepted from (%s, %hu) to (%s, %hu)\n", caddr, cport, saddr, sport);
692 
693 }
694 
695 static void
check_successful_op(struct fuzz_iscsi_dev_ctx * dev_ctx,struct fuzz_iscsi_io_ctx * io_ctx)696 check_successful_op(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
697 {
698 	if (g_is_valid_opcode) {
699 		fprintf(stderr, "Sent a valid opcode PDU.\n");
700 		dev_ctx->num_valid_pdus++;
701 	} else {
702 		fprintf(stderr, "Sent an invalid opcode PDU.\n");
703 	}
704 }
705 
706 /* submit requests begin */
707 static void
dev_submit_requests(struct fuzz_iscsi_dev_ctx * dev_ctx)708 dev_submit_requests(struct fuzz_iscsi_dev_ctx *dev_ctx)
709 {
710 	struct fuzz_iscsi_io_ctx *io_ctx = NULL;
711 	uint8_t opcode;
712 	struct spdk_iscsi_pdu *req_pdu;
713 	struct iscsi_bhs *bhs;
714 	struct iscsi_bhs_nop_out *nop_out_req;
715 	struct iscsi_bhs_scsi_req *scsi_req;
716 	struct iscsi_bhs_task_req *task_req;
717 	struct iscsi_bhs_text_req *text_req;
718 	struct iscsi_bhs_data_out *data_out_req;
719 	struct iscsi_bhs_snack_req *snack_req;
720 	unsigned int rand_seed;
721 	bool is_p99;
722 
723 	g_is_valid_opcode = true;
724 
725 	/* Random PDU */
726 	opcode = rand() % 0x3f;
727 	fprintf(stderr,	"Random request bhs.opcode of Initiator is 0x%x.\n", opcode);
728 
729 	if ((opcode == ISCSI_OP_LOGIN) || (opcode == ISCSI_OP_LOGOUT)) {
730 		/* only need send next */
731 		fprintf(stderr, "LOGIN and LOGOUT opcodes are ignored here.\n");
732 		return;
733 	}
734 
735 	req_pdu = iscsi_get_pdu(dev_ctx->conn);
736 	req_pdu->writev_offset = 0;
737 	req_pdu->hdigest_valid_bytes = 0;
738 	req_pdu->ahs_valid_bytes = 0;
739 	req_pdu->data_buf_len = 0;
740 
741 	dev_ctx->conn->sess->session_type = SESSION_TYPE_NORMAL;
742 
743 	io_ctx = &dev_ctx->io_ctx;
744 
745 	switch (opcode) {
746 	case ISCSI_OP_NOPOUT:
747 		nop_out_req = (struct iscsi_bhs_nop_out *)&req_pdu->bhs;
748 		io_ctx->req.nop_out_req = nop_out_req;
749 		break;
750 	case ISCSI_OP_SCSI:
751 		scsi_req = (struct iscsi_bhs_scsi_req *)&req_pdu->bhs;
752 		io_ctx->req.scsi_req = scsi_req;
753 		break;
754 	case ISCSI_OP_TASK:
755 		task_req = (struct iscsi_bhs_task_req *)&req_pdu->bhs;
756 		io_ctx->req.task_req = task_req;
757 		break;
758 	case ISCSI_OP_TEXT:
759 		text_req = (struct iscsi_bhs_text_req *)&req_pdu->bhs;
760 		io_ctx->req.text_req = text_req;
761 		break;
762 	case ISCSI_OP_SCSI_DATAOUT:
763 		data_out_req = (struct iscsi_bhs_data_out *)&req_pdu->bhs;
764 		io_ctx->req.data_out_req = data_out_req;
765 		break;
766 	case ISCSI_OP_SNACK:
767 		snack_req = (struct iscsi_bhs_snack_req *)&req_pdu->bhs;
768 		io_ctx->req.snack_req = snack_req;
769 		break;
770 	default:
771 		bhs = (struct iscsi_bhs *)&req_pdu->bhs;
772 		io_ctx->req.bhs = bhs;
773 		g_is_valid_opcode = false;
774 		break;
775 	}
776 
777 	prep_iscsi_pdu_bhs_opcode_cmd(dev_ctx, io_ctx);
778 	io_ctx->req.bhs->opcode = opcode;
779 	req_pdu->bhs.opcode = opcode;
780 	req_pdu->bhs.immediate = 1;
781 	req_pdu->bhs.reserved = 0;
782 	req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
783 	req_pdu->bhs.total_ahs_len = 0;
784 	req_pdu->bhs.stat_sn = 0;
785 	DSET24(req_pdu->bhs.data_segment_len, 0);
786 
787 	if (opcode <= ISCSI_OP_TEXT) {
788 		rand_seed = time(NULL);
789 		is_p99 = rand_r(&rand_seed) % 100 == 0 ? true : false;
790 		if (!is_p99) { /* Remaining 1% */
791 			switch (opcode) {
792 			case ISCSI_OP_NOPOUT:
793 				if (req_pdu->bhs.immediate) {
794 					io_ctx->req.nop_out_req->cmd_sn = dev_ctx->current_cmd_sn;
795 				} else {
796 					io_ctx->req.nop_out_req->cmd_sn = dev_ctx->current_cmd_sn++;
797 				}
798 				break;
799 			case ISCSI_OP_SCSI:
800 				if (req_pdu->bhs.immediate) {
801 					io_ctx->req.scsi_req->cmd_sn = dev_ctx->current_cmd_sn;
802 				} else {
803 					io_ctx->req.scsi_req->cmd_sn = dev_ctx->current_cmd_sn++;
804 				}
805 				break;
806 			case ISCSI_OP_TASK:
807 				if (req_pdu->bhs.immediate) {
808 					io_ctx->req.task_req->cmd_sn = dev_ctx->current_cmd_sn;
809 				} else {
810 					io_ctx->req.task_req->cmd_sn = dev_ctx->current_cmd_sn++;
811 				}
812 				break;
813 			case ISCSI_OP_TEXT:
814 				if (req_pdu->bhs.immediate) {
815 					io_ctx->req.text_req->cmd_sn = dev_ctx->current_cmd_sn;
816 				} else {
817 					io_ctx->req.text_req->cmd_sn = dev_ctx->current_cmd_sn++;
818 				}
819 				break;
820 			default:
821 				break;
822 			}
823 		}
824 	}
825 
826 	if (opcode == ISCSI_OP_SCSI) {
827 		/* avoid ((R_bit != 0) && (W_bit != 0)) is true */
828 		io_ctx->req.scsi_req->read_bit = 0;
829 		io_ctx->req.scsi_req->write_bit = 0;
830 	}
831 
832 	if (opcode == ISCSI_OP_TEXT) {
833 		/* avoid: (F_bit && C_bit) is true */
834 		io_ctx->req.text_req->flags = 0;
835 		/* avoid: correct itt is not equal to the current itt */
836 		io_ctx->req.text_req->itt = 0;
837 	}
838 
839 	fprintf(stderr, "Dumping this request bhs contents now.\n");
840 	print_req_obj(dev_ctx, io_ctx);
841 
842 	check_successful_op(dev_ctx, io_ctx);
843 	dev_ctx->num_sent_pdus++;
844 
845 	iscsi_conn_write_pdu(dev_ctx->conn, req_pdu,
846 			     iscsi_conn_pdu_generic_complete, NULL);
847 }
848 /* submit requests end */
849 
850 static int
poll_dev(void * ctx)851 poll_dev(void *ctx)
852 {
853 	struct fuzz_iscsi_dev_ctx *dev_ctx = ctx;
854 	struct spdk_iscsi_conn *conn = dev_ctx->conn;
855 	uint64_t current_ticks;
856 	struct spdk_iscsi_pdu *pdu, *tmp;
857 
858 	current_ticks = spdk_get_ticks();
859 	if (current_ticks > g_runtime_ticks) {
860 		g_run = false;
861 	}
862 
863 	if (!g_run) {
864 		/* Logout PDU */
865 		fuzz_iscsi_send_logout_request(dev_ctx);
866 		fuzz_iscsi_handle_incoming_pdus(conn);
867 
868 		TAILQ_FOREACH_SAFE(pdu, &g_get_pdu_list, tailq, tmp) {
869 			TAILQ_REMOVE(&g_get_pdu_list, pdu, tailq);
870 			iscsi_put_pdu(pdu);
871 		}
872 
873 		spdk_sock_close(&conn->sock);
874 
875 		TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp) {
876 			TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
877 			iscsi_put_pdu(pdu);
878 		}
879 
880 		free(conn);
881 
882 		spdk_poller_unregister(&dev_ctx->poller);
883 		__sync_sub_and_fetch(&g_num_active_threads, 1);
884 
885 		return -1;
886 	}
887 
888 	if (conn->is_logged_out) {
889 		spdk_sock_close(&conn->sock);
890 		iscsi_fuzz_conn_reset(conn, &dev_ctx->sess);
891 		iscsi_fuzz_sock_connect(conn);
892 		usleep(1000);
893 
894 		/* Login PDU */
895 		fuzz_iscsi_send_login_request(dev_ctx, SESSION_TYPE_NORMAL);
896 	} else if (conn->full_feature == 1) {
897 		dev_submit_requests(dev_ctx);
898 	}
899 
900 	spdk_sock_flush(conn->sock);
901 
902 	fuzz_iscsi_handle_incoming_pdus(conn);
903 
904 	return 0;
905 }
906 
907 static void
start_io(void * ctx)908 start_io(void *ctx)
909 {
910 	struct fuzz_iscsi_dev_ctx *dev_ctx = ctx;
911 
912 	dev_ctx->sess.ExpCmdSN = 0;
913 	dev_ctx->sess.MaxCmdSN = 64;
914 	dev_ctx->sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
915 	dev_ctx->sess.MaxOutstandingR2T = 1;
916 	dev_ctx->sess.tag = 1;
917 	dev_ctx->sess.tsih = 256;
918 
919 	dev_ctx->conn = calloc(1, sizeof(*dev_ctx->conn));
920 	assert(dev_ctx->conn != NULL);
921 	TAILQ_INIT(&dev_ctx->conn->write_pdu_list);
922 
923 	iscsi_fuzz_conn_reset(dev_ctx->conn, &dev_ctx->sess);
924 	iscsi_fuzz_sock_connect(dev_ctx->conn);
925 	usleep(1000);
926 
927 	/* Login PDU */
928 	fuzz_iscsi_send_login_request(dev_ctx, SESSION_TYPE_NORMAL);
929 
930 	if (g_random_seed) {
931 		dev_ctx->random_seed = g_random_seed;
932 	} else {
933 		dev_ctx->random_seed = spdk_get_ticks();
934 	}
935 
936 	dev_ctx->poller = SPDK_POLLER_REGISTER(poll_dev, dev_ctx, 0);
937 	if (dev_ctx->poller == NULL) {
938 		return;
939 	}
940 }
941 
942 static int
check_app_completion(void * ctx)943 check_app_completion(void *ctx)
944 {
945 	if (g_num_active_threads == 0) {
946 		spdk_poller_unregister(&g_app_completion_poller);
947 		printf("Fuzzing completed. Shutting down the fuzz application.\n\n");
948 		cleanup();
949 		spdk_app_stop(0);
950 	}
951 	return 0;
952 }
953 
954 static void
begin_iscsi_fuzz(void * ctx)955 begin_iscsi_fuzz(void *ctx)
956 {
957 	struct fuzz_iscsi_dev_ctx *dev_ctx;
958 	int rc;
959 
960 	g_runtime_ticks = spdk_get_ticks() + g_runtime * spdk_get_ticks_hz();
961 
962 	g_valid_buffer = spdk_malloc(0x1000, 0x200, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_SHARE);
963 	if (g_valid_buffer == NULL) {
964 		fprintf(stderr, "Failed to allocate a valid buffer for random PDUs\n");
965 		goto out;
966 	}
967 
968 	rc = fuzz_iscsi_dev_init();
969 	if (rc) {
970 		fprintf(stderr, "fuzz_iscsi_dev_init() failed.\n");
971 		goto out;
972 	}
973 
974 	TAILQ_FOREACH(dev_ctx, &g_dev_list, link) {
975 		assert(dev_ctx->thread != NULL);
976 		spdk_thread_send_msg(dev_ctx->thread, start_io, dev_ctx);
977 		__sync_add_and_fetch(&g_num_active_threads, 1);
978 	}
979 
980 	g_app_completion_poller = SPDK_POLLER_REGISTER(check_app_completion, NULL, 1000000);
981 	if (g_app_completion_poller == NULL) {
982 		fprintf(stderr, "Failed to register a poller for test completion checking.\n");
983 		goto out;
984 	}
985 
986 	return;
987 out:
988 	cleanup();
989 	spdk_app_stop(0);
990 }
991 
992 static void
iscsi_fuzz_usage(void)993 iscsi_fuzz_usage(void)
994 {
995 	fprintf(stderr, " -T <path>                 iSCSI Target IP address.\n");
996 	fprintf(stderr, " -S <integer>              Seed value for test.\n");
997 	fprintf(stderr,
998 		" -t <integer>              Time in seconds to run the fuzz test. Only valid if -j is not specified.\n");
999 }
1000 
1001 static int
iscsi_fuzz_parse(int ch,char * arg)1002 iscsi_fuzz_parse(int ch, char *arg)
1003 {
1004 	int64_t error_test;
1005 
1006 	switch (ch) {
1007 	case 'T':
1008 		g_tgt_ip = optarg;
1009 		break;
1010 	case 'S':
1011 		error_test = spdk_strtol(arg, 10);
1012 		if (error_test < 0) {
1013 			fprintf(stderr, "Invalid value supplied for the random seed.\n");
1014 			return -1;
1015 		} else {
1016 			g_random_seed = error_test;
1017 		}
1018 		break;
1019 	case 't':
1020 		g_runtime = spdk_strtol(optarg, 10);
1021 		if (g_runtime <= 0 || g_runtime > MAX_RUNTIME_S) {
1022 			fprintf(stderr, "You must supply a positive runtime value less than %d.\n", MAX_RUNTIME_S);
1023 			return -1;
1024 		}
1025 		break;
1026 	case '?':
1027 	default:
1028 		iscsi_fuzz_usage();
1029 		return -EINVAL;
1030 	}
1031 
1032 	return 0;
1033 }
1034 
1035 int
main(int argc,char ** argv)1036 main(int argc, char **argv)
1037 {
1038 	struct spdk_app_opts opts = {};
1039 	int rc;
1040 
1041 	g_runtime = DEFAULT_RUNTIME;
1042 	srand((unsigned)time(0));
1043 
1044 	TAILQ_INIT(&g_get_pdu_list);
1045 
1046 	spdk_app_opts_init(&opts, sizeof(opts));
1047 	opts.name = "iscsi_fuzz";
1048 	opts.rpc_addr = NULL;
1049 
1050 	if ((rc = spdk_app_parse_args(argc, argv, &opts, "T:S:t:", NULL, iscsi_fuzz_parse,
1051 				      iscsi_fuzz_usage) != SPDK_APP_PARSE_ARGS_SUCCESS)) {
1052 		return rc;
1053 	}
1054 
1055 	rc = spdk_app_start(&opts, begin_iscsi_fuzz, NULL);
1056 
1057 	spdk_app_fini();
1058 	return rc;
1059 }
1060