Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6624920rwr; Tue, 25 Apr 2023 00:57:38 -0700 (PDT) X-Google-Smtp-Source: AKy350aaC+kppR84ulUXjNjVI5c9dlt7OcOERBocb+ni3lX/jrNwT5x+blhyFPozdwDUTUXlsgGk X-Received: by 2002:a17:90b:3110:b0:246:fd44:eb6f with SMTP id gc16-20020a17090b311000b00246fd44eb6fmr16253661pjb.39.1682409458279; Tue, 25 Apr 2023 00:57:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682409458; cv=none; d=google.com; s=arc-20160816; b=Gdf6mVUa1rwFKjbYnhDS7p8P/pqLx6wvnR0fxePo0D8x/GJ5XOr/1k4HTF7W7vbUdF pD3Jy2cg7x1z8OhgZVFy/43eQG60tooKneY69sk5KMQQoO/K6z/nLv/1v/GO+ZtImFFU ytQxCga/4vpC2Y0psr4NUwP/5DHQZDFQ7vDMJnxvpJnqaTbiKpUcwy8A1LQ8W59FWnnt wr8yP25O0tRxeqFAAv//3X2qEyxIbGAV/KwP42BXzKgMYfeS+k05C7pdsOVtmjWo7rYQ HhT9zs9nWBBeASaCOvd7h0dNbONQ/8AeOmL1QryFGufJV1sZBXiHjDEpqCE5bOGnNEnm 7BCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-filter; bh=x/dcm2H+pi6gUOqFCVOxGTCuLDZsjOAvdeX9hDCiq50=; b=eIw2g4Rda9zQjWtmCsZmHhFPtYKJU+NyilzsB8a+JRwmGazTCtjSehptwamBb6s8fc jDxUvYZzuxmKbLaig6ZHE/3W0Nc4yVKruws9kTAZwqtorI1hWHzgaSujWru2dxWD65F6 llI/91GnpMfOq30k+joB1wtGVLaUH+69z7hitBF5Ct3scjXAT9yJ2Jj1L9iMC7Ec4Gy/ p7EMTF0nIPXA/5m/Gz+6/gJBpZHMZH1yyuNqHY8r/8eUmwQZL9IYLY22DVYPhQ8Pmif4 LJLJ6sxlQbq7FWLxD1fZYLJjeOIcE38GSfdGdsJgMOFCMx9mwlAF0ovM+z2JQDjagYU9 UPeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=ha5x9mUs; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v20-20020a634654000000b005139e0d2b5csi8298052pgk.487.2023.04.25.00.57.26; Tue, 25 Apr 2023 00:57:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-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=@ispras.ru header.s=default header.b=ha5x9mUs; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233303AbjDYHyl (ORCPT + 62 others); Tue, 25 Apr 2023 03:54:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231137AbjDYHyk (ORCPT ); Tue, 25 Apr 2023 03:54:40 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC44A93; Tue, 25 Apr 2023 00:54:38 -0700 (PDT) Received: from fpc (unknown [10.10.165.13]) by mail.ispras.ru (Postfix) with ESMTPSA id B178F4076B3E; Tue, 25 Apr 2023 07:54:33 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru B178F4076B3E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1682409273; bh=x/dcm2H+pi6gUOqFCVOxGTCuLDZsjOAvdeX9hDCiq50=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ha5x9mUs3UEtB19J1XCR4MRHoL6FR6LT2yNTo2JYsvcMVjPHA/bq8OmLTktyraXoP 4/NlJ+NtnRg2/Kn+PNV3/5ADOUAnJwDLm9OT+nhQsXJColKxExzaplOSPtM7HygpfJ +ysErXCQ/eOuCMgonfLq9PWYyVCbcoQv3zfUOLYU= Date: Tue, 25 Apr 2023 10:54:26 +0300 From: Fedor Pchelkin To: Hillf Danton Cc: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= , Kalle Valo , linux-kernel@vger.kernel.org, syzbot+f2cb6e0ffdb961921e4d@syzkaller.appspotmail.com, syzbot+df61b36319e045c00a08@syzkaller.appspotmail.com, linux-wireless@vger.kernel.org, netdev@vger.kernel.org, Alexey Khoroshilov , lvc-project@linuxtesting.org Subject: Re: [PATCH v2] wifi: ath9k: fix races between ath9k_wmi_cmd and ath9k_wmi_ctrl_rx Message-ID: <20230425075426.ubfnohsqe3c2cjdq@fpc> References: <20230424191826.117354-1-pchelkin@ispras.ru> <20230425033832.2041-1-hdanton@sina.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230425033832.2041-1-hdanton@sina.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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-wireless@vger.kernel.org On Tue, Apr 25, 2023 at 11:38:32AM +0800, Hillf Danton wrote: > On 24 Apr 2023 22:18:26 +0300 Fedor Pchelkin > > Currently, the synchronization between ath9k_wmi_cmd() and > > ath9k_wmi_ctrl_rx() is exposed to a race condition which, although being > > rather unlikely, can lead to invalid behaviour of ath9k_wmi_cmd(). > > > > Consider the following scenario: > > > > CPU0 CPU1 > > > > ath9k_wmi_cmd(...) > > mutex_lock(&wmi->op_mutex) > > ath9k_wmi_cmd_issue(...) > > wait_for_completion_timeout(...) > > --- > > timeout > > --- > > /* the callback is being processed > > * before last_seq_id became zero > > */ > > ath9k_wmi_ctrl_rx(...) > > spin_lock_irqsave(...) > > /* wmi->last_seq_id check here > > * doesn't detect timeout yet > > */ > > spin_unlock_irqrestore(...) > > /* last_seq_id is zeroed to > > * indicate there was a timeout > > */ > > wmi->last_seq_id = 0 > > Without wmi->wmi_lock held, updating last_seq_id on the waiter side > means it is random on the waker side, so the fix below is incorrect. > Thank you for noticing! Of course that should be done. > > mutex_unlock(&wmi->op_mutex) > > return -ETIMEDOUT > > > > ath9k_wmi_cmd(...) > > mutex_lock(&wmi->op_mutex) > > /* the buffer is replaced with > > * another one > > */ > > wmi->cmd_rsp_buf = rsp_buf > > wmi->cmd_rsp_len = rsp_len > > ath9k_wmi_cmd_issue(...) > > spin_lock_irqsave(...) > > spin_unlock_irqrestore(...) > > wait_for_completion_timeout(...) > > /* the continuation of the > > * callback left after the first > > * ath9k_wmi_cmd call > > */ > > ath9k_wmi_rsp_callback(...) > > /* copying data designated > > * to already timeouted > > * WMI command into an > > * inappropriate wmi_cmd_buf > > */ > > memcpy(...) > > complete(&wmi->cmd_wait) > > /* awakened by the bogus callback > > * => invalid return result > > */ > > mutex_unlock(&wmi->op_mutex) > > return 0 > > > > To fix this, move ath9k_wmi_rsp_callback() under wmi_lock inside > > ath9k_wmi_ctrl_rx() so that the wmi->cmd_wait can be completed only for > > initially designated wmi_cmd call, otherwise the path would be rejected > > with last_seq_id check. > > > > Also move recording the rsp buffer and length into ath9k_wmi_cmd_issue() > > under the same wmi_lock with last_seq_id update to avoid their racy > > changes. > > Better in a seperate one. Well, they are parts of the same problem but now it seems more relevant to divide the patch in two: the first one for incorrect last_seq_id synchronization and the second one for recording rsp buffer under the lock. Thanks!