virtfs-proxy-helper: Fix a resource leak in main()
PATCH 1/1 virtfs-proxy-helper: Fix a resource leak in main()
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed. When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of main().
--- fsdev/virtfs-proxy-helper.c | 3 +++ 1 file changed, 3 insertions(+)
fsdev/virtfs-proxy-helper.c +3 -0
@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name) { +close (sock); + }g_free (sock_name);do_log (LOG_INFO ,"Done\n" );closelog ();
Signature · 1 line
2.19.1
Quoted 17 lines · Alex Chen <[email protected]> 于2020年11月26日周四 下午6:29写道:
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed. When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Reported-by Euler Robot <[email protected]>Signed-off-by Alex Chen <[email protected]>--- fsdev/virtfs-proxy-helper.c | 3 +++ 1 file changed, 3 insertions(+)fsdev/virtfs-proxy-helper.c +0
-0
fsdev/virtfs-proxy-helper.c int main(int argc, char **argv) · line 1159
Quoted 6 lines
fsdev/virtfs-proxy-helper.c +3
-0@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name) { +close (sock); + }
If 'proxy_socket' failed, you call close(-1).
Maybe following is better?
if (sock >= 0) {
close(sock);
}Thanks,
Li Qiang
Quoted 7 lines
fsdev/virtfs-proxy-helper.c +0
-1g_free (sock_name);do_log (LOG_INFO ,"Done\n" );closelog (); --2.19.1
Quoted 27 lines · Li Qiang
Alex Chen <[email protected]>
Quoted 17 lines
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed. When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Reported-by Euler Robot <[email protected]>Signed-off-by Alex Chen <[email protected]>--- fsdev/virtfs-proxy-helper.c | 3 +++ 1 file changed, 3 insertions(+)fsdev/virtfs-proxy-helper.c +0
-0fsdev/virtfs-proxy-helper.c int main(int argc, char **argv) · line 1159
Quoted 6 lines
fsdev/virtfs-proxy-helper.c +3
-0@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name) { +close (sock); + }If '
proxy_socket' failed, you call close(-1).
Quoted 6 lines
Maybe following is better?
if (sock >= 0) { close(sock); }
Hi Qiang,
Thanks for your review.
The 'sock' need to be closed only when option -s is used, that is when 'sock_name' is not NULL. So maybe the following is better?
fsdev/virtfs-proxy-helper.c +3 -0
@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name && (sock >=0 )) { +close (sock); + }g_free (sock_name);do_log (LOG_INFO ,"Done\n" );closelog ();
Thanks,
Alex
Quoted 36 lines · Alex Chen <[email protected]> 于2020年11月26日周四 下午7:40写道:
Quoted 27 lines · Li Qiang
Alex Chen <[email protected]>
Quoted 17 lines
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed. When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Reported-by Euler Robot <[email protected]>Signed-off-by Alex Chen <[email protected]>--- fsdev/virtfs-proxy-helper.c | 3 +++ 1 file changed, 3 insertions(+)fsdev/virtfs-proxy-helper.c +0
-0fsdev/virtfs-proxy-helper.c int main(int argc, char **argv) · line 1159
Quoted 6 lines
fsdev/virtfs-proxy-helper.c +3
-0@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name) { +close (sock); + }If '
proxy_socket' failed, you call close(-1).Quoted 6 lines
Maybe following is better?
if (sock >= 0) { close(sock); }
Quoted 5 lines
Hi Qiang,
Thanks for your review.
The 'sock' need to be closed only when option -s is used, that is when 'sock_name' is not NULL. So maybe the following is better?
Yes, you're right.
fsdev/virtfs-proxy-helper.c int main(int argc, char **argv) · line 1157
Quoted 10 lines
fsdev/virtfs-proxy-helper.c +1
-0@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name && (sock >=0 )) {
No need parenthesis for 'sock>=0'?
Thanks,
Li Qiang
Quoted 9 lines
fsdev/virtfs-proxy-helper.c +2
-0+close (sock); + }g_free (sock_name);do_log (LOG_INFO ,"Done\n" );closelog ();Thanks,
Alex
Quoted 2 lines · On Thu, 26 Nov 2020 10:16:24 +0000 Alex Chen <[email protected]> wrote:
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed.
The process running virtfs-proxy-helper has its own copy of the fd inherited from its parent. And this fd will be closed eventually when the process terminates.
Quoted 3 lines
When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().
Same here, the new socket fd is closed when the process terminates.
The only justification to merge such a change would be if the code was sitting in some other function, in which case we should indeed do proper rollback. But it is main() here, so this patch isn't needed.
Quoted 1 line
Reported-by Euler Robot <[email protected]>
Can you provide a copy of the report in case I'm missing something ?
Quoted 19 lines
Signed-off-by Alex Chen <[email protected]>--- fsdev/virtfs-proxy-helper.c | 3 +++ 1 file changed, 3 insertions(+)fsdev/virtfs-proxy-helper.c +3
-0@@ -1154,6 +1154,9 @@ int main(int argc, char **argv)process_requests (sock); error:g_free (rpath); +if (sock_name) { +close (sock); + }g_free (sock_name);do_log (LOG_INFO ,"Done\n" );closelog ();
Hi Greg,
Thanks for your review.
Quoted 10 lines · Greg Kurz
Quoted 2 lines · On Thu, 26 Nov 2020 10:16:24 +0000 Alex Chen <[email protected]> wrote:
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed.
The process running virtfs-proxy-helper has its own copy of the fd inherited from its parent. And this fd will be closed eventually when the process terminates.
Quoted 6 lines
Quoted 3 lines
When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Same here, the new socket fd is closed when the process terminates.
IMO, it's best to explicitly release resources before the process terminates, just as the variable 'rpath' is explicitly freed in main(),
so socket fd also needs to be explicitly closed here.
Looking forward to your reply.
Quoted 11 lines
The only justification to merge such a change would be if the code was sitting in some other function, in which case we should indeed do proper rollback. But it is
main()here, so this patch isn't needed.Quoted 1 line
Reported-by Euler Robot <[email protected]>Can you provide a copy of the report in case I'm missing something ?
Our codecheck tool reports a resource leak here, which is relatively simple, like the one below, I did not attach it.
---------------------
"Resource leak: sock"
---------------------
Thanks,
Alex
Quoted 10 lines · On Donnerstag, 26. November 2020 14:15:51 CET Alex Chen wrote:
Hi Greg,
Thanks for your review.
Quoted 5 lines · Greg Kurz
On Thu, 26 Nov 2020 10:16:24 +0000
Quoted 2 lines · Alex Chen
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed.
So somebody is really using the 9p proxy driver for something; interesting.
Quoted 10 lines
Quoted 10 lines
The process running virtfs-proxy-helper has its own copy of the fd inherited from its parent. And this fd will be closed eventually when the process terminates.
Quoted 2 lines
When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Same here, the new socket fd is closed when the process terminates.
Does it? I haven't reviewed much of the 9p proxy code yet, however if chroot() fails for instance, the fd would leak right now, wouldn't it?
Or was your argument that it's the OS's job to free any file descriptor automatically on process terminations in general?
Quoted 16 lines
IMO, it's best to explicitly release resources before the process terminates, just as the variable 'rpath' is explicitly freed in
main(), so socket fd also needs to be explicitly closed here.Looking forward to your reply.
Quoted 9 lines
The only justification to merge such a change would be if the code was sitting in some other function, in which case we should indeed do proper rollback. But it is
main()here, so this patch isn't needed.Quoted 1 line
Reported-by Euler Robot <[email protected]>Can you provide a copy of the report in case I'm missing something ?
Quoted 6 lines
Our codecheck tool reports a resource leak here, which is relatively simple, like the one below, I did not attach it.
---------------------
"Resource leak: sock"
---------------------
Yeah, not very helpful that output.
Quoted 3 lines
Thanks,
Alex
Best regards,
Christian Schoenebeck
Quoted 19 lines · On Thu, 26 Nov 2020 18:52:39 +0100 Christian Schoenebeck…
Quoted 10 lines · On Donnerstag, 26. November 2020 14:15:51 CET Alex Chen wrote:
Hi Greg,
Thanks for your review.
Quoted 5 lines · Greg Kurz
On Thu, 26 Nov 2020 10:16:24 +0000
Quoted 2 lines · Alex Chen
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed.
So somebody is really using the 9p proxy driver for something; interesting.
Quoted 5 lines
Quoted 5 lines
The process running virtfs-proxy-helper has its own copy of the fd inherited from its parent. And this fd will be closed eventually when the process terminates.
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Same here, the new socket fd is closed when the process terminates.
Does it? I haven't reviewed much of the 9p proxy code yet, however if
chroot()fails for instance, the fd would leak right now, wouldn't it?
This is done just at the end of main()... the leak won't last long.
Quoted 3 lines
Or was your argument that it's the OS's job to free any file descriptor automatically on process terminations in general?
That's exactly my point.
The only justification that'd deserve to be in the changelog of such a patch is something like "because this is good practice to rollback in case code moves to another function than main()".
Quoted 16 lines
Quoted 16 lines
IMO, it's best to explicitly release resources before the process terminates, just as the variable 'rpath' is explicitly freed in
main(), so socket fd also needs to be explicitly closed here.Looking forward to your reply.
Quoted 9 lines
The only justification to merge such a change would be if the code was sitting in some other function, in which case we should indeed do proper rollback. But it is
main()here, so this patch isn't needed.Quoted 1 line
Reported-by Euler Robot <[email protected]>Can you provide a copy of the report in case I'm missing something ?
Quoted 9 lines
Quoted 6 lines
Our codecheck tool reports a resource leak here, which is relatively simple, like the one below, I did not attach it.
---------------------
"Resource leak: sock"
---------------------Yeah, not very helpful that output.
Indeed :D
Quoted 8 lines
Quoted 3 lines
Thanks,
AlexBest regards,
Christian Schoenebeck
Quoted 33 lines · On Donnerstag, 26. November 2020 19:27:19 CET Greg Kurz wrote:
On Thu, 26 Nov 2020 18:52:39 +0100
Quoted 19 lines · Christian Schoenebeck
Quoted 10 lines · On Donnerstag, 26. November 2020 14:15:51 CET Alex Chen wrote:
Hi Greg,
Thanks for your review.
Quoted 5 lines · Greg Kurz
On Thu, 26 Nov 2020 10:16:24 +0000
Quoted 2 lines · Alex Chen
Only one of the options -s and -f can be used. When -f is used, the fd is created externally and does not need to be closed.
So somebody is really using the 9p proxy driver for something; interesting.
Quoted 4 lines
Quoted 4 lines
The process running virtfs-proxy-helper has its own copy of the fd inherited from its parent. And this fd will be closed eventually when the process terminates.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
When -s is used, a new socket fd is created, and this socket fd needs to be closed at the end of
main().Same here, the new socket fd is closed when the process terminates.
Does it? I haven't reviewed much of the 9p proxy code yet, however if
chroot()fails for instance, the fd would leak right now, wouldn't it?This is done just at the end of
main()... the leak won't last long.
Quoted 8 lines
Quoted 2 lines
Or was your argument that it's the OS's job to free any file descriptor automatically on process terminations in general?
That's exactly my point.
The only justification that'd deserve to be in the changelog of such a patch is something like "because this is good practice to rollback in case code moves to another function than
main()".
Well, the actual motivation was rather a pragmatic one: to shut up a sanitizer's false positive, which I can understand.
Another option would be using a global variable for the fd instead of a temporary on stack. That should shut up the sanitizer as well and would not introduce change to the program flow.
I leave that up to Greg to decide whether or not to handle this. I'm Switzerland on this one.
Best regards,
Christian Schoenebeck
On Thu, 26 Nov 2020 19:44:24 +0100
Christian Schoenebeck <[email protected]> wrote:
[...]
Quoted 7 lines
Quoted 3 lines
The only justification that'd deserve to be in the changelog of such a patch is something like "because this is good practice to rollback in case code moves to another function than
main()".Well, the actual motivation was rather a pragmatic one: to shut up a sanitizer's false positive, which I can understand.
Yes, this should also be mentioned in the changelog.
Quoted 4 lines
Another option would be using a global variable for the fd instead of a temporary on stack. That should shut up the sanitizer as well and would not introduce change to the program flow.
Using the same sock variable for an fd that is either passed to us or that we create is a very poor programming choice actually... :(
So if the motivation is just to make "Euler Robot" happy and this can be addressed as you suggest, I personally prefer that rather than piling up fixes on broken code.
Quoted 3 lines
I leave that up to Greg to decide whether or not to handle this. I'm Switzerland on this one.
This won't go into QEMU 5.2 anyway since we only merge fixes for critical bugs or regressions at this point. No hurry to decide anything now :)
Quoted 4 lines
Best regards,
Christian Schoenebeck