Skip to contentSkip to conversation

use-after-free while iterating session->conn_list (two places)

PATCH In review Anton Kovalenko 2 participants 2 messages May 3, 2017 – May 10, 2017 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 use-after-free while iterating session->conn_list (two places)

​

Hi,
Please consider applying two patches in the attachment.
The problem (accessing session->conn_list after freeing session) was discovered using valgrind.

Note that while list_for_each_entry_safe is safe against current list element destruction,
it is UNSAFE against the traversed list_head (third argument) becoming invalid during iteration. That's exactly what happens when the last connection of a session goes away (conn_exit -> session_put -> use-after-free).

Signature · 2 lines
Best regards,
Anton Kovalenko

From 16e64b10cbe829d0b0d06d22a54efd79b161427a Mon Sep 17 00:00:00 2001
From: Anton Kovalenko <[email protected]>
Date: Wed, 3 May 2017 14:46:38 +0300
Subject: [PATCH 2/3] Avoid dangling session reference in login_security_done

---
 usr/iscsi/iscsid.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
usr/iscsi/iscsid.c +2 -1
index 700e2af..d5fe6b3 100644
@@ -267,11 +267,12 @@ static void login_security_done(struct iscsi_connection *conn)
 			struct iscsi_connection *ent, *next;
 
 			/* do session reinstatement */
-
+			session_get(session);
 			list_for_each_entry_safe(ent, next, &session->conn_list,
 						 clist) {
 				conn_close(ent);
 			}
+			session_put(session);
 
 			session = NULL;
 		} else if (req->tsih != session->tsih) {
Signature · 1 line
2.11.0

From 67f7b51e6eddc55736a0bb85cbb3af95e73b68d6 Mon Sep 17 00:00:00 2001
From: Anton Kovalenko <[email protected]>
Date: Wed, 3 May 2017 12:46:53 +0300
Subject: [PATCH 3/3] Avoid session* dangling reference on forced target
destroy.

Closing the last connection of a session also releases the session itself, making session->conn_list inaccessible.

Wrapping conn_list iteration into session_get/session_put prevents session destruction while its conn_list is being iterated.

---
 usr/iscsi/target.c | 2 ++
 1 file changed, 2 insertions(+)
usr/iscsi/target.c +2 -0
index bf54190..7d71206 100644
@@ -411,9 +411,11 @@ void iscsi_target_destroy(int tid, int force)
 	}
 
 	list_for_each_entry_safe(session, stmp, &target->sessions_list, slist) {
+		session_get(session);
 		list_for_each_entry_safe(conn, ctmp, &session->conn_list, clist) {
 			conn_close(conn);
 		}
+		session_put(session);
 	}
 
 	if (!list_empty(&target->sessions_list)) {
Signature · 1 line
2.11.0
#1 lore raw
Quoted 5 lines · On Wed, 3 May 2017 16:35:45 +0000 Anton Kovalenko…

The problem (accessing session->conn_list after freeing session) was discovered using valgrind.

Note that while list_for_each_entry_safe is safe against current list element destruction,
it is UNSAFE against the traversed list_head (third argument) becoming invalid during iteration. That's exactly what happens when the last connection of a session goes away (conn_exit -> session_put -> use-after-free).

Great catch, applied both. Any other places like this?

Thanks a lot!