diff mbox series

scsi: libiscsi: Fix possible null-pointer dereferences in iscsi_conn_get_addr_param()

Message ID 20190729091339.30815-1-baijiaju1990@gmail.com (mailing list archive)
State Changes Requested
Headers show
Series scsi: libiscsi: Fix possible null-pointer dereferences in iscsi_conn_get_addr_param() | expand

Commit Message

Jia-Ju Bai July 29, 2019, 9:13 a.m. UTC
In iscsi_conn_get_addr_param(), sin6 may be NULL when reaching lines
3479 and 3487:
    len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
    len = sprintf(buf, "%hu\n", be16_to_cpu(sin6->sin6_port));

Thus, possible null-pointer dereferences may occur.

To fix these bugs, sin6 is checked before being used, and len is
initialized to -EINVAL.

These bugs are found by a static analysis tool STCheck written by us.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 drivers/scsi/libiscsi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Markus Elfring Aug. 27, 2019, 5:33 a.m. UTC | #1
> These bugs are found by a static analysis tool STCheck written by us.

Would you like to improve any more commit descriptions also by
adjusting such a wording?

Regards,
Markus
diff mbox series

Patch

diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
index ebd47c0cf9e9..58f072d5c43f 100644
--- a/drivers/scsi/libiscsi.c
+++ b/drivers/scsi/libiscsi.c
@@ -3457,7 +3457,7 @@  int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
 {
 	struct sockaddr_in6 *sin6 = NULL;
 	struct sockaddr_in *sin = NULL;
-	int len;
+	int len = -EINVAL;
 
 	switch (addr->ss_family) {
 	case AF_INET:
@@ -3475,14 +3475,14 @@  int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
 	case ISCSI_HOST_PARAM_IPADDRESS:
 		if (sin)
 			len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
-		else
+		else if (sin6)
 			len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
 		break;
 	case ISCSI_PARAM_CONN_PORT:
 	case ISCSI_PARAM_LOCAL_PORT:
 		if (sin)
 			len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
-		else
+		else if (sin6)
 			len = sprintf(buf, "%hu\n",
 				      be16_to_cpu(sin6->sin6_port));
 		break;