Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp92592iof; Sun, 5 Jun 2022 22:07:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxb9kmYTsohqeoR7PET+OSeB6Bx9HNA6RzckmQnVWM1IZeoaBNbgDVERg5UCzthCOpp2Qkr X-Received: by 2002:a17:90b:20d4:b0:1e2:fadf:3f09 with SMTP id ju20-20020a17090b20d400b001e2fadf3f09mr24643434pjb.236.1654492053828; Sun, 05 Jun 2022 22:07:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654492053; cv=none; d=google.com; s=arc-20160816; b=cmVU19jp9wZJPrzcCMdG7YoE2EQCcBPlE6pNDrdraXivauoI2cXcTuz5pk30Kbq5tN M7is4aDj2z2ZWS/JuCDNYAjXMJpIE19jndCZXVH13yV9sv8yK1EKhEYl9sZQyzOVCE1n Sw8QwDv/rswBioUgB6iZFxJSqtfy48p46Dd/IRsyTT3STpjS+FpojNqU/rq9gRTbECG2 40qSg0cmSR+oPbv6MtItdQ/Gs66eXa7QgGK1uvigYNfdKALv3Ve5qBNOMtUJZ7PZRNSg S+vV7jAzrCzqoD3HDK4U2nqb8uQzVh6FY/OsWCJnwZtLYcMZ0dm/K3Upl2pGYrLcA6W8 cy4A== 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=dsHhTxcEgbvI+LEpm+xq12q+iOdDbhn0siSUH0XqsHI=; b=AMvawnq6E3x3Wv2mcLy5tl+hevMPVNsANr+ilF2vlHyXHthkbrgg3Ue9a/HvRpg2j4 sfgovnL91jfZuFMzM+Ilx1LZWEk1gb18Bes6dWqsJX1jSH2f9I5UypB1w1AUJVv17HyA VFOjkeGVXkJUD6e8UtFyrxqPchvqdxG8Ty+94+2XrKLnAia3VZPPEEU8mWGwOkdnAr39 OxtRusBf9j5f2MIY2dru8KymRvRZmViSBZ+Iji8R4XhiZyvTq3DkqqGOfLd6cTTW3J42 OPU9W1Aa7NKpUy3jcqVivMZIkAe2Kh1FK9mxgaeJ+fjO7GwPySYPNEsiy3WlwILy8AqH ia/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dv8C97dr; 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 j3-20020a056a00174300b0051bcd7f3cd6si17629947pfc.278.2022.06.05.22.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:07:33 -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=dv8C97dr; 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 18059139ADF; Sun, 5 Jun 2022 21:16:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346490AbiFCSAV (ORCPT + 99 others); Fri, 3 Jun 2022 14:00:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347199AbiFCRwG (ORCPT ); Fri, 3 Jun 2022 13:52:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81421580FB; Fri, 3 Jun 2022 10:50:18 -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 1E1E660F38; Fri, 3 Jun 2022 17:50:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28A55C385A9; Fri, 3 Jun 2022 17:50:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278617; bh=jHyM+b8du+a0HmzbYBcvIzJ3XESf7qdxLXW4V9tafjk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dv8C97drc2hx3SG2X8abSEg3MRSaksqd5Z1wVHc0l+ITD3ZVicez29FRydzIQTBJ/ bGgdKpPdl+z7+veJBcIr73jzsoDL9cFReOyLTagxa01Du8un3EGTSfueDHwygj7jwd uCjNGDXntTfynAtoRYAx6VfBoxxXIkWxXbv3SuQw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lin Ma , "David S. Miller" , Sasha Levin Subject: [PATCH 5.15 06/66] nfc: pn533: Fix buggy cleanup order Date: Fri, 3 Jun 2022 19:42:46 +0200 Message-Id: <20220603173820.850495224@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173820.663747061@linuxfoundation.org> References: <20220603173820.663747061@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: Lin Ma [ Upstream commit b8cedb7093b2d1394cae9b86494cba4b62d3a30a ] When removing the pn533 device (i2c or USB), there is a logic error. The original code first cancels the worker (flush_delayed_work) and then destroys the workqueue (destroy_workqueue), leaving the timer the last one to be deleted (del_timer). This result in a possible race condition in a multi-core preempt-able kernel. That is, if the cleanup (pn53x_common_clean) is concurrently run with the timer handler (pn533_listen_mode_timer), the timer can queue the poll_work to the already destroyed workqueue, causing use-after-free. This patch reorder the cleanup: it uses the del_timer_sync to make sure the handler is finished before the routine will destroy the workqueue. Note that the timer cannot be activated by the worker again. static void pn533_wq_poll(struct work_struct *work) ... rc = pn533_send_poll_frame(dev); if (rc) return; if (cur_mod->len == 0 && dev->poll_mod_count > 1) mod_timer(&dev->listen_timer, ...); That is, the mod_timer can be called only when pn533_send_poll_frame() returns no error, which is impossible because the device is detaching and the lower driver should return ENODEV code. Signed-off-by: Lin Ma Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/nfc/pn533/pn533.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/nfc/pn533/pn533.c b/drivers/nfc/pn533/pn533.c index d32aec0c334f..6dc0af63440f 100644 --- a/drivers/nfc/pn533/pn533.c +++ b/drivers/nfc/pn533/pn533.c @@ -2789,13 +2789,14 @@ void pn53x_common_clean(struct pn533 *priv) { struct pn533_cmd *cmd, *n; + /* delete the timer before cleanup the worker */ + del_timer_sync(&priv->listen_timer); + flush_delayed_work(&priv->poll_work); destroy_workqueue(priv->wq); skb_queue_purge(&priv->resp_q); - del_timer(&priv->listen_timer); - list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) { list_del(&cmd->queue); kfree(cmd); -- 2.35.1