Skip to contentSkip to conversation

xl: close nullfd after dup2'ing it to stdin

PATCH Superseded Ian Campbell 4 participants 9 messages Feb 16, 2016 – Mar 1, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xl: close nullfd after dup2'ing it to stdin

Taking care not to do so if nullfd happens (somehow) to have the same fd number as stdin/out/err.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Compile tested only.

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.html

---
 tools/libxl/xl_cmdimpl.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/libxl/xl_cmdimpl.c +10 -0
index d07ccb2..f38e3dd 100644
@@ -505,6 +505,16 @@ static int do_daemonize(char *name, const char *pidfile)
     dup2(logfile, 1);
     dup2(logfile, 2);
 
+    /* Close nullfd unless it happens to == std{in,out,err} */
+    switch (nullfd) {
+    case 0:
+    case 1:
+    case 2:
+        break;
+    default:
+        close(nullfd);
+    }
+
     CHK_SYSCALL(daemon(0, 1));
 
     if (pidfile) {
Signature · 1 line
2.1.4
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Ian Campbell

Taking care not to do so if nullfd happens (somehow) to have the same fd number as stdin/out/err.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Quoted 6 lines

Compile tested only.

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.html

Quoted 28 lines
---
 tools/libxl/xl_cmdimpl.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/libxl/xl_cmdimpl.c +10 -0
index d07ccb2..f38e3dd 100644
@@ -505,6 +505,16 @@ static int do_daemonize(char *name, const char *pidfile)
     dup2(logfile, 1);
     dup2(logfile, 2);
 
+    /* Close nullfd unless it happens to == std{in,out,err} */
+    switch (nullfd) {
+    case 0:
+    case 1:
+    case 2:
+        break;
+    default:
+        close(nullfd);
+    }
+
     CHK_SYSCALL(daemon(0, 1));
 
     if (pidfile) {
Signature · 1 line
2.1.4
#2 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[PATCH] xl: close nullfd after dup2'ing it to stdin"):

Quoted 2 lines

Taking care not to do so if nullfd happens (somehow) to have the same fd number as stdin/out/err.

I think that can only happen if the program (the process) has a serious problem: ie, fd 0 1 or 2 would have to be closed. If that happens many other things can go badly wrong.

If this is causing Coverity to complain I would suggest adding
assert(nullfd >= 3);
assert(logfile >= 3);
instead.

Ian.

#3 lore raw
Quoted 8 lines · Ian Jackson

Ian Campbell writes ("[PATCH] xl: close nullfd after dup2'ing it to stdin"):

Quoted 1 line

Taking care not to do so if nullfd happens (somehow) to have the

same

Quoted 1 line

fd number as stdin/out/err.

I think that can only happen if the program (the process) has a serious problem: ie, fd 0 1 or 2 would have to be closed.

Yes, that was my thought and what I wanted to guard against.

Quoted 1 line

If that happens many other things can go badly wrong.

Indeed. I've seen this happen in other scenarios with non-C programs forking and execing stuff with stdio fds closed.

Quoted 4 lines

If this is causing Coverity to complain I would suggest adding

   assert(nullfd >= 3);
   assert(logfile >= 3);

instead.

Coverity wasn't complaining about this particular aspect, it was only complaining about the leak of nullfd, avoiding stdin/out/err was just me being belt and braces about the possibility of nullfd being one of the stdio fds. I'm happy with the assert approach too.

Ian

#4 lore raw

PATCH v2 1/1 xl: close nullfd after dup2'ing it to stdin

We assert that nullfd if not std{in,out,err} since that would result in closing one of the just dup2'd fds. For this to happen std{in,out,err} would have needed to be closed, at which point all sorts of other things could go wrong.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Compile tested only.

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.html

---
v2: Assert logfile and nullfd are not stdio fds

---
 tools/libxl/xl_cmdimpl.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/libxl/xl_cmdimpl.c +5 -0
index 9958d8a..a377de1 100644
@@ -499,12 +499,17 @@ static int do_daemonize(char *name, const char *pidfile)
 
     CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
     free(fullname);
+    assert(logfile >= 3);
 
     CHK_SYSCALL(nullfd = open("/dev/null", O_RDONLY));
+    assert(nullfd >= 3);
+
     dup2(nullfd, 0);
     dup2(logfile, 1);
     dup2(logfile, 2);
 
+    close(nullfd);
+
     CHK_SYSCALL(daemon(0, 1));
 
     if (pidfile) {
Signature · 1 line
2.1.4
#5 lore raw
Quoted 10 lines · Ian Campbell

We assert that nullfd if not std{in,out,err} since that would result in closing one of the just dup2'd fds. For this to happen std{in,out,err} would have needed to be closed, at which point all sorts of other things could go wrong.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Quoted 7 lines

Compile tested only.

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.
html

ping?

Quoted 22 lines

---
v2: Assert logfile and nullfd are not stdio fds
---
 tools/libxl/xl_cmdimpl.c | 5 +++++

 1 file changed, 5 insertions(+)
tools/libxl/xl_cmdimpl.c +0 -0
index 9958d8a..a377de1 100644
@@ -499,12 +499,17 @@ static int do_daemonize(char *name, const char
 *pidfile)


     CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
     free(fullname);
+    assert(logfile >= 3);

     CHK_SYSCALL(nullfd = open("/dev/null", O_RDONLY));
+    assert(nullfd >= 3);
+
     dup2(nullfd, 0);

Quoted 8 lines

     dup2(logfile, 1);
     dup2(logfile, 2);

+    close(nullfd);
+
     CHK_SYSCALL(daemon(0, 1));

     if (pidfile) {

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw
Quoted 13 lines · Ian Campbell
Quoted 12 lines · Ian Campbell

We assert that nullfd if not std{in,out,err} since that would result in closing one of the just dup2'd fds. For this to happen std{in,out,err} would have needed to be closed, at which point all sorts of other things could go wrong.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Compile tested only.

Quoted 7 lines
Quoted 5 lines

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.
html

ping?

Ian, you wouldn't have a git branch with all your outstanding patches you had posted somewhere?

Just in case we don't get to them done by feature freeze window and somebody starts replaying these patches..

Quoted 28 lines
Quoted 28 lines

---
v2: Assert logfile and nullfd are not stdio fds
---
 tools/libxl/xl_cmdimpl.c | 5 +++++

 1 file changed, 5 insertions(+)
tools/libxl/xl_cmdimpl.c +0 -0
index 9958d8a..a377de1 100644
@@ -499,12 +499,17 @@ static int do_daemonize(char *name, const char
 *pidfile)


     CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
     free(fullname);
+    assert(logfile >= 3);

     CHK_SYSCALL(nullfd = open("/dev/null", O_RDONLY));
+    assert(nullfd >= 3);
+
     dup2(nullfd, 0);
     dup2(logfile, 1);
     dup2(logfile, 2);

+    close(nullfd);
+

Quoted 8 lines
Quoted 3 lines

     CHK_SYSCALL(daemon(0, 1));

     if (pidfile) {

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw
Quoted 20 lines · Konrad Rzeszutek Wilk
Quoted 19 lines · Ian Campbell
Quoted 17 lines · Ian Campbell

We assert that nullfd if not std{in,out,err} since that would result in closing one of the just dup2'd fds. For this to happen std{in,out,err} would have needed to be closed, at which point all sorts of other things could go wrong.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Compile tested only.

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.
html

Quoted 8 lines
Quoted 1 line

ping?

Ian, you wouldn't have a git branch with all your outstanding patches you had posted somewhere?

Just in case we don't get to them done by feature freeze window and somebody starts replaying these patches..

Bug fixes are allowed to go in even after the freeze.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by on v2 1/1
#8 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[PATCH v2] xl: close nullfd after dup2'ing it to stdin"):

Quoted 10 lines

We assert that nullfd if not std{in,out,err} since that would result in closing one of the just dup2'd fds. For this to happen std{in,out,err} would have needed to be closed, at which point all sorts of other things could go wrong.

CID: 1130519

It was previously hypothesised[0] that fixing 1130516 would solve this too, but that appears to not have been the case.

Quoted 8 lines

Compile tested only.

[0] http://lists.xenproject.org/archives/html/xen-devel/2013-11/msg02931.html

---
v2: Assert logfile and nullfd are not stdio fds

(The copy to ijc may bounce I guess...)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel