Skip to contentSkip to conversation

tools/xl: Open xldevd.log with O_CLOEXEC

PATCH In review Andrew Cooper 6 participants 9 messages Jun 21, 2024 – Jun 24, 2024

lore
Share & tools
lore
#0 lore raw

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

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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);
 
Signature · 1 line
2.39.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

Quoted 34 lines

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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);
 
Signature · 1 line
2.39.2
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
Acked-by on v2 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 32 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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));

Everytime we use O_CLOEXEC, we add in the C file

    #ifndef O_CLOEXEC
    #define O_CLOEXEC 0
    #endif

we don't need to do that anymore?
Or I guess we'll see if someone complain when they try to build on an ancien version of Linux.

Thanks,

--

Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#3 lore raw
Quoted 39 lines · Anthony PERARD
Quoted 32 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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));

Everytime we use O_CLOEXEC, we add in the C file

Quoted 8 lines
    #ifndef O_CLOEXEC
    #define O_CLOEXEC 0
    #endif

we don't need to do that anymore?
Or I guess we'll see if someone complain when they try to build on an ancien version of Linux.

Thanks.  I did originally have that ifdefary here, but then I noticed that this isn't the first instance like this in xl, and I'm going to be using that as a justification soon to explicitly drop support for Linux < 2.6.23.

~Andrew

#4 lore raw
Quoted 47 lines · Andrew Cooper
Quoted 46 lines · Anthony PERARD
Quoted 32 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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));

Everytime we use O_CLOEXEC, we add in the C file

    #ifndef O_CLOEXEC
    #define O_CLOEXEC 0
    #endif

we don't need to do that anymore?
Or I guess we'll see if someone complain when they try to build on an ancien version of Linux.

Quoted 6 lines
Quoted 1 line

Thanks.  I did originally have that ifdefary here, but then I noticed that this isn't the first instance like this in xl, and I'm going to be using that as a justification soon to explicitly drop support for Linux < 2.6.23.

Just to mention that this is a two fold thing: I surely don't try to run up-to-date Xen on top of this old a Linux kernel, but what is used for building is still what the distro (with a very old kernel) would have put there.

Jan

#5 lore raw
In reply to Anthony PERARD
Quoted 40 lines · Anthony PERARD
Quoted 32 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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));

Everytime we use O_CLOEXEC, we add in the C file

Quoted 6 lines
    #ifndef O_CLOEXEC
    #define O_CLOEXEC 0
    #endif

we don't need to do that anymore?
Or I guess we'll see if someone complain when they try to build on an ancien version of Linux.

I'm pretty certain I'll run into that issue on one of my pretty old systems, but if the general view is that we don't care about such environments anymore, then so be it (and I'll take care of such issues locally).

Jan

#6 lore raw
In reply to Anthony PERARD
Quoted 39 lines · Anthony PERARD
Quoted 32 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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));

Everytime we use O_CLOEXEC, we add in the C file

Quoted 6 lines
    #ifndef O_CLOEXEC
    #define O_CLOEXEC 0
    #endif

we don't need to do that anymore?
Or I guess we'll see if someone complain when they try to build on an ancien version of Linux.

I double checked, and it turns out it was an FD_CLOEXEC, not an O_CLOEXEC which xl was using, so we don't have a pre-existing example.

Therefore I've re-added this at the top of xl_utils.c

~Andrew

Reviewed-by on v2 1/1
#7 lore raw
In reply to Andrew Cooper
Quoted 32 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so after setting up stdout/stderr, it's only the logfile fd which will close on exec().

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

Also entirely speculative based on the QubesOS ticket.

v2:
* Extend the commit message to explain why stdout/stderr aren't closed by this change

For 4.19. This bugfix was posted earlier, but fell between the cracks.

---
 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 275

Quoted 7 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));
     free(fullname);
     assert(logfile >= 3);

Definitely an improvement. I would also add O_NOCTTY to work around a particularly unfortunate Linux kernel design decision, but that can either be fixed up on commit or be a separate patch.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#8 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

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

Note this is specifically safe; dup2() leaves O_CLOEXEC disabled on newfd, so
after setting up stdout/stderr, it's only the logfile fd which will close on
exec().

Quoted 3 lines

~ Oleksii

Quoted 36 lines

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

Also entirely speculative based on the QubesOS ticket.

v2:
 * Extend the commit message to explain why stdout/stderr aren't closed by
   this change

For 4.19.  This bugfix was posted earlier, but fell between the cracks.
---
 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);
      }


-    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);