Skip to contentSkip to conversation

tools/xl: Open xldevd.log with O_CLOEXEC

PATCH Superseded Andrew Cooper 2 participants 6 messages May 7, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xl: Open xldevd.log with O_CLOEXEC

xl devd has been observed leaking /var/log/xldevd.log into children.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Demi Marie Obenour <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>

Also entirely speculative based on the QubesOS ticket.

---
 tools/xl/xl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_utils.c +1 -1
index 17489d182954..060186db3a59 100644
@@ -270,7 +270,7 @@ int do_daemonize(const char *name, const char *pidfile)
         exit(-1);
     }
 
-    CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
+    CHK_SYSCALL(logfile = open(fullname, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644));
     free(fullname);
     assert(logfile >= 3);
 

base-commit: ebab808eb1bb8f24c7d0dd41b956e48cb1824b81
prerequisite-patch-id: 212e50457e9b6bdfd06a97da545a5aa7155bb919

Signature · 1 line
2.30.2
#1 lore raw
Quoted 21 lines · Andrew Cooper

xl devd has been observed leaking /var/log/xldevd.log into children.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Demi Marie Obenour <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>

Also entirely speculative based on the QubesOS ticket.

---
 tools/xl/xl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_utils.c +0 -0
index 17489d182954..060186db3a59 100644

tools/xl/xl_utils.c int do_daemonize(const char *name, const char *pidfile) · line 273

Quoted 5 lines
tools/xl/xl_utils.c +1 -1
@@ -270,7 +270,7 @@ int do_daemonize(const char *name, const char *pidfile)
         exit(-1);
     }
 
-    CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
+    CHK_SYSCALL(logfile = open(fullname, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644));

This one might be not enough, as the FD gets dup2()-ed to stdout/stderr just outside of the context here, and then inherited by various hotplug script. Just adding O_CLOEXEC here means the hotplug scripts will run with stdout/stderr closed. The scripts shipped with Xen do redirect stderr to a log quite early, but a) it doesn't do it for stdout, and b) custom hotplug scripts are a valid use case.
Without that, I see at least few potential issues:
- some log messages may be lost (minor, but annoying)
- something might simply fail on writing to a closed FD, breaking the hotplug script
- FD 1 will be used as first free FD for any open() or similar call - if a tool later tries writing something to stdout, it will gets written to that FD - worse of all three

What should be the behavior of hotplug scripts logging? Should they always take care of their own logging? If so, the hotplug calling part should redirect stdout/stderr to /dev/null IMO. But if xl should provide some default logging for them (like, the xldevd.log here?), then the O_CLOEXEC should be set only after duplicating logfile over stdout/err.

Quoted 6 lines
tools/xl/xl_utils.c +0 -0
     free(fullname);
     assert(logfile >= 3);
 

base-commit: ebab808eb1bb8f24c7d0dd41b956e48cb1824b81
prerequisite-patch-id: 212e50457e9b6bdfd06a97da545a5aa7155bb919

Which one is this? I don't see it in staging, nor in any of your branches on xenbits. Lore finds "tools/libxs: Open /dev/xen/xenbus fds as O_CLOEXEC" which I guess is correct, but I have no idea how it correlates it, as this hash doesn't appear anywhere in the message, nor its headers...

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#2 lore raw
Quoted 25 lines · Marek Marczykowski-Górecki
Quoted 24 lines · Andrew Cooper

xl devd has been observed leaking /var/log/xldevd.log into children.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Demi Marie Obenour <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>

Also entirely speculative based on the QubesOS ticket.

---
 tools/xl/xl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_utils.c +0 -0
index 17489d182954..060186db3a59 100644
@@ -270,7 +270,7 @@ int do_daemonize(const char *name, const char *pidfile)
         exit(-1);
     }
 
Quoted 6 lines

tools/xl/xl_utils.c int do_daemonize(const char *name, const char *pidfile) · line 273

Quoted 2 lines
tools/xl/xl_utils.c +1 -1
-    CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
+    CHK_SYSCALL(logfile = open(fullname, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644));

This one might be not enough, as the FD gets dup2()-ed to stdout/stderr just outside of the context here, and then inherited by various hotplug script. Just adding O_CLOEXEC here means the hotplug scripts will run with stdout/stderr closed.

Lovely :(  Yes - this won't work.  I guess what we want instead is:

tools/xl/xl_utils.c +0 -0
index 060186db3a59..a0ce7dd7fa21 100644
@@ -282,6 +282,7 @@ int do_daemonize(const char *name, const char *pidfile)

     dup2(logfile, 2);

     close(nullfd);
+    close(logfile);

     CHK_SYSCALL(daemon(0, 1));

which at least means there's not a random extra fd attached to the logfile.

Quoted 11 lines
Quoted 6 lines
tools/xl/xl_utils.c +0 -0
     free(fullname);
     assert(logfile >= 3);
 

base-commit: ebab808eb1bb8f24c7d0dd41b956e48cb1824b81
prerequisite-patch-id: 212e50457e9b6bdfd06a97da545a5aa7155bb919

Which one is this? I don't see it in staging, nor in any of your branches on xenbits. Lore finds "tools/libxs: Open /dev/xen/xenbus fds as O_CLOEXEC" which I guess is correct, but I have no idea how it correlates it, as this hash doesn't appear anywhere in the message, nor its headers...

It's the libxs patch, but rebased over yesterday's push to staging. auto-base doesn't work quite so well in cases like this.

~Andrew

#3 lore raw
Quoted 41 lines · Andrew Cooper
Quoted 25 lines · Marek Marczykowski-Górecki
Quoted 24 lines · Andrew Cooper

xl devd has been observed leaking /var/log/xldevd.log into children.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Demi Marie Obenour <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>

Also entirely speculative based on the QubesOS ticket.

---
 tools/xl/xl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_utils.c +0 -0
index 17489d182954..060186db3a59 100644
@@ -270,7 +270,7 @@ int do_daemonize(const char *name, const char *pidfile)
         exit(-1);
     }
 
Quoted 6 lines

tools/xl/xl_utils.c int do_daemonize(const char *name, const char *pidfile) · line 273

Quoted 2 lines
tools/xl/xl_utils.c +1 -1
-    CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
+    CHK_SYSCALL(logfile = open(fullname, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644));

This one might be not enough, as the FD gets dup2()-ed to stdout/stderr just outside of the context here, and then inherited by various hotplug script. Just adding O_CLOEXEC here means the hotplug scripts will run with stdout/stderr closed.

Lovely :(  Yes - this won't work.  I guess what we want instead is:

tools/xl/xl_utils.c +0 -0
index 060186db3a59..a0ce7dd7fa21 100644
@@ -282,6 +282,7 @@ int do_daemonize(const char *name, const char *pidfile)
      dup2(logfile, 2);
Quoted 7 lines


     close(nullfd);
+    close(logfile);

     CHK_SYSCALL(daemon(0, 1));

which at least means there's not a random extra fd attached to the logfile.

But logfile is a global variable, and it looks to be used in dolog()...

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#4 lore raw
Quoted 35 lines · Marek Marczykowski-Górecki
Quoted 34 lines · Andrew Cooper
Quoted 25 lines · Marek Marczykowski-Górecki
Quoted 24 lines · Andrew Cooper

xl devd has been observed leaking /var/log/xldevd.log into children.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Demi Marie Obenour <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>

Also entirely speculative based on the QubesOS ticket.

---
 tools/xl/xl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_utils.c +0 -0
index 17489d182954..060186db3a59 100644
@@ -270,7 +270,7 @@ int do_daemonize(const char *name, const char *pidfile)
         exit(-1);
     }
 
Quoted 6 lines

tools/xl/xl_utils.c int do_daemonize(const char *name, const char *pidfile) · line 273

Quoted 2 lines
tools/xl/xl_utils.c +1 -1
-    CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
+    CHK_SYSCALL(logfile = open(fullname, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644));

This one might be not enough, as the FD gets dup2()-ed to stdout/stderr just outside of the context here, and then inherited by various hotplug script. Just adding O_CLOEXEC here means the hotplug scripts will run with stdout/stderr closed.

Lovely :(  Yes - this won't work.  I guess what we want instead is:

Quoted 14 lines
Quoted 6 lines
tools/xl/xl_utils.c +0 -0
index 060186db3a59..a0ce7dd7fa21 100644
@@ -282,6 +282,7 @@ int do_daemonize(const char *name, const char *pidfile)
      dup2(logfile, 2);
Quoted 7 lines


     close(nullfd);
+    close(logfile);

     CHK_SYSCALL(daemon(0, 1));

which at least means there's not a random extra fd attached to the logfile.

But logfile is a global variable, and it looks to be used in dolog()...

Urgh, fine.  Lets go back to your suggestion of setting CLOEXEC after dup()ing.

~Andrew

#5 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 25 lines · Marek Marczykowski-Górecki
Quoted 24 lines · Andrew Cooper

xl devd has been observed leaking /var/log/xldevd.log into children.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Demi Marie Obenour <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>

Also entirely speculative based on the QubesOS ticket.

---
 tools/xl/xl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_utils.c +0 -0
index 17489d182954..060186db3a59 100644
@@ -270,7 +270,7 @@ int do_daemonize(const char *name, const char *pidfile)
         exit(-1);
     }
 
Quoted 16 lines

tools/xl/xl_utils.c int do_daemonize(const char *name, const char *pidfile) · line 273

Quoted 2 lines
tools/xl/xl_utils.c +1 -1
-    CHK_SYSCALL(logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644));
+    CHK_SYSCALL(logfile = open(fullname, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644));

This one might be not enough, as the FD gets dup2()-ed to stdout/stderr just outside of the context here, and then inherited by various hotplug script. Just adding O_CLOEXEC here means the hotplug scripts will run with stdout/stderr closed. The scripts shipped with Xen do redirect stderr to a log quite early, but a) it doesn't do it for stdout, and b) custom hotplug scripts are a valid use case.
Without that, I see at least few potential issues:
- some log messages may be lost (minor, but annoying)
- something might simply fail on writing to a closed FD, breaking the hotplug script
- FD 1 will be used as first free FD for any open() or similar call - if a tool later tries writing something to stdout, it will gets written to that FD - worse of all three

Wait, the above is wrong, dup does not copy the O_CLOEXEC flag over to the new FD. So, maybe your patch is correct after all.

Quoted 23 lines

What should be the behavior of hotplug scripts logging? Should they always take care of their own logging? If so, the hotplug calling part should redirect stdout/stderr to /dev/null IMO. But if xl should provide some default logging for them (like, the xldevd.log here?), then the O_CLOEXEC should be set only after duplicating logfile over stdout/err.

Quoted 6 lines
tools/xl/xl_utils.c +0 -0
     free(fullname);
     assert(logfile >= 3);
 

base-commit: ebab808eb1bb8f24c7d0dd41b956e48cb1824b81
prerequisite-patch-id: 212e50457e9b6bdfd06a97da545a5aa7155bb919

Which one is this? I don't see it in staging, nor in any of your branches on xenbits. Lore finds "tools/libxs: Open /dev/xen/xenbus fds as O_CLOEXEC" which I guess is correct, but I have no idea how it correlates it, as this hash doesn't appear anywhere in the message, nor its headers...

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab