Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1650234pxy; Mon, 2 Aug 2021 07:05:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/uSB1vWqCsrq/yKklBObJFTOfs4zZCeTlsqo6qouWz8go0Km2m3ABx0f5lwhacKuEjBTr X-Received: by 2002:a05:6402:b19:: with SMTP id bm25mr19507784edb.213.1627913124029; Mon, 02 Aug 2021 07:05:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627913124; cv=none; d=google.com; s=arc-20160816; b=fmQBWsY5NJdFIUNlJFtzUWKNNdsvRuNusVTnkOqpZeAAynu3Mz0nfZMp3x5Uzx65UC 22ezMVevzi6AukMFPjxQremCGAnqftnTEoLlTN4BZpahXlNH3+cRHjPzpjXNyQls6xfg mtF7pYL3IZqINfK7r6Q6o3VekL+NdfwSEryybprKvdZyzblC9FcjAqy0mj59uOEAVzWu XODqtTMZ9F3m6LSVlZXlEjhglP9/7QpyaADz3LTFSxWj/V2QtURKo3FqnAsLJIUKWFuO 9uPkDNVMI/OpA6jbf3ZmGKLJEI2ryAKJ/fpd2EusOCZQTdlwzkoWreVzLy3MdsIfkkVc Cnrw== 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=unSJeTaxPhELQ3+KJ4/zyKuMweaY9H0pajMTLL27Zz+dC2H8UaTNvy5LyUHZX5oRmP CVOMgehu1NJ7PoUCxRU75wqPuc0wUmzKxHStn9CFtAakV2tUnN8n3mi9MgQKzs5SfHTZ wss2I3G4eQvmtFQVm+YhmssUyG6D3ItEr1pOK33rFx/QSpIeJcjX0iouca+Caf1FyKjC ThFj0PqwagLZ9K5Sl4MLjqCUQKBRLYv7u9poOSLKqw/l763cveX99o6WO3ATsigJ2/4w 0AwHeQwOfA2Uoioc/0C9T87KprF8Yz17i28OSxEjXiNPx5LSfWPxcrscx4Sf8eG1c2zt QWXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=usDSF1TZ; 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 t6si8948929edj.154.2021.08.02.07.04.59; Mon, 02 Aug 2021 07:05:24 -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=usDSF1TZ; 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 S235282AbhHBODf (ORCPT + 99 others); Mon, 2 Aug 2021 10:03:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:40832 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235426AbhHBNzk (ORCPT ); Mon, 2 Aug 2021 09:55:40 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1FF3660FC4; Mon, 2 Aug 2021 13:54:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912452; bh=+dsKwnnReKoHkgjNqx1g7+OyrDcwFH6QCnfIcEnTTsU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=usDSF1TZtgPciPBsjd4uElXcuNkNBHPl7UMRDdQ/4pAGFN4/8z5FgbGTahGZva+4k +K4BfP3tSOR9XE2Rwj2ykGahSa3/vrHUj4z+NwaHTY/1ZdtG+T/aiiPgi2rnlyz40O 2SeKOugOokrZOqLfyIG+uDyxXyGe4Ld4s5crCqAw= 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.10 67/67] can: j1939: j1939_session_deactivate(): clarify lifetime of session object Date: Mon, 2 Aug 2021 15:45:30 +0200 Message-Id: <20210802134341.333745979@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134339.023067817@linuxfoundation.org> References: <20210802134339.023067817@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; }