xenstored file descriptor leak
Hello,
on NetBSD I'm tracking down an issue where xenstored never closes its file descriptor to /var/run/xenstored/socket and instead loops at 100% CPU on these descriptors.
xenstored loops because poll(2) returns a POLLIN event for these descriptors but they are marked is_ignored = true.
I'm seeing this with xen 4.15, 4.13 and has also been reported with 4.11 with latest security patches.
It seems to have started with patches for XSA-115 (A user reported this for 4.11)
I've tracked it down to a difference in poll(2) implementation; it seems that linux will return something that is not (POLLIN|POLLOUT) when a socket if closed; while NetBSD returns POLLIN (this matches the NetBSD's man page).
First I think there may be a security issue here, as, even on linux it should be possible for a client to cause a socket to go to the is_ignored state, while still sending data and cause xenstored to go to a 100% CPU loop. I think this is needed anyway:
xenstored_core.c renamed from xenstored_core.c.orig +6 -3
@@ -397,9 +397,12 @@ !list_empty(&conn->out_list))) *ptimeout =0 ; }else { -short events =POLLIN |POLLPRI ; -if (!list_empty (&conn->out_list)) - events |=POLLOUT ; +short events =0 ; +if (!conn->is_ignored) { + events |=POLLIN |POLLPRI ; +if (!list_empty (&conn->out_list)) + events |=POLLOUT ; + } conn->pollfd_idx =set_fd (conn->fd, events); } }
Now I wonder if, on NetBSD at last, a read error or short read shouldn't cause the socket to be closed, as with:
@@ -1561,6 +1565,8 @@
bad_client:
ignore_connection(conn);
+ /* we don't want to keep this connection alive */
+ talloc_free(conn);
}
static void handle_output(struct connection *conn)
what do you think ?
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 13 lines · Manuel Bouyer
Hello,
on NetBSD I'm tracking down an issue where xenstored never closes its file descriptor to/var/run/xenstored/socketand instead loops at 100% CPU on these descriptors.xenstored loops because poll(2) returns a POLLIN event for these descriptors but they are marked
is_ignored= true.I'm seeing this with xen 4.15, 4.13 and has also been reported with 4.11 with latest security patches.
It seems to have started with patches for XSA-115 (A user reported this for 4.11)
Quoted 4 lines
I've tracked it down to a difference in poll(2) implementation; it seems that linux will return something that is not (POLLIN|POLLOUT) when a socket if closed; while NetBSD returns POLLIN (this matches the NetBSD's man page).
Yeah, Linux seems to return POLLHUP additionally.
Quoted 4 lines
First I think there may be a security issue here, as, even on linux it should be possible for a client to cause a socket to go to the
is_ignoredstate, while still sending data and cause xenstored to go to a 100% CPU loop.
No security issue, as sockets are restricted to dom0 and user root.
In case you are suspecting a security issue, please don't send such mails to xen-devel in future, but to [email protected].
Quoted 10 lines
I think this is needed anyway:
xenstored_core.c renamed from xenstored_core.c.orig +0
-2@@ -397,9 +397,12 @@ !list_empty(&conn->out_list))) *ptimeout =0 ; }else { -short events =POLLIN |POLLPRI ; -if (!list_empty (&conn->out_list))
xenstored_core.c line 408
Quoted 10 lines
xenstored_core.c renamed from xenstored_core.c.orig +6
-1- events |=POLLOUT ; +short events =0 ; +if (!conn->is_ignored) { + events |=POLLIN |POLLPRI ; +if (!list_empty (&conn->out_list)) + events |=POLLOUT ; + } conn->pollfd_idx =set_fd (conn->fd, events); } }
Yes, I think this is a good idea.
xenstored_core.c line 1570
Quoted 11 lines
Now I wonder if, on NetBSD at last, a read error or short read shouldn't cause the socket to be closed, as with:
xenstored_core.c renamed from xenstored_core.c.orig +2
-0@@ -1561,6 +1565,8 @@ bad_client:ignore_connection (conn); +/* we don't want to keep this connection alive */ +talloc_free (conn); }
This is wrong for non-socket connections, as we want to keep the domain in question to be known to xenstored.
For socket connections this should be okay, though.
Juergen
Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Manuel Bouyer
Hello,
on NetBSD I'm tracking down an issue where xenstored never closes its file descriptor to/var/run/xenstored/socketand instead loops at 100% CPU on these descriptors.xenstored loops because poll(2) returns a POLLIN event for these descriptors but they are marked
is_ignored= true.I'm seeing this with xen 4.15, 4.13 and has also been reported with 4.11 with latest security patches.
It seems to have started with patches for XSA-115 (A user reported this for 4.11)
Quoted 6 lines
Quoted 4 lines
I've tracked it down to a difference in poll(2) implementation; it seems that linux will return something that is not (POLLIN|POLLOUT) when a socket if closed; while NetBSD returns POLLIN (this matches the NetBSD's man page).
Yeah, Linux seems to return POLLHUP additionally.
Overall, it seems that the poll(2) behavior with non-regular files is highly OS-dependant when it comes to border cases (like a remote close of a socket)
Quoted 10 lines
Quoted 5 lines
First I think there may be a security issue here, as, even on linux it should be possible for a client to cause a socket to go to the
is_ignoredstate, while still sending data and cause xenstored to go to a 100% CPU loop.No security issue, as sockets are restricted to dom0 and user root.
In case you are suspecting a security issue, please don't send such mails to xen-devel in future, but to [email protected].
Yes, sorry. Will do next time.
Quoted 15 lines
Quoted 15 lines
I think this is needed anyway:
xenstored_core.c renamed from xenstored_core.c.orig +3
-3@@ -397,9 +397,12 @@ !list_empty(&conn->out_list))) *ptimeout =0 ; }else { -short events =POLLIN |POLLPRI ; -if (!list_empty (&conn->out_list)) - events |=POLLOUT ; +short events =0 ; +if (!conn->is_ignored) { + events |=POLLIN |POLLPRI ;
Quoted 8 lines
xenstored_core.c line 408
Quoted 6 lines
xenstored_core.c renamed from xenstored_core.c.orig +3
-0+if (!list_empty (&conn->out_list)) + events |=POLLOUT ; + } conn->pollfd_idx =set_fd (conn->fd, events); } }Yes, I think this is a good idea.
Well, after some sleep I don't think it is. We should always keep at last POLLIN or we will never notice a socket close otherwise.
Quoted 15 lines
xenstored_core.c line 1569
Quoted 11 lines
Now I wonder if, on NetBSD at last, a read error or short read shouldn't cause the socket to be closed, as with:
xenstored_core.c renamed from xenstored_core.c.orig +2
-0@@ -1561,6 +1565,8 @@ bad_client:ignore_connection (conn); +/* we don't want to keep this connection alive */ +talloc_free (conn); }This is wrong for non-socket connections, as we want to keep the domain in question to be known to xenstored.
Quoted 1 line
For socket connections this should be okay, though.
What are "non-socket connections" BTW ? I don't think I've seen one in my test.
Is there a way to know if a connection is socket or non-socket ?
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 63 lines · Manuel Bouyer
Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Manuel Bouyer
Hello,
on NetBSD I'm tracking down an issue where xenstored never closes its file descriptor to/var/run/xenstored/socketand instead loops at 100% CPU on these descriptors.xenstored loops because poll(2) returns a POLLIN event for these descriptors but they are marked
is_ignored= true.I'm seeing this with xen 4.15, 4.13 and has also been reported with 4.11 with latest security patches.
It seems to have started with patches for XSA-115 (A user reported this for 4.11)Quoted 6 lines
Quoted 4 lines
I've tracked it down to a difference in poll(2) implementation; it seems that linux will return something that is not (POLLIN|POLLOUT) when a socket if closed; while NetBSD returns POLLIN (this matches the NetBSD's man page).
Yeah, Linux seems to return POLLHUP additionally.
Overall, it seems that the poll(2) behavior with non-regular files is highly OS-dependant when it comes to border cases (like a remote close of a socket)
Quoted 10 lines
Quoted 5 lines
First I think there may be a security issue here, as, even on linux it should be possible for a client to cause a socket to go to the
is_ignoredstate, while still sending data and cause xenstored to go to a 100% CPU loop.No security issue, as sockets are restricted to dom0 and user root.
In case you are suspecting a security issue, please don't send such mails to xen-devel in future, but to [email protected].
Yes, sorry. Will do next time.
Quoted 15 lines
Quoted 15 lines
I think this is needed anyway:
xenstored_core.c renamed from xenstored_core.c.orig +3
-3@@ -397,9 +397,12 @@ !list_empty(&conn->out_list))) *ptimeout =0 ; }else { -short events =POLLIN |POLLPRI ; -if (!list_empty (&conn->out_list)) - events |=POLLOUT ; +short events =0 ; +if (!conn->is_ignored) { + events |=POLLIN |POLLPRI ;Quoted 8 lines
xenstored_core.c line 408
Quoted 6 lines
xenstored_core.c renamed from xenstored_core.c.orig +3
-0+if (!list_empty (&conn->out_list)) + events |=POLLOUT ; + } conn->pollfd_idx =set_fd (conn->fd, events); } }Yes, I think this is a good idea.
Quoted 2 lines
Well, after some sleep I don't think it is. We should always keep at last POLLIN or we will never notice a socket close otherwise.
Adding the fd of an ignored socket connection to the list is the real problem here. Why should that be done?
Quoted 16 lines
Quoted 16 lines
xenstored_core.c line 1569
Quoted 12 lines
Now I wonder if, on NetBSD at last, a read error or short read shouldn't cause the socket to be closed, as with:
xenstored_core.c renamed from xenstored_core.c.orig +2
-0@@ -1561,6 +1565,8 @@ bad_client:ignore_connection (conn); +/* we don't want to keep this connection alive */ +talloc_free (conn); }This is wrong for non-socket connections, as we want to keep the domain in question to be known to xenstored.
Quoted 4 lines
Quoted 1 line
For socket connections this should be okay, though.
What are "non-socket connections" BTW ? I don't think I've seen one in my test.
Every connection to another domain.
Quoted 1 line
Is there a way to know if a connection is socket or non-socket ?
Active socket connections have conn->fd >= 0.
Juergen
Quoted 8 lines · Jürgen Groß
Quoted 5 lines
Quoted 2 lines
[...]
Yes, I think this is a good idea.Well, after some sleep I don't think it is. We should always keep at last POLLIN or we will never notice a socket close otherwise.
Adding the fd of an ignored socket connection to the list is the real problem here. Why should that be done?
If we don't do it, we never notice when the socket is closed and the file descriptor will stay forever. When I tried it, I had about 50 zombie file descriptors open in xenstored, after starting only 2 domains.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Review line 1569
Quoted 10 lines
Now I wonder if, on NetBSD at last, a read error or short read shouldn't cause the socket to be closed, as with:
(quoted diff) +2
-0@@ -1561,6 +1565,8 @@ bad_client: ignore_connection(conn); + /* we don't want to keep this connection alive */ + talloc_free(conn); }This is wrong for non-socket connections, as we want to keep the domain in question to be known to xenstored.
For socket connections this should be okay, though.
Quoted 8 lines
Quoted 2 lines
What are "non-socket connections" BTW ? I don't think I've seen one in my test.
Every connection to another domain.
Quoted 1 line
Is there a way to know if a connection is socket or non-socket ?
Active socket connections have
conn->fd>= 0.
OK, I'll rework my patch. Thanks
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 7 lines · Manuel Bouyer
Quoted 6 lines · Jürgen Groß
Quoted 5 lines
Quoted 2 lines
[...]
Yes, I think this is a good idea.Well, after some sleep I don't think it is. We should always keep at last POLLIN or we will never notice a socket close otherwise.
Quoted 6 lines
Quoted 2 lines
Adding the fd of an ignored socket connection to the list is the real problem here. Why should that be done?
If we don't do it, we never notice when the socket is closed and the file descriptor will stay forever. When I tried it, I had about 50 zombie file descriptors open in xenstored, after starting only 2 domains.
This shouldn't happen in case we are closing the socket actively.
In the end we should just do a talloc_free(conn) in ignore_connection() if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.
Juergen
Quoted 7 lines · Jürgen Groß
[...]
This shouldn't happen in case we are closing the socket actively.In the end we should just do a
talloc_free(conn)inignore_connection()if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.
Hello
here's an updated patch which works for me. Does anyone see a problem with it ? If not I will submit it for commit.
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
xenstored_core.c renamed from xenstored_core.c.orig +9 -3
@@ -397,9 +397,12 @@ !list_empty(&conn->out_list))) *ptimeout =0 ; }else { -short events =POLLIN |POLLPRI ; -if (!list_empty (&conn->out_list)) - events |=POLLOUT ; +short events =0 ; +if (!conn->is_ignored) { + events |=POLLIN |POLLPRI ; +if (!list_empty (&conn->out_list)) + events |=POLLOUT ; + } conn->pollfd_idx =set_fd (conn->fd, events); } }
@@ -1440,6 +1443,9 @@talloc_free (conn->in); conn->in =NULL ; +/* if this is a socket connection, drop it now */ +if (conn->fd >=0 ) +talloc_free (conn); }static const char *sockmsg_string (enum xsd_sockmsg_type type)
Quoted 4 lines · Manuel Bouyer
Quoted 3 lines · Jürgen Groß
[...]
This shouldn't happen in case we are closing the socket actively.
Quoted 9 lines
Quoted 4 lines
In the end we should just do a
talloc_free(conn)inignore_connection()if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.Hello
here's an updated patch which works for me. Does anyone see a problem with it ? If not I will submit it for commit.
Do you really need the first hunk? I would have thought just freeing conn in ignore_connection() is enough.
In case you are seeing problems without the first hunk, please say so in a comment added to this hunk in order to avoid it being removed sometimes in the future.
Juergen
Quoted 15 lines · Jürgen Groß
Quoted 4 lines · Manuel Bouyer
Quoted 3 lines · Jürgen Groß
[...]
This shouldn't happen in case we are closing the socket actively.Quoted 9 lines
Quoted 4 lines
In the end we should just do a
talloc_free(conn)inignore_connection()if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.Hello
here's an updated patch which works for me. Does anyone see a problem with it ? If not I will submit it for commit.
Quoted 6 lines
Do you really need the first hunk? I would have thought just freeing conn in
ignore_connection()is enough.In case you are seeing problems without the first hunk, please say so in a comment added to this hunk in order to avoid it being removed sometimes in the future.
No I don't need it. From your previous comments I though it was a good idea to keep it, but I can remove it if you think it's better.
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 19 lines · Manuel Bouyer
Quoted 18 lines · Jürgen Groß
Quoted 4 lines · Manuel Bouyer
Quoted 3 lines · Jürgen Groß
[...]
This shouldn't happen in case we are closing the socket actively.Quoted 9 lines
Quoted 4 lines
In the end we should just do a
talloc_free(conn)inignore_connection()if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.Hello
here's an updated patch which works for me. Does anyone see a problem with it ? If not I will submit it for commit.Do you really need the first hunk? I would have thought just freeing conn in
ignore_connection()is enough.
Quoted 6 lines
Quoted 3 lines
In case you are seeing problems without the first hunk, please say so in a comment added to this hunk in order to avoid it being removed sometimes in the future.
No I don't need it. From your previous comments I though it was a good idea to keep it, but I can remove it if you think it's better.
Yes, please remove it.
Juergen
Manuel Bouyer
Reviewed-by Jürgen GroßQuoted 20 lines · Jürgen Groß
Quoted 19 lines · Manuel Bouyer
Quoted 18 lines · Jürgen Groß
Quoted 4 lines · Manuel Bouyer
Quoted 3 lines · Jürgen Groß
[...]
This shouldn't happen in case we are closing the socket actively.Quoted 9 lines
Quoted 4 lines
In the end we should just do a
talloc_free(conn)inignore_connection()if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.Hello
here's an updated patch which works for me. Does anyone see a problem with it ? If not I will submit it for commit.Do you really need the first hunk? I would have thought just freeing conn in
ignore_connection()is enough.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
In case you are seeing problems without the first hunk, please say so in a comment added to this hunk in order to avoid it being removed sometimes in the future.
No I don't need it. From your previous comments I though it was a good idea to keep it, but I can remove it if you think it's better.
Yes, please remove it.
Will do
In the patch I'm going to submit, may I add
?
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 28 lines · Manuel Bouyer
Quoted 27 lines · Jürgen Groß
Quoted 25 lines · Manuel Bouyer
Quoted 15 lines · Jürgen Groß
Quoted 4 lines · Manuel Bouyer
Quoted 3 lines · Jürgen Groß
[...]
This shouldn't happen in case we are closing the socket actively.Quoted 9 lines
Quoted 4 lines
In the end we should just do a
talloc_free(conn)inignore_connection()if it is a socket based one. This should revert the critical modification of the XSA-115 fixes for sockets while keeping the desired effect for domain connections.Hello
here's an updated patch which works for me. Does anyone see a problem with it ? If not I will submit it for commit.Quoted 6 lines
Do you really need the first hunk? I would have thought just freeing conn in
ignore_connection()is enough.In case you are seeing problems without the first hunk, please say so in a comment added to this hunk in order to avoid it being removed sometimes in the future.
No I don't need it. From your previous comments I though it was a good idea to keep it, but I can remove it if you think it's better.
Quoted 9 lines
Quoted 1 line
Yes, please remove it.
Will do
In the patch I'm going to submit, may I add
Reviewed-by Jürgen Groß <[email protected]>?
Let me see the patch (including commit message) first, please.
So just send out as a regular patch, and I'll respond accordingly. :-)
Juergen
Quoted 11 lines · Jürgen Groß
Quoted 7 lines
Will do
In the patch I'm going to submit, may I add
Reviewed-by Jürgen Groß <[email protected]>?
Let me see the patch (including commit message) first, please.
So just send out as a regular patch, and I'll respond accordingly. :-)
Attached is the new patch.
As commit message I suggest:
xenstored: close socket connections on error
On error, don't keep socket connection in ignored state but close them. When the remote end of a socket is closed, xenstored will flag it as an error and switch the connection to ignored. But on some OSes (e.g. NetBSD), poll(2) will return only POLLIN in this case, so sockets in ignored state will stay open forever in xenstored (and it will loop with CPU 100% busy).
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
xenstored_core.c renamed from xenstored_core.c.orig +3 -0
@@ -1440,6 +1440,9 @@talloc_free (conn->in); conn->in =NULL ; +/* if this is a socket connection, drop it now */ +if (conn->fd >=0 ) +talloc_free (conn); }static const char *sockmsg_string (enum xsd_sockmsg_type type)
Quoted 17 lines · Manuel Bouyer
Quoted 11 lines · Jürgen Groß
Quoted 7 lines
Will do
In the patch I'm going to submit, may I add
Reviewed-by Jürgen Groß <[email protected]>?
Let me see the patch (including commit message) first, please.
So just send out as a regular patch, and I'll respond accordingly. :-)
Attached is the new patch.
As commit message I suggest:
Quoted 9 lines
xenstored: close socket connections on error
On error, don't keep socket connection in ignored state but close them. When the remote end of a socket is closed, xenstored will flag it as an error and switch the connection to ignored. But on some OSes (e.g. NetBSD), poll(2) will return only POLLIN in this case, so sockets in ignored state will stay open forever in xenstored (and it will loop with CPU 100% busy).
Uh, this is no regular patch. You've sent correct patches before, haven't you? The patch should have a Signed-off-by: and in this case a
Fixes: tag as well (hint: the patch breaking your use case was commit d2fa370d3ef9cbe22d7256c608671cdcdf6e0083). See
https://wiki.xenproject.org/wiki/Submitting_Xen_Project_Patches
Juergen
Quoted 1 line · Jürgen Groß
Uh, this is no regular patch.
I though by regular patch, you meants a plain diff -u
Quoted 1 line
You've sent correct patches before,
Yes, and it's very time-consuming. This is why I want to have it right the first time and not go through sevreral iterations with this protocol.
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 10 lines · Manuel Bouyer
Quoted 1 line · Jürgen Groß
Uh, this is no regular patch.
I though by regular patch, you meants a plain diff -u
Quoted 1 line
You've sent correct patches before,
Yes, and it's very time-consuming. This is why I want to have it right the first time and not go through sevreral iterations with this protocol.
Its not that hard if you have a proper git tree...
Juergen
Quoted 6 lines · Jürgen Groß
Quoted 5 lines · Manuel Bouyer
Quoted 1 line · Jürgen Groß
Uh, this is no regular patch.
I though by regular patch, you meants a plain diff -u
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
You've sent correct patches before,
Yes, and it's very time-consuming. This is why I want to have it right the first time and not go through sevreral iterations with this protocol.
Its not that hard if you have a proper git tree...
git is the problem actually. I'm not used to it ...
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --
Quoted 9 lines · Manuel Bouyer
Quoted 8 lines · Jürgen Groß
Quoted 7 lines · Manuel Bouyer
Quoted 1 line · Jürgen Groß
Uh, this is no regular patch.
I though by regular patch, you meants a plain diff -u
Quoted 1 line
You've sent correct patches before,
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Yes, and it's very time-consuming. This is why I want to have it right the first time and not go through sevreral iterations with this protocol.
Its not that hard if you have a proper git tree...
git is the problem actually. I'm not used to it ...
Not using git on a daily basis I really suggest to use stgit as an add.on:
https://wiki.xenproject.org/wiki/Managing_Xen_Patches_with_StGit
It makes handling multiple iterations of a patch rather easy.
Juergen
Quoted 6 lines · Jürgen Groß
Not using git on a daily basis I really suggest to use stgit as an add.on:
https://wiki.xenproject.org/wiki/Managing_Xen_Patches_with_StGit
It makes handling multiple iterations of a patch rather easy.
thanks. When I started, I looked at the wiki for instructions about patches, but didn't find any ...
Signature · 3 lines
Manuel Bouyer <[email protected]> NetBSD: 26 ans d'experience feront toujours la difference --