All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ] gatt-server: Flush notify multiple buffer when full and fix overflow
@ 2021-06-11 12:29 Sebastian Urban
  2021-06-11 12:52 ` [BlueZ] " bluez.test.bot
  2021-06-11 22:13 ` [PATCH BlueZ] " Luiz Augusto von Dentz
  0 siblings, 2 replies; 10+ messages in thread
From: Sebastian Urban @ 2021-06-11 12:29 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Sebastian Urban

This fixes the calculation of available buffer space in
bt_gatt_server_send_notification and sends pending notifications
immediately when there is no more room to add a notification.

Previously there was a buffer overflow caused by incorrect calculation
of available buffer space: data->offset can equal data->len
from a previous call to this function, leading
(data->len - data->offset) to underflow after data->offset += 2.
---
 src/shared/gatt-server.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/shared/gatt-server.c b/src/shared/gatt-server.c
index 970c35f94..d53efe782 100644
--- a/src/shared/gatt-server.c
+++ b/src/shared/gatt-server.c
@@ -1700,20 +1700,35 @@ bool bt_gatt_server_send_notification(struct bt_gatt_server *server,
 	if (!server || (length && !value))
 		return false;
 
-	if (multiple)
+	if (multiple) {
 		data = server->nfy_mult;
 
+		/* flush buffered data if this request hits buffer size limit */
+		if (data && data->offset > 0 &&
+				data->len - data->offset < 4 + length) {
+			if (server->nfy_mult->id)
+				timeout_remove(server->nfy_mult->id);
+			notify_multiple(server);
+			data = NULL;
+		}
+	}
+
 	if (!data) {
 		data = new0(struct nfy_mult_data, 1);
 		data->len = bt_att_get_mtu(server->att) - 1;
 		data->pdu = malloc(data->len);
 	}
 
+	if (multiple) {
+		if (data->len - data->offset < 4 + length)
+			return false;
+	} else {
+		if (data->len - data->offset < 2 + length)
+			return false;
+	}
+
 	put_le16(handle, data->pdu + data->offset);
 	data->offset += 2;
-
-	length = MIN(data->len - data->offset, length);
-
 	if (multiple) {
 		put_le16(length, data->pdu + data->offset);
 		data->offset += 2;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread
* [PATCH BlueZ] gatt-server: Flush notify multiple buffer when full and fix overflow
@ 2021-06-11  8:19 Sebastian Urban
  2021-06-11  8:42 ` [BlueZ] " bluez.test.bot
  0 siblings, 1 reply; 10+ messages in thread
From: Sebastian Urban @ 2021-06-11  8:19 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Sebastian Urban

This fixes the calculation of available buffer space in
bt_gatt_server_send_notification and sends pending notifications
immediately when there is no more room to add a notification.

Previously there was a buffer overflow caused by incorrect calculation
of available buffer space: data->offset can equal data->len
from a previous call to this function, leading
(data->len - data->offset) to underflow after data->offset += 2.
---
 src/shared/gatt-server.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/shared/gatt-server.c b/src/shared/gatt-server.c
index 970c35f94..e7155f16a 100644
--- a/src/shared/gatt-server.c
+++ b/src/shared/gatt-server.c
@@ -1700,20 +1700,34 @@ bool bt_gatt_server_send_notification(struct bt_gatt_server *server,
 	if (!server || (length && !value))
 		return false;
 
-	if (multiple)
+	if (multiple) {
 		data = server->nfy_mult;
 
+		/* Flush buffered data, if this request hits buffer size limit */
+		if (data && data->offset > 0 && data->len - data->offset < 4 + length) {
+			if (server->nfy_mult->id)
+				timeout_remove(server->nfy_mult->id);
+			notify_multiple(server);
+			data = NULL;
+		}
+	}
+
 	if (!data) {
 		data = new0(struct nfy_mult_data, 1);
 		data->len = bt_att_get_mtu(server->att) - 1;
 		data->pdu = malloc(data->len);
 	}
 
+	if (multiple) {
+		if (data->len - data->offset < 4 + length)
+			return false;
+	} else {
+		if (data->len - data->offset < 2 + length)
+			return false;
+	}
+
 	put_le16(handle, data->pdu + data->offset);
 	data->offset += 2;
-
-	length = MIN(data->len - data->offset, length);
-
 	if (multiple) {
 		put_le16(length, data->pdu + data->offset);
 		data->offset += 2;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-06-14 21:06 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-11 12:29 [PATCH BlueZ] gatt-server: Flush notify multiple buffer when full and fix overflow Sebastian Urban
2021-06-11 12:52 ` [BlueZ] " bluez.test.bot
2021-06-11 22:13 ` [PATCH BlueZ] " Luiz Augusto von Dentz
2021-06-12  9:29   ` Sebastian Urban
2021-06-12  9:32   ` Sebastian Urban
2021-06-12  9:56     ` [BlueZ] " bluez.test.bot
2021-06-13 10:39       ` [PATCH BlueZ] " Sebastian Urban
2021-06-13 11:04         ` [BlueZ] " bluez.test.bot
2021-06-14 21:06           ` Luiz Augusto von Dentz
  -- strict thread matches above, loose matches on Subject: below --
2021-06-11  8:19 [PATCH BlueZ] " Sebastian Urban
2021-06-11  8:42 ` [BlueZ] " bluez.test.bot

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.