Fix a number of static analysis issues #6
Subsystems (2)
cover Fix a number of static analysis issues #6
Last batch of static analysis fixes, all the other warnings were filed as issues against Coverity, or were already submitted as part of earlier patch sets.
Let me know if you have any questions about those patches, or the earlier ones.
Bastien Nocera (8):
sdp: Ensure size doesn't overflow tools/isotest: Ensure ret doesn't overflow
health: mcap: Ensure sent doesn't overflow shared/tester: Add early failure check
mesh: Fix possible integer overflow shared/gatt-db: Fix possible buffer overrun shared/btsnoop: Avoid underflowing toread variable
monitor: Check for possible integer underflow
lib/sdp.c | 6 +++--- mesh/net.c | 13 +++++++++++-- monitor/control.c | 8 +++++++- profiles/health/mcap.c | 2 +- src/shared/btsnoop.c | 7 ++++++- src/shared/gatt-db.c | 11 ++++++++--- src/shared/tester.c | 2 ++ tools/isotest.c | 2 ++ 8 files changed, 40 insertions(+), 11 deletions(-)
Signature · 1 line
2.45.2
PATCH 1/8 sdp: Ensure size doesn't overflow
Error: INTEGER_OVERFLOW (CWE-190): [#def1] [important]bluez-5.77/lib/sdp.c:1685:2: tainted_data_argument: The check "sent < size" contains the tainted expression "sent" which causes "size" to be considered tainted. bluez-5.77/lib/sdp.c:1686:3: overflow: The expression "size - sent" is deemed overflowed because at least one of its arguments has overflowed. bluez-5.77/lib/sdp.c:1686:3: overflow_sink: "size - sent", which might have underflowed, is passed to "send(session->sock, buf + sent, size - sent, 0)".
1684|
1685| while (sent < size) {
1686|-> int n = send(session->sock, buf + sent, size - sent, 0);
1687| if (n < 0)
1688| return -1;
---
lib/sdp.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)lib/sdp.c +3 -3
@@ -1678,13 +1678,13 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId)return NULL ; } -static int sdp_send_req (sdp_session_t *session,uint8_t *buf,uint32_t size) +static int sdp_send_req (sdp_session_t *session,uint8_t *buf,size_t size) { -uint32_t sent =0 ; +size_t sent =0 ;while (sent < size) {int n =send (session->sock, buf + sent, size - sent,0 ); -if (n <0 ) +if (n <0 || sent >SIZE_MAX - n)return -1 ; sent += n; }
Signature · 1 line
2.45.2
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=876731
---Test result---
Test Summary:
CheckPatch PASS 3.58 seconds GitLint FAIL 3.12 seconds BuildEll PASS 24.61 seconds BluezMake PASS 1794.29 seconds MakeCheck PASS 13.96 seconds MakeDistcheck PASS 179.69 seconds CheckValgrind PASS 255.36 seconds CheckSmatch PASS 357.62 seconds bluezmakeextell PASS 120.81 seconds IncrementalBuild PASS 13591.94 seconds ScanBuild PASS 1074.61 seconds
Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[BlueZ,1/8] sdp: Ensure size doesn't overflow
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B1 Line exceeds max length (160>80): "bluez-5.77/lib/sdp.c:1685:2: tainted_data_argument: The check "sent < size" contains the tainted expression "sent" which causes "size" to be considered tainted."
5: B1 Line exceeds max length (142>80): "bluez-5.77/lib/sdp.c:1686:3: overflow: The expression "size - sent" is deemed overflowed because at least one of its arguments has overflowed."
6: B1 Line exceeds max length (152>80): "bluez-5.77/lib/sdp.c:1686:3: overflow_sink: "size - sent", which might have underflowed, is passed to "send(session->sock, buf + sent, size - sent, 0)"."
8: B3 Line contains hard tab characters (\t): "1685| while (sent < size) {"
9: B3 Line contains hard tab characters (\t): "1686|-> int n = send(session->sock, buf + sent, size - sent, 0);"
10: B3 Line contains hard tab characters (\t): "1687| if (n < 0)"
11: B3 Line contains hard tab characters (\t): "1688| return -1;"
[BlueZ,2/8] tools/isotest: Ensure ret doesn't overflow
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B1 Line exceeds max length (165>80): "bluez-5.77/tools/isotest.c:778:2: tainted_data_argument: The check "ret < count" contains the tainted expression "ret" which causes "count" to be considered tainted."
5: B1 Line exceeds max length (147>80): "bluez-5.77/tools/isotest.c:779:3: overflow: The expression "count - ret" is deemed overflowed because at least one of its arguments has overflowed."
6: B1 Line exceeds max length (237>80): "bluez-5.77/tools/isotest.c:779:3: overflow_sink: "count - ret", which might have underflowed, is passed to "read(fd, buf + ret, count - ret)". [Note: The source code implementation of the function has been overridden by a builtin model.]"
8: B3 Line contains hard tab characters (\t): "778| while (ret < count) {"
9: B3 Line contains hard tab characters (\t): "779|-> len = read(fd, buf + ret, count - ret);"
10: B3 Line contains hard tab characters (\t): "780| if (len < 0)"
11: B3 Line contains hard tab characters (\t): "781| return -errno;"
[BlueZ,3/8] health: mcap: Ensure sent doesn't overflow
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B1 Line exceeds max length (172>80): "bluez-5.77/profiles/health/mcap.c:390:2: tainted_data_argument: The check "sent < size" contains the tainted expression "sent" which causes "size" to be considered tainted."
5: B1 Line exceeds max length (154>80): "bluez-5.77/profiles/health/mcap.c:391:3: overflow: The expression "size - sent" is deemed overflowed because at least one of its arguments has overflowed."
6: B1 Line exceeds max length (155>80): "bluez-5.77/profiles/health/mcap.c:391:3: overflow_sink: "size - sent", which might have underflowed, is passed to "write(sock, buf_b + sent, size - sent)"."
8: B3 Line contains hard tab characters (\t): "390| while (sent < size) {"
9: B3 Line contains hard tab characters (\t): "391|-> int n = write(sock, buf_b + sent, size - sent);"
10: B3 Line contains hard tab characters (\t): "392| if (n < 0)"
11: B3 Line contains hard tab characters (\t): "393| return -1;"
[BlueZ,4/8] shared/tester: Add early failure check
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
7: B1 Line exceeds max length (107>80): "bluez-5.77/src/shared/tester.c:946:2: return_constant: Function call "io_send(io, iov, 1)" may return -107."
8: B1 Line exceeds max length (123>80): "bluez-5.77/src/shared/tester.c:946:2: assignment: Assigning: "len" = "io_send(io, iov, 1)". The value of "len" is now -107."
9: B1 Line exceeds max length (258>80): "bluez-5.77/src/shared/tester.c:948:2: overrun-buffer-arg: Calling "tester_monitor" with "iov->iov_base" and "len" is suspicious because of the very large index, 18446744073709551509. The index may be due to a negative parameter being interpreted as unsigned."
10: B3 Line contains hard tab characters (\t): "946| len = io_send(io, iov, 1);"
12: B3 Line contains hard tab characters (\t): "948|-> tester_monitor('<', 0x0004, 0x0000, iov->iov_base, len);"
14: B3 Line contains hard tab characters (\t): "950| g_assert_cmpint(len, ==, iov->iov_len);"[BlueZ,5/8] mesh: Fix possible integer overflow
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B1 Line exceeds max length (120>80): "bluez-5.77/mesh/net.c:3164:4: cast_overflow: Truncation due to cast operation on "msg->len - seg_off" from 32 to 8 bits."
5: B1 Line exceeds max length (95>80): "bluez-5.77/mesh/net.c:3164:4: overflow_assign: "seg_len" is assigned from "msg->len - seg_off"."
6: B1 Line exceeds max length (306>80): "bluez-5.77/mesh/net.c:3178:2: overflow_sink: "seg_len", which might have overflowed, is passed to "mesh_crypto_packet_build(false, msg->ttl, seq_num, msg->src, msg->remote, 0, msg->segmented, msg->key_aid, msg->szmic, false, msg->seqZero, segO, segN, msg->buf + seg_off, seg_len, packet + 1, &packet_len)"."
8: B3 Line contains hard tab characters (\t): "3177| /* TODO: Are we RXing on an LPN's behalf? Then set RLY bit */"
9: B3 Line contains hard tab characters (\t): "3178|-> if (!mesh_crypto_packet_build(false, msg->ttl, seq_num, msg->src,"
10: B3 Line contains hard tab characters (\t): "3179| msg->remote, 0, msg->segmented,"
11: B3 Line contains hard tab characters (\t): "3180| msg->key_aid, msg->szmic, false,"
[BlueZ,6/8] shared/gatt-db: Fix possible buffer overrun
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
13: B1 Line exceeds max length (152>80): "bluez-5.77/src/shared/gatt-db.c:612:2: assignment: Assigning: "len" = "uuid_to_le(uuid, value)". The value of "len" is now between 0 and 31 (inclusive)."
14: B1 Line exceeds max length (206>80): "bluez-5.77/src/shared/gatt-db.c:614:2: overrun-buffer-arg: Overrunning array "value" of 16 bytes by passing it to a function which accesses it at byte offset 30 using argument "len" (which evaluates to 31)."
15: B3 Line contains hard tab characters (\t): "612| len = uuid_to_le(uuid, value);"
17: B3 Line contains hard tab characters (\t): "614|-> service->attributes[0] = new_attribute(service, handle, type, value,"
18: B3 Line contains hard tab characters (\t): "615| len);"
19: B3 Line contains hard tab characters (\t): "616| if (!service->attributes[0]) {"[BlueZ,7/8] shared/btsnoop: Avoid underflowing toread variable
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B1 Line exceeds max length (136>80): "bluez-5.77/src/shared/btsnoop.c:556:3: underflow: The decrement operator on the unsigned variable "toread" might result in an underflow."
5: B1 Line exceeds max length (236>80): "bluez-5.77/src/shared/btsnoop.c:572:2: overflow_sink: "toread", which might have underflowed, is passed to "read(btsnoop->fd, data, toread)". [Note: The source code implementation of the function has been overridden by a builtin model.]"
6: B3 Line contains hard tab characters (\t): "570| }"
8: B3 Line contains hard tab characters (\t): "572|-> len = read(btsnoop->fd, data, toread);"
9: B3 Line contains hard tab characters (\t): "573| if (len < 0) {"10: B3 Line contains hard tab characters (\t): "574| btsnoop->aborted = true;"
[BlueZ,8/8] monitor: Check for possible integer underflow
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B1 Line exceeds max length (205>80): "bluez-5.77/monitor/control.c:1094:2: tainted_data_return: Called function "recv(data->fd, data->buf + data->offset, 1490UL - data->offset, MSG_DONTWAIT)", and a possible return value may be less than zero."
5: B1 Line exceeds max length (144>80): "bluez-5.77/monitor/control.c:1094:2: assign: Assigning: "len" = "recv(data->fd, data->buf + data->offset, 1490UL - data->offset, MSG_DONTWAIT)"."
6: B1 Line exceeds max length (119>80): "bluez-5.77/monitor/control.c:1099:2: overflow: The expression "data->offset" is considered to have possibly overflowed."
7: B1 Line exceeds max length (165>80): "bluez-5.77/monitor/control.c:1115:3: overflow: The expression "data->offset -= pktlen + 6" is deemed overflowed because at least one of its arguments has overflowed."
8: B1 Line exceeds max length (265>80): "bluez-5.77/monitor/control.c:1118:4: overflow_sink: "data->offset", which might have underflowed, is passed to "memmove(data->buf, data->buf + 6 + pktlen, data->offset)". [Note: The source code implementation of the function has been overridden by a builtin model.]"
10: B3 Line contains hard tab characters (\t): "1117| if (data->offset > 0)" 11: B3 Line contains hard tab characters (\t): "1118|-> memmove(data->buf, data->buf + MGMT_HDR_SIZE + pktlen," 12: B3 Line contains hard tab characters (\t): "1119| data->offset);" 13: B3 Line contains hard tab characters (\t): "1120| }"
---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz
maintainerRe: [BlueZ 1/8] sdp: Ensure size doesn't overflow
In reply to Bastien NoceraHi Bastien,
Quoted 23 lines · Bastien Nocera
Error:
INTEGER_OVERFLOW(CWE-190): [#def1] [important]bluez-5.77/lib/sdp.c:1685:2:tainted_data_argument: The check "sent < size" contains the tainted expression "sent" which causes "size" to be considered tainted.bluez-5.77/lib/sdp.c:1686:3: overflow: The expression "size - sent" is deemed overflowed because at least one of its arguments has overflowed.bluez-5.77/lib/sdp.c:1686:3:overflow_sink: "size - sent", which might have underflowed, is passed to "send(session->sock, buf + sent, size - sent, 0)".1684| 1685| while (sent < size) { 1686|-> int n = send(session->sock, buf + sent, size - sent, 0); 1687| if (n < 0) 1688| return -1; --- lib/sdp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)lib/sdp.c +0
-1@@ -1678,13 +1678,13 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId)return NULL ; } -static int sdp_send_req (sdp_session_t *session,uint8_t *buf,uint32_t size)
lib/sdp.c sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) · line 1688
Quoted 10 lines
lib/sdp.c +3
-2+static int sdp_send_req (sdp_session_t *session,uint8_t *buf,size_t size) { -uint32_t sent =0 ; +size_t sent =0 ;while (sent < size) {int n =send (session->sock, buf + sent, size - sent,0 ); -if (n <0 ) +if (n <0 || sent >SIZE_MAX - n)return -1 ;
Not really following you here, it seems the problem is that sent + n could overflow causing sent to loop around but if that happens don't we get a problem with send syscall itself? Using size_t makes sense but I guess we want n to also be ssize_t then, and perhap use if (n < 0 || n > size - sent) logic if we cannot trust syscalls returns the correct number of bytes.
Quoted 6 lines
lib/sdp.c +0
-1sent += n; } --2.45.2
Signature · 1 line
Luiz Augusto von Dentz
Re: [BlueZ 1/8] sdp: Ensure size doesn't overflow
Quoted 42 lines · Luiz Augusto von Dentz
Hi Bastien,
Quoted 38 lines · Bastien Nocera
Error:
INTEGER_OVERFLOW(CWE-190): [#def1] [important]bluez-5.77/lib/sdp.c:1685:2:tainted_data_argument: The check "sent < size" contains the tainted expression "sent" which causes "size" to be considered tainted.bluez-5.77/lib/sdp.c:1686:3: overflow: The expression "size - sent" is deemed overflowed because at least one of its arguments has overflowed.bluez-5.77/lib/sdp.c:1686:3:overflow_sink: "size - sent", which might have underflowed, is passed to "send(session->sock, buf + sent, size - sent, 0)".1684| 1685| while (sent < size) { 1686|-> int n = send(session->sock, buf + sent,size - sent, 0);
1687| if (n < 0)
1688| return -1;
---
lib/sdp.c | 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)lib/sdp.c +2
-2@@ -1678,13 +1678,13 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) return NULL; } -static int sdp_send_req (sdp_session_t *session,uint8_t *buf, uint32_t size) +static int sdp_send_req (sdp_session_t *session,uint8_t *buf,size_t size) { - uint32_t sent = 0; + size_t sent = 0;
Quoted 13 lines
Quoted 6 lines
lib/sdp.c +0
-0while (sent < size) {int n =
send(session->sock, buf + sent, size - sent, 0);
- if (n < 0)
+ if (n < 0 || sent >SIZE_MAX- n) return -1;Not really following you here, it seems the problem is that sent + n could overflow causing sent to loop around but if that happens don't we get a problem with send syscall itself? Using
size_tmakes sense but I guess we want n to also bessize_tthen, and perhap use if (n < 0 || n > size - sent) logic if we cannot trust syscalls returns the correct number of bytes.
I don't think that the static analysis is this clever, it just wants to make sure that bounds checking is done to avoid integer overflows.
I'm fine with quieting Coverity on my end, after fixing the data types if that's alright with you.
Also goes for patch 8 of this series.
Quoted 9 lines
Quoted 7 lines
sent += n;
}
--
2.45.2
PATCH 2/8 tools/isotest: Ensure ret doesn't overflow
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def20] [important]bluez-5.77/tools/isotest.c:778:2: tainted_data_argument: The check "ret < count" contains the tainted expression "ret" which causes "count" to be considered tainted. bluez-5.77/tools/isotest.c:779:3: overflow: The expression "count - ret" is deemed overflowed because at least one of its arguments has overflowed.bluez-5.77/tools/isotest.c:779:3: overflow_sink: "count - ret", which might have underflowed, is passed to "read(fd, buf + ret, count - ret)". [Note: The source code implementation of the function has been overridden by a builtin model.]
777|
778| while (ret < count) {
779|-> len = read(fd, buf + ret, count - ret);
780| if (len < 0)
781| return -errno;
---
tools/isotest.c | 2 ++
1 file changed, 2 insertions(+)tools/isotest.c +2 -0
@@ -779,6 +779,8 @@ static int read_stream(int fd, ssize_t count) len =read (fd, buf + ret, count - ret);if (len <0 )return -errno; +if (len >SSIZE_MAX - ret) +return -EOVERFLOW ; ret += len;usleep (1000 );
Signature · 1 line
2.45.2
PATCH 3/8 health: mcap: Ensure sent doesn't overflow
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def13] [important]bluez-5.77/profiles/health/mcap.c:390:2: tainted_data_argument: The check "sent < size" contains the tainted expression "sent" which causes "size" to be considered tainted. bluez-5.77/profiles/health/mcap.c:391:3: overflow: The expression "size - sent" is deemed overflowed because at least one of its arguments has overflowed. bluez-5.77/profiles/health/mcap.c:391:3: overflow_sink: "size - sent", which might have underflowed, is passed to "write(sock, buf_b + sent, size - sent)".
389|
390| while (sent < size) {
391|-> int n = write(sock, buf_b + sent, size - sent);
392| if (n < 0)
393| return -1;
---
profiles/health/mcap.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)profiles/health/mcap.c +1 -1
@@ -389,7 +389,7 @@ int mcap_send_data(int sock, const void *buf, uint32_t size)while (sent < size) {int n =write (sock, buf_b + sent, size - sent); -if (n <0 ) +if (n <0 || n >SSIZE_MAX - sent)return -1 ; sent += n; }
Signature · 1 line
2.45.2
PATCH 4/8 shared/tester: Add early failure check
In reply to Bastien NoceraAdd a similar assertion to the other tests to avoid passing negative len to tester_monitor() which might result in crashes.
Error: OVERRUN (CWE-119): [#def13] [important]bluez-5.77/src/shared/tester.c:946:2: return_constant: Function call "io_send(io, iov, 1)" may return -107.bluez-5.77/src/shared/tester.c:946:2: assignment: Assigning: "len" = "io_send(io, iov, 1)". The value of "len" is now -107.bluez-5.77/src/shared/tester.c:948:2: overrun-buffer-arg: Calling "tester_monitor" with "iov->iov_base" and "len" is suspicious because of the very large index, 18446744073709551509. The index may be due to a negative parameter being interpreted as unsigned.
946| len = io_send(io, iov, 1);
947|
948|-> tester_monitor('<', 0x0004, 0x0000, iov->iov_base, len);
949|
950| g_assert_cmpint(len, ==, iov->iov_len);
---
src/shared/tester.c | 2 ++
1 file changed, 2 insertions(+)src/shared/tester.c +2 -0
@@ -945,6 +945,8 @@ static bool test_io_send(struct io *io, void *user_data) len =io_send (io, iov,1 ); +g_assert (len >0 ); +tester_monitor ('<' ,0x0004 ,0x0000 , iov->iov_base, len); g_assert_cmpint(len, ==, iov->iov_len);
Signature · 1 line
2.45.2
PATCH 5/8 mesh: Fix possible integer overflow
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def1] [important]bluez-5.77/mesh/net.c:3164:4: cast_overflow: Truncation due to cast operation on "msg->len - seg_off" from 32 to 8 bits.bluez-5.77/mesh/net.c:3164:4: overflow_assign: "seg_len" is assigned from "msg->len - seg_off".bluez-5.77/mesh/net.c:3178:2: overflow_sink: "seg_len", which might have overflowed, is passed to "mesh_crypto_packet_build(false, msg->ttl, seq_num, msg->src, msg->remote, 0, msg->segmented, msg->key_aid, msg->szmic, false, msg->seqZero, segO, segN, msg->buf + seg_off, seg_len, packet + 1, &packet_len)".
3176| 3177| /* TODO: Are we RXing on an LPN's behalf? Then set RLY bit */ 3178|-> if (!mesh_crypto_packet_build(false, msg->ttl, seq_num, msg->src, 3179| msg->remote, 0, msg->segmented, 3180| msg->key_aid, msg->szmic, false,
X
--- mesh/net.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
mesh/net.c +11 -2
@@ -3149,13 +3149,22 @@ static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval,uint32_t seq_num;if (msg->segmented) { +if (msg->len < seg_off) { +l_error ("Failed to build packet" ); +return false; + }/* Send each segment on unique seq_num */ seq_num =mesh_net_next_seq_num (net); -if (msg->len - seg_off >SEG_OFF (1 )) +if (msg->len - seg_off >SEG_OFF (1 )) { seg_len =SEG_OFF (1 ); -else + }else { +if (msg->len - seg_off >UINT8_MAX ) { +l_error ("Failed to build packet" ); +return false; + } seg_len = msg->len - seg_off; + } }else {/* Send on same seq_num used for Access Layer */ seq_num = msg->seqAuth;
Signature · 1 line
2.45.2
PATCH 6/8 shared/gatt-db: Fix possible buffer overrun
In reply to Bastien Nocerauuid_to_le() returns one of the possible values from bt_uuid_len(). bt_uuid_len() returns "type / 8".
type is a value between 0 and 128, but could be something else depending on the validity of the UUID that's parsed. So an invalid value of type between 128 and 256 would trigger an overrun.
Add a check to make sure that an invalid type isn't used to calculate the length.
Error: OVERRUN (CWE-119): [#def6] [important]bluez-5.77/src/shared/gatt-db.c:612:2: assignment: Assigning: "len" = "uuid_to_le(uuid, value)". The value of "len" is now between 0 and 31 (inclusive). bluez-5.77/src/shared/gatt-db.c:614:2: overrun-buffer-arg: Overrunning array "value" of 16 bytes by passing it to a function which accesses it at byte offset 30 using argument "len" (which evaluates to 31).
612| len = uuid_to_le(uuid, value);
613|
614|-> service->attributes[0] = new_attribute(service, handle, type, value,
615| len);
616| if (!service->attributes[0]) {
---
src/shared/gatt-db.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)src/shared/gatt-db.c +8 -3
@@ -560,9 +560,14 @@ static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)return bt_uuid_len (uuid); } -bt_uuid_to_uuid128 (uuid, &uuid128); -bswap_128 (&uuid128.value.u128, dst); -return bt_uuid_len (&uuid128); +if (uuid->type ==BT_UUID32 || + uuid->type ==BT_UUID128 ) { +bt_uuid_to_uuid128 (uuid, &uuid128); +bswap_128 (&uuid128.value.u128, dst); +return bt_uuid_len (&uuid128); + } + +return 0 ; }static bool le_to_uuid (const uint8_t *src,size_t len,bt_uuid_t *uuid)
Signature · 1 line
2.45.2
Luiz Augusto von Dentz
maintainerRe: [BlueZ 6/8] shared/gatt-db: Fix possible buffer overrun
Hi Bastien,
Quoted 27 lines · Bastien Nocera
uuid_to_le()returns one of the possible values frombt_uuid_len().bt_uuid_len()returns "type / 8".
type is a value between 0 and 128, but could be something else depending on the validity of the UUID that's parsed. So an invalid value of type between 128 and 256 would trigger an overrun.Add a check to make sure that an invalid type isn't used to calculate the length.
Error: OVERRUN (CWE-119): [#def6] [important]
bluez-5.77/src/shared/gatt-db.c:612:2: assignment: Assigning: "len" = "uuid_to_le(uuid, value)". The value of "len" is now between 0 and 31 (inclusive).bluez-5.77/src/shared/gatt-db.c:614:2: overrun-buffer-arg: Overrunning array "value" of 16 bytes by passing it to a function which accesses it at byte offset 30 using argument "len" (which evaluates to 31).612| len = uuid_to_le(uuid, value); 613| 614|-> service->attributes[0] = new_attribute(service, handle, type, value, 615| len); 616| if (!service->attributes[0]) { --- src/shared/gatt-db.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)src/shared/gatt-db.c +0
-0
src/shared/gatt-db.c static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst) · line 563
Quoted 7 lines
src/shared/gatt-db.c +1
-3@@ -560,9 +560,14 @@ static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)return bt_uuid_len (uuid); } -bt_uuid_to_uuid128 (uuid, &uuid128); -bswap_128 (&uuid128.value.u128, dst); -return bt_uuid_len (&uuid128); + if (uuid->type == BT_UUID32 ||
BT_UUID32 is not really valid in LE, so Id leave this check to be:
switch (uuid->type) {
case BT_UUID16:
put_le16(uuid->value.u16, dst);
return bt_uuid_len(uuid);
case BT_UUID128:
bt_uuid_to_uuid128(uuid, &uuid128);
bswap_128(&uuid128.value.u128, dst);
return bt_uuid_len(&uuid128);
}return 0;
We do however have bt_uuid_to_le with the only difference being that it is more generic and it doesn't return the actual bytes written to the dst, anyway we could replace the code above with:
src/shared/gatt-db.c +2 -9
@@ -553,16 +553,9 @@ bool gatt_db_isempty(struct gatt_db *db)static int uuid_to_le (const bt_uuid_t *uuid,uint8_t *dst) { -bt_uuid_t uuid128; +bt_uuid_to_le (uuid, dst); -if (uuid->type ==BT_UUID16 ) { -put_le16 (uuid->value.u16, dst); -return bt_uuid_len (uuid); - } - -bt_uuid_to_uuid128 (uuid, &uuid128); -bswap_128 (&uuid128.value.u128, dst); -return bt_uuid_len (&uuid128); +return bt_uuid_len (uuid) ==4 ?16 :bt_uuid_len (uuid); }
Quoted 14 lines
src/shared/gatt-db.c +7
-1+ uuid->type == BT_UUID128) { +bt_uuid_to_uuid128 (uuid, &uuid128); +bswap_128 (&uuid128.value.u128, dst); +return bt_uuid_len (&uuid128); + } + +return 0 ; }static bool le_to_uuid (const uint8_t *src,size_t len,bt_uuid_t *uuid) --2.45.2
Signature · 1 line
Luiz Augusto von Dentz
Re: [BlueZ 6/8] shared/gatt-db: Fix possible buffer overrun
Quoted 76 lines · Luiz Augusto von Dentz
Hi Bastien,
Quoted 35 lines · Bastien Nocera
uuid_to_le()returns one of the possible values frombt_uuid_len().bt_uuid_len()returns "type / 8".
type is a value between 0 and 128, but could be something else depending on the validity of the UUID that's parsed. So an invalid value of type between 128 and 256 would trigger an overrun.Add a check to make sure that an invalid type isn't used to calculate
the length.Error: OVERRUN (CWE-119): [#def6] [important]
bluez-5.77/src/shared/gatt-db.c:612:2: assignment: Assigning: "len" = "uuid_to_le(uuid, value)". The value of "len" is now between 0 and 31 (inclusive).bluez-5.77/src/shared/gatt-db.c:614:2: overrun-buffer-arg:
Overrunning array "value" of 16 bytes by passing it to a function which accesses it at byte offset 30 using argument "len" (which evaluates to 31).612| len = uuid_to_le(uuid, value); 613| 614|-> service->attributes[0] = new_attribute(service, handle,type, value,
615|
len);
616| if (!service->attributes[0]) {
---
src/shared/gatt-db.c | 11 ++++++++---1 file changed, 8 insertions(+), 3 deletions(-)src/shared/gatt-db.c +0
-0src/shared/gatt-db.c static int uuid_to_le(const bt_uuid_t *uuid, · line 564
Quoted 8 lines
src/shared/gatt-db.c +1
-3@@ -560,9 +560,14 @@ static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst) return bt_uuid_len(uuid); } - bt_uuid_to_uuid128(uuid, &uuid128); - bswap_128(&uuid128.value.u128, dst); - return bt_uuid_len(&uuid128); + if (uuid->type == BT_UUID32 ||
BT_UUID32is not really valid in LE, so Id leave this check to be:switch (
uuid->type) {
caseBT_UUID16:
put_le16(uuid->value.u16, dst);
returnbt_uuid_len(uuid);
caseBT_UUID128:
bt_uuid_to_uuid128(uuid, &uuid128);
bswap_128(&uuid128.value.u128, dst);
returnbt_uuid_len(&uuid128);
}return 0;
We do however have
bt_uuid_to_lewith the only difference being that it is more generic and it doesn't return the actual bytes written to the dst, anyway we could replace the code above with:src/shared/gatt-db.c +1
-1@@ -553,16 +553,9 @@ bool gatt_db_isempty(struct gatt_db *db) static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst) { - bt_uuid_t uuid128; + bt_uuid_to_le(uuid, dst);
src/shared/gatt-db.c bool gatt_db_isempty(struct gatt_db *db) · line 558
Quoted 10 lines
src/shared/gatt-db.c +1
-8- if (uuid->type == BT_UUID16) { - put_le16(uuid->value.u16, dst); - return bt_uuid_len(uuid); - } - - bt_uuid_to_uuid128(uuid, &uuid128); - bswap_128(&uuid128.value.u128, dst); - return bt_uuid_len(&uuid128); + return bt_uuid_len(uuid) == 4 ? 16 : bt_uuid_len(uuid);
Or this with error checking?
static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
{
if (bt_uuid_to_le(uuid, dst) < 0)
return 0;
return bt_uuid_len(uuid) == 4 ? 16 : bt_uuid_len(uuid);
}Quoted 20 lines
src/shared/gatt-db.c +0
-0}Quoted 15 lines
src/shared/gatt-db.c +7
-0+ uuid->type == BT_UUID128) { + bt_uuid_to_uuid128(uuid, &uuid128); + bswap_128(&uuid128.value.u128, dst); + return bt_uuid_len(&uuid128); + } + + return 0; }static bool
le_to_uuid(const uint8_t *src, size_t len, bt_uuid_t *uuid)
--
2.45.2
Luiz Augusto von Dentz
maintainerRe: [BlueZ 6/8] shared/gatt-db: Fix possible buffer overrun
Hi Bastien,
Quoted 91 lines · Bastien Nocera
Quoted 76 lines · Luiz Augusto von Dentz
Hi Bastien,
Quoted 35 lines · Bastien Nocera
uuid_to_le()returns one of the possible values frombt_uuid_len().bt_uuid_len()returns "type / 8".
type is a value between 0 and 128, but could be something else depending on the validity of the UUID that's parsed. So an invalid value of type between 128 and 256 would trigger an overrun.Add a check to make sure that an invalid type isn't used to calculate
the length.Error: OVERRUN (CWE-119): [#def6] [important]
bluez-5.77/src/shared/gatt-db.c:612:2: assignment: Assigning: "len" = "uuid_to_le(uuid, value)". The value of "len" is now between 0 and 31 (inclusive).bluez-5.77/src/shared/gatt-db.c:614:2: overrun-buffer-arg:
Overrunning array "value" of 16 bytes by passing it to a function which accesses it at byte offset 30 using argument "len" (which evaluates to 31).612| len = uuid_to_le(uuid, value); 613| 614|-> service->attributes[0] = new_attribute(service, handle, type, value, 615| len); 616| if (!service->attributes[0]) { --- src/shared/gatt-db.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)src/shared/gatt-db.c +0
-0src/shared/gatt-db.c static int uuid_to_le(const bt_uuid_t *uuid, · line 564
Quoted 8 lines
src/shared/gatt-db.c +1
-3@@ -560,9 +560,14 @@ static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)return bt_uuid_len (uuid); } -bt_uuid_to_uuid128 (uuid, &uuid128); -bswap_128 (&uuid128.value.u128, dst); -return bt_uuid_len (&uuid128); + if (uuid->type == BT_UUID32 ||
BT_UUID32is not really valid in LE, so Id leave this check to be:switch (
uuid->type) {
caseBT_UUID16:put_le16(uuid->value.u16, dst); return bt_uuid_len(uuid); case BT_UUID128: bt_uuid_to_uuid128(uuid, &uuid128); bswap_128(&uuid128.value.u128, dst); return bt_uuid_len(&uuid128); }return 0;
We do however have
bt_uuid_to_lewith the only difference being that it is more generic and it doesn't return the actual bytes written to the dst, anyway we could replace the code above with:src/shared/gatt-db.c +1
-1@@ -553,16 +553,9 @@ bool gatt_db_isempty(struct gatt_db *db)static int uuid_to_le (const bt_uuid_t *uuid,uint8_t *dst) { -bt_uuid_t uuid128; +bt_uuid_to_le (uuid, dst);src/shared/gatt-db.c bool gatt_db_isempty(struct gatt_db *db) · line 558
Quoted 10 lines
src/shared/gatt-db.c +1
-8-if (uuid->type ==BT_UUID16 ) { -put_le16 (uuid->value.u16, dst); -return bt_uuid_len (uuid); - } - -bt_uuid_to_uuid128 (uuid, &uuid128); -bswap_128 (&uuid128.value.u128, dst); -return bt_uuid_len (&uuid128); +return bt_uuid_len (uuid) ==4 ?16 :bt_uuid_len (uuid);Or this with error checking?
Quoted 6 lines
static int
uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst){ if (bt_uuid_to_le(uuid, dst) < 0) return 0; return bt_uuid_len(uuid) == 4 ? 16 : bt_uuid_len(uuid); }
Yep, we should check the return of bt_uuid_to_le.
Quoted 21 lines
Quoted 20 lines
src/shared/gatt-db.c +0
-0}Quoted 15 lines
src/shared/gatt-db.c +7
-0+ uuid->type == BT_UUID128) { +bt_uuid_to_uuid128 (uuid, &uuid128); +bswap_128 (&uuid128.value.u128, dst); +return bt_uuid_len (&uuid128); + } + +return 0 ; }static bool le_to_uuid(const
uint8_t*src,size_tlen,bt_uuid_t
*uuid)
--
2.45.2
Signature · 1 line
Luiz Augusto von Dentz
PATCH 7/8 shared/btsnoop: Avoid underflowing toread variable
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def8] [important]bluez-5.77/src/shared/btsnoop.c:556:3: underflow: The decrement operator on the unsigned variable "toread" might result in an underflow.bluez-5.77/src/shared/btsnoop.c:572:2: overflow_sink: "toread", which might have underflowed, is passed to "read(btsnoop->fd, data, toread)". [Note: The source code implementation of the function has been overridden by a builtin model.]
570| }
571|
572|-> len = read(btsnoop->fd, data, toread);
573| if (len < 0) {
574| btsnoop->aborted = true;
---
src/shared/btsnoop.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)src/shared/btsnoop.c +6 -1
@@ -530,7 +530,7 @@ bool btsnoop_read_hci(struct btsnoop *btsnoop, struct timeval *tv, } toread =be32toh (pkt.len); -if (toread >BTSNOOP_MAX_PACKET_SIZE ) { +if (toread >BTSNOOP_MAX_PACKET_SIZE || toread <1 ) { btsnoop->aborted = true;return false; }
@@ -569,6 +569,11 @@ bool btsnoop_read_hci(struct btsnoop *btsnoop, struct timeval *tv,return false; } +if (toread ==0 ) { + btsnoop->aborted = true; +return false; + } + len =read (btsnoop->fd, data, toread);if (len <0 ) { btsnoop->aborted = true;
Signature · 1 line
2.45.2
PATCH 8/8 monitor: Check for possible integer underflow
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def4] [important]bluez-5.77/monitor/control.c:1094:2: tainted_data_return: Called function "recv(data->fd, data->buf + data->offset, 1490UL - data->offset, MSG_DONTWAIT)", and a possible return value may be less than zero. bluez-5.77/monitor/control.c:1094:2: assign: Assigning: "len" = "recv(data->fd, data->buf + data->offset, 1490UL - data->offset, MSG_DONTWAIT)".bluez-5.77/monitor/control.c:1099:2: overflow: The expression "data->offset" is considered to have possibly overflowed.bluez-5.77/monitor/control.c:1115:3: overflow: The expression "data->offset -= pktlen + 6" is deemed overflowed because at least one of its arguments has overflowed.bluez-5.77/monitor/control.c:1118:4: overflow_sink: "data->offset", which might have underflowed, is passed to "memmove(data->buf, data->buf + 6 + pktlen, data->offset)". [Note: The source code implementation of the function has been overridden by a builtin model.]
1116| 1117| if (data->offset > 0) 1118|-> memmove(data->buf, data->buf + MGMT_HDR_SIZE + pktlen, 1119| data->offset); 1120| } --- monitor/control.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
monitor/control.c +7 -1
@@ -18,6 +18,7 @@#include <stdbool.h> #include <stddef.h> #include <errno.h> +#include <limits.h> #include <unistd.h> #include <stdlib.h> #include <string.h>
@@ -1091,9 +1092,14 @@ static void client_callback(int fd, uint32_t events, void *user_data)return ; } +if (sizeof (data->buf) <= data->offset) +return ; + len =recv (data->fd, data->buf + data->offset,sizeof (data->buf) - data->offset,MSG_DONTWAIT ); -if (len <0 ) +if (len <0 || + len >UINT16_MAX || +UINT16_MAX - data->offset > len)return ; data->offset += len;
Signature · 1 line
2.45.2
Luiz Augusto von Dentz
maintainerRe: [BlueZ 8/8] monitor: Check for possible integer underflow
Hi Bastien,
Quoted 32 lines · Bastien Nocera
Error:
INTEGER_OVERFLOW(CWE-190): [#def4] [important]bluez-5.77/monitor/control.c:1094:2:tainted_data_return: Called function "recv(data->fd, data->buf + data->offset, 1490UL - data->offset, MSG_DONTWAIT)", and a possible return value may be less than zero.bluez-5.77/monitor/control.c:1094:2: assign: Assigning: "len" = "recv(data->fd, data->buf + data->offset, 1490UL - data->offset, MSG_DONTWAIT)".bluez-5.77/monitor/control.c:1099:2: overflow: The expression "data->offset" is considered to have possibly overflowed.bluez-5.77/monitor/control.c:1115:3: overflow: The expression "data->offset-= pktlen + 6" is deemed overflowed because at least one of its arguments has overflowed.bluez-5.77/monitor/control.c:1118:4:overflow_sink: "data->offset", which might have underflowed, is passed to "memmove(data->buf, data->buf + 6 + pktlen, data->offset)". [Note: The source code implementation of the function has been overridden by a builtin model.]1116| 1117| if (data->offset > 0) 1118|-> memmove(data->buf, data->buf + MGMT_HDR_SIZE + pktlen, 1119| data->offset); 1120| } --- monitor/control.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)monitor/control.c +1
-0@@ -18,6 +18,7 @@#include <stdbool.h> #include <stddef.h> #include <errno.h> +#include <limits.h> #include <unistd.h> #include <stdlib.h> #include <string.h> @@ -1091,9 +1092,14 @@ static void client_callback(int fd, uint32_t events, void *user_data)return ; }
monitor/control.c static void client_callback(int fd, uint32_t events, void *user_data) · line 1103
Quoted 10 lines
monitor/control.c +6
-1+if (sizeof (data->buf) <= data->offset) +return ; + len =recv (data->fd, data->buf + data->offset,sizeof (data->buf) - data->offset,MSG_DONTWAIT ); -if (len <0 ) +if (len <0 || + len >UINT16_MAX || +UINT16_MAX - data->offset > len)return ;
Not really sure why we would be using UINT16_MAX here instead of BTSNOOP_MAX_PACKET_SIZE or sizeof(data->buf), and perhaps we should really check that len > sizeof(data->buf) - data->offset since that implicitly guarantees data->offset + len <= sizeof(data->buf), anyway this is again suggesting that a syscall can return a bigger value than it was asked for.
Quoted 5 lines
monitor/control.c +0
-1data->offset += len; --2.45.2
Signature · 1 line
Luiz Augusto von Dentz