Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2143841iof; Tue, 7 Jun 2022 21:12:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5wB9xDGKrPKebk6CLcG5suxRbgMa8BbDB9fS6qzy2xX5nUMSzfMdoa4xl7fj/84/ur+Jn X-Received: by 2002:a05:6a00:1811:b0:51b:fec8:be7b with SMTP id y17-20020a056a00181100b0051bfec8be7bmr18781109pfa.22.1654661570717; Tue, 07 Jun 2022 21:12:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654661570; cv=none; d=google.com; s=arc-20160816; b=ctI75UBMKg/ieCvCEbqLySfBEYo26fcTCGErrh2jZGPFSK+uQFRIl8GCrMrfyfRkcF xplDpeD2xBXHmytw+1B4ixK/JzeU7uT4IXU6vCBrBvynXUjVO8MLeZXgggVgd38Uufwm zPuIwKrgE/W0qEprzbCexCeWCwhRcm2NTE7Kg5D7UsOw1olX4yrqIh3u+bJ9a7d4H2DE T+beFUGSYjA6dPr2rZCq3HLYAFlpjQtE6d/dH3DGlTDRcQWjwq++ZSd6IAlvljrH3z7O kVQOOCobc/0bt1YqO3akyrBV+GSv59zd8Qb3Lv3vrbdMYxRiSHOFAAzmOlMq2gDOwTuK G3Og== 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=I6lvIul305tv9Rn9/UK0sYWkHbyDkTIA+Vm3uVBwmhuhvdC6O1T+SUlG8kKwc0Gmc3 tBTDdc6WtqxNht4E/xa1TWLT6GOrIwQxC55+RTXxiI5ocXVBdjsI1aeOehNPzqnPtJBN XH8zrptPDMQ69jmTSCdR1BH9PXhkD81ICbMnsQy8C8SMTWUdePHDpQhpmCwDHpf8MyAh kpJDtZ7JP3W59+J1pogokhYN1OJpuqnJOgB0iwb/9kK/znbGdk7ZIC/+dPM1eunFfufO wCpFqkeXnLWXubAzzBkVPjKUNMSyv5NzvtbYXtKsBHQjGJHUi4n3lhd+gF0CDjkcc4YR VULg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uf67M0Kz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id nv17-20020a17090b1b5100b001e844b796ecsi22988784pjb.174.2022.06.07.21.12.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 21:12:50 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uf67M0Kz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 0A72D1EA079; Tue, 7 Jun 2022 20:54:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348886AbiFGRzA (ORCPT + 99 others); Tue, 7 Jun 2022 13:55:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348073AbiFGRgD (ORCPT ); Tue, 7 Jun 2022 13:36:03 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09AB92A413; Tue, 7 Jun 2022 10:32:10 -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 3CA29B82185; Tue, 7 Jun 2022 17:32:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A77C4C385A5; Tue, 7 Jun 2022 17:32:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654623128; bh=ucIaXTRbVB/oL/7bl68xYrtIpt1U6H0UrivdO4ahS1Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uf67M0KzbW0ThCmB5d4VYiGBBJwyWXQZvRk6KSM8MURMLOdE9Q/aP/CVITIHD3+94 7086N9ilOzmTAkn9oROXXb/txhIR5cH35na5YUxA81P0e2ljsVGFAmwOz9TprpIVew /4G7yzIT5fMLVMVUx7mwLdyHjy8BhiG4sAAaC2Lo= 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.10 306/452] crypto: cryptd - Protect per-CPU resource by disabling BH. Date: Tue, 7 Jun 2022 19:02:43 +0200 Message-Id: <20220607164917.677884001@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164908.521895282@linuxfoundation.org> References: <20220607164908.521895282@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