Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i2c_master): NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL #14845

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pback34
Copy link

@pback34 pback34 commented Nov 7, 2024

Description:

This pull request addresses a potential crash in the i2c_master_bus_destroy function of the ESP-IDF I2C master driver. The issue arises when i2c_master->base is NULL, leading to a NULL pointer dereference and subsequent program crash.


Issue Details:

  • Scenario:
    • When attempting to create a new I2C master bus using i2c_new_master_bus, if the bus is not available (e.g., all I2C ports are in use), the function i2c_acquire_bus_handle fails.
    • As a result, i2c_master->base remains NULL.
    • When i2c_new_master_bus encounters this failure, it calls i2c_master_bus_destroy to clean up.
    • The i2c_master_bus_destroy function does not check if i2c_master->base is NULL before using it.
    • This leads to a NULL pointer dereference when calling functions like i2c_common_deinit_pins(i2c_master->base) and i2c_release_bus_handle(i2c_master->base), causing a crash.

Proposed Fix:

  • Add NULL Check:

  • Introduced a check to verify if i2c_master->base is not NULL before dereferencing it in i2c_master_bus_destroy.

  • This ensures that functions which require i2c_master->base only execute when it's valid.

  • Minimal Changes:

  • The fix is minimal and preserves the existing logic and flow of the function.

  • The conditional checks and resource cleanup are maintained as per the original implementation.

@CLAassistant
Copy link

CLAassistant commented Nov 7, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

github-actions bot commented Nov 7, 2024

Messages
📖 You might consider squashing your 3 commits (simplifying branch history).

👋 Hello pback34, we appreciate your contribution to this project!


📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more.

🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project.

Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Addressing info messages (📖) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests via this public GitHub repository.

This GitHub project is public mirror of our internal git repository

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved, we synchronize it into our internal git repository.
4. In the internal git repository we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
5. If the change is approved and passes the tests it is merged into the default branch.
5. On next sync from the internal git repository merged change will appear in this public GitHub repository.

Generated by 🚫 dangerJS against 8567a45

@pback34 pback34 changed the title Fix NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL Fix NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL Nov 7, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label Nov 7, 2024
@github-actions github-actions bot changed the title Fix NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL Fix NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL (IDFGH-14024) Nov 7, 2024
@pback34 pback34 changed the title Fix NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL (IDFGH-14024) fix(i2c_master): NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL Nov 7, 2024
i2c_common_deinit_pins(i2c_master->base);

if (i2c_release_bus_handle(i2c_master->base) == ESP_OK) {
if (i2c_master) {

Choose a reason for hiding this comment

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

this specific check for non-null is redundant at this point since the pointer will have been dereferenced already and has been validated already as part of the first line in the function.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch. Please see if latest looks correct.

Choose a reason for hiding this comment

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

Looks much better!

@KaeLL
Copy link
Contributor

KaeLL commented Nov 7, 2024

That's a good candidate to use esp_check's ESP_GOTO_ON_ERROR/FALSE to improve error handling.

@@ -808,16 +811,18 @@ static esp_err_t i2c_master_bus_destroy(i2c_master_bus_handle_t bus_handle)
}
}
bus_handle = NULL;
free(i2c_master);
} else {
free(i2c_master);
Copy link

Choose a reason for hiding this comment

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

Unless the ESP coding style suggests this, I would refactor the free(i2c_master); calls into a single statement at the end end remove two else clauses.

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Opened Issue is new labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants