Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp707906pxb; Thu, 5 Nov 2020 10:48:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJwd7xLZoplEnsDFndIhyDfNyuaqvsXQV7qDL5pcbMa69bq8kYOfGM/4CUHSCdHB5W8pA71p X-Received: by 2002:a17:906:3fd3:: with SMTP id k19mr3966340ejj.434.1604602109225; Thu, 05 Nov 2020 10:48:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604602109; cv=none; d=google.com; s=arc-20160816; b=i2RU7Oxe5/Cj0nIv1GvKjG+m11Ulu9KCagbbbNEaPz5nGeIxC5wUBXGjdrypBytFhE NcW0ewsaBGtij0PfIEqXUfWt3tJbYnOupEnlggFbTVYX5goFhuUt4t/1xJSydfecUzRf iale2DPM0yrjj9uPxZdMmcXYmzvDRluqt+cd+zX/dmZ3C/t+n2aTcmOlsT8zmc8dK74g mB8DawC1beZa4UD1s0CWocf68Sw9sfQ7i+bQSELhZrVpLZgHjlqn0P1RCMiabrsJEj7z x1aZAvpYRUAYbqPE9TPBd9lnTNrxXlDYfHhmDfHF7+qAzxWP3lELapnaqB27DP5yyhR7 ekFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=q8t9KS2qHyeChpe+/aXEEd5vx3s5DgUjsPKIGwSLJCQ=; b=hKksgmuqdy3t3yV07xIVKUZsOA1QGXc4odXf0AVryUatV7Yg1rx5+u1yi8xkhuwjbI 8rW2DAqaKoYlo9HNZT85iT0op5CtPbvPzbslsckMGyV4JHYls5Zp14QIDmQxRlW3MTFF adTZzFD2Ad8LmaIN8ZYtMycSgS1gSWUvpuHAKzim00LkW4BYjnfmueSJfiRIqyoDIHXL rkvLuijtUOJKeoU/R1y1r8oCzvuvprez5ztvPfEtK244+1+U1O7LduMY2t4orPIcFw1E Rq17son+3YWwsvbRwN2XVom3jtO6NJJMzQibjiuKG0T+apfnMuLkajZ6ZKXp+sz2/7BF 60pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=FP8A7+op; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m8si1871226edj.357.2020.11.05.10.47.46; Thu, 05 Nov 2020 10:48:29 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=FP8A7+op; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730973AbgKESqi (ORCPT + 99 others); Thu, 5 Nov 2020 13:46:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727376AbgKESqh (ORCPT ); Thu, 5 Nov 2020 13:46:37 -0500 Received: from mail-oo1-xc42.google.com (mail-oo1-xc42.google.com [IPv6:2607:f8b0:4864:20::c42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A995BC0613CF for ; Thu, 5 Nov 2020 10:46:37 -0800 (PST) Received: by mail-oo1-xc42.google.com with SMTP id v123so680000ooa.5 for ; Thu, 05 Nov 2020 10:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=q8t9KS2qHyeChpe+/aXEEd5vx3s5DgUjsPKIGwSLJCQ=; b=FP8A7+opYW/IeGYoMaXsNuEaLh0EMFqyya0eoKbbnw2Nvy5DGVa9g9FSeel7v5NMwT 9O41k8GHHhZo2tg24jr/kaphd5qHg8uTrzHegy8/jccVbapSQGY+e0Zp3ikS8WjxNWbv Q5dC3v/uVru4ZXbQMGDqJ7BmQEtydm8WKx3iyppf/7O33Suuu+hAPYYl4ZvD1i19cMTz VMNy8UPg6Uqe3WtIILshbiqkAXfnCLCg2CDJNezP4rNuFiFDr7YGo0cekpSlRoL7O5c8 wwdOGeCLtVxZChEZg+zyLoa9Yft/n6ldB3qYjy5I1BEZaXNwPvMBl9oaRsxHvrBO74HU REIQ== 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=q8t9KS2qHyeChpe+/aXEEd5vx3s5DgUjsPKIGwSLJCQ=; b=ZfbbLI9O6VLEQxBsLpgQ5IDfIXBAs6w0yfV4MTxsUNEG7HEo9nHh0rs8ancy7HgQjr K/9s2hBQ0XqJOWpBFQFrBww034NZamMktekfjaTFC+tUav0QFHWWNOq44aXqGHKP7/WX 3IirU8/qUbsnpm9SeWPuRpsRJitDm+8SircaVNH+yib5fQKbGizJSZCM12Wg25VOXvx+ rgV95MMiiF/hxUAwVYrdPwjptnr0kWs5UFAI9pN7U7JxA6GEC9VSmLKC+NpC10T99V8Q FUKuyH8vdomRcYsiiOAUjD4nVCgzccVZU/9y8lIknYKS8NaC2Io1UZu9Ve2A+Vsd/0xn Sfaw== X-Gm-Message-State: AOAM530f0nAjady4HjmVAfOyHu+BUeX7AqhPDUrE94vqTTXfGcvyLrCv AsQ1lkv+NHCUdjPx0fjMlX1xW02NPkVcGm6uW/vYLzaUtoQ= X-Received: by 2002:a4a:e04a:: with SMTP id v10mr2762252oos.24.1604601996497; Thu, 05 Nov 2020 10:46:36 -0800 (PST) MIME-Version: 1.0 References: <20201105002412.37720-1-sonnysasaka@chromium.org> In-Reply-To: <20201105002412.37720-1-sonnysasaka@chromium.org> From: Luiz Augusto von Dentz Date: Thu, 5 Nov 2020 10:46:25 -0800 Message-ID: Subject: Re: [PATCH BlueZ] gatt-client: Do not continue service discovery if disconnected To: Sonny Sasaka Cc: "linux-bluetooth@vger.kernel.org" , Archie Pusaka Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Sonny, On Wed, Nov 4, 2020 at 10:01 PM Sonny Sasaka wrote: > > If device is disconnected, discovery_complete_op should not continue > processing since this can lead to a crash because the GATT-related > objects may already be freed. > > Sample crash stack trace: > 0 gatt_db_service_get_handles (service=0x1751130, service=0x1751130, end_handle=0x7ffcd600806e, start_handle=0x7ffcd600806c) at src/shared/gatt-db.c:569 > 1 gatt_db_attribute_get_service_data (attrib=, start_handle=0x7ffcd600806c, end_handle=0x7ffcd600806e, primary=0x0, uuid=0x0) at src/shared/gatt-db.c:1657 > 2 0x00000000004983a8 in discovery_op_complete (op=op@entry=0x173b320, success=, err=err@entry=10 '\n') at src/shared/gatt-client.c:406 > 3 0x000000000049a548 in discover_descs_cb (success=, att_ecode=, result=, user_data=0x173b320) at src/shared/gatt-client.c:915 > 4 0x00000000004a1d87 in discovery_op_complete (op=0x1748450, success=, ecode=) at src/shared/gatt-helpers.c:615 > 5 0x00000000004a2379 in discover_descs_cb (opcode=, pdu=0x174d551, length=, user_data=0x1748450) at src/shared/gatt-helpers.c:1465 > 6 0x00000000004966db in handle_rsp (pdu_len=4, pdu=, opcode=, chan=0x17483c0) at src/shared/att.c:814 > 7 can_read_data (io=, user_data=0x17483c0) at src/shared/att.c:1011 Weird, are you still receiving data after the device is disconnected? > 8 0x00000000004a0853 in watch_callback (channel=, cond=, user_data=) at src/shared/io-glib.c:157 > 9 0x00007fb3f2d7fe87 in g_main_context_dispatch () from /usr/lib64/libglib-2.0.so.0 > 10 0x00007fb3f2d80230 in ?? () from /usr/lib64/libglib-2.0.so.0 > 11 0x00007fb3f2d80542 in g_main_loop_run () from /usr/lib64/libglib-2.0.so.0 > 12 0x00000000004a0e25 in mainloop_run () at src/shared/mainloop-glib.c:66 > 13 0x00000000004a11f2 in mainloop_run_with_signal (func=func@entry=0x43f200 , user_data=user_data@entry=0x0) at src/shared/mainloop-notify.c:188 > 14 0x000000000040c72e in main (argc=, argv=) at src/main.c:959 > > Reviewed-By: Archie Pusaka > > --- > src/device.c | 3 +++ > src/shared/gatt-client.c | 20 ++++++++++++++++++++ > src/shared/gatt-client.h | 2 ++ > 3 files changed, 25 insertions(+) > > diff --git a/src/device.c b/src/device.c > index a5ef46730..9dad746eb 100644 > --- a/src/device.c > +++ b/src/device.c > @@ -586,6 +586,9 @@ static void gatt_client_cleanup(struct btd_device *device) > device->gatt_ready_id = 0; > } > > + /* Make sure that service discovery is stopped if any is in progress */ > + bt_gatt_client_reset_in_discovery(device->client); Does this means that other references will also get interrupted? That is something very weird here since when disconnected the bt_att instance should actually have informed the bt_gatt_client and the cleanup should had happened there. > + > bt_gatt_client_unref(device->client); > device->client = NULL; > } > diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c > index 8becf1c6c..2ba6efe9a 100644 > --- a/src/shared/gatt-client.c > +++ b/src/shared/gatt-client.c > @@ -104,6 +104,9 @@ struct bt_gatt_client { > > struct bt_gatt_request *discovery_req; > unsigned int mtu_req_id; > + > + /* Whether there is a service discovery operation ongoing */ > + bool in_discovery; > }; > > struct request { > @@ -381,6 +384,12 @@ static void discovery_op_complete(struct discovery_op *op, bool success, > { > const struct queue_entry *svc; > > + /* Service discovery may be reset due to disconnection */ > + if (!op->client->in_discovery) > + return; > + > + op->client->in_discovery = false; > + > op->success = success; > > /* Read database hash if discovery has been successful */ > @@ -1857,6 +1866,9 @@ static void process_service_changed(struct bt_gatt_client *client, > { > struct discovery_op *op; > > + if (client->in_discovery) > + goto fail; > + > op = discovery_op_create(client, start_handle, end_handle, > service_changed_complete, > service_changed_failure); > @@ -1869,6 +1881,7 @@ static void process_service_changed(struct bt_gatt_client *client, > discovery_op_ref(op), > discovery_op_unref); > if (client->discovery_req) { > + client->in_discovery = true; > client->in_svc_chngd = true; > return; > } > @@ -2057,6 +2070,7 @@ static bool gatt_client_init(struct bt_gatt_client *client, uint16_t mtu) > return false; > } > > + client->in_discovery = true; > client->in_init = true; > > return true; > @@ -2080,6 +2094,7 @@ discover: > } > > done: > + client->in_discovery = true; > client->in_init = true; > return true; > } > @@ -3694,3 +3709,8 @@ int bt_gatt_client_get_security(struct bt_gatt_client *client) > > return bt_att_get_security(client->att, NULL); > } > + > +void bt_gatt_client_reset_in_discovery(struct bt_gatt_client *client) > +{ > + client->in_discovery = false; > +} > diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h > index dc5102394..dcd8e5cf6 100644 > --- a/src/shared/gatt-client.h > +++ b/src/shared/gatt-client.h > @@ -126,3 +126,5 @@ bool bt_gatt_client_unregister_notify(struct bt_gatt_client *client, > > bool bt_gatt_client_set_security(struct bt_gatt_client *client, int level); > int bt_gatt_client_get_security(struct bt_gatt_client *client); > + > +void bt_gatt_client_reset_in_discovery(struct bt_gatt_client *client); > -- > 2.26.2 > -- Luiz Augusto von Dentz