mirror of
https://github.com/apache/nuttx.git
synced 2026-06-01 07:45:16 +08:00
libc/fclose: Validate the user provided stream pointer
Check that the provided stream pointer is really opened for the group before closing & freeing it. Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
This commit is contained in:
committed by
Xiang Xiao
parent
e272181007
commit
c9776bf8a6
@@ -36,6 +36,8 @@
|
|||||||
# include <android/fdsan.h>
|
# include <android/fdsan.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <nuttx/queue.h>
|
||||||
|
|
||||||
#include "libc.h"
|
#include "libc.h"
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -69,6 +71,33 @@ int fclose(FAR FILE *stream)
|
|||||||
|
|
||||||
if (stream)
|
if (stream)
|
||||||
{
|
{
|
||||||
|
bool stdstream = (stream == stdin || stream == stdout ||
|
||||||
|
stream == stderr);
|
||||||
|
bool found = stdstream;
|
||||||
|
FAR sq_entry_t *curr;
|
||||||
|
|
||||||
|
slist = lib_get_streams();
|
||||||
|
|
||||||
|
nxmutex_lock(&slist->sl_lock);
|
||||||
|
|
||||||
|
/* Verify that the stream pointer is valid. */
|
||||||
|
|
||||||
|
for (curr = sq_peek(&slist->sl_queue); curr && !found;
|
||||||
|
curr = sq_next(curr))
|
||||||
|
{
|
||||||
|
if (stream == (FAR FILE *)curr)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
nxmutex_unlock(&slist->sl_lock);
|
||||||
|
errcode = EINVAL;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
ret = OK;
|
ret = OK;
|
||||||
|
|
||||||
/* If the stream was opened for writing, then flush the stream */
|
/* If the stream was opened for writing, then flush the stream */
|
||||||
@@ -81,16 +110,14 @@ int fclose(FAR FILE *stream)
|
|||||||
|
|
||||||
/* Skip close the builtin streams(stdin, stdout and stderr) */
|
/* Skip close the builtin streams(stdin, stdout and stderr) */
|
||||||
|
|
||||||
if (stream == stdin || stream == stdout || stream == stderr)
|
if (stdstream)
|
||||||
{
|
{
|
||||||
|
nxmutex_unlock(&slist->sl_lock);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove FILE structure from the stream list */
|
/* Remove FILE structure from the stream list */
|
||||||
|
|
||||||
slist = lib_get_streams();
|
|
||||||
nxmutex_lock(&slist->sl_lock);
|
|
||||||
|
|
||||||
sq_rem(&stream->fs_entry, &slist->sl_queue);
|
sq_rem(&stream->fs_entry, &slist->sl_queue);
|
||||||
|
|
||||||
nxmutex_unlock(&slist->sl_lock);
|
nxmutex_unlock(&slist->sl_lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user