Skip to content

Address memory leaks/mitigate buffer overflows#161

Merged
andersson merged 5 commits intolinux-msm:masterfrom
igoropaniuk:memleaks_sec_issues
Feb 25, 2026
Merged

Address memory leaks/mitigate buffer overflows#161
andersson merged 5 commits intolinux-msm:masterfrom
igoropaniuk:memleaks_sec_issues

Conversation

@igoropaniuk
Copy link
Copy Markdown
Contributor

No description provided.

@igoropaniuk
Copy link
Copy Markdown
Contributor Author

@lumag @andersson @konradybcio could you take a look please

Comment thread qdl.c Outdated
Copy link
Copy Markdown
Collaborator

@andersson andersson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the three malloc/free patches contains motivation to why we should fix it up. Please include your motivation, so that others know why these changes matters and why they should do the same.

Comment thread firehose.c Outdated
Comment thread qdl.c Outdated
Comment thread qdl.c Outdated
Comment thread qdl.c Outdated
Comment thread qdl.c Outdated
Comment thread qdl.c Outdated
The `part_entry_lba` field in `struct gpt_header` is uint64_t, but
the local `lba` variable was declared as `unsigned int`, silently
truncating values above UINT_MAX. The buffer `lba_buf[10]` is also
too small: a 64-bit value can be up to 20 decimal digits, requiring
at least 21 bytes including the null terminator. Combined with an
unchecked sprintf(), this is a stack buffer overflow for any LBA
value exceeding 9 digits.

Fix by:
- declaring `lba` as uint64_t to match the source type
- increasing lba_buf to 21 bytes
- using snprintf() with sizeof to prevent overruns
- using PRIu64 for correct format specifier

Signed-off-by: Igor Opaniuk <igor.opaniuk@oss.qualcomm.com>
@igoropaniuk igoropaniuk force-pushed the memleaks_sec_issues branch 2 times, most recently from 5f782fc to 4e80cdb Compare February 13, 2026 09:36
@igoropaniuk
Copy link
Copy Markdown
Contributor Author

igoropaniuk commented Feb 13, 2026

@andersson @lumag

None of the three malloc/free patches contains motivation to why we should fix it up. Please include your motivation, so that others know why these changes matters and why they should do the same.

Fixed.

I also reworked completely the patch addressing the images[] parsing memory leaks, where rather restructuring the parser loop with break/cleanup logic added a fix this at the ownership level. Additionally added one more small fix in a separate commit

The sector probe buffer in firehose_try_configure() is a small,
bounded allocation (max 4096 bytes) used only within this function
scope. Replace malloc() with alloca() to:

- avoid the need for a free() path and NULL-check error handling
- simplify control flow, as the buffer is automatically released
  on function return

The upper bound is compile-time constant (largest entry in
sector_sizes[]), well within safe stack allocation limits.

Signed-off-by: Igor Opaniuk <igor.opaniuk@oss.qualcomm.com>
malloc() can return NULL on allocation failure. Without a check,
the subsequent read(fd, ptr, len) dereferences a NULL pointer,
causing undefined behavior (typically a segfault).

Signed-off-by: Igor Opaniuk <igor.opaniuk@oss.qualcomm.com>
The `len` parameter is size_t (unsigned) while `actual` from
libusb_bulk_transfer() is int (signed). Their direct comparison
on line 331 triggers -Wsign-compare: if `actual` were somehow
negative, the implicit conversion to size_t would produce a
large positive value, making the comparison silently wrong.

Fixes: b4030fa ("usb: Fix checkpatch warning about unnecessary parenthesis")
Signed-off-by: Igor Opaniuk <igor.opaniuk@oss.qualcomm.com>
@igoropaniuk
Copy link
Copy Markdown
Contributor Author

Friendly ping on this PR.
Let me know if any changes are needed. Thanks!

Copy link
Copy Markdown
Collaborator

@andersson andersson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Unfortunately I recently changed the model for how the archive is managed (no longer assigned to images[0] automatically).

Comment thread qdl.c Outdated
Comment thread qdl.c
@igoropaniuk
Copy link
Copy Markdown
Contributor Author

@andersson both comments are addressed. Thanks!

On error, decode_programmer_archive() and decode_sahara_config() leak
both the input blob and any images partially loaded before the failure.

Fix by extending their error paths to call sahara_images_free() and
free the blob, mirroring the cleanup already done on success. Also
introduce sahara_images_free() to consolidate teardown, drop the
misleading 'const' from sahara_image.name, and release sahara_images
in qdl_flash() on all exit paths.

Signed-off-by: Igor Opaniuk <igor.opaniuk@oss.qualcomm.com>
@andersson andersson merged commit ff592b7 into linux-msm:master Feb 25, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants