nshlib/nsh_fscmds.c: Allocate a PATH_MAX sized buffer for unlink_recursive #3222
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This fixes heap corruption when deleting a folder containing other folders or files. The issue appeared at commit 131d50a, which removed the stack-based temporary buffer.
unlink_recursive requires that the path is provided in PATH_MAX sized buffer. It concatenates sub-folder or file names to the same buffer.
nsh_getfullpath just allocates a buffer using strdup, so there is no room for concatenating more data to it.
To keep the stack usage smaller, instead of reverting the breaking commit, allocate the temporary buffer with lib_get_pathbuffer instead.
Impact
Fixes "rm -rf " in nsh.
Testing
Tested on imx9 hardware in CONFIG_BUILD_FLAT after mounting sdcard on /fs/microsd and executing the following sequence:
With this patch, the sequence above can be executed many times.
Without the fix, the system crashes: