Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp476387imn; Wed, 27 Jul 2022 11:16:02 -0700 (PDT) X-Google-Smtp-Source: AGRyM1szNFE3vsMuNYoGeQM4Q4UEhCJOoq/l8oPG5jmvHn2HSmhl0tsfOlSVnkuxiti4XZiiwlYc X-Received: by 2002:a05:6402:5201:b0:43b:f621:3ce0 with SMTP id s1-20020a056402520100b0043bf6213ce0mr17598408edd.22.1658945761765; Wed, 27 Jul 2022 11:16:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658945761; cv=none; d=google.com; s=arc-20160816; b=ZUQpZBKNmRb5N3oh3nJkiPs+xnYPylAbL21jQ0bbRG/lD+2ZGEly9yRdfC49xSKG/r 1u4RMrtDBGqAEJN8XAeQbGWYqP26Cq6ZZRR7iD1KBeSZueGWQCGBMG8/C3BZzTdaToC+ XGy9fP5Qd3SGcw7mLhAx9Q90ukFpXx6P7wrL1SWn43mu48jeViWb8HAJMnChgXYhUbvt 2jWvCuIROuyQ5FNLwNd9B+r4MB5H87xszty0r3XUNnvDsSxiPxJY6QL0xro4Wud/Ml3W Ffm/YABauIPhsmMT5d4bt0/L4FbhJ2q8cDzvJsdO8lI2dEBiU9VVSH6hPhBacdLb39vj Xrug== 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=ttKiTQi19w9tpLpEx1SnBA8j7GHva97iPOyDphRtjb0=; b=tqB7VKlrtHZq0nUm7xWuHruSWVwl3RgzIxYUWBoAxhRoNli8yn9kOkSMIN4UZVjV4y 8VYfRir/GDiht2wlB8b2KzjMv/1iBn26U2wjc8a3Uez3qK8k8YvixV6A9Sr/2X/ghTpf 1P+Z98mgpKMdb186mneU9u2TC/aK/WtfGB25Ws4YGbRq8ma7YGJmJTw2K2IgXS1khveu wueK7cLosuIo8RoAxnOYei6x7wXQYNNRkNQFy62BmBCxT7Z3cWjndz6X9jUWVKiVH6ym RTn6kQR8A3paPMFRA9RrhU/zfLzPUrORObgdy7m/Jjg1WTPtUFqLQ4hc5sOSdKIuVTt1 Rxfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VeU37VX+; 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 m18-20020a056402511200b0043be073d81asi13086321edd.6.2022.07.27.11.15.36; Wed, 27 Jul 2022 11:16:01 -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=VeU37VX+; 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 S240792AbiG0QxO (ORCPT + 99 others); Wed, 27 Jul 2022 12:53:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240678AbiG0Qvt (ORCPT ); Wed, 27 Jul 2022 12:51:49 -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 0593E62A45; Wed, 27 Jul 2022 09:33:40 -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 AA7E5B821B9; Wed, 27 Jul 2022 16:33:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECB2DC433C1; Wed, 27 Jul 2022 16:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658939618; bh=jhNgT1LJj06EUMlFkugow3Oyu2gxzxnFI3j2dECvXGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VeU37VX+hKZDztCZIHWEERbeossfm0pu66bBGONOmqHkIP1kJ0I6nJ/ZXEjgtzOdm chdCssvJtXuAxX8UW2hH3/OZOKvLOSrGi+HexZbqKCimGuJWa4aZL2k/+7cWrs0YSE oMK0tgCpCjuk1PeAYwuaFasqV4sJnuBXKp9HgRFQ= 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 5.10 044/105] net/tls: Fix race in TLS device down flow Date: Wed, 27 Jul 2022 18:10:30 +0200 Message-Id: <20220727161013.862652671@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220727161012.056867467@linuxfoundation.org> References: <20220727161012.056867467@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 6ae2ce411b4b..23eab7ac43ee 100644 --- a/net/tls/tls_device.c +++ b/net/tls/tls_device.c @@ -97,13 +97,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); } @@ -194,8 +197,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_GPL(tls_device_sk_destruct); -- 2.35.1