Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1835133ybg; Thu, 4 Jun 2020 21:54:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjd5x+FLCSdH5YCYhsfeql6aDrs/DQ9AXcGmv7MYarcyxaKzFtiXjIhu5M1EBI/aDHArsl X-Received: by 2002:a50:d490:: with SMTP id s16mr7316865edi.242.1591332887898; Thu, 04 Jun 2020 21:54:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591332887; cv=none; d=google.com; s=arc-20160816; b=Y+jPWZ6/WITUkvO/DcZw5Uhr9tfiHeGalNnBkdNDGkNkNmpQ2E5DQMOhQnTno643hp 8rQ40LuNv0TBXMcTmM3fUb3xNYbCVLOOz1bXubUV+rArdebN7nUI9e+QB9ZyVvUvObcr W87ItHKgMV9jgX9JjLebOYIGQEaBqq5L9Io8rRGPw50VaaJQ+fh1m3zeyTGLFGmsk/Nl CL3yCXOOQTXbTxroWf4LeSd8XOVEmGs/03n7k94e8/Wz7bpTZi792RazYmck9mtK0VwQ dbFr+jVjBjLSq+3NcZhnpiKwRDabE9CSzvW6o9khom0XHiEAInKQRjQz/AR/yy+OXGbn /yrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=iSK3iGrwNXhNPMJarS5DBwkVfOTARnkNBQgwEVg9fzQ=; b=joQdQKqJzkdCDrPskgSMT/PiYeulLX//P8zo9pF/LpHWV9Ln9Bnmo0TYGGRRudHMnh g0Mm+sQx8EOEP6iRlXpN6BwRfLseyntydHOp9XJF2fVewockWv1NxaNB7k+4TSQw8D64 85sHkU11HgZV478I3bSntREllctfe0YJeg9fKKXjHBZo53kidTJfGOpRbscxcpPLKb8l u42EarGmYUyzemaSe+oPdGOewuoN2vZd9eINIBXubJ3YIM6pcdYfJRCj08teiQvQEX7z Ln8HmeBnjDcC0kf4alMsAyfQl9hTv7aqQb7nHU1XLSEXWV4OMJoco2Gyi76vBBXE6cn6 QBhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OLKa3Eru; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ar23si2670872ejc.169.2020.06.04.21.54.02; Thu, 04 Jun 2020 21:54:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OLKa3Eru; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726021AbgFEEx6 (ORCPT + 99 others); Fri, 5 Jun 2020 00:53:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725280AbgFEEx5 (ORCPT ); Fri, 5 Jun 2020 00:53:57 -0400 Received: from mail-vs1-xe41.google.com (mail-vs1-xe41.google.com [IPv6:2607:f8b0:4864:20::e41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6293C08C5C1 for ; Thu, 4 Jun 2020 21:53:57 -0700 (PDT) Received: by mail-vs1-xe41.google.com with SMTP id g129so4889035vsc.4 for ; Thu, 04 Jun 2020 21:53:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iSK3iGrwNXhNPMJarS5DBwkVfOTARnkNBQgwEVg9fzQ=; b=OLKa3Eru19miQbfdsFwziHODftiQNhKc4EdQde9u1NnrdN7pNq03T9y6y7pRRxwOH/ 3O+4MIcvxf/abLwwAyZDvo3EVUiawKW9fPciyLPXrNe3mzA4cb0fsaqJEtKQPGjet+0x yUiD4YjaWXlzJaW3tX5tQX009Vhxc4SCODiWc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iSK3iGrwNXhNPMJarS5DBwkVfOTARnkNBQgwEVg9fzQ=; b=LxNp/YSFEtAU0QIVN6bi2e3AeiteYm8wUvPk+op8u1Y/QKYPlz+zaM91yi9RNnzlo3 XIosBB5JOwNi47XBAWJOk6c2WWOIMW7PoITFIoSjvLHE9XbK6R4eLBJ2dCV12PHNIKLo TiXq+qGmYwYtRPIvRAlelA6MtM/SNF3RhsWeXOeKrPlx4Kvi2Ccjs91mAwXCA0sWRFu5 rEC7UXRe5yExVFPnfv7j4699z4VOaoxK4i7/dx4OD+ddjyd5aBqlQ9VZxls67AxFSkUC I3eqp+u2UunlovUamT0E6BnPnyqLMqnwb0J7b+oC+3bZDGoHvmMJ2wS6L2uieXIDi/I9 HFKg== X-Gm-Message-State: AOAM532qkkul9VVpqt4r+Wb+yNmoZlG2JpC4E8Vv25BJy0UxTYyPq9WU 45LkZEBMUGdYiKy+Dv2M4fVvqGebum67NFYbxZ9a8w== X-Received: by 2002:a67:3c6:: with SMTP id 189mr6264601vsd.71.1591332836701; Thu, 04 Jun 2020 21:53:56 -0700 (PDT) MIME-Version: 1.0 References: <1591279032-7527-1-git-send-email-gubbaven@codeaurora.org> In-Reply-To: <1591279032-7527-1-git-send-email-gubbaven@codeaurora.org> From: Abhishek Pandit-Subedi Date: Thu, 4 Jun 2020 21:53:42 -0700 Message-ID: Subject: Re: [PATCH v1] Bluetooth: hci_qca: Fix double free during SSR timeout To: Venkata Lakshmi Narayana Gubba Cc: Marcel Holtmann , Johan Hedberg , Matthias Kaehlcke , LKML , Bluez mailing list , robh@kernel.org, hemantg@codeaurora.org, linux-arm-msm@vger.kernel.org, bgodavar@codeaurora.org, Claire Chang , Sean Paul , rjliao@codeaurora.org, Yoni Shavit Content-Type: text/plain; charset="UTF-8" Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi, On Thu, Jun 4, 2020 at 6:59 AM Venkata Lakshmi Narayana Gubba wrote: > > Due to race conditions between qca_hw_error and qca_controller_memdump > during SSR timeout,the same pointer is freed twice. Which results to > double free error. Now a lock is acquired while SSR state moved to timeout. suggestion: Change "which results to double free error" to "This results in a double free." suggestion: Change "while SSR state moved to timeout" to "when SSR state is changed to timeout" > > Signed-off-by: Venkata Lakshmi Narayana Gubba > --- > drivers/bluetooth/hci_qca.c | 19 ++++++++++++++----- > 1 file changed, 14 insertions(+), 5 deletions(-) > > diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c > index 836949d..9110775 100644 > --- a/drivers/bluetooth/hci_qca.c > +++ b/drivers/bluetooth/hci_qca.c > @@ -983,8 +983,11 @@ static void qca_controller_memdump(struct work_struct *work) > while ((skb = skb_dequeue(&qca->rx_memdump_q))) { > > mutex_lock(&qca->hci_memdump_lock); > - /* Skip processing the received packets if timeout detected. */ > - if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) { > + /* Skip processing the received packets if timeout detected > + * or memdump collection completed. > + */ > + if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT || > + qca->memdump_state == QCA_MEMDUMP_COLLECTED) { > mutex_unlock(&qca->hci_memdump_lock); > return; > } > @@ -1485,7 +1488,7 @@ static void qca_hw_error(struct hci_dev *hdev, u8 code) > { > struct hci_uart *hu = hci_get_drvdata(hdev); > struct qca_data *qca = hu->priv; > - struct qca_memdump_data *qca_memdump = qca->qca_memdump; > + struct qca_memdump_data *qca_memdump = NULL; > char *memdump_buf = NULL; > > set_bit(QCA_HW_ERROR_EVENT, &qca->flags); > @@ -1509,9 +1512,10 @@ static void qca_hw_error(struct hci_dev *hdev, u8 code) > qca_wait_for_dump_collection(hdev); > } > > + mutex_lock(&qca->hci_memdump_lock); > if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) { > bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout"); > - mutex_lock(&qca->hci_memdump_lock); > + qca_memdump = qca->qca_memdump; > if (qca_memdump) > memdump_buf = qca_memdump->memdump_buf_head; > vfree(memdump_buf); This section of code looks a bit unclear because it's only partially in an if statement. Suggestion: if (qca->qca_memdump) { vfree(qca->qca_memdump->memdump_buf_head); kfree(qca->qca_memdump); qca->qca_memdump = NULL; } > @@ -1520,8 +1524,13 @@ static void qca_hw_error(struct hci_dev *hdev, u8 code) > qca->memdump_state = QCA_MEMDUMP_TIMEOUT; > cancel_delayed_work(&qca->ctrl_memdump_timeout); > skb_queue_purge(&qca->rx_memdump_q); > - mutex_unlock(&qca->hci_memdump_lock); > + } > + mutex_unlock(&qca->hci_memdump_lock); > + > + if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT || > + qca->memdump_state == QCA_MEMDUMP_COLLECTED) { > cancel_work_sync(&qca->ctrl_memdump_evt); > + skb_queue_purge(&qca->rx_memdump_q); > } Earlier in the function, you call qca_wait_for_dump_collection for [Idle, Collecting] so the state should be either [Timeout, Collected] at this branch. So, you can remove the `cancel_delayed_work` and `skb_queue_purge` from above and just leave it only in the bottom branch. Currently you're duplicating these calls unnecessarily. I don't know if we discussed this in an earlier review but I noticed that `qca_wait_for_dump_collection` doesn't actually pay attention to the return value of `wait_on_bit_timeout`. I don't have context for the order of calls anymore but is there a possibility for that timeout to complete before `qca_memdump_timeout` is called? In that case, you should probably set the state to timeout in `qca_wait_for_dump_collection` as well. > > clear_bit(QCA_HW_ERROR_EVENT, &qca->flags); > -- > QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member > of Code Aurora Forum, hosted by The Linux Foundation >