Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1479953ybg; Thu, 11 Jun 2020 10:55:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyx5paeNIEctxWAHIveXSBcdFWSAfUGV4ejiY18ZbM+qH4+Y34nJFP7SJnSz8hDYrVzF2nJ X-Received: by 2002:a17:906:c828:: with SMTP id dd8mr9757863ejb.550.1591898145644; Thu, 11 Jun 2020 10:55:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591898145; cv=none; d=google.com; s=arc-20160816; b=bLGK7gPzXCU7mgpISPNM1QOueuh8vG3vqWJVoWY6vOqdqr3TDYBM4zurSwEYV3YeTw ZfG+6JOEpd1NmIOlbbeIyWcWhxIHcZstJnjo+4qD8V2hYhJ+/PcXwTJYnxy+B38+N6BT wO1OyhFktpAydw9VeWmqTaOvuKydEvNbSAJgsTvShDC5AH/fKoFggnCeA1HNn6RDJh7C xPE4DdidV6dTWe/wOuVdmqQ66GRrQYA4RY9/0Wus3RTGiYzAmE1ZE1hLC9pNbSCBSVVY RQBDCs0UNE2pqZ1l1yEkMNE+kgkO25rjOi23xv+CVL7iMhkFILW4HBekgD3WHsq+rGEV 9izw== 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=WK0NrPsODwq6NLtm1x2TruwNmW9+Z82xJBxzgf0rZXg=; b=wFYQaxwtGeUzKMBLuWA1xom1dyG/HNwmgtIQxW5SScORIcvv/yjWHJ8Wl5GCzEEhx0 PuO6zG2y/Ayra66jcWwroUoPJdPx+wyouMzV5uh5FHe5Nj+gJkBfU0szfQoBcCzO8X26 1suSvTOgpxu/oEF/0Na3bqUVqkmoqd2Mhtgzcm5OWTYGf0zCVNYWhbQ4O7N6DADlqchc pt9qvpJ1Mh32DqkDQmbLQG9m090devNgsMYxQJOoDneRvHYKOxwFf/5hDqMBJX/SS2Ig 3mX8GGfDLi5lWFWm+hu11evWKKd8zGwIxX2vpu2hlFewkME11fPV8fiQ4BECU75asxgm GFpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel-com.20150623.gappssmtp.com header.s=20150623 header.b=1nRiJaRw; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i16si2244847ejf.732.2020.06.11.10.54.57; Thu, 11 Jun 2020 10:55:45 -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=@intel-com.20150623.gappssmtp.com header.s=20150623 header.b=1nRiJaRw; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726817AbgFKRbU (ORCPT + 99 others); Thu, 11 Jun 2020 13:31:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726456AbgFKRbT (ORCPT ); Thu, 11 Jun 2020 13:31:19 -0400 Received: from mail-lj1-x242.google.com (mail-lj1-x242.google.com [IPv6:2a00:1450:4864:20::242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97357C03E96F for ; Thu, 11 Jun 2020 10:31:19 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id x18so7898467lji.1 for ; Thu, 11 Jun 2020 10:31:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WK0NrPsODwq6NLtm1x2TruwNmW9+Z82xJBxzgf0rZXg=; b=1nRiJaRwOpthoxYA7Ta+juLCUTJiYTlo09Tb8ShedxMyj0/YzxKPHZTdT0+DpG9lQR W/lxmzjiHYSWQfBQBWv0EoOrzM4Xlp4PBqcEvzHYwSDh0DE7vY4MnUUhKgx+SU/txp10 itPxn9HPohApjh3NEoKYRjbNAKrahep8nq6a7xhVKN6Bq5ZJvjbKevsvFxB9F5xkqXJq ED1nBrR5vzsctdNdWsAuzDBAR/lERQy3PC1dyOEuSAnvUSvEeoaX7SppZxgS1LeWy+ER heMXmFgQ9ZQgNIZ73FG2bRtChQ4EbKgYP6IBt2tC8rd48oNnJxQctHKjTHu5bqmHpkvk sxoQ== 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=WK0NrPsODwq6NLtm1x2TruwNmW9+Z82xJBxzgf0rZXg=; b=UVg+QkdTu3Lj2r76fGLFNLtOGB+fY/mqTdWFV4KYwi/fBaOqv+qmQdRHoIXvFnqty+ +c06nWje71eoE8ZZSRVXdRWQ/2i3bHbmjz1ceB7EyjYn0TuXSfVdOlaVh4xsE8Yux6OH oDe4SkfdkRmsDDpzkvvmUGSUqW8STGwPjiBGZ+y40vfh7QGYymcCAP7vpkNw0RMuKt/b tnEUpxmCN3lATiVawKP5YIsvtAwBmAC59miJwPp+Qb2P14X/WcyoRYCqC1kPr7P9tYmt CXmb5lHNMNJcePUkKDamtscesQ+WHH9S76QcnZ+r/lWtCY0D/TSANHCi9g5I9jgzajhH Huag== X-Gm-Message-State: AOAM531lSgAGLh07Xabag6C/coCtO61lLdKtbYPriMqITTcF/RFDpZOQ QwS3COE6p8fkcIXOco30pj2v31cLKLqx/Q9TQP4HLg== X-Received: by 2002:a05:651c:313:: with SMTP id a19mr4034008ljp.57.1591896678001; Thu, 11 Jun 2020 10:31:18 -0700 (PDT) MIME-Version: 1.0 References: <20200608180241.BlueZ.v1.1.Ibf8331f6c835d53fe7ca978de962f93981573d9a@changeid> In-Reply-To: From: "Von Dentz, Luiz" Date: Thu, 11 Jun 2020 10:31:06 -0700 Message-ID: Subject: Re: [BlueZ PATCH v1] adapter: Fix the unref and reset of watch_client's members To: Miao-chen Chou 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 Miao, On Wed, Jun 10, 2020 at 3:18 PM Miao-chen Chou wrote: > > 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. Wasn't that the error we were generating before? Afaik both start and stop discovery were doing the same in this regard. > 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? Yep, it should be fixed now. > (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? Yep, that will need tracking of the client who initiated the operation, I will send a patch for that later today. > + 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.