Skip to contentSkip to conversation

Socket leak on each call to qemu_socket()

Qemu-devel Mark Pizzolato 3 participants 5 messages Oct 9, 2015 – Oct 13, 2015

lore
Share & tools
lore
#0 lore raw

Public bug reported:

On any host platform where SOCK_CLOEXEC is defined (Linux at least), a socket is leaked on each call to qemu_socket() AND the socket returned hasn't been created with the desired SOCK_CLOEXEC attribute. The qemu_socket routine is:

Line 272 of util/osdep.c:
/*
* Opens a socket with FD_CLOEXEC set
*/
int qemu_socket(int domain, int type, int protocol)

{
    int ret;
#ifdef SOCK_CLOEXEC
    ret = socket(domain, type | SOCK_CLOEXEC, protocol);
    if (ret != -1 || errno != EINVAL) {
        return ret;
    }
#endif
    ret = socket(domain, type, protocol);
    if (ret >= 0) {
        qemu_set_cloexec(ret);
    }

    return ret;
}

** Affects: qemu
Importance: Undecided
Status: New

Signature · 40 lines
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1504513

Title:
  Socket leak on each call to qemu_socket()

Status in QEMU:
  New

Bug description:
  On any host platform where SOCK_CLOEXEC is defined (Linux at least), a
  socket is leaked on each call to qemu_socket() AND the socket returned
  hasn't been created with the desired SOCK_CLOEXEC attribute.  The
  qemu_socket routine is:

  Line 272 of util/osdep.c:
  /*
   * Opens a socket with FD_CLOEXEC set
   */
  int qemu_socket(int domain, int type, int protocol)
  {
      int ret;

  #ifdef SOCK_CLOEXEC
      ret = socket(domain, type | SOCK_CLOEXEC, protocol);
      if (ret != -1 || errno != EINVAL) {
          return ret;
      }
  #endif
      ret = socket(domain, type, protocol);
      if (ret >= 0) {
          qemu_set_cloexec(ret);
      }

      return ret;
  }

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1504513/+subscriptions
#1 lore raw

I'm not sure that my original report was distributed to the folks who need to see this. My primary email address has a DKIM policy (DMARC) which says that all messages from my address are signed. I received various DMARC reports which said that the bug report sent as "From:
[email protected]" were rejected. The bug report messages were reasonably sent with an SMTP envelope from of qemu-devel- [email protected] and a "Sender: qemu-devel- [email protected]". Hmm... Maybe 163.com is rewriting message headers. In any case, the message would have passed the DMARC check if my email address was in a "Reply-To: [email protected]" header (there was no Reply-To: header), and the "From:" header was a @nongnu.org address.

I have changed my account to use my users.sourceforge.com forwarding address and I'm now generating this comment so that hopefully, the whole message will be widely distributed with a from address which doesn't have the same DMARC policy.

Signature · 40 lines
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1504513

Title:
  Socket leak on each call to qemu_socket()

Status in QEMU:
  New

Bug description:
  On any host platform where SOCK_CLOEXEC is defined (Linux at least), a
  socket is leaked on each call to qemu_socket() AND the socket returned
  hasn't been created with the desired SOCK_CLOEXEC attribute.  The
  qemu_socket routine is:

  Line 272 of util/osdep.c:
  /*
   * Opens a socket with FD_CLOEXEC set
   */
  int qemu_socket(int domain, int type, int protocol)
  {
      int ret;

  #ifdef SOCK_CLOEXEC
      ret = socket(domain, type | SOCK_CLOEXEC, protocol);
      if (ret != -1 || errno != EINVAL) {
          return ret;
      }
  #endif
      ret = socket(domain, type, protocol);
      if (ret >= 0) {
          qemu_set_cloexec(ret);
      }

      return ret;
  }

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1504513/+subscriptions
#2 lore raw
In reply to Mark Pizzolato
Quoted 12 lines · Mark Pizzolato

Public bug reported:

On any host platform where SOCK_CLOEXEC is defined (Linux at least), a socket is leaked on each call to qemu_socket() AND the socket returned hasn't been created with the desired SOCK_CLOEXEC attribute. The qemu_socket routine is:

Line 272 of util/osdep.c:
/*
* Opens a socket with FD_CLOEXEC set
*/
int qemu_socket(int domain, int type, int protocol)

Quoted 7 lines
{
    int ret;
#ifdef SOCK_CLOEXEC
    ret = socket(domain, type | SOCK_CLOEXEC, protocol);
    if (ret != -1 || errno != EINVAL) {
        return ret;

If socket() succeeded (ret != -1), we return the socket.

If socket() failed with anything but EINVAL (ret == -1 && errno != EINVAL), we return -1 with errno set.

Quoted 1 line
    }

Here, ret == -1 && errno == EINVAL.

Quoted 8 lines
#endif
    ret = socket(domain, type, protocol);
    if (ret >= 0) {
        qemu_set_cloexec(ret);
    }

    return ret;
}

How can this leak a socket?

How can this return a socket with FD_CLOEXEC not set?

#3 lore raw
Quoted 37 lines · On Sunday, October 11, 2015 at 11:58 PM. Markus Armbruster wrote:
Quoted 12 lines · Mark Pizzolato

Public bug reported:

On any host platform where SOCK_CLOEXEC is defined (Linux at least), a socket is leaked on each call to qemu_socket() AND the socket returned hasn't been created with the desired SOCK_CLOEXEC attribute. The qemu_socket routine is:

Line 272 of util/osdep.c:
/*
* Opens a socket with FD_CLOEXEC set
*/
int qemu_socket(int domain, int type, int protocol)

Quoted 7 lines
{
    int ret;
#ifdef SOCK_CLOEXEC
    ret = socket(domain, type | SOCK_CLOEXEC, protocol);
    if (ret != -1 || errno != EINVAL) {
        return ret;

If socket() succeeded (ret != -1), we return the socket.

If socket() failed with anything but EINVAL (ret == -1 && errno != EINVAL), we return -1 with errno set.

Quoted 1 line
    }

Here, ret == -1 && errno == EINVAL.

Quoted 6 lines
#endif
    ret = socket(domain, type, protocol);
    if (ret >= 0) {
        qemu_set_cloexec(ret);
    }
Quoted 6 lines
Quoted 2 lines
    return ret;
}

How can this leak a socket?

How can this return a socket with FD_CLOEXEC not set?

All I can say is "OOPS!!" Sorry for bothering you. I misread the status check after the first socket() call.

I'm in the process of lifting qemu's slirp code and dropping it into another open source project. Since I'm trying to use all the code in the slirp directory without modification I'm digging through where it now depends on other qemu code. I quickly looked at the qemu_socket() routine and read it wrong.

Once again, sorry.

- Mark Pizzolato

#4 lore raw
Quoted 48 lines · Mark Pizzolato - Info Comm
Quoted 37 lines · On Sunday, October 11, 2015 at 11:58 PM. Markus Armbruster wrote:
Quoted 12 lines · Mark Pizzolato

Public bug reported:

On any host platform where SOCK_CLOEXEC is defined (Linux at least), a socket is leaked on each call to qemu_socket() AND the socket returned hasn't been created with the desired SOCK_CLOEXEC attribute. The qemu_socket routine is:

Line 272 of util/osdep.c:
/*
* Opens a socket with FD_CLOEXEC set
*/
int qemu_socket(int domain, int type, int protocol)

Quoted 7 lines
{
    int ret;
#ifdef SOCK_CLOEXEC
    ret = socket(domain, type | SOCK_CLOEXEC, protocol);
    if (ret != -1 || errno != EINVAL) {
        return ret;

If socket() succeeded (ret != -1), we return the socket.

If socket() failed with anything but EINVAL (ret == -1 && errno != EINVAL), we return -1 with errno set.

Quoted 1 line
    }

Here, ret == -1 && errno == EINVAL.

Quoted 6 lines
#endif
    ret = socket(domain, type, protocol);
    if (ret >= 0) {
        qemu_set_cloexec(ret);
    }
Quoted 6 lines
Quoted 2 lines
    return ret;
}

How can this leak a socket?

How can this return a socket with FD_CLOEXEC not set?

All I can say is "OOPS!!" Sorry for bothering you. I misread the status check after the first socket() call.

Quoted 7 lines

I'm in the process of lifting qemu's slirp code and dropping it into another open source project. Since I'm trying to use all the code in the slirp directory without modification I'm digging through where it now depends on other qemu code. I quickly looked at the qemu_socket() routine and read it wrong.

Once again, sorry.

Happens to all of us from time to time :)