Skip to content

Conversation

@cdesjardins
Copy link
Contributor

@cdesjardins cdesjardins commented May 30, 2024

If fs_unmount is invoked while fs i/o is ongoing then it will cause a NULL pointer exception due to the unchecked and unprotected dereference of the fs pointer.

Fixes #73501

@github-actions
Copy link

Hello @cdesjardins, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@de-nordic
Copy link
Contributor

de-nordic commented May 30, 2024

This should be configurable by user as it is up to user setup, mostly, where mounted fs is used by more than one thread.
Users that do not have the issue should be able to turn off the feature as it adds code and impacts execution paths.

Please also take a look at this comment #73507 (comment) regarding commit message format. Thanks.

subsys/fs/fs.c Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct mutext to use. This one is used for protecting mount points so that when path to FS is figured out the mount list do not get removed/added.
Using this mutex for ops like fs_open means that you are globally locking VFS to perform operation of some mount point, so if you take a lot of time to write to one mount point the other thread has to wait on own fs_ op, even if that is applied to different mount point.

@cdesjardins cdesjardins force-pushed the main branch 2 times, most recently from f5c0c72 to 1d38e82 Compare May 30, 2024 15:16
The mp pointer is in fs_file_t and fs_dir_t so if the fs pointer is made
NULL then subsequent file I/O operations will cause a NULL pointer
exception. Removing the mount point from the list is threadsafe and
should be sufficient.

Signed-off-by: Chris Desjardins <[email protected]>
@cdesjardins
Copy link
Contributor Author

Thanks for your comments, and indeed I believe there is a much simpler solution which I have tested on my platform and pushed.

@de-nordic
Copy link
Contributor

de-nordic commented May 30, 2024

Thanks for your comments, and indeed I believe there is a much simpler solution which I have tested on my platform and pushed.

The issue you have logged is still correct.

goto unmount_err;
}

/* clear file system interface */
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that this fixes problem with NULL pointer exception but unfortunately now systems continue write to system that is no longer valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my testing the i/o errors out and returns an error code, which is a huge improvement over the NULL pointer exception.

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the "Fixes #73501" from commit and description as this does not fix the problem, it only makes it manifest in a less violent way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it does fix "the problem", which is NULL pointer dereference caused by a user removing a usb drive during file i/o.

Copy link
Contributor

Choose a reason for hiding this comment

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

@de-nordic Please check #78018 as an alternative.

@MaureenHelm
Copy link
Member

@de-nordic please revisit

Copy link
Contributor

@de-nordic de-nordic left a comment

Choose a reason for hiding this comment

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

OK, let it be.
It still does not make the FS thread safe, but reduces fallout from unmounting system somebody else is using.

A proper way would be to not allow unmounting system being used by somebody else
I think that #78018 may deliver that, once completed.

@de-nordic de-nordic added this to the v4.0.0 milestone Oct 14, 2024
@mmahadevan108 mmahadevan108 added the bug The issue is a bug, or the PR is fixing a bug label Nov 6, 2024
@pdgendt pdgendt changed the title Make the fs fully threadsafe fs: Fix null pointer exception caused by async fs_unmount Nov 7, 2024
@mmahadevan108 mmahadevan108 merged commit 862af5e into zephyrproject-rtos:main Nov 8, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: File System bug The issue is a bug, or the PR is fixing a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.c null pointer exception when doing file i/o in a thread, and fs_unmount in another thread.

7 participants