usb: dev-mtp: close fd in usb_mtp_object_readdir()
PATCH 1/1 usb: dev-mtp: close fd in usb_mtp_object_readdir()
Spotted by Coverity: CID 1397070
--- hw/usb/dev-mtp.c | 2 ++ 1 file changed, 2 insertions(+)
hw/usb/dev-mtp.c +2 -0
@@ -666,6 +666,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) } dir =fdopendir (fd);if (!dir) { +close (fd);return ; }#ifdef CONFIG_INOTIFY1
@@ -682,6 +683,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)usb_mtp_add_child (s, o, entry->d_name); }closedir (dir); +close (fd); }/* ----------------------------------------------------------------------- */
Signature · 1 line
2.17.1
Quoted 1 line · Li Qiang
Spotted by Coverity: CID 1397070
Closing a CVE to open a CID :)
Quoted 11 lines
Signed-off-by Li Qiang <[email protected]>--- hw/usb/dev-mtp.c | 2 ++ 1 file changed, 2 insertions(+)hw/usb/dev-mtp.c +0
-0
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 670
Quoted 5 lines
hw/usb/dev-mtp.c +1
-0@@ -666,6 +666,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) } dir =fdopendir (fd);if (!dir) { +close (fd);return ;
This or:
goto cleanup_fd;
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 685
Quoted 6 lines
hw/usb/dev-mtp.c +0
-0}#ifdef CONFIG_INOTIFY1 @@ -682,6 +683,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)usb_mtp_add_child (s, o, entry->d_name); }closedir (dir);
And:
cleanup_fd:
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 689
Quoted 5 lines
hw/usb/dev-mtp.c +1
-0+close (fd); }/* ----------------------------------------------------------------------- */
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 671
Quoted 13 lines · Li Qiang
Spotted by Coverity: CID 1397070
hw/usb/dev-mtp.c +1
-0@@ -666,6 +666,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) } dir =fdopendir (fd);if (!dir) { +close (fd);return ; }
Ok, clearly a bug.
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 686
Quoted 6 lines
hw/usb/dev-mtp.c +1
-0#ifdef CONFIG_INOTIFY1 @@ -682,6 +683,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)usb_mtp_add_child (s, o, entry->d_name); }closedir (dir); +close (fd);
Not fully sure this is correct.
The fdopendir manpage says the app should not use fd any more after successfully calling fdopendir(), and I assumed that includes calling close(). But I've seen the Coverity message warning this one too, so maybe I'm wrong ...
cheers,
Gerd
Quoted 13 lines · Gerd Hoffmann
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 686
Quoted 8 lines · Li Qiang
Spotted by Coverity: CID 1397070
#ifdef
CONFIG_INOTIFY1hw/usb/dev-mtp.c +1
-0@@ -682,6 +683,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)usb_mtp_add_child (s, o, entry->d_name); }closedir (dir); +close (fd);Not fully sure this is correct.
Quoted 3 lines
The fdopendir manpage says the app should not use fd any more after successfully calling
fdopendir(), and I assumed that includes callingclose().
Yes. The POSIX spec is clearer:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopendir.html
"Upon successful return from fdopendir(), the file descriptor is under
the control of the system, and if any attempt is made to close the file descriptor, or to modify the state of the associated description, other than by means of closedir(), readdir(), readdir_r(), rewinddir(), or seekdir(), the behavior is undefined. Upon calling closedir() the file descriptor shall be closed."
Quoted 2 lines
But I've seen the Coverity message warning this one too, so maybe I'm wrong ...
If coverity insists on a close(fd) then that's a coverity bug :-)
thanks
-- PMM
Quoted 18 lines · Peter Maydell
Quoted 13 lines · Gerd Hoffmann
hw/usb/dev-mtp.c static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) · line 686
Quoted 8 lines · Li Qiang
Spotted by Coverity: CID 1397070
#ifdef
CONFIG_INOTIFY1hw/usb/dev-mtp.c +1
-0@@ -682,6 +683,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)usb_mtp_add_child (s, o, entry->d_name); }closedir (dir); +close (fd);Not fully sure this is correct.
Quoted 3 lines
The fdopendir manpage says the app should not use fd any more after successfully calling
fdopendir(), and I assumed that includes callingclose().
Quoted 8 lines
Yes. The POSIX spec is clearer:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopendir.html
"Upon successful return fromfdopendir(), the file descriptor is under
the control of the system, and if any attempt is made to close the file descriptor, or to modify the state of the associated description, other than by means ofclosedir(),readdir(),readdir_r(),rewinddir(), orseekdir(), the behavior is undefined. Upon callingclosedir()the file descriptor shall be closed."
Thanks for looking it up.
Patch added to usb queue, with the second chunk dropped.
cheers,
Gerd