Skip to content

Commit 0c71437

Browse files
oleremmarckleinebudde
authored andcommitted
can: j1939: j1939_session_deactivate(): clarify lifetime of session object
The j1939_session_deactivate() is decrementing the session ref-count and potentially can free() the session. This would cause use-after-free situation. However, the code calling j1939_session_deactivate() does always hold another reference to the session, so that it would not be free()ed in this code path. This patch adds a comment to make this clear and a WARN_ON, to ensure that future changes will not violate this requirement. Further this patch avoids dereferencing the session pointer as a precaution to avoid use-after-free if the session is actually free()ed. Fixes: 9d71dd0 ("can: add support of SAE J1939 protocol") Link: https://lore.kernel.org/r/[email protected] Reported-by: Xiaochen Zou <[email protected]> Signed-off-by: Oleksij Rempel <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 54f9333 commit 0c71437

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

net/can/j1939/transport.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1075,11 +1075,16 @@ static bool j1939_session_deactivate_locked(struct j1939_session *session)
10751075

10761076
static bool j1939_session_deactivate(struct j1939_session *session)
10771077
{
1078+
struct j1939_priv *priv = session->priv;
10781079
bool active;
10791080

1080-
j1939_session_list_lock(session->priv);
1081+
j1939_session_list_lock(priv);
1082+
/* This function should be called with a session ref-count of at
1083+
* least 2.
1084+
*/
1085+
WARN_ON_ONCE(kref_read(&session->kref) < 2);
10811086
active = j1939_session_deactivate_locked(session);
1082-
j1939_session_list_unlock(session->priv);
1087+
j1939_session_list_unlock(priv);
10831088

10841089
return active;
10851090
}

0 commit comments

Comments
 (0)