Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1650639pxy; Mon, 2 Aug 2021 07:05:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgubB5IFCC2ladUHgUyXTXi2eppwLrcQ8mZ9/OC3x1gR7CQsxz7jxZ+M7EeEMdCimso3pO X-Received: by 2002:a17:906:1dd2:: with SMTP id v18mr15156603ejh.506.1627913145608; Mon, 02 Aug 2021 07:05:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627913145; cv=none; d=google.com; s=arc-20160816; b=gRhaQGizTsfdimETov4bXHpkMyWSgxoA09Wsr2I2teowca0PjYJpuek0gzWOlWGkBj jIjzB/54HSBzpVRstykpRFUrrCn3BZ0bLw8O30QOE5Dwb2DuF9t+k1y5kJCwkHSef1UV VJLGXKTTlmlc2NwMnVwKMYyOZ+b/Vbtod47D4tbTn/YojzXjeXylNaUTMVg4Q0W73dI6 7+sMmMiR2uYaZifNssrOeEOylYQ4Bsuv3FTf3QxJXbF0tKKd4SgefcvSrzGJG9PwEUAZ BpmlsrKRfm8TcSWo6/ElXoGEmG4MetC/zl6AVB3XCUyfdOcmNGDmP9hL0RVe+mTpfV2Y K3og== 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=AKvr6kDTiFcU2zS2oRgYSpzJNaKUAcDRRKdKDeLZQvJtw3KiwQbb2a/VVJB3zQuu2o ygcbCDqxjEkgotpg0WaRzLm/rK5L5fN3CYsOWMSlglhiGdmJldkboSwEmQ92h8U90GuI zsw2sJxRGIW83/xSZreCZd8fI3Cz7uACdjCY0KpPXTo7pbwFoYf5HeiJGHpElJymZl7E KaiIcVOpe/4mT6gLi8qp4CUGYyMDkEOvsjuMYp18wEAEUl7T5c0WgBX+/dJ11iV2SYkg fpOTOx59K/poBo7zLdr+qy0pIM+6IZnNNA2XYacyLCQeuL2F4xkbmyUrp4LOK9ee1Bel ZgTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=znAjx3ZM; 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 n2si10340435edi.122.2021.08.02.07.05.22; Mon, 02 Aug 2021 07:05:45 -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=znAjx3ZM; 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 S236037AbhHBOBm (ORCPT + 99 others); Mon, 2 Aug 2021 10:01:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:34568 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234620AbhHBNwz (ORCPT ); Mon, 2 Aug 2021 09:52:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4DDC160555; Mon, 2 Aug 2021 13:51:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912308; bh=+dsKwnnReKoHkgjNqx1g7+OyrDcwFH6QCnfIcEnTTsU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=znAjx3ZMIzqsWV0VAYMvoyVP2tGPdxkqCknjHBi628tzURjPqik3N0TcmhEQ74Wii yq//58kPElZzS6ffygGiiUbBY/bqpfqZTRZYDFXj294jTLXna1xgTKefKb7OzIuniU A+OmUPcEumZAzs5XFYCnYxjd2SYxMUkzMPR0LgGE= 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.4 40/40] can: j1939: j1939_session_deactivate(): clarify lifetime of session object Date: Mon, 2 Aug 2021 15:45:20 +0200 Message-Id: <20210802134336.673660790@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134335.408294521@linuxfoundation.org> References: <20210802134335.408294521@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; }