Memory leaks and memory used after free fixes
cover Memory leaks and memory used after free fixes
This patch fixes memory leaks and usage of unallocated memory.
The endpoint is initialized after allocation, so that it will not contain random pointers.
Rework parse_base and parse_array. Add missing unregister in bap_exit.
Set the lpac in the stream to NULL as it is freed in another place and will result in a dangling pointer inside the stream.
Andrei Istodorescu (2):
bap: Remove memory leaks and buffer usage after free. shared/bap: Set stream lpac to NULL after removing the stream.
profiles/audio/bap.c | 66 ++++++++++++++++++++++++++------------------ src/shared/bap.c | 4 ++- 2 files changed, 42 insertions(+), 28 deletions(-)
base-commit: df658c6c4ab5bd5ec4a8a3f8faa36e0d0a5f906a --
2.39.2
PATCH 1/2 bap: Remove memory leaks and buffer usage after free.
Make sure the endpoint does not contain uninitialized pointers after creation. Rework parse_base and parse_array. Add missing unregister in bap_exit.
--- profiles/audio/bap.c | 66 ++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 27 deletions(-)
profiles/audio/bap.c +39 -27
@@ -278,16 +278,17 @@ static const GDBusPropertyTable ep_properties[] = {static int parse_array (DBusMessageIter *iter,struct iovec **iov) {DBusMessageIter array; +struct iovec tmp_iov = {0 };if (!iov)return 0 ; -if (!(*iov)) - *iov = new0(struct iovec, 1); -dbus_message_iter_recurse (iter, &array); -dbus_message_iter_get_fixed_array (&array, &(*iov)->iov_base, - (int *)&(*iov)->iov_len); +dbus_message_iter_get_fixed_array (&array, &(tmp_iov).iov_base, + (int *)&(tmp_iov).iov_len); + + *iov =util_iov_dup (&tmp_iov,1 ); +return 0 ; }
@@ -300,7 +301,8 @@ static bool parse_base(void *data, size_t len, util_debug_func_t func, .iov_base = data, .iov_len = len, }; - +struct iovec *csc_iov =NULL ; +struct iovec *meta_iov =NULL ;uint8_t capsLen, metaLen;uint8_t *hexstream;
@@ -330,22 +332,21 @@ static bool parse_base(void *data, size_t len, util_debug_func_t func, "Codec", codec->id, codec->cid, codec->vid); } +/* Fetch Codec Specific Configuration */ if (!util_iov_pull_u8 (&iov, &capsLen))return false;util_debug (func,NULL ,"CC Len %d" , capsLen);if (!capsLen)return false; -if (caps) { -if (!(*caps)) - *caps = new0(struct iovec, 1); - (*caps)->iov_len = capsLen; - (*caps)->iov_base = iov.iov_base; - } + csc_iov = new0(struct iovec, 1); +util_iov_memcpy (csc_iov, iov.iov_base, capsLen); + +/* Print Codec Specific Configuration */ for (int i =0 ; capsLen >1 ; i++) {struct bt_ltv *ltv =util_iov_pull_mem (&iov,sizeof (*ltv)); -uint8_t *caps; +uint8_t *csc;if (!ltv) { util_debug(func, NULL, "Unable to parse %s",
@@ -356,35 +357,46 @@ static bool parse_base(void *data, size_t len, util_debug_func_t func,util_debug (func,NULL ,"%s #%u: len %u type %u" ,"CC" , i, ltv->len, ltv->type); - caps =util_iov_pull_mem (&iov, ltv->len -1 ); -if (!caps) { + csc =util_iov_pull_mem (&iov, ltv->len -1 ); +if (!csc) {util_debug (func,NULL ,"Unable to parse %s" ,"CC" );return false; } -util_hexdump (' ' , caps, ltv->len -1 , func,NULL ); +util_hexdump (' ' , csc, ltv->len -1 , func,NULL ); capsLen -= (ltv->len +1 ); } +/* Fetch Metadata */ if (!util_iov_pull_u8 (&iov, &metaLen))return false;util_debug (func,NULL ,"Metadata Len %d" , metaLen);if (!metaLen)return false; -if (meta) { -if (!(*meta)) - *meta = new0(struct iovec, 1); - (*meta)->iov_len = metaLen; - (*meta)->iov_base = iov.iov_base; - } + + meta_iov = new0(struct iovec, 1); +util_iov_memcpy (meta_iov, iov.iov_base, metaLen); hexstream =util_iov_pull_mem (&iov, metaLen);if (!hexstream)return false;util_hexdump (' ' , hexstream, metaLen, func,NULL ); +/* Update caps and meta with received Codec Specific Configuration and Metadata */ +if (caps) { +if (*caps) +util_iov_free (*caps,1 ); + *caps = csc_iov; + } + +if (meta) { +if (*meta) +util_iov_free (*meta,1 ); + *meta = meta_iov; + } +return true; }
@@ -780,7 +792,6 @@ static void iso_bcast_confirm_cb(GIOChannel *io, GError *err, void *user_data)char address[18 ];struct bap_ep *ep;int fd; -struct iovec *base_io;uint32_t presDelay;uint8_t numSubgroups;uint8_t numBis;
@@ -807,12 +818,11 @@ static void iso_bcast_confirm_cb(GIOChannel *io, GError *err, void *user_data)if (!ep)return ; +/* Update endpoint with the values of the peer's (BAP source) QOS */ update_bcast_qos (&qos, &ep->qos); - base_io = new0(struct iovec, 1); -util_iov_memcpy (base_io, base.base, base.base_len); - -parse_base (base_io->iov_base, base_io->iov_len, bap_debug, +/* Update endpoint with the value received from the peer's (BAP source) BASE */ +parse_base (base.base, base.base_len, bap_debug, &presDelay, &numSubgroups, &numBis, &codec, &ep->caps, &ep->metadata);
@@ -917,6 +927,7 @@ static struct bap_ep *ep_register_bcast(struct bap_data *data,return ep; ep = new0(struct bap_ep, 1); +memset (ep,0 ,sizeof (struct bap_ep )); ep->data = data; ep->lpac = lpac; ep->rpac = rpac;
@@ -2288,6 +2299,7 @@ static int bap_init(void)static void bap_exit (void ) {btd_profile_unregister (&bap_profile); +btd_profile_unregister (&bap_bcast_profile);bt_bap_unregister (bap_id); }
--
2.39.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=787894
---Test result---
Test Summary:
CheckPatch FAIL 1.11 seconds GitLint FAIL 0.77 seconds BuildEll PASS 31.36 seconds BluezMake PASS 957.44 seconds MakeCheck PASS 13.20 seconds MakeDistcheck PASS 181.99 seconds CheckValgrind PASS 305.45 seconds CheckSmatch PASS 415.94 seconds bluezmakeextell PASS 126.55 seconds IncrementalBuild PASS 1631.48 seconds ScanBuild PASS 1338.75 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[BlueZ,1/2] bap: Remove memory leaks and buffer usage after free. WARNING:LONG_LINE_COMMENT: line length of 90 exceeds 80 columns #216: FILE: profiles/audio/bap.c:387:
+ /* Update caps and meta with received Codec Specific Configuration and Metadata */
WARNING:LONG_LINE_COMMENT: line length of 87 exceeds 80 columns #251: FILE: profiles/audio/bap.c:824:
+ /* Update endpoint with the value received from the peer's (BAP source) BASE */
/github/workspace/src/src/13399999.patch total: 0 errors, 2 warnings, 150 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/src/13399999.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
[BlueZ,2/2] shared/bap: Set stream lpac to NULL after removing the stream. WARNING:LONG_LINE_COMMENT: line length of 111 exceeds 80 columns #119: FILE: src/shared/bap.c:2685:
+ stream->lpac = NULL; /* mark stream->lpac to NULL, as it will be freed in bt_bap_remove_pac */
/github/workspace/src/src/13400000.patch total: 0 errors, 1 warnings, 11 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/src/13400000.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[BlueZ,1/2] bap: Remove memory leaks and buffer usage after free.
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
1: T3 Title has trailing punctuation (.): "[BlueZ,1/2] bap: Remove memory leaks and buffer usage after free."
[BlueZ,2/2] shared/bap: Set stream lpac to NULL after removing the stream.
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
1: T3 Title has trailing punctuation (.): "[BlueZ,2/2] shared/bap: Set stream lpac to NULL after removing the stream."
---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz
maintainerRe: [PATCH BlueZ 1/2] bap: Remove memory leaks and buffer usage after free.
In reply to Andrei IstodorescuHi Andrei,
Quoted 4 lines · Andrei Istodorescu
Make sure the endpoint does not contain uninitialized pointers after creation. Rework
parse_baseandparse_array. Add missing unregister inbap_exit.
Ive addressed something similar in the following change:
https://patchwork.kernel.org/project/bluetooth/patch/[email protected]/
Quoted 135 lines
--- profiles/audio/bap.c | 66 ++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 27 deletions(-)profiles/audio/bap.c +34
-23@@ -278,16 +278,17 @@ static const GDBusPropertyTable ep_properties[] = {static int parse_array (DBusMessageIter *iter,struct iovec **iov) {DBusMessageIter array; +struct iovec tmp_iov = {0 };if (!iov)return 0 ; -if (!(*iov)) - *iov = new0(struct iovec, 1); -dbus_message_iter_recurse (iter, &array); -dbus_message_iter_get_fixed_array (&array, &(*iov)->iov_base, - (int *)&(*iov)->iov_len); +dbus_message_iter_get_fixed_array (&array, &(tmp_iov).iov_base, + (int *)&(tmp_iov).iov_len); + + *iov =util_iov_dup (&tmp_iov,1 ); +return 0 ; }@@ -300,7 +301,8 @@ static bool parse_base(void *data, size_t len, util_debug_func_t func, .iov_base = data, .iov_len = len, }; - +struct iovec *csc_iov =NULL ; +struct iovec *meta_iov =NULL ;uint8_t capsLen, metaLen;uint8_t *hexstream;@@ -330,22 +332,21 @@ static bool parse_base(void *data, size_t len, util_debug_func_t func, "Codec", codec->id, codec->cid, codec->vid); } +/* Fetch Codec Specific Configuration */ if (!util_iov_pull_u8 (&iov, &capsLen))return false;util_debug (func,NULL ,"CC Len %d" , capsLen);if (!capsLen)return false; -if (caps) { -if (!(*caps)) - *caps = new0(struct iovec, 1); - (*caps)->iov_len = capsLen; - (*caps)->iov_base = iov.iov_base; - } + csc_iov = new0(struct iovec, 1); +util_iov_memcpy (csc_iov, iov.iov_base, capsLen); + +/* Print Codec Specific Configuration */ for (int i =0 ; capsLen >1 ; i++) {struct bt_ltv *ltv =util_iov_pull_mem (&iov,sizeof (*ltv)); -uint8_t *caps; +uint8_t *csc;if (!ltv) { util_debug(func, NULL, "Unable to parse %s",@@ -356,35 +357,46 @@ static bool parse_base(void *data, size_t len, util_debug_func_t func,util_debug (func,NULL ,"%s #%u: len %u type %u" ,"CC" , i, ltv->len, ltv->type); - caps =util_iov_pull_mem (&iov, ltv->len -1 ); -if (!caps) { + csc =util_iov_pull_mem (&iov, ltv->len -1 ); +if (!csc) {util_debug (func,NULL ,"Unable to parse %s" ,"CC" );return false; } -util_hexdump (' ' , caps, ltv->len -1 , func,NULL ); +util_hexdump (' ' , csc, ltv->len -1 , func,NULL ); capsLen -= (ltv->len +1 ); } +/* Fetch Metadata */ if (!util_iov_pull_u8 (&iov, &metaLen))return false;util_debug (func,NULL ,"Metadata Len %d" , metaLen);if (!metaLen)return false; -if (meta) { -if (!(*meta)) - *meta = new0(struct iovec, 1); - (*meta)->iov_len = metaLen; - (*meta)->iov_base = iov.iov_base; - } + + meta_iov = new0(struct iovec, 1); +util_iov_memcpy (meta_iov, iov.iov_base, metaLen); hexstream =util_iov_pull_mem (&iov, metaLen);if (!hexstream)return false;util_hexdump (' ' , hexstream, metaLen, func,NULL ); +/* Update caps and meta with received Codec Specific Configuration and Metadata */ +if (caps) { +if (*caps) +util_iov_free (*caps,1 ); + *caps = csc_iov; + } + +if (meta) { +if (*meta) +util_iov_free (*meta,1 ); + *meta = meta_iov; + } +return true; }@@ -780,7 +792,6 @@ static void iso_bcast_confirm_cb(GIOChannel *io, GError *err, void *user_data)char address[18 ];struct bap_ep *ep;int fd; -struct iovec *base_io;uint32_t presDelay;uint8_t numSubgroups;uint8_t numBis;
profiles/audio/bap.c static void iso_bcast_confirm_cb(GIOChannel *io, GError *err, void *user_data) · line 815
Quoted 10 lines
profiles/audio/bap.c +1
-4@@ -807,12 +818,11 @@ static void iso_bcast_confirm_cb(GIOChannel *io, GError *err, void *user_data)if (!ep)return ; +/* Update endpoint with the values of the peer's (BAP source) QOS */ update_bcast_qos (&qos, &ep->qos); - base_io = new0(struct iovec, 1); -util_iov_memcpy (base_io, base.base, base.base_len); - - parse_base(base_io->iov_base, base_io->iov_len, bap_debug,
Ive made quite a few changes to how broadcast works, so please have a look in the following set:
https://patchwork.kernel.org/project/bluetooth/list/?series=788264
profiles/audio/bap.c static struct bap_ep *ep_register_bcast(struct bap_data *data, · line 930
Quoted 10 lines
profiles/audio/bap.c +3
-0+/* Update endpoint with the value received from the peer's (BAP source) BASE */ +parse_base (base.base, base.base_len, bap_debug, &presDelay, &numSubgroups, &numBis, &codec, &ep->caps, &ep->metadata);@@ -917,6 +927,7 @@ static struct bap_ep *ep_register_bcast(struct bap_data *data,return ep; ep = new0(struct bap_ep, 1); +memset (ep,0 ,sizeof (struct bap_ep ));
new0 already does initialize to 0.
profiles/audio/bap.c static int bap_init(void) · line 2302
Quoted 8 lines
profiles/audio/bap.c +1
-0ep->data = data; ep->lpac = lpac; ep->rpac = rpac;@@ -2288,6 +2299,7 @@ static int bap_init(void)static void bap_exit (void ) {btd_profile_unregister (&bap_profile); +btd_profile_unregister (&bap_bcast_profile);
The above should probably be sent as a separate fix.
Quoted 6 lines
profiles/audio/bap.c +0
-1bt_bap_unregister (bap_id); } --2.39.2
Signature · 1 line
Luiz Augusto von Dentz
PATCH 2/2 shared/bap: Set stream lpac to NULL after removing the stream.
In reply to Andrei IstodorescuThis is required as the pac is freed in another place and will result in a dangling pointer.
--- src/shared/bap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
src/shared/bap.c +3 -1
@@ -2681,8 +2681,10 @@ static void remove_streams(void *data, void *user_data)struct bt_bap_stream *stream; stream =queue_remove_if (bap->streams, match_stream_lpac, pac); -if (stream) +if (stream) { + stream->lpac =NULL ;/* mark stream->lpac to NULL, as it will be freed in bt_bap_remove_pac */ bt_bap_stream_release (stream,NULL ,NULL ); + } }bool bt_bap_remove_pac (struct bt_bap_pac *pac)
--
2.39.2