Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp836503ybg; Wed, 10 Jun 2020 15:19:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXqeG6Ec0lhYDgJup4T0gfIjIA8bMFyyAoKuyIBCHWd0dPX7RpP0NjnJbObWkp+szWjSGw X-Received: by 2002:a17:906:f2d9:: with SMTP id gz25mr5825921ejb.467.1591827545465; Wed, 10 Jun 2020 15:19:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591827545; cv=none; d=google.com; s=arc-20160816; b=LfLVbv2rhl+Vd2eb9uoaj/OHChn1SOWahYnLGiCCLUFT46CeckVwvFhd89mBEngMMk kVfmaB8s72Zv9x7/CBxYGfjUw6GL0j8svh14m0xaFvHSrbbyAPeBtAbGe9lrfjBoK7Ze Ir9XnrBZsNTs5GCX7MFgChgtljoilBZhFM+MVVa5h4wXGIdmF33Oi8jXB3625uBa+LqS eZv7w2k4OL/unDHXqalT0y/f7aevw936NOFp4TEloXvmUb7lPAPnPSNEURdbZ7zTohQD +BhW8v7Hwa5MwJUY9sM049NRJNVLKDRrXsTNljwCvMpjy7gDRjC9HYxLbhs2DXZNyv9g 7gZA== 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=joN/NypMEb5B3vAjvRuQgl8f961A77Cll7A6oEoPNR0=; b=QGJFn35Lford/8JkRzso0yj0ZJi8ZocF9YSEGhnWyFF2NPAlrDVyOTL6czT3CxKFzD O1fCFgalv7AjyO8SFDGKcacqeHmXy/PBqsgUxXICqbYpOid+XW1h7+CaA1IjQx/DNrrl Q9EUnSP5Rz5vqG1krqtbSkjz9wKSZiULW1b3ovcPLDXKCbGwGHvI4WEiNR1Wq3X7k+wT 6qeyTl1uK8NYIxVkl1TnmPaAeqojEkNrRCmE53Nbphv8oDO4Olrc2pUGq9gO+pqqh9UC L6ZWEbvYrPIeeqfth4PcrQuLZCXO9FsE3r9Bnn3HQiGStUtlVd5nEUPiSgohYpa0Jxoy WHgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LlXS6q1Y; 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 dn21si1043357ejc.19.2020.06.10.15.18.19; Wed, 10 Jun 2020 15:19:05 -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=LlXS6q1Y; 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 S1726405AbgFJWSL (ORCPT + 99 others); Wed, 10 Jun 2020 18:18:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbgFJWSL (ORCPT ); Wed, 10 Jun 2020 18:18:11 -0400 Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03DABC03E96B for ; Wed, 10 Jun 2020 15:18:10 -0700 (PDT) Received: by mail-lj1-x244.google.com with SMTP id a9so4513275ljn.6 for ; Wed, 10 Jun 2020 15:18:10 -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=joN/NypMEb5B3vAjvRuQgl8f961A77Cll7A6oEoPNR0=; b=LlXS6q1YHBr9aToc+tYDsESfV1pu+vjA6+kraT1OiPIv3If8pviBMXOUIzdf5PGEkr O19uFXHKU85MMNFR/JzbacQk9/5RmKFSqADEGsydSOp1axbRxny8BA5lt7y0lTJ/VWhs a72jcG0QVzm2aklINzM65AWa7kVHF8B66R3p4= 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=joN/NypMEb5B3vAjvRuQgl8f961A77Cll7A6oEoPNR0=; b=YuqGZgAqVcEjBJE5x/KxJdmHOgI06NeEaxK2Ha4b1DW+meLKQXEY/Gb23w/ai8MtwX OwQ3tF6MeZXW/BK7UCvQZf6gXq3O0xJmxpUoY0Rh22cU4THC8J22lmml9MfAlz07aI2x OQywJZk2emjM3TRj/9vWakzoL8SVgtAwYKmdHSihR258iAFqzwmnqql+Hj8OHGrbfvZu fS3G/xOWAyacXQWfpkyY/jRXYsU9lfPRwHY+pmoE8VyfVcPI8gzz6+it4Qq2hn8VvKgy gDVI5sQpm1vXz7ewPJSbITgjfKaOQgoFh3//HaI+Sb0Yl6dO9Y4hZJazuFlRwc1K9xCP +qAw== X-Gm-Message-State: AOAM531zyux5cGoqwl59NJYOWB3VUVzNj2cX0EJsgqJ4kH2nsVuSv9zM hSaV7ySTcBqMQuRLVP1WaPARZuWLo3uf/skpdIur0g== X-Received: by 2002:a05:651c:10f:: with SMTP id a15mr2673341ljb.190.1591827489292; Wed, 10 Jun 2020 15:18:09 -0700 (PDT) MIME-Version: 1.0 References: <20200608180241.BlueZ.v1.1.Ibf8331f6c835d53fe7ca978de962f93981573d9a@changeid> In-Reply-To: From: Miao-chen Chou Date: Wed, 10 Jun 2020 15:17:58 -0700 Message-ID: Subject: Re: [BlueZ PATCH v1] adapter: Fix the unref and reset of watch_client's members To: "Von Dentz, Luiz" Cc: Bluetooth Kernel Mailing List , Michael Sun , Alain Michaud , Yoni Shavit , Sonny Sasaka 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 Luiz, For 0001-adapter-Do-not-remove-client-watch-directly-if-disco.patch, it looks good to me. For 0002-adapter-Consolitate-code-for-discovery-reply.patch, please see me following comments. +static void discovery_reply(struct watch_client *client, uint8_t status) +{ + DBusMessage *reply; + + if (!client->msg) + return; + + if (!status) { It'd better to change this to "if (status == MGMT_STATUS_SUCCESS) {". + g_dbus_send_reply(dbus_conn, client->msg, DBUS_TYPE_INVALID); + } else { + reply = btd_error_busy(client->msg); + g_dbus_send_message(dbus_conn, reply); + } + + dbus_message_unref(client->msg); + client->msg = NULL; +} I also notice that we treated the status other than MGMT_STATUS_SUCCESS to be busy, but this can be addressed as a separate patch. For 0003-adapter-Fix-possible-crash-when-stopping-discovery.patch, I had few comments here. (1) I didn't see the corresponding changes to pass the pointer of the adapter as the user data when sending MGMT_OP_STOP_DISCOVERY command. Should it be part of the patch? (2) This does resolve the crashing due to use-after-free of a watch_client. However, the following logic doesn't seem to be correct to me. If you recall the call path that we discussed, which is "client1 start_discovery() -> client1 start_discovery_complete() -> client1 stop_discovery() -> client2 start_discovery() -> client1 detach from D-Bus which triggers discovery_disconnect() -> client1 stop_discovery_complete() -> crash)", when client2 starts the discovery, client2 is added to adapter->discovery_list, so once stop_discovery_complete() is called with client1, client2 is the only client in adapter->discovery_list. And this statement remains true even with this patch. That being said, the following "client = adapter->discovery_list->data" would return client2, which is not supposed to be replied by stop_discovery_complete() issued by client1. Agree? + if (!adapter->discovery_list) + return; + + client = adapter->discovery_list->data; Thanks, Miao On Tue, Jun 9, 2020 at 2:25 PM Von Dentz, Luiz wrote: > > Hi, > > > On Mon, Jun 8, 2020 at 6:11 PM Von Dentz, Luiz wrote: > > > > Hi Miao, > > > > On Mon, Jun 8, 2020 at 6:03 PM Miao-chen Chou wrote: > >> > >> This properly handles the unref of client->msg in > >> stop_discovery_complete() and the reset of it. This also handles the unref > >> of client->msg, the reset of client->watch and the reset of client->msg in > >> start_discovery_complete(). > >> > >> The following test was performed: > >> (1) Intentionally changed the MGMT status other than MGMT_STATUS_SUCCESS > >> in stop_discovery_complete() and start_discovery_complete() and built > >> bluetoothd. > >> (2) In bluetoothctl console, issued scan on/scan off to invoke > >> StartDiscovery and verified that new discovery requests can be processed. > >> > >> Reviewed-by: Alain Michaud > >> Reviewed-by: Sonny Sasaka > >> --- > >> > >> src/adapter.c | 5 +++++ > >> 1 file changed, 5 insertions(+) > >> > >> diff --git a/src/adapter.c b/src/adapter.c > >> index 76acfea70..0857a3115 100644 > >> --- a/src/adapter.c > >> +++ b/src/adapter.c > >> @@ -1652,6 +1652,9 @@ fail: > >> reply = btd_error_busy(client->msg); > >> g_dbus_send_message(dbus_conn, reply); > >> g_dbus_remove_watch(dbus_conn, client->watch); > > > > > > We shouldn't be removing the watch directly since the client may have registered filters so we let discovery_remove do it by calling discovery_free if necessary. > > > >> > >> + client->watch = 0; > >> + dbus_message_unref(client->msg); > >> + client->msg = NULL; > >> discovery_remove(client, false); > >> return; > >> } > >> @@ -1926,6 +1929,8 @@ static void stop_discovery_complete(uint8_t status, uint16_t length, > >> if (client->msg) { > >> reply = btd_error_busy(client->msg); > >> g_dbus_send_message(dbus_conn, reply); > >> + dbus_message_unref(client->msg); > >> + client->msg = NULL; > >> } > >> goto done; > >> } > >> -- > >> 2.26.2 > > > > > > Ive sent similar fixes upstream, let me attach them here just in case. > > Any comments on these changes, I would like to push them as soon as possible.