[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#512657: bogl: mark internal-use file descriptors close-on-exec



Package: bogl
Version: 0.1.18-2
Severity: wishlist
Tags: patch
User: ubuntu-devel@lists.ubuntu.com
Usertags: origin-ubuntu ubuntu-patch jaunty

bterm leaks several of the file descriptors it uses internally to its
child process. This is messy and could potentially lead to some
interesting bugs. How does the attached patch look, which marks internal
file descriptors as close-on-exec? I think I've hit the right set of fds
here.

Thanks,

-- 
Colin Watson                                       [cjwatson@ubuntu.com]
  * Set many internal file descriptors close-on-exec, to avoid leaking them
    to the program called by bterm.

diff -Nru bogl-0.1.18/bogl-bgf.c bogl-0.1.18/bogl-bgf.c
--- bogl-0.1.18/bogl-bgf.c	2001-12-01 17:04:42.000000000 +0000
+++ bogl-0.1.18/bogl-bgf.c	2009-01-22 13:38:58.000000000 +0000
@@ -20,6 +20,9 @@
   if (fd == -1)
     return 0;
 
+  if (bogl_cloexec(fd) < 0)
+    return 0;
+
   if (fstat(fd, &buf))
     return 0;
 
diff -Nru bogl-0.1.18/bogl.c bogl-0.1.18/bogl.c
--- bogl-0.1.18/bogl.c	2006-10-20 21:11:19.000000000 +0100
+++ bogl-0.1.18/bogl.c	2009-01-22 13:40:39.000000000 +0000
@@ -124,12 +124,16 @@
     fb = open ("/dev/fb/0", O_RDWR);
   if (fb < 0)
     return bogl_fail ("opening /dev/fb0: %s", strerror (errno));
+  if (bogl_cloexec (fb) < 0)
+    return bogl_fail ("setting /dev/fb0 close-on-exec: %s", strerror (errno));
 
   tty = open ("/dev/tty0", O_RDWR);
   if (tty < 0)
     tty = open ("/dev/vc/0", O_RDWR);
   if (tty < 0)
     return bogl_fail ("opening /dev/tty0: %s", strerror (errno));
+  if (bogl_cloexec (tty) < 0)
+    return bogl_fail ("setting /dev/tty0 close-on-exec: %s", strerror (errno));
 
   if (-1 == ioctl (tty, VT_GETSTATE, &vts))
     return bogl_fail ("can't get VT state: %s", strerror (errno));
@@ -630,3 +634,19 @@
 
   return 0;
 }
+
+/* Set a file descriptor to close-on-exec. */
+int
+bogl_cloexec(int fd)
+{
+  int flags;
+
+  flags = fcntl (fd, F_GETFD);
+  if (flags < 0)
+    return flags;
+
+  if (fcntl (fd, F_SETFD, flags | FD_CLOEXEC) < 0)
+    return -1;
+
+  return 0;
+}
diff -Nru bogl-0.1.18/boglP.h bogl-0.1.18/boglP.h
--- bogl-0.1.18/boglP.h	2001-12-01 17:04:42.000000000 +0000
+++ bogl-0.1.18/boglP.h	2009-01-22 13:38:26.000000000 +0000
@@ -25,4 +25,6 @@
 
 int bogl_fail (const char *, ...);
 
+int bogl_cloexec (int fd);
+
 #endif /* boglP_h */
diff -Nru bogl-0.1.18/boml.c bogl-0.1.18/boml.c
--- bogl-0.1.18/boml.c	2005-09-21 18:15:33.000000000 +0100
+++ bogl-0.1.18/boml.c	2009-01-22 13:43:23.000000000 +0000
@@ -614,6 +614,10 @@
   fd = open ("/dev/gpmdata", O_RDONLY | O_NONBLOCK);
   if (fd < 0)
     return 0;
+  if (bogl_cloexec (fd) < 0) {
+    close (fd);
+    return 0;
+  }
   
   /* Poll the mouse whether or not we could find gpm, in
      case it starts up later; but keep searching in that case. */
@@ -640,6 +644,10 @@
   fd = open("/dev/input/mice", O_RDONLY | O_NONBLOCK);
   if(fd < 0)
     return;
+  if (bogl_cloexec(fd) < 0) {
+    close(fd);
+    return;
+  }
 
   add_mouse(T_PS2, fd);
 }
@@ -659,6 +667,10 @@
   fd = open ("/dev/psaux", O_RDWR | O_NONBLOCK);
   if (fd < 0)
     return;
+  if (bogl_cloexec (fd) < 0) {
+    close (fd);
+    return;
+  }
 
   write (fd, s2, sizeof s2);
   usleep (30000);
@@ -695,6 +707,10 @@
   int fd = open ("/dev/inportbm", O_RDONLY | O_NONBLOCK);
   if (fd < 0)
     return;
+  if (bogl_cloexec (fd) < 0) {
+    close (fd);
+    return;
+  }
 
   add_mouse (T_MS_BUS, fd);
 }
@@ -707,6 +723,10 @@
   int fd = open ("/dev/adbmouse", O_RDONLY | O_NONBLOCK);
   if (fd < 0)
     return;
+  if (bogl_cloexec (fd) < 0) {
+    close (fd);
+    return;
+  }
 
   add_mouse (T_ADB, fd);
 }
@@ -721,6 +741,10 @@
   int fd = open ("/dev/sunmouse", O_RDONLY | O_NONBLOCK);
   if (fd < 0)
     return;
+  if (bogl_cloexec (fd) < 0) {
+    close (fd);
+    return;
+  }
 
   add_mouse (T_SUN, fd);
 }
@@ -978,6 +1002,11 @@
   fd = open (port, O_RDWR | O_NONBLOCK);
   if (fd < 0)
     return fd;
+  if (bogl_cloexec (fd) < 0)
+    {
+      close (fd);
+      return -1;
+    }
 
   /* Reset file so it is no longer in non-blocking mode. */
   if (fcntl (fd, F_SETFL, 0) < 0)

Reply to: