Skip to content

Commit ff85a1e

Browse files
committed
nshlib/nsh_fscmds.c: Allocate a PATH_MAX sized buffer for unlink_recursive
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. Signed-off-by: Jukka Laitinen <[email protected]>
1 parent 3f6fc4b commit ff85a1e

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

nshlib/nsh_fscmds.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,10 @@ int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
22332233
{
22342234
if (recursive)
22352235
{
2236-
ret = unlink_recursive(fullpath, &stat);
2236+
FAR char *buf = lib_get_pathbuffer();
2237+
strlcpy(buf, fullpath, PATH_MAX);
2238+
ret = unlink_recursive(buf, &stat);
2239+
lib_put_pathbuffer(buf);
22372240
}
22382241
else
22392242
{

0 commit comments

Comments
 (0)