diff mbox series

[BlueZ,1/2] bap: Remove memory leaks and buffer usage after free.

Message ID 20230927062436.6306-2-andrei.istodorescu@nxp.com (mailing list archive)
State New, archived
Headers show
Series Memory leaks and memory used after free fixes | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/CheckPatch warning 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.
tedd_an/GitLint fail 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."
tedd_an/BuildEll success Build ELL PASS
tedd_an/BluezMake success Bluez Make PASS
tedd_an/MakeCheck success Bluez Make Check PASS
tedd_an/MakeDistcheck success Make Distcheck PASS
tedd_an/CheckValgrind success Check Valgrind PASS
tedd_an/CheckSmatch success CheckSparse PASS
tedd_an/bluezmakeextell success Make External ELL PASS
tedd_an/IncrementalBuild success Incremental Build PASS
tedd_an/ScanBuild success Scan Build PASS

Commit Message

Andrei Istodorescu Sept. 27, 2023, 6:24 a.m. UTC
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(-)

--
2.39.2

Comments

bluez.test.bot@gmail.com Sept. 27, 2023, 8:02 a.m. UTC | #1
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 Sept. 27, 2023, 9:55 p.m. UTC | #2
Hi Andrei,

On Tue, Sep 26, 2023 at 11:24 PM Andrei Istodorescu
<andrei.istodorescu@nxp.com> wrote:
>
> Make sure the endpoint does not contain uninitialized pointers after
> creation. Rework parse_base and parse_array. Add missing unregister in
> bap_exit.

Ive addressed something similar in the following change:

https://patchwork.kernel.org/project/bluetooth/patch/20230927214003.1873224-13-luiz.dentz@gmail.com/

> ---
>  profiles/audio/bap.c | 66 ++++++++++++++++++++++++++------------------
>  1 file changed, 39 insertions(+), 27 deletions(-)
>
> diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
> index ee90426b9812..b164f68d3187 100644
> --- a/profiles/audio/bap.c
> +++ b/profiles/audio/bap.c
> @@ -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,

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

> +       /* 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.

>         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);

The above should probably be sent as a separate fix.

>         bt_bap_unregister(bap_id);
>  }
>
> --
> 2.39.2
>
diff mbox series

Patch

diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
index ee90426b9812..b164f68d3187 100644
--- a/profiles/audio/bap.c
+++ b/profiles/audio/bap.c
@@ -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);
 }