From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v2 4/5] client/mgmt: Fix potentially overflowing call to snprintf
Date: Mon, 28 Apr 2025 15:51:21 -0400 [thread overview]
Message-ID: <20250428195122.2000808-4-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20250428195122.2000808-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
The return value of a call to snprintf is the number of characters that
would have been written to the buffer assuming there was sufficient
space.
In the event that the operation reaches the end of the buffer and more
than one character is discarded, the return value will be greater than
the buffer size.
Fixes: https://github.com/bluez/bluez/issues/1216
Fixes: https://github.com/bluez/bluez/issues/1217
Fixes: https://github.com/bluez/bluez/issues/1218
Fixes: https://github.com/bluez/bluez/issues/1219
---
client/mgmt.c | 48 ++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 40 insertions(+), 8 deletions(-)
diff --git a/client/mgmt.c b/client/mgmt.c
index 86b5879db8b0..faa97a159e3c 100644
--- a/client/mgmt.c
+++ b/client/mgmt.c
@@ -316,9 +316,17 @@ static const char *options2str(uint32_t options)
str[0] = '\0';
for (i = 0; i < NELEM(options_str); i++) {
- if ((options & (1 << i)) != 0)
- off += snprintf(str + off, sizeof(str) - off, "%s ",
+ if ((options & (1 << i)) != 0) {
+ int n = snprintf(str + off, sizeof(str) - off, "%s ",
options_str[i]);
+
+ if (n < 0 || n >= (int)(sizeof(str) - off)) {
+ str[off] = '\0';
+ break;
+ }
+
+ off += n;
+ }
}
return str;
@@ -372,9 +380,17 @@ static const char *settings2str(uint32_t settings)
str[0] = '\0';
for (i = 0; i < NELEM(settings_str); i++) {
- if ((settings & (1 << i)) != 0)
- off += snprintf(str + off, sizeof(str) - off, "%s ",
+ if ((settings & (1 << i)) != 0) {
+ int n = snprintf(str + off, sizeof(str) - off, "%s ",
settings_str[i]);
+
+ if (n < 0 || n >= (int)(sizeof(str) - off)) {
+ str[off] = '\0';
+ break;
+ }
+
+ off += n;
+ }
}
return str;
@@ -4490,9 +4506,17 @@ static const char *adv_flags2str(uint32_t flags)
str[0] = '\0';
for (i = 0; i < NELEM(adv_flags_str); i++) {
- if ((flags & (1 << i)) != 0)
- off += snprintf(str + off, sizeof(str) - off, "%s ",
+ if ((flags & (1 << i)) != 0) {
+ int n = snprintf(str + off, sizeof(str) - off, "%s ",
adv_flags_str[i]);
+
+ if (n < 0 || n >= (int)(sizeof(str) - off)) {
+ str[off] = '\0';
+ break;
+ }
+
+ off += n;
+ }
}
return str;
@@ -5429,9 +5453,17 @@ static const char *phys2str(uint32_t phys)
str[0] = '\0';
for (i = 0; i < NELEM(phys_str); i++) {
- if ((phys & (1 << i)) != 0)
- off += snprintf(str + off, sizeof(str) - off, "%s ",
+ if ((phys & (1 << i)) != 0) {
+ int n = snprintf(str + off, sizeof(str) - off, "%s ",
phys_str[i]);
+
+ if (n < 0 || n >= (int)(sizeof(str) - off)) {
+ str[off] = '\0';
+ break;
+ }
+
+ off += n;
+ }
}
return str;
--
2.49.0
next prev parent reply other threads:[~2025-04-28 19:51 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-28 19:51 [PATCH BlueZ v2 1/5] main: Fix comparison of narrow type with wide type in loop condition Luiz Augusto von Dentz
2025-04-28 19:51 ` [PATCH BlueZ v2 2/5] client/mgmt: " Luiz Augusto von Dentz
2025-04-28 19:51 ` [PATCH BlueZ v2 3/5] test-runner: Fix potentially overflowing call to snprintf Luiz Augusto von Dentz
2025-04-28 19:51 ` Luiz Augusto von Dentz [this message]
2025-04-28 19:51 ` [PATCH BlueZ v2 5/5] shared/bap: Too few arguments to formatting function Luiz Augusto von Dentz
2025-04-28 21:14 ` [BlueZ,v2,1/5] main: Fix comparison of narrow type with wide type in loop condition bluez.test.bot
2025-04-28 21:40 ` [PATCH BlueZ v2 1/5] " patchwork-bot+bluetooth
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=20250428195122.2000808-4-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox