Fix a number of static analysis issues #5
Subsystems (2)
v2 cover Fix a number of static analysis issues #5
Fixed review comments in:
gatt-server: Don't allocate negative data shared/shell: Free memory allocated by wordexp() tools/mesh: Fix integer overflow due to cast operation
Bastien Nocera (11):
gatt-server: Don't allocate negative data shared/shell: Free memory allocated by wordexp() shared/shell: Fix fd leak if -s is passed multiple times
btsnoop: Fix possible negative memcpy length
sdp: Fix possible null dereference
sdp: Fix mismatched int casting
emulator: Fix integer truncation warnings
gatt-server: Fix integer overflow due to cast operation
mesh: Fix integer overflow due to cast operation tools/mesh: Fix integer overflow due to cast operation unit/ringbuf: Fix ineffective guard due to signedness
emulator/amp.c | 3 ++- emulator/bthost.c | 8 +++++--- lib/sdp.c | 6 +++++- mesh/pb-adv.c | 2 +- src/shared/gatt-server.c | 8 ++++---- src/shared/ringbuf.c | 2 +- src/shared/shell.c | 29 ++++++++++++++++++++--------- tools/btsnoop.c | 4 ++-- tools/mesh/mesh-db.c | 9 ++++----- 9 files changed, 44 insertions(+), 27 deletions(-)
Signature · 1 line
2.45.2
PATCH v2 1/11 gatt-server: Don't allocate negative data
Set a lower-bound to the data MTU to avoid allocating -1 elements if bt_att_get_mtu() returns zero.
Error: OVERRUN (CWE-119): [#def36] [important]bluez-5.76/src/shared/gatt-server.c:1121:2: zero_return: Function call "bt_att_get_mtu(server->att)" returns 0.bluez-5.76/src/shared/gatt-server.c:1121:2: assignment: Assigning: "data->mtu" = "bt_att_get_mtu(server->att)". The value of "data->mtu" is now 0.bluez-5.76/src/shared/gatt-server.c:1123:19: assignment: Assigning: "__n" = "(size_t)(data->mtu - 1UL)". The value of "__n" is now 18446744073709551615.bluez-5.76/src/shared/gatt-server.c:1123:19: assignment: Assigning: "__s" = "1UL".bluez-5.76/src/shared/gatt-server.c:1123:19: overrun-buffer-arg: Calling "memset" with "__p" and "__n * __s" is suspicious because of the very large index, 18446744073709551615. The index may be due to a negative parameter being interpreted as unsigned. [Note: The source code implementation of the function has been overridden by a builtin model.]
1121| data->mtu = bt_att_get_mtu(server->att); 1122| data->length = 0; 1123|-> data->rsp_data = new0(uint8_t, data->mtu - 1); 1124| 1125| return data; --- src/shared/gatt-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/shared/gatt-server.c +1 -1
@@ -1120,7 +1120,7 @@ static struct read_mult_data *read_mult_data_new(struct bt_gatt_server *server, data->cur_handle =0 ; data->mtu =bt_att_get_mtu (server->att); data->length =0 ; - data->rsp_data =new0 (uint8_t, data->mtu -1 ); + data->rsp_data =new0 (uint8_t,MAX (data->mtu,1 ) -1 );return data; }
Signature · 1 line
2.45.2
Luiz Augusto von Dentz
maintainerRe: [BlueZ v2 01/11] gatt-server: Don't allocate negative data
Hi Bastien,
Quoted 7 lines · Bastien Nocera
Set a lower-bound to the data MTU to avoid allocating -1 elements if
bt_att_get_mtu()returns zero.Error: OVERRUN (CWE-119): [#def36] [important]
bluez-5.76/src/shared/gatt-server.c:1121:2:zero_return: Function call "bt_att_get_mtu(server->att)" returns 0.bluez-5.76/src/shared/gatt-server.c:1121:2: assignment: Assigning: "data->mtu" = "bt_att_get_mtu(server->att)". The value of "data->mtu" is now 0.
We are disconnected or have an invalid bt_att instance if bt_att_get_mtu returns 0 so it is probably pointless to attempt to send any response if that is the case.
Quoted 29 lines
bluez-5.76/src/shared/gatt-server.c:1123:19: assignment: Assigning: "__n" = "(size_t)(data->mtu - 1UL)". The value of "__n" is now 18446744073709551615.bluez-5.76/src/shared/gatt-server.c:1123:19: assignment: Assigning: "__s" = "1UL".bluez-5.76/src/shared/gatt-server.c:1123:19: overrun-buffer-arg: Calling "memset" with "__p" and "__n*__s" is suspicious because of the very large index, 18446744073709551615. The index may be due to a negative parameter being interpreted as unsigned. [Note: The source code implementation of the function has been overridden by a builtin model.]1121| data->mtu = bt_att_get_mtu(server->att); 1122| data->length = 0; 1123|-> data->rsp_data = new0(uint8_t, data->mtu - 1); 1124| 1125| return data; --- src/shared/gatt-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/shared/gatt-server.c +1
-2@@ -1120,7 +1120,7 @@ static struct read_mult_data *read_mult_data_new(struct bt_gatt_server *server, data->cur_handle =0 ; data->mtu =bt_att_get_mtu (server->att); data->length =0 ; - data->rsp_data =new0 (uint8_t, data->mtu -1 ); + data->rsp_data =new0 (uint8_t,MAX (data->mtu,1 ) -1 );return data; } --2.45.2
Signature · 1 line
Luiz Augusto von Dentz
Re: [BlueZ v2 01/11] gatt-server: Don't allocate negative data
Quoted 14 lines · Luiz Augusto von Dentz
Hi Bastien,
Quoted 10 lines · Bastien Nocera
Set a lower-bound to the data MTU to avoid allocating -1 elements if
bt_att_get_mtu()returns zero.Error: OVERRUN (CWE-119): [#def36] [important]
bluez-5.76/src/shared/gatt-server.c:1121:2:zero_return: Function call "bt_att_get_mtu(server->att)" returns 0.bluez-5.76/src/shared/gatt-server.c:1121:2: assignment: Assigning:
"data->mtu" = "bt_att_get_mtu(server->att)". The value of "data-
Quoted 5 lines
Quoted 1 line
Quoted 1 line
mtu" is now 0.
We are disconnected or have an invalid
bt_attinstance ifbt_att_get_mtureturns 0 so it is probably pointless to attempt to send any response if that is the case.
Same as for patch 8 in this series:
gatt-server: Fix integer overflow due to cast operation
Is "1" a valid value here?
Quoted 41 lines
Quoted 39 lines
bluez-5.76/src/shared/gatt-server.c:1123:19: assignment: Assigning:
"__n" = "(size_t)(data->mtu - 1UL)". The value of "__n" is now 18446744073709551615.bluez-5.76/src/shared/gatt-server.c:1123:19: assignment: Assigning:
"__s" = "1UL".bluez-5.76/src/shared/gatt-server.c:1123:19: overrun-buffer-arg:
Calling "memset" with "__p" and "__n*__s" is suspicious because of the very large index, 18446744073709551615. The index may be due to a negative parameter being interpreted as unsigned. [Note: The source code implementation of the function has been overridden by a builtin model.]1121| data->mtu = bt_att_get_mtu(server->att); 1122| data->length = 0; 1123|-> data->rsp_data = new0(uint8_t, data->mtu - 1); 1124| 1125| return data; --- src/shared/gatt-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/shared/gatt-server.c +1
-1@@ -1120,7 +1120,7 @@ static struct read_mult_data *read_mult_data_new(struct bt_gatt_server *server, data->cur_handle = 0; data->mtu = bt_att_get_mtu(server->att); data->length = 0; - data->rsp_data = new0(uint8_t, data->mtu - 1); + data->rsp_data = new0(uint8_t, MAX(data->mtu, 1) - 1); return data;}
--
2.45.2
Re: [BlueZ v2 01/11] gatt-server: Don't allocate negative data
Quoted 18 lines · Bastien Nocera
Quoted 17 lines · Luiz Augusto von Dentz
Hi Bastien,
Quoted 12 lines · Bastien Nocera
Set a lower-bound to the data MTU to avoid allocating -1 elements if
bt_att_get_mtu()returns zero.Error: OVERRUN (CWE-119): [#def36] [important]
bluez-5.76/src/shared/gatt-server.c:1121:2:zero_return: Function call "bt_att_get_mtu(server->att)" returns 0.bluez-5.76/src/shared/gatt-server.c:1121:2: assignment:
Assigning:
"data->mtu" = "bt_att_get_mtu(server->att)". The value of "data-Quoted 1 line
mtu" is now 0.
Quoted 8 lines
Quoted 3 lines
We are disconnected or have an invalid
bt_attinstance ifbt_att_get_mtureturns 0 so it is probably pointless to attempt to send any response if that is the case.Same as for patch 8 in this series:
gatt-server: Fix integer overflow due to cast operationIs "1" a valid value here?
Never mind, Coverity is happy, so I'm happy too.
Quoted 47 lines
Quoted 46 lines
Quoted 44 lines
bluez-5.76/src/shared/gatt-server.c:1123:19: assignment:
Assigning:
"__n" = "(size_t)(data->mtu - 1UL)". The value of "__n" is now 18446744073709551615.bluez-5.76/src/shared/gatt-server.c:1123:19: assignment:
Assigning:
"__s" = "1UL".bluez-5.76/src/shared/gatt-server.c:1123:19: overrun-buffer-arg:
Calling "memset" with "__p" and "__n*__s" is suspicious because of the very large index, 18446744073709551615. The index may be due
to a negative parameter being interpreted as unsigned. [Note: The source code implementation of the function has been overridden by a
builtin model.]1121| data->mtu = bt_att_get_mtu(server->att); 1122| data->length = 0; 1123|-> data->rsp_data = new0(uint8_t, data->mtu - 1); 1124| 1125| return data; --- src/shared/gatt-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/shared/gatt-server.c +1
-1@@ -1120,7 +1120,7 @@ static struct read_mult_data *read_mult_data_new(struct bt_gatt_server *server, data->cur_handle = 0; data->mtu = bt_att_get_mtu(server->att); data->length = 0; - data->rsp_data = new0(uint8_t, data->mtu - 1); + data->rsp_data = new0(uint8_t, MAX(data->mtu, 1) - 1); return data;}
--
2.45.2
PATCH v2 2/11 shared/shell: Free memory allocated by wordexp()
In reply to Bastien NoceraError: RESOURCE_LEAK (CWE-772): [#def38] [important]bluez-5.76/src/shared/shell.c:519:2: alloc_arg: "parse_args" allocates memory that is stored into "w.we_wordv". bluez-5.76/src/shared/shell.c:523:3: leaked_storage: Variable "w" going out of scope leaks the storage "w.we_wordv" points to.
521| "Unable to parse mandatory command arguments: %s", man ); 522| free(man); 523|-> return -EINVAL; 524| } 525|
Error: RESOURCE_LEAK (CWE-772): [#def40] [important]bluez-5.76/src/shared/shell.c:1113:3: alloc_arg: "wordexp" allocates memory that is stored into "w.we_wordv". bluez-5.76/src/shared/shell.c:1114:4: leaked_storage: Variable "w" going out of scope leaks the storage "w.we_wordv" points to.
1112| 1113| if (wordexp(rl_line_buffer, &w, WRDE_NOCMD)) 1114|-> return NULL; 1115| 1116| matches = menu_completion(default_menu, text, w.we_wordc,
Error: RESOURCE_LEAK (CWE-772): [#def42] [important]bluez-5.76/src/shared/shell.c:1412:2: alloc_arg: "wordexp" allocates memory that is stored into "w.we_wordv". bluez-5.76/src/shared/shell.c:1415:3: leaked_storage: Variable "w" going out of scope leaks the storage "w.we_wordv" points to.
1413| switch (err) {
1414| case WRDE_BADCHAR:
1415|-> return -EBADMSG;
1416| case WRDE_BADVAL:
1417| case WRDE_SYNTAX:Error: RESOURCE_LEAK (CWE-772): [#def43] [important]bluez-5.76/src/shared/shell.c:1412:2: alloc_arg: "wordexp" allocates memory that is stored into "w.we_wordv". bluez-5.76/src/shared/shell.c:1418:3: leaked_storage: Variable "w" going out of scope leaks the storage "w.we_wordv" points to.
1416| case WRDE_BADVAL: 1417| case WRDE_SYNTAX: 1418|-> return -EINVAL; 1419| case WRDE_NOSPACE: 1420| return -ENOMEM;
Error: RESOURCE_LEAK (CWE-772): [#def44] [important]bluez-5.76/src/shared/shell.c:1412:2: alloc_arg: "wordexp" allocates memory that is stored into "w.we_wordv". bluez-5.76/src/shared/shell.c:1420:3: leaked_storage: Variable "w" going out of scope leaks the storage "w.we_wordv" points to.
1418| return -EINVAL; 1419| case WRDE_NOSPACE: 1420|-> return -ENOMEM; 1421| case WRDE_CMDSUB: 1422| if (wordexp(input, &w, 0))
Error: RESOURCE_LEAK (CWE-772): [#def45] [important]bluez-5.76/src/shared/shell.c:1422:3: alloc_arg: "wordexp" allocates memory that is stored into "w.we_wordv". bluez-5.76/src/shared/shell.c:1423:4: leaked_storage: Variable "w" going out of scope leaks the storage "w.we_wordv" points to.
1421| case WRDE_CMDSUB: 1422| if (wordexp(input, &w, 0)) 1423|-> return -ENOEXEC; 1424| break; 1425| }; --- src/shared/shell.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
src/shared/shell.c +16 -6
@@ -452,13 +452,23 @@ static void shell_print_menu_zsh_complete(void) } } +static int _wordexp (const char *restrict s,wordexp_t *restrict p,int flags) +{ +int ret; + + ret =wordexp (s, p, flags); +if (ret !=0 ) +wordfree (p); +return ret; +} +static int parse_args (char *arg,wordexp_t *w,char *del,int flags) {char *str; str =strdelimit (arg, del,'"' ); -if (wordexp (str, w, flags)) { +if (_wordexp (str, w, flags) !=0 ) {free (str);return -EINVAL ; }
@@ -537,7 +547,7 @@ static int cmd_exec(const struct bt_shell_menu_entry *entry, goto fail; } - flags |=WRDE_APPEND ; + flags |=WRDE_APPEND |WRDE_REUSE ; opt =strdup (entry->arg + len +1 ); optional:
@@ -1043,7 +1053,7 @@ static char **args_completion(const struct bt_shell_menu_entry *entry, int argc, args.we_offs =0 ;wordfree (&args); -if (wordexp (str, &args,WRDE_NOCMD )) +if (_wordexp (str, &args,WRDE_NOCMD )) goto done; rl_completion_display_matches_hook =NULL ;
@@ -1115,7 +1125,7 @@ static char **shell_completion(const char *text, int start, int end)if (start >0 ) {wordexp_t w; -if (wordexp (rl_line_buffer, &w,WRDE_NOCMD )) +if (_wordexp (rl_line_buffer, &w,WRDE_NOCMD ))return NULL ; matches = menu_completion(default_menu, text, w.we_wordc,
@@ -1416,7 +1426,7 @@ int bt_shell_exec(const char *input)if (data.monitor)bt_log_printf (0xffff , data.name,LOG_INFO ,"%s" , input); - err =wordexp (input, &w,WRDE_NOCMD ); + err =_wordexp (input, &w,WRDE_NOCMD );switch (err) {case WRDE_BADCHAR :return -EBADMSG ;
@@ -1426,7 +1436,7 @@ int bt_shell_exec(const char *input)case WRDE_NOSPACE :return -ENOMEM ;case WRDE_CMDSUB : -if (wordexp (input, &w,0 )) +if (_wordexp (input, &w,0 ))return -ENOEXEC ;break ; };
Signature · 1 line
2.45.2
Re: [BlueZ v2 02/11] shared/shell: Free memory allocated by wordexp()
On Fri, 2024-07-05 at 10:57 +0200, Bastien Nocera wrote:
<snip>
Quoted 27 lines
src/shared/shell.c +0
-0@@ -452,13 +452,23 @@ static void shell_print_menu_zsh_complete(void) } }
+static int_wordexp(const char *restrict s, wordexp_t *restrict p, int flags)src/shared/shell.c +9
-0+{ +int ret; + + ret =wordexp (s, p, flags); +if (ret !=0 ) +wordfree (p); +return ret; +} +static int
parse_args(char *arg, wordexp_t *w, char *del, int flags){
char *str;
str =strdelimit(arg, del, '"');src/shared/shell.c +1
-1-if (wordexp (str, w, flags)) { +if (_wordexp (str, w, flags) !=0 ) {
Quoted 3 lines
free(str);
return-EINVAL;
}
Any reason not to pick this patch up?
You can see here:
https://sourceware.org/git/?p=glibc.git;a=blob;f=posix/wordexp.c;h=a7362ef31b05280001e961c3a630e953110b7397;hb=HEAD#l2203
that wordexp() will return without freeing we_wordv if there's an error and the WRDE_APPEND flag isn't set.
Let me know if you want me to re-spin this one with a different style, or want me to add the info above to the commit message.
Cheers
Quoted 50 lines
src/shared/shell.c +0
-0@@ -537,7 +547,7 @@ static int cmd_exec(const structbt_shell_menu_entry *entry, goto fail; }src/shared/shell.c +1
-1- flags |=WRDE_APPEND ; + flags |=WRDE_APPEND |WRDE_REUSE ;opt =
strdup(entry->arg + len + 1);
optional:src/shared/shell.c +0
-0@@ -1043,7 +1053,7 @@ static char **args_completion(const struct bt_shell_menu_entry *entry, int argc, args.we_offs = 0; wordfree(&args);src/shared/shell.c +1
-1-if (wordexp (str, &args,WRDE_NOCMD )) +if (_wordexp (str, &args,WRDE_NOCMD ))goto done;
rl_completion_display_matches_hook= NULL;src/shared/shell.c +0
-0@@ -1115,7 +1125,7 @@ static char **shell_completion(const char *text, int start, int end) if (start > 0) { wordexp_t w;src/shared/shell.c +1
-1-if (wordexp (rl_line_buffer, &w,WRDE_NOCMD )) +if (_wordexp (rl_line_buffer, &w,WRDE_NOCMD ))return NULL;
matches = menu_completion(default_menu, text, w.we_wordc,src/shared/shell.c +0
-0@@ -1416,7 +1426,7 @@ int bt_shell_exec(const char *input) if (data.monitor) bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input);src/shared/shell.c +1
-1- err =wordexp (input, &w,WRDE_NOCMD ); + err =_wordexp (input, &w,WRDE_NOCMD );switch (err) {
caseWRDE_BADCHAR:
return-EBADMSG;src/shared/shell.c +0
-0@@ -1426,7 +1436,7 @@ int bt_shell_exec(const char *input)case
WRDE_NOSPACE:
return-ENOMEM;
caseWRDE_CMDSUB:src/shared/shell.c +1
-1-if (wordexp (input, &w,0 )) +if (_wordexp (input, &w,0 ))return
-ENOEXEC;
break;
};
PATCH v2 3/11 shared/shell: Fix fd leak if -s is passed multiple times
In reply to Bastien NoceraError: RESOURCE_LEAK (CWE-772): [#def37] [important]bluez-5.76/src/shared/shell.c:1305:5: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.] bluez-5.76/src/shared/shell.c:1305:5: var_assign: Assigning: "data.init_fd" = handle returned from "open(optarg, 0)". bluez-5.76/src/shared/shell.c:1305:5: overwrite_var: Overwriting handle "data.init_fd" in "data.init_fd = open(optarg, 0)" leaks the handle.
1303| case 's':
1304| if (optarg)
1305|-> data.init_fd = open(optarg, O_RDONLY);
1306| if (data.init_fd < 0)
1307| printf("Unable to open %s: %s (%d)\n", optarg,
---
src/shared/shell.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)src/shared/shell.c +4 -3
@@ -1312,11 +1312,12 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt) data.mode =1 ; goto done;case 's' : -if (optarg) +if (optarg && data.init_fd <0 ) { data.init_fd =open (optarg,O_RDONLY ); -if (data.init_fd <0 ) -printf ("Unable to open %s: %s (%d)\n" , optarg, +if (data.init_fd <0 ) +printf ("Unable to open %s: %s (%d)\n" , optarg,strerror (errno), errno); + }break ;case 't' :if (optarg)
Signature · 1 line
2.45.2
PATCH v2 4/11 btsnoop: Fix possible negative memcpy length
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def41] [important]bluez-5.76/tools/btsnoop.c:438:2: tainted_data_return: Called function "read(fd, buf, toread)", and a possible return value may be less than zero. bluez-5.76/tools/btsnoop.c:438:2: assign: Assigning: "len" = "read(fd, buf, toread)".bluez-5.76/tools/btsnoop.c:473:4: overflow: The cast of "len - 9L", which is potentially negative, to an unsigned type could result in an overflow.
471| /* next 4 bytes are data len and cid */
472| current_cid = buf[8] << 8 | buf[7];
473|-> memcpy(pdu_buf, buf + 9, len - 9);
474| pdu_len = len - 9;
475| } else if (acl_flags & 0x01) {Error: INTEGER_OVERFLOW (CWE-190): [#def42] [important]bluez-5.76/tools/btsnoop.c:438:2: tainted_data_return: Called function "read(fd, buf, toread)", and a possible return value may be less than zero. bluez-5.76/tools/btsnoop.c:438:2: assign: Assigning: "len" = "read(fd, buf, toread)".bluez-5.76/tools/btsnoop.c:476:4: overflow: The cast of "len - 5L", which is potentially negative, to an unsigned type could result in an overflow.
474| pdu_len = len - 9;
475| } else if (acl_flags & 0x01) {
476|-> memcpy(pdu_buf + pdu_len, buf + 5, len - 5);
477| pdu_len += len - 5;
478| }
---
tools/btsnoop.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)tools/btsnoop.c +2 -2
@@ -448,7 +448,7 @@ next_packet: acl_flags = buf[2 ] >>4 ;/* use only packet with ACL start flag */ -if (acl_flags &0x02 ) { +if ((acl_flags &0x02 ) && len >9 ) {if (current_cid ==0x0040 && pdu_len >0 ) {int i;if (!pdu_first)
@@ -472,7 +472,7 @@ next_packet: current_cid = buf[8 ] <<8 | buf[7 ];memcpy (pdu_buf, buf +9 , len -9 ); pdu_len = len -9 ; - }else if (acl_flags &0x01 ) { + }else if ((acl_flags &0x01 ) && len >5 ) {memcpy (pdu_buf + pdu_len, buf +5 , len -5 ); pdu_len += len -5 ; }
Signature · 1 line
2.45.2
PATCH v2 5/11 sdp: Fix possible null dereference
In reply to Bastien NoceraFrom the BlueZ scanbot:lib/sdp.c:586:12: warning: Access to field 'attrId' results in a dereference of a null pointer (loaded from variable 'd')
d->attrId = attr;
~ ^
lib/sdp.c:967:10: warning: Access to field 'dtd' results in a dereference of a null pointer (loaded from variable 'd')
switch (d->dtd) {
^~~~~~
---
lib/sdp.c | 4 ++++
1 file changed, 4 insertions(+)lib/sdp.c +4 -0
@@ -582,6 +582,8 @@ int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)if (p)return -1 ; +if (!d) +return -1 ; d->attrId = attr; rec->attrlist =sdp_list_insert_sorted (rec->attrlist, d, sdp_attrid_comp_func);
@@ -964,6 +966,8 @@ static void data_seq_free(sdp_data_t *seq)void sdp_data_free (sdp_data_t *d) { +if (!d) +return ;switch (d->dtd) {case SDP_SEQ8 :case SDP_SEQ16 :
Signature · 1 line
2.45.2
PATCH v2 6/11 sdp: Fix mismatched int casting
In reply to Bastien Nocera--- lib/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sdp.c +1 -1
@@ -506,7 +506,7 @@ sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length,for (i =0 ; i < len; i++) {sdp_data_t *data; -int8_t dtd = *(uint8_t *) dtds[i]; +uint8_t dtd = *(uint8_t *) dtds[i];if (dtd >=SDP_SEQ8 && dtd <=SDP_ALT32 ) data = (sdp_data_t *) values[i];
Signature · 1 line
2.45.2
PATCH v2 7/11 emulator: Fix integer truncation warnings
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def1] [important]bluez-5.76/emulator/amp.c:693:2: cast_overflow: Truncation due to cast operation on "(remain_assoc_len > 248) ? 248 : remain_assoc_len" from 32 to 16 bits.bluez-5.76/emulator/amp.c:693:2: overflow_assign: "fragment_len" is assigned from "(remain_assoc_len > 248) ? 248 : remain_assoc_len".bluez-5.76/emulator/amp.c:698:2: overflow_sink: "fragment_len", which might have overflowed, is passed to "memcpy(rsp.assoc_fragment, amp->local_assoc + len_so_far, fragment_len)". [Note: The source code implementation of the function has been overridden by a builtin model.]
696| rsp.phy_handle = cmd->phy_handle; 697| rsp.remain_assoc_len = cpu_to_le16(remain_assoc_len); 698|-> memcpy(rsp.assoc_fragment, amp->local_assoc + len_so_far, 699| fragment_len); 700|
Error: INTEGER_OVERFLOW (CWE-190): [#def2] [important]bluez-5.76/emulator/amp.c:701:2: cast_overflow: Truncation due to cast operation on "4 + fragment_len" from 32 to 8 bits. bluez-5.76/emulator/amp.c:701:2: overflow_sink: "4 + fragment_len", which might have overflowed, is passed to "cmd_complete(amp, 5130, &rsp, 4 + fragment_len)".
699| fragment_len); 700| 701|-> cmd_complete(amp, BT_HCI_CMD_READ_LOCAL_AMP_ASSOC, 702| &rsp, 4 + fragment_len); 703| }
Error: INTEGER_OVERFLOW (CWE-190): [#def4] [important]bluez-5.76/emulator/bthost.c:3309:3: cast_overflow: Truncation due to cast operation on "len - offset" from 32 to 8 bits.bluez-5.76/emulator/bthost.c:3309:3: overflow_assign: "cp->data_len" is assigned from "len - offset".bluez-5.76/emulator/bthost.c:3317:2: overflow_sink: "cp->data_len", which might have overflowed, is passed to "memcpy(cp->data, data + offset, cp->data_len)". [Note: The source code implementation of the function has been overridden by a builtin model.]
3315| } 3316| 3317|-> memcpy(cp->data, data + offset, cp->data_len); 3318| 3319| send_command(bthost, BT_HCI_CMD_LE_SET_PA_DATA, buf, --- emulator/amp.c | 3 ++- emulator/bthost.c | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-)
emulator/amp.c +2 -1
@@ -680,7 +680,8 @@ static void cmd_read_local_amp_assoc(struct bt_amp *amp, {const struct bt_hci_cmd_read_local_amp_assoc *cmd = data;struct bt_hci_rsp_read_local_amp_assoc rsp; -uint16_t len_so_far, remain_assoc_len, fragment_len; +uint16_t len_so_far, remain_assoc_len; +size_t fragment_len;if (cmd->phy_handle != amp->phy_handle) { cmd_status(amp, BT_HCI_ERR_INVALID_PARAMETERS,
emulator/bthost.c +5 -3
@@ -3290,6 +3290,7 @@ static void set_pa_data(struct bthost *bthost, const uint8_t *data, {struct bt_hci_cmd_le_set_pa_data *cp;uint8_t buf[sizeof (*cp) +BT_PA_MAX_DATA_LEN ]; +size_t data_len; cp = (void *)buf;
@@ -3299,14 +3300,14 @@ static void set_pa_data(struct bthost *bthost, const uint8_t *data, cp->handle =1 ;if (len - offset >BT_PA_MAX_DATA_LEN ) { - cp->data_len =BT_PA_MAX_DATA_LEN ; + data_len =BT_PA_MAX_DATA_LEN ;if (!offset) cp->operation =0x01 ;else cp->operation =0x00 ; }else { - cp->data_len = len - offset; + data_len = len - offset;if (!offset) cp->operation =0x03 ;
@@ -3314,7 +3315,8 @@ static void set_pa_data(struct bthost *bthost, const uint8_t *data, cp->operation =0x02 ; } -memcpy (cp->data, data + offset, cp->data_len); +memcpy (cp->data, data + offset, data_len); + cp->data_len = data_len;send_command (bthost,BT_HCI_CMD_LE_SET_PA_DATA , buf,sizeof (*cp) + cp->data_len);
Signature · 1 line
2.45.2
PATCH v2 8/11 gatt-server: Fix integer overflow due to cast operation
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def25] [important]bluez-5.76/src/shared/gatt-server.c:927:2: cast_overflow: Truncation due to cast operation on "((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len" from 32 to 16 bits.bluez-5.76/src/shared/gatt-server.c:927:2: overflow_sink: "((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len", which might have overflowed, is passed to "bt_att_chan_send(op->chan, rsp_opcode, (len ? value : NULL), (((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len), NULL, NULL, NULL)".
925| rsp_opcode = get_read_rsp_opcode(op->opcode); 926| 927|-> bt_att_chan_send_rsp(op->chan, rsp_opcode, len ? value : NULL, 928| MIN((unsigned int) mtu - 1, len)); 929| async_read_op_destroy(op); --- src/shared/gatt-server.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
src/shared/gatt-server.c +3 -3
@@ -908,7 +908,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err,struct async_read_op *op = user_data;struct bt_gatt_server *server = op->server;uint8_t rsp_opcode; -uint16_t mtu; +size_t mtu;uint16_t handle;DBG (server,"Read Complete: err %d" , err);
@@ -916,7 +916,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, mtu =bt_att_get_mtu (server->att); handle =gatt_db_attribute_get_handle (attr); -if (err) { +if (err || mtu <=1 ) {bt_att_chan_send_error_rsp (op->chan, op->opcode, handle, err);async_read_op_destroy (op);return ;
@@ -925,7 +925,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, rsp_opcode =get_read_rsp_opcode (op->opcode);bt_att_chan_send_rsp (op->chan, rsp_opcode, len ? value :NULL , -MIN ((unsigned int ) mtu -1 , len)); +MIN (mtu -1 , len));async_read_op_destroy (op); }
Signature · 1 line
2.45.2
Luiz Augusto von Dentz
maintainerRe: [BlueZ v2 08/11] gatt-server: Fix integer overflow due to cast operation
Hi Bastien,
Quoted 27 lines · Bastien Nocera
Error:
INTEGER_OVERFLOW(CWE-190): [#def25] [important]bluez-5.76/src/shared/gatt-server.c:927:2:cast_overflow: Truncation due to cast operation on "((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len" from 32 to 16 bits.bluez-5.76/src/shared/gatt-server.c:927:2:overflow_sink: "((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len", which might have overflowed, is passed to "bt_att_chan_send(op->chan,rsp_opcode, (len ? value : NULL), (((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len), NULL, NULL, NULL)".925| rsp_opcode = get_read_rsp_opcode(op->opcode); 926| 927|-> bt_att_chan_send_rsp(op->chan, rsp_opcode, len ? value : NULL, 928| MIN((unsigned int) mtu - 1, len)); 929| async_read_op_destroy(op); --- src/shared/gatt-server.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)src/shared/gatt-server.c +1
-1@@ -908,7 +908,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err,struct async_read_op *op = user_data;struct bt_gatt_server *server = op->server;uint8_t rsp_opcode; -uint16_t mtu; +size_t mtu;uint16_t handle;DBG (server,"Read Complete: err %d" , err);
src/shared/gatt-server.c static void read_complete_cb(struct gatt_db_attribute *attr, int err, · line 919
Quoted 5 lines
src/shared/gatt-server.c +1
-1@@ -916,7 +916,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, mtu =bt_att_get_mtu (server->att); handle =gatt_db_attribute_get_handle (attr); -if (err) { +if (err || mtu <=1 ) {
Also pointless here if the mtu is 0 then we should not attempt to send anything.
src/shared/gatt-server.c static void read_complete_cb(struct gatt_db_attribute *attr, int err, · line 928
Quoted 9 lines
src/shared/gatt-server.c +1
-1bt_att_chan_send_error_rsp (op->chan, op->opcode, handle, err);async_read_op_destroy (op);return ;@@ -925,7 +925,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, rsp_opcode =get_read_rsp_opcode (op->opcode);bt_att_chan_send_rsp (op->chan, rsp_opcode, len ? value :NULL , -MIN ((unsigned int ) mtu -1 , len)); +MIN (mtu -1 , len));
And this is incorrect as well, we need the mtu of the channel here not bt_att_get_mtu.
Quoted 7 lines
src/shared/gatt-server.c +0
-1async_read_op_destroy (op); } --2.45.2
Signature · 1 line
Luiz Augusto von Dentz
Re: [BlueZ v2 08/11] gatt-server: Fix integer overflow due to cast operation
Quoted 44 lines · Luiz Augusto von Dentz
Hi Bastien,
Quoted 40 lines · Bastien Nocera
Error:
INTEGER_OVERFLOW(CWE-190): [#def25] [important]bluez-5.76/src/shared/gatt-server.c:927:2:cast_overflow:
Truncation due to cast operation on "((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len" from 32 to 16 bits.bluez-5.76/src/shared/gatt-server.c:927:2:overflow_sink:
"((unsigned int)mtu - 1U < len) ? (unsigned int)mtu - 1U : len", which might have overflowed, is passed to "bt_att_chan_send(op-Quoted 1 line
chan,
rsp_opcode, (len ? value : NULL), (((unsigned int)mtu - 1U <len) ? (unsigned int)mtu - 1U : len), NULL, NULL, NULL)".
925| rsp_opcode = get_read_rsp_opcode(op->opcode); 926| 927|-> bt_att_chan_send_rsp(op->chan, rsp_opcode, len ? value :NULL,
928|MIN((unsigned int) mtu - 1, len));
929|async_read_op_destroy(op);
---
src/shared/gatt-server.c | 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)src/shared/gatt-server.c +1
-1@@ -908,7 +908,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, struct async_read_op *op = user_data; struct bt_gatt_server *server = op->server; uint8_t rsp_opcode; - uint16_t mtu; + size_t mtu; uint16_t handle;
DBG(server, "Read Complete: err %d", err);src/shared/gatt-server.c +0
-0@@ -916,7 +916,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, mtu = bt_att_get_mtu(server->att); handle = gatt_db_attribute_get_handle(attr);
Quoted 5 lines
src/shared/gatt-server.c static void read_complete_cb(struct gatt_db_attribute *attr, int err, · line 919
Quoted 2 lines
src/shared/gatt-server.c +1
-1- if (err) { + if (err || mtu <= 1) {Also pointless here if the mtu is 0 then we should not attempt to send anything.
There's more than one fix in this patch. One is the data type used (should be a size_t, not a uint16_t), the other is avoid a negative or zero pdu value. What happens to guard against a "1" mtu? Do we need to guard against it?
Quoted 9 lines
Quoted 9 lines
src/shared/gatt-server.c +0
-0bt_att_chan_send_error_rsp(op->chan, op->opcode, handle, err);
async_read_op_destroy(op);
return;src/shared/gatt-server.c +0
-0@@ -925,7 +925,7 @@ static void read_complete_cb(struct gatt_db_attribute *attr, int err, rsp_opcode = get_read_rsp_opcode(op->opcode);
Quoted 9 lines
Quoted 5 lines
bt_att_chan_send_rsp(op->chan,
rsp_opcode, len ? value :
NULL,
-MIN((unsigned int) mtu - 1, len));
+MIN(mtu - 1, len));And this is incorrect as well, we need the mtu of the channel here not
bt_att_get_mtu.
I see you fixed this in 110a8b47a4f159a8239795255b6c1c0edd79e4cd Thanks!
Quoted 10 lines
Quoted 8 lines
async_read_op_destroy(op);
}--
2.45.2
PATCH v2 9/11 mesh: Fix integer overflow due to cast operation
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def15] [important]bluez-5.76/mesh/pb-adv.c:174:4: cast_overflow: Truncation due to cast operation on "size - consumed" from 32 to 8 bits.bluez-5.76/mesh/pb-adv.c:174:4: overflow_assign: "seg_size" is assigned from "size - consumed".bluez-5.76/mesh/pb-adv.c:177:3: overflow_sink: "seg_size", which might have overflowed, is passed to "memcpy(buf + 7, data + consumed, seg_size)". [Note: The source code implementation of the function has been overridden by a builtin model.]
175| 176| buf[6] = (i << 2) | 0x02; 177|-> memcpy(buf + 7, data + consumed, seg_size); 178| 179| pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500,
Error: INTEGER_OVERFLOW (CWE-190): [#def16] [important]bluez-5.76/mesh/pb-adv.c:179:3: cast_overflow: Truncation due to cast operation on "seg_size + 7" from 32 to 16 bits. bluez-5.76/mesh/pb-adv.c:179:3: overflow_sink: "seg_size + 7", which might have overflowed, is passed to "pb_adv_send(session, 0, 500, buf, seg_size + 7)".
177| memcpy(buf + 7, data + consumed, seg_size); 178| 179|-> pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500, 180| buf, seg_size + 7); --- mesh/pb-adv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mesh/pb-adv.c +1 -1
@@ -166,7 +166,7 @@ static void send_adv_segs(struct pb_adv_session *session, const uint8_t *data, consumed = init_size;for (i =1 ; i <= max_seg; i++) { -uint8_t seg_size;/* Amount of payload data being sent */ +size_t seg_size;/* Amount of payload data being sent */ if (size - consumed >PB_ADV_MTU -1 ) seg_size =PB_ADV_MTU -1 ;
Signature · 1 line
2.45.2
PATCH v2 10/11 tools/mesh: Fix integer overflow due to cast operation
In reply to Bastien NoceraError: INTEGER_OVERFLOW (CWE-190): [#def29] [important]bluez-5.76/tools/mesh/mesh-db.c:551:3: cast_overflow: Truncation due to cast operation on "ele_cnt" from 32 to 8 bits. bluez-5.76/tools/mesh/mesh-db.c:551:3: overflow_sink: "ele_cnt", which might have overflowed, is passed to "remote_add_node((uint8_t const *)uuid, unicast, ele_cnt, key_idx)".
549| continue;
550|
551|-> remote_add_node((const uint8_t *)uuid, unicast, ele_cnt,
552| key_idx);
553| for (j = 1; j < key_cnt; j++) {
---
tools/mesh/mesh-db.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)tools/mesh/mesh-db.c +4 -5
@@ -503,7 +503,8 @@ static void load_remotes(json_object *jcfg)uint8_t uuid[16 ];uint16_t unicast, key_idx;const char *str; -int ele_cnt, key_cnt; +uint8_t ele_cnt; +int key_cnt;int j; jnode =json_object_array_get_idx (jnodes, i);
@@ -528,14 +529,12 @@ static void load_remotes(json_object *jcfg)continue ;json_object_object_get_ex (jnode,"elements" , &jarray); -if (!jarray ||json_object_get_type (jarray) != json_type_array) +if (!jarray ||json_object_get_type (jarray) != json_type_array || +json_object_array_length (jarray) >MAX_ELE_COUNT )continue ; ele_cnt =json_object_array_length (jarray); -if (ele_cnt >MAX_ELE_COUNT ) -continue ; -json_object_object_get_ex (jnode,"netKeys" , &jarray);if (!jarray ||json_object_get_type (jarray) != json_type_array)continue ;
Signature · 1 line
2.45.2
PATCH v2 11/11 unit/ringbuf: Fix ineffective guard due to signedness
In reply to Bastien Nocera"len - end > 0" can never be false because "end" is unsigned, so the whole left handside of the expression is unsigned, so always positive.
Error: INTEGER_OVERFLOW (CWE-190): [#def22] [important]bluez-5.76/src/shared/ringbuf.c:240:2: ineffective_check: The check "len - end > 0UL", which appears to be a guard against integer overflow, is not a useful guard because it is either always true, or never true. This taints "len". bluez-5.76/src/shared/ringbuf.c:242:3: overflow: The expression "len - end" might be negative, but is used in a context that treats it as unsigned.bluez-5.76/src/shared/ringbuf.c:242:3: overflow_sink: "len - end", which might be negative, is passed to "memcpy(ringbuf->buffer, str + end, len - end)". [Note: The source code implementation of the function has been overridden by a builtin model.]
240| if (len - end > 0) {
241| /* Put the remainder of string at the beginning */
242|-> memcpy(ringbuf->buffer, str + end, len - end);
243|
244| if (ringbuf->in_tracing)
---
src/shared/ringbuf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)src/shared/ringbuf.c +1 -1
@@ -237,7 +237,7 @@ int ringbuf_vprintf(struct ringbuf *ringbuf, const char *format, va_list ap) ringbuf->in_tracing (ringbuf->buffer + offset, end, ringbuf->in_data); -if (len - end >0 ) { +if ((size_t ) len > end) {/* Put the remainder of string at the beginning */ memcpy (ringbuf->buffer, str + end, len - end);
Signature · 1 line
2.45.2
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 18 lines · On Fri, 5 Jul 2024 10:57:28 +0200 you wrote:
Fixed review comments in:
gatt-server: Don't allocate negative data shared/shell: Free memory allocated bywordexp()tools/mesh: Fix integer overflow due to cast operationBastien Nocera (11):
gatt-server: Don't allocate negative data shared/shell: Free memory allocated bywordexp()shared/shell: Fix fd leak if -s is passed multiple times
btsnoop: Fix possible negative memcpy length
sdp: Fix possible null dereference
sdp: Fix mismatched int casting
emulator: Fix integer truncation warnings
gatt-server: Fix integer overflow due to cast operation
mesh: Fix integer overflow due to cast operationtools/mesh: Fix integer overflow due to cast operation unit/ringbuf: Fix ineffective guard due to signedness
Quoted 1 line
[...]
Here is the summary with links:
- [BlueZ,v2,01/11] gatt-server: Don't allocate negative data (no matching commit)
- [BlueZ,v2,02/11] shared/shell: Free memory allocated by wordexp() (no matching commit)
- [BlueZ,v2,03/11] shared/shell: Fix fd leak if -s is passed multiple times (no matching commit)
- [BlueZ,v2,04/11] btsnoop: Fix possible negative memcpy length
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=8de21f74c5f3 - [BlueZ,v2,05/11] sdp: Fix possible null dereference https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=0de4b9f71eb9 - [BlueZ,v2,06/11] sdp: Fix mismatched int casting https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=0b52ecca60ea - [BlueZ,v2,07/11] emulator: Fix integer truncation warnings https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=1d73dc6a1a9a - [BlueZ,v2,08/11] gatt-server: Fix integer overflow due to cast operation (no matching commit) - [BlueZ,v2,09/11] mesh: Fix integer overflow due to cast operation https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=249d2120bd90 - [BlueZ,v2,10/11] tools/mesh: Fix integer overflow due to cast operation (no matching commit) - [BlueZ,v2,11/11] unit/ringbuf: Fix ineffective guard due to signedness https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=c44a2a233d1b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html