Skip to contentSkip to conversation

io: avoid potential double close of qio_channel_file's fd

PATCH In review Filip Hejsek 2 participants 4 messages Oct 12 – Nov 12

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io: avoid potential double close of qio_channel_file's fd

Make qio_channel_file_close() set fd to -1 even if qemu_close() returns an error so that qio_channel_file_finalize() doesn't call close again.

On Linux, close() always closes the fd, even when it returns an error. Because the fd can be reused after it is closed, the close must not be retried.

---
 io/channel-file.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
io/channel-file.c +9 -3
index 5cef75a67c..afc9640518 100644
@@ -255,12 +255,18 @@ static int qio_channel_file_close(QIOChannel *ioc,
 {
     QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
 
-    if (qemu_close(fioc->fd) < 0) {
+    int ret = qemu_close(fioc->fd);
+    /*
+     * The fd should now have been closed even if we got an error. There are
+     * obscure systems where this isn't always the case, but an fd leak on
+     * obscure systems is less bad than double close on common systems.
+     */
+    fioc->fd = -1;
+    if (ret < 0) {
         error_setg_errno(errp, errno,
-                         "Unable to close file");
+                         "Got an error while closing file");
         return -1;
     }
-    fioc->fd = -1;
     return 0;
 }
 

---
base-commit: 615ece3c406b262996e569f29ea6eed0d81c4d8d
change-id: 20261012-fix-double-close-7bc5b144cc94

Best regards,

Signature · 1 line
Filip Hejsek <[email protected]>
#1 lore raw
Quoted 17 lines · Filip Hejsek

Make qio_channel_file_close() set fd to -1 even if qemu_close() returns an error so that qio_channel_file_finalize() doesn't call close again.

On Linux, close() always closes the fd, even when it returns an error. Because the fd can be reused after it is closed, the close must not be retried.

---
  io/channel-file.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
io/channel-file.c +0 -0
index 5cef75a67c..afc9640518 100644

io/channel-file.c static int qio_channel_file_close(QIOChannel *ioc, · line 263

Quoted 10 lines
io/channel-file.c +6 -1
@@ -255,12 +255,18 @@ static int qio_channel_file_close(QIOChannel *ioc,
  {
      QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
 
-    if (qemu_close(fioc->fd) < 0) {
+    int ret = qemu_close(fioc->fd);
+    /*
+     * The fd should now have been closed even if we got an error. There are
+     * obscure systems where this isn't always the case, but an fd leak on
+     * obscure systems is less bad than double close on common systems.
+     */

Hmm. It looks like linux is the obscure system now, not the other way around. Before, POSIX.1-2008 stated that after EINTR, the state of the FD is unspecified. However, POSIX.1-2024 states that after EINTR, the FD should remain open. And linux isn't going to change the way close(2) works there.

And there's no posix_close(fd, flags). Which is not available on linux (yet).

And there's also an additional possible errno value here: EINPROGRESS.

The other possible failure cases seems to be consistent.

So at least, it looks like, the comment needs to be updated. Yes, linux is the main target of qemu, but not only, -- and channel-file is used in the tools too (qemu-nbd, qemu-img, etc) - where it is rather irrelevant - keep it open or close it again later.

Why do we care about this close(2) return value here anyway? Have you seen actual failure case?

Thanks,

/mjt

#2 lore raw
Quoted 21 lines · Michael Tokarev
Quoted 20 lines · Filip Hejsek

Make qio_channel_file_close() set fd to -1 even if qemu_close() returns an error so that qio_channel_file_finalize() doesn't call close again.

On Linux, close() always closes the fd, even when it returns an error. Because the fd can be reused after it is closed, the close must not be retried.

---
  io/channel-file.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
io/channel-file.c +0 -0
index 5cef75a67c..afc9640518 100644
@@ -255,12 +255,18 @@ static int qio_channel_file_close(QIOChannel *ioc,
  {
      QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
 
Quoted 13 lines

io/channel-file.c static int qio_channel_file_close(QIOChannel *ioc, · line 263

Quoted 7 lines
io/channel-file.c +6 -1
-    if (qemu_close(fioc->fd) < 0) {
+    int ret = qemu_close(fioc->fd);
+    /*
+     * The fd should now have been closed even if we got an error. There are
+     * obscure systems where this isn't always the case, but an fd leak on
+     * obscure systems is less bad than double close on common systems.
+     */

Hmm. It looks like linux is the obscure system now, not the other way around. Before, POSIX.1-2008 stated that after EINTR, the state of the FD is unspecified. However, POSIX.1-2024 states that after EINTR, the FD should remain open. And linux isn't going to change the way close(2) works there.

The system where close() may keep the fd open is HP-UX, which QEMU doesn't support. On Linux, macOS, Windows, and FreeBSD/NetBSD/OpenBSD, the fd is always closed. Of these, at least Linux can close the fd and return EINTR when the syscall is interrupted, in violation of POSIX.1- 2024.

Quoted 4 lines

And there's no posix_close(fd, flags). Which is not available on linux (yet).

And there's also an additional possible errno value here: EINPROGRESS.

Linux doesn't use EINPROGRESS. I think the kernel folks were opposed to it. Also see <https://sourceware.org/bugzilla/show_bug.cgi?id=14627>.

Quoted 9 lines

The other possible failure cases seems to be consistent.

So at least, it looks like, the comment needs to be updated. Yes, linux is the main target of qemu, but not only, -- and channel-file is used in the tools too (qemu-nbd, qemu-img, etc) - where it is rather irrelevant - keep it open or close it again later.

Why do we care about this close(2) return value here anyway? Have you seen actual failure case?

This was spotted while reading the code, so no failures observed. But the risk isn't an incorrect return value, it's about finalize() calling close() a second time. That applies to the tools as well.

So what about changing the comment to this? I can send a v2 if we agree on wording.

Set fioc->fd to -1, to prevent qio_channel_file_finalize() from closing it again.

POSIX.1-2024 specifies that the fd is closed even when close() returns an error, except when the error is EINTR. The systems that QEMU supports however violate the standard and always close the fd, even when returning EINTR. If the fd was actually closed, we must not try to close it a second time as we could accidentally close someone else's fd.

Regards,
Filip Hejsek

#3 lore raw
Quoted 8 lines · Filip Hejsek
Quoted 5 lines · Michael Tokarev

Hmm. It looks like linux is the obscure system now, not the other way around. Before, POSIX.1-2008 stated that after EINTR, the state of the FD is unspecified. However, POSIX.1-2024 states that after EINTR, the FD should remain open. And linux isn't going to change the way close(2) works there.

Quoted 5 lines

The system where close() may keep the fd open is HP-UX, which QEMU doesn't support. On Linux, macOS, Windows, and FreeBSD/NetBSD/OpenBSD, the fd is always closed. Of these, at least Linux can close the fd and return EINTR when the syscall is interrupted, in violation of POSIX.1- 2024.

Yes, I understand. Yet, POSIX standartized the HP-UX behavior, so all other systems has become "obscure" this way.

Quoted 6 lines
Quoted 2 lines

Why do we care about this close(2) return value here anyway? Have you seen actual failure case?

This was spotted while reading the code, so no failures observed. But the risk isn't an incorrect return value, it's about finalize() calling close() a second time. That applies to the tools as well.

I mean, this weren't a problem until now, for all the decades qemu exists. Not a problem in practice, that is.

I understand what the problem we have. And yes it is better to keep such an FD "dangling" in case of close() returned error and haven't actually closed the FD, than to close an innocent/reused FD.

Quoted 6 lines

So what about changing the comment to this? I can send a v2 if we agree on wording.

Set fioc->fd to -1, to prevent qio_channel_file_finalize() from closing it again.

Quoted 6 lines

POSIX.1-2024 specifies that the fd is closed even when close() returns an error, except when the error is EINTR. The systems that QEMU supports however violate the standard and always close the fd, even when returning EINTR. If the fd was actually closed, we must not try to close it a second time as we could accidentally close someone else's fd.

Yes, let's pick this commit instead. No need to re-send.

Thanks,

/mjt