Skip to content

Commit

Permalink
can: j1939: j1939_session_deactivate(): clarify lifetime of session o…
Browse files Browse the repository at this point in the history
…bject

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/20210714111602.24021-1-o.rempel@pengutronix.de
Reported-by: Xiaochen Zou <xzou017@ucr.edu>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
  • Loading branch information
olerem authored and marckleinebudde committed Jul 24, 2021
1 parent 54f9333 commit 0c71437
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions net/can/j1939/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,16 @@ static bool j1939_session_deactivate_locked(struct j1939_session *session)

static bool j1939_session_deactivate(struct j1939_session *session)
{
struct j1939_priv *priv = session->priv;
bool active;

j1939_session_list_lock(session->priv);
j1939_session_list_lock(priv);
/* This function should be called with a session ref-count of at
* least 2.
*/
WARN_ON_ONCE(kref_read(&session->kref) < 2);
active = j1939_session_deactivate_locked(session);
j1939_session_list_unlock(session->priv);
j1939_session_list_unlock(priv);

return active;
}
Expand Down

0 comments on commit 0c71437

Please sign in to comment.