All of lore.kernel.org
 help / color / mirror / Atom feed
From: valorcool@gmail.com
To: linux-bluetooth@vger.kernel.org
Cc: luiz.von.dentz@intel.com, vinit.mehta@nxp.com,
	mahesh.talewad@nxp.com, devyani.godbole@nxp.com,
	iulia.tanasescu@nxp.com, mihai-octavian.urzica@nxp.com,
	Stanislavs Nilovs <valorcool@gmail.com>
Subject: [PATCH BlueZ v1 0/1] Fix memory leak when adding GATT Characteristic
Date: Tue, 12 Aug 2025 11:00:43 +0300	[thread overview]
Message-ID: <20250812080044.131526-1-valorcool@gmail.com> (raw)

From: Stanislavs Nilovs <valorcool@gmail.com>

Hello,

As a self-education of Bluetooth world and future usage at work, I have
written small GATT Server project. It doesn't have a lot of stuff (like
authentication) yet, but basic functionality like read/write was tested
with gatttool on PC and some BLE app on my Android phone.

Recently I started to integrate it into propriatary Bluetooth stack at work.
When I started testing with BlueZ daemon - it segfaulted each time I was
trying to pair over BLE. When pairing over BLE BlueZ is reading GATT
information from remote device. After further debugging turns out I wrongly
assumed it is allowed by the specification for GATT Characteristic Value 
handle to be the same as GATT Characteristic Handle. However, this
didn't change the fact that BlueZ doesn't handle this case
properly and crashes.

Looking at the master code, the 4465c577778d812702d752dfd2812e25a2f69b31
commit has fixed a segfault as it added a check for Characteristic value
to not be NULL. However, there are still problems left after this fix:

First of all, the reason for segfault to appear was the overwrite of the
original attribute pointer with new one, which didn't had value pointer
allocated. With the above commit it may still happen, but we exit early,
before segfault appears in memcmp.

Second, the change of above commit is not freeing memory on error as it
is done in other error checks above in the "service_insert_characteristic"
function.

Bluetoothd address sanitizer backtrace:
=================================================================
==88967==ERROR: LeakSanitizer: detected memory leaks
    
Direct leak of 768 byte(s) in 6 object(s) allocated from:
    #0 0x768d1f8b3ec7 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x59f0cac1ea07 in util_malloc src/shared/util.c:46
    
Indirect leak of 576 byte(s) in 18 object(s) allocated from:
    #0 0x768d1f8b3ec7 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x59f0cac1ea07 in util_malloc src/shared/util.c:46
    
Indirect leak of 114 byte(s) in 6 object(s) allocated from:
    #0 0x768d1f8b4097 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    #1 0x59f0cac75d6c in new_attribute src/shared/gatt-db.c:222
    
SUMMARY: AddressSanitizer: 1458 byte(s) leaked in 30 allocation(s).

The patch fixes abscent cleanup as well as adds additional check to exit
early if value handle is below or equal to Characteristic handle.

Stanislavs Nilovs (1):
  Fix memory leak when adding GATT Characteristic

 src/shared/gatt-db.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

-- 
2.48.1


             reply	other threads:[~2025-08-12  8:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-12  8:00 valorcool [this message]
2025-08-12  8:00 ` [PATCH BlueZ v1 1/1] Fix memory leak when adding GATT Characteristic valorcool
2025-08-12  9:39   ` bluez.test.bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250812080044.131526-1-valorcool@gmail.com \
    --to=valorcool@gmail.com \
    --cc=devyani.godbole@nxp.com \
    --cc=iulia.tanasescu@nxp.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.von.dentz@intel.com \
    --cc=mahesh.talewad@nxp.com \
    --cc=mihai-octavian.urzica@nxp.com \
    --cc=vinit.mehta@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.