Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2196549iof; Tue, 7 Jun 2022 22:54:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/66QXfZYGv88xMPPl8tffOAx5JqMHBE9mLTRglrjOeViwgluEOVzrs3Mqjcil83SrKbuK X-Received: by 2002:a63:8942:0:b0:3fc:ffa6:378c with SMTP id v63-20020a638942000000b003fcffa6378cmr25253678pgd.305.1654667668134; Tue, 07 Jun 2022 22:54:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667668; cv=none; d=google.com; s=arc-20160816; b=jBZKclqb24lGiTos2qLVDuJjSzOqbab/J+lVKhhTKXXhOIHCAZ3+yOV/S0kygaZnng KLbFJFNcPojmxB7baKkvrT4V1p9jTzhqPQIlCddMzpTJsoGGSBt8QBPnN+ERxip1XGrE BE39thT4cpuZTA7IUYc0NvQK/9Oj9iSJMAeXInCFT+frHUF/eTMaeSVWNpgF53ORdR9x M/CLBddmxHpcwzN2pkhdEbihKCeAGk3hxv1d7l00froPoBd2KAAycCSWazrvDu5dGI67 HYAixXH5Dp/ETXFU1APNDG7Z3dsz4wJTwJPGxomUZXPl/4yoaegp8p/z56J5gAz1pBav Q8cg== 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=9Fgqy5x2jMO/vEXc1khT+kg6sLIZqz7V+NBFGF/WmA4=; b=h7CXCWyLOr5o7XKMmtxMS7aGr3MXPGOj4o8xHxdFrqaLeEw9cAGS3kjOCEVs969rW7 5n4DKxqp6/4p98/5TBx9X9ydWZvEew60BtpZ9QF/KrIYSkhO3bBkwQLwJAvyVBc6WcJ9 tfigzbcfJtvd66dKVftGVJZ92wyBzXuaPj3IjQSdzdhcaUzlhTxQ8Cv6VSI9IGf6YDPK LDmaw/u2m515r6f73JzEtDYiWFb6NEtbP2fSvaOjHcycp1qFAsVjLQriXJ3egO++kG11 l1OjQFxXYXF6jFKXTD0KQHbojwfZm2w6vX5oP/Vqiua3YRR0+H8pAE3uy9kz0ttaed1/ GL+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=j8LYjj+I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id w2-20020a170902e88200b0015f179cded2si29296577plg.118.2022.06.07.22.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:54:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=j8LYjj+I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 01D98258B40; Tue, 7 Jun 2022 22:21:00 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441912AbiFHAx0 (ORCPT + 99 others); Tue, 7 Jun 2022 20:53:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354515AbiFGWCX (ORCPT ); Tue, 7 Jun 2022 18:02:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C183424E1CB; Tue, 7 Jun 2022 12:14:25 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 58EB061917; Tue, 7 Jun 2022 19:14:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BCE1C385A2; Tue, 7 Jun 2022 19:14:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629252; bh=ucIaXTRbVB/oL/7bl68xYrtIpt1U6H0UrivdO4ahS1Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j8LYjj+I5mrX0ynIRqh8vWCV7awnFk6LGj8CN3j10HcEJUMXwVa8EoeiklpcA7CPB yjNobT6srLlc+BgtsGttNb0lZ0n4DEPyZM/gG+zrJTh/dYh3R0sAG1HLINXinH0VxY si96CXhbgZ9VzKF9hNlfp9BV/SsWKup5HzvYQ588= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sebastian Andrzej Siewior , Herbert Xu , Sasha Levin Subject: [PATCH 5.18 623/879] crypto: cryptd - Protect per-CPU resource by disabling BH. Date: Tue, 7 Jun 2022 19:02:21 +0200 Message-Id: <20220607165020.935015007@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Sebastian Andrzej Siewior [ Upstream commit 91e8bcd7b4da182e09ea19a2c73167345fe14c98 ] The access to cryptd_queue::cpu_queue is synchronized by disabling preemption in cryptd_enqueue_request() and disabling BH in cryptd_queue_worker(). This implies that access is allowed from BH. If cryptd_enqueue_request() is invoked from preemptible context _and_ soft interrupt then this can lead to list corruption since cryptd_enqueue_request() is not protected against access from soft interrupt. Replace get_cpu() in cryptd_enqueue_request() with local_bh_disable() to ensure BH is always disabled. Remove preempt_disable() from cryptd_queue_worker() since it is not needed because local_bh_disable() ensures synchronisation. Fixes: 254eff771441 ("crypto: cryptd - Per-CPU thread implementation...") Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- crypto/cryptd.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/crypto/cryptd.c b/crypto/cryptd.c index a1bea0f4baa8..668095eca0fa 100644 --- a/crypto/cryptd.c +++ b/crypto/cryptd.c @@ -39,6 +39,10 @@ struct cryptd_cpu_queue { }; struct cryptd_queue { + /* + * Protected by disabling BH to allow enqueueing from softinterrupt and + * dequeuing from kworker (cryptd_queue_worker()). + */ struct cryptd_cpu_queue __percpu *cpu_queue; }; @@ -125,28 +129,28 @@ static void cryptd_fini_queue(struct cryptd_queue *queue) static int cryptd_enqueue_request(struct cryptd_queue *queue, struct crypto_async_request *request) { - int cpu, err; + int err; struct cryptd_cpu_queue *cpu_queue; refcount_t *refcnt; - cpu = get_cpu(); + local_bh_disable(); cpu_queue = this_cpu_ptr(queue->cpu_queue); err = crypto_enqueue_request(&cpu_queue->queue, request); refcnt = crypto_tfm_ctx(request->tfm); if (err == -ENOSPC) - goto out_put_cpu; + goto out; - queue_work_on(cpu, cryptd_wq, &cpu_queue->work); + queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work); if (!refcount_read(refcnt)) - goto out_put_cpu; + goto out; refcount_inc(refcnt); -out_put_cpu: - put_cpu(); +out: + local_bh_enable(); return err; } @@ -162,15 +166,10 @@ static void cryptd_queue_worker(struct work_struct *work) cpu_queue = container_of(work, struct cryptd_cpu_queue, work); /* * Only handle one request at a time to avoid hogging crypto workqueue. - * preempt_disable/enable is used to prevent being preempted by - * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent - * cryptd_enqueue_request() being accessed from software interrupts. */ local_bh_disable(); - preempt_disable(); backlog = crypto_get_backlog(&cpu_queue->queue); req = crypto_dequeue_request(&cpu_queue->queue); - preempt_enable(); local_bh_enable(); if (!req) -- 2.35.1