Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1659379pxy; Mon, 2 Aug 2021 07:15:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzyl96MLBiXB3cczJK2TS7/Oqax3W4aR3GL1iJAlr90I8IrGA6R/CXdyFfkQFurGotHaVOq X-Received: by 2002:a6b:8d0d:: with SMTP id p13mr540426iod.8.1627913757385; Mon, 02 Aug 2021 07:15:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627913757; cv=none; d=google.com; s=arc-20160816; b=YbAEmI3vxWT+O26kMtN7igoKtGJdilZI1jlus5eOkt9dxI2Xi7Bo26UO3YKPGy1du6 hR7pReQz7Sj7pdaEBwmu8UN7Znq0QoQQ7223+iOHmwSE3qIajr4qby3wb8OlR4dAqnRF crajKQ0+EQF1D+aZdSujRJHRH/v3in9+116jx9uhIVrJHpshB915fEAdQsVhwB67YEX1 g0MeXiIWdUGXKwb8Zwm/RV1gfOHkHytBwcz12MWinxzEhtO+JMxZzl7Fqeun12wstyHU XjkXMV4iR8to2okhmcHpRyxYaGJS1Xq0G+QP62ZCQo4Pcebx9JxY5BKrahS8YBls86f9 Q6NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sDI3H4jVgT9k/i27D398j9V9yet6oQimgd3v5/9B45A=; b=pSrgr0WjhKlMGKfon23Km8j6yoliDdC4/qoi7WGVvheiUzd9wOKEZVMOyHVdP6UxxV CV+rYE/bd5PIMicKg11aBvCAY38UHKZDAO3F1dqUsSqrvpoISnkI1D0rbnxRc0Gz2Top R1cQplNOEpnSSHEJ7rrA7dGV5Pf5wBEHQ3oRF2URaDN3KlhtmhbXEpy5ndCwSkCOsrJI p6b6yIG9YxGb5B+X1+dGtQd19H1GPVk2Sm9HDXLpPEAAPVg79Yqrl0Aupbb4ZClcUlzK oGRdFhPBEG0G/YhX/vgV1maHn7PIrlRiBZN9LGSDf4IdbHYLI8azwAbGK4ehfJppjc5l 9jZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UlrA2ZSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q17si5006114ilm.64.2021.08.02.07.15.46; Mon, 02 Aug 2021 07:15:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UlrA2ZSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238305AbhHBOOq (ORCPT + 99 others); Mon, 2 Aug 2021 10:14:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:48986 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236999AbhHBOE1 (ORCPT ); Mon, 2 Aug 2021 10:04:27 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DFB0661244; Mon, 2 Aug 2021 13:57:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912677; bh=+dsKwnnReKoHkgjNqx1g7+OyrDcwFH6QCnfIcEnTTsU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UlrA2ZSvakTQ1IxuyYGiQTpOQmp2ZT/oHw6iZtNZ+0U4OjFVvzG2c4ejJcT/syipX /nMT+YfIiwPaqrYBiZ3oKch7oV5EmHGjcey7sHJm6azaeBAItDLhL4QPpsOc5uwdAE Em5MWLAJTWZY/QhuieUe8MBcleJVkiQjXbRx7SRM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaochen Zou , Oleksij Rempel , Marc Kleine-Budde Subject: [PATCH 5.13 102/104] can: j1939: j1939_session_deactivate(): clarify lifetime of session object Date: Mon, 2 Aug 2021 15:45:39 +0200 Message-Id: <20210802134347.355455768@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134344.028226640@linuxfoundation.org> References: <20210802134344.028226640@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleksij Rempel commit 0c71437dd50dd687c15d8ca80b3b68f10bb21d63 upstream. 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: 9d71dd0c7009 ("can: add support of SAE J1939 protocol") Link: https://lore.kernel.org/r/20210714111602.24021-1-o.rempel@pengutronix.de Reported-by: Xiaochen Zou Signed-off-by: Oleksij Rempel Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- net/can/j1939/transport.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) --- a/net/can/j1939/transport.c +++ b/net/can/j1939/transport.c @@ -1075,11 +1075,16 @@ static bool j1939_session_deactivate_loc 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; }