Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp484539imn; Wed, 27 Jul 2022 11:29:50 -0700 (PDT) X-Google-Smtp-Source: AGRyM1shfqqth7HCNOulV1ojRCbf/OuNxGMorJjCtX7U7MQWQFx+xJQT+OMM78CThBHEcBT1rySf X-Received: by 2002:a17:90b:1e43:b0:1f2:5dd5:f1a8 with SMTP id pi3-20020a17090b1e4300b001f25dd5f1a8mr6028418pjb.144.1658946590076; Wed, 27 Jul 2022 11:29:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658946590; cv=none; d=google.com; s=arc-20160816; b=mnywNMtutpsxjPmm4r96O/wZlNRrT+lXaFFOocpa+Q/BmuyGBYLw8sKag/V+9ENtoq qgZkvkbNO6FRhtaAhhp9gLpzDo0eGc+ZL5SqGOPxOrBn78TJWIGu7KoEtYxj89QVU7OA roLqcmhYbrNeAOwBqrkNQzbaw126iZSX3UYVV275qgjCQtbJWt9R0hcL15cuaZGJ9ICk kc1qakg/dNphFY03Ol28Vroi91272PAaECUz0NJXZkVrMfKVxSyCbFX1GG3ESZez3we6 PNWceLXGlZCVdHGh2limqWivXzS6KbpKPEGddKMVDpFn6EzpsM1ItNRX9B9+xg7FVrJ3 jcMQ== 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=iAwvGJTF5uMJ8M0JK2vFwANA7AXym1QCNW0ateAGY0o=; b=i/H8fYouSVyvNEQHxqpWflqgjJTDkNxCpq7CGORhZcpL2c7LmeAhznSRaAr5KPrTQJ 2+pxItG1TjXcQ5QVIJe7gX+ZSCAQxTCPWT0U8Un5uTtuXhyRowp8Ska5jOyS6UeWjM5m mbb4HHuEFdcw5H78I1CzXOosqr/ib4PSegJJKcgnjVC1HcSGW6Hwyv5yFXXJQJat/7Ph lgN7lZpV+X7xEPllJkrk9NBx6acz1u7m2fi5I6mHlDnLX8eqG2pXcihMVfqpLoiOjhff G+EiHmPsgOasNdTTgquveDDq7QExLi4rDa9PbQupeEhz10KTgC93W1TJWPM3dTGi7SHX P8Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AnbXHEog; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t14-20020a170902b20e00b0016a17da4ad3si19053498plr.339.2022.07.27.11.29.34; Wed, 27 Jul 2022 11:29:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AnbXHEog; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S237743AbiG0Qch (ORCPT + 99 others); Wed, 27 Jul 2022 12:32:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236667AbiG0QcE (ORCPT ); Wed, 27 Jul 2022 12:32:04 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08F8F52FD4; Wed, 27 Jul 2022 09:26:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 98DB0B821C1; Wed, 27 Jul 2022 16:26:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00C31C433C1; Wed, 27 Jul 2022 16:25:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658939159; bh=BFe+5I4xOULB9vvt8wktkW2SV/HxHs/G7cPmPTVPfpY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AnbXHEog6+ZLtrFBy3/314pQs/CEcrk0QglXKLafr5i5dLwaiuV4WcT2hzcHS2k71 i8PEkHEa11OZc5ZiGcXJg6yWn0MCYrbZmzcT+/v5MoqTElPuGJ8qB32aBmVW3fd1fE NifkBn9mXDfbFLViP3kjHIjClNfXyIfCJIqDCwTk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maxim Mikityanskiy , Tariq Toukan , Jakub Kicinski , "David S. Miller" , Sasha Levin Subject: [PATCH 4.19 16/62] net/tls: Fix race in TLS device down flow Date: Wed, 27 Jul 2022 18:10:25 +0200 Message-Id: <20220727161004.820683348@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220727161004.175638564@linuxfoundation.org> References: <20220727161004.175638564@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tariq Toukan [ Upstream commit f08d8c1bb97c48f24a82afaa2fd8c140f8d3da8b ] Socket destruction flow and tls_device_down function sync against each other using tls_device_lock and the context refcount, to guarantee the device resources are freed via tls_dev_del() by the end of tls_device_down. In the following unfortunate flow, this won't happen: - refcount is decreased to zero in tls_device_sk_destruct. - tls_device_down starts, skips the context as refcount is zero, going all the way until it flushes the gc work, and returns without freeing the device resources. - only then, tls_device_queue_ctx_destruction is called, queues the gc work and frees the context's device resources. Solve it by decreasing the refcount in the socket's destruction flow under the tls_device_lock, for perfect synchronization. This does not slow down the common likely destructor flow, in which both the refcount is decreased and the spinlock is acquired, anyway. Fixes: e8f69799810c ("net/tls: Add generic NIC offload infrastructure") Reviewed-by: Maxim Mikityanskiy Signed-off-by: Tariq Toukan Reviewed-by: Jakub Kicinski Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/tls/tls_device.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c index 228e3ce48d43..b290eb3ae155 100644 --- a/net/tls/tls_device.c +++ b/net/tls/tls_device.c @@ -110,13 +110,16 @@ static void tls_device_queue_ctx_destruction(struct tls_context *ctx) unsigned long flags; spin_lock_irqsave(&tls_device_lock, flags); + if (unlikely(!refcount_dec_and_test(&ctx->refcount))) + goto unlock; + list_move_tail(&ctx->list, &tls_device_gc_list); /* schedule_work inside the spinlock * to make sure tls_device_down waits for that work. */ schedule_work(&tls_device_gc_work); - +unlock: spin_unlock_irqrestore(&tls_device_lock, flags); } @@ -214,8 +217,7 @@ void tls_device_sk_destruct(struct sock *sk) clean_acked_data_disable(inet_csk(sk)); } - if (refcount_dec_and_test(&tls_ctx->refcount)) - tls_device_queue_ctx_destruction(tls_ctx); + tls_device_queue_ctx_destruction(tls_ctx); } EXPORT_SYMBOL(tls_device_sk_destruct); -- 2.35.1