[PATCH 2/2] Xinit: close stdin to avoid leak of file descriptior to the Xorg session.

Jeremy Huddleston jeremyhu at apple.com
Mon Jul 25 17:29:05 PDT 2011


What's the point of setsid(2)?  It certainly doesn't fall under the title of the patch.

Also, you don't need:
+         close (STDIN_FILENO);

That is done for you by dup2(2).  Also doing so could result in a problem if this code were used in a multithreaded application.  If another thread called open(2) between the close(2) and the dup2(2), it would get STDIN_FILENO as its fd and you would then close it with your dup2(2).

From dup(2)'s man page:
     In dup2(), the value of the new descriptor fildes2 is specified.  If
     fildes and fildes2 are equal, then dup2() just returns fildes2; no other
     changes are made to the existing descriptor.  Otherwise, if descriptor
     fildes2 is already in use, it is first deallocated as if a close(2) call
     had been done first.


On Jul 25, 2011, at 16:52, Matěj Cepl wrote:

> Signed-off-by: Matěj Cepl <mcepl at redhat.com>
> ---
> xinit.c |   14 +++++++++++++-
> 1 files changed, 13 insertions(+), 1 deletions(-)
> 
> diff --git a/xinit.c b/xinit.c
> index 42ff008..7f56aab 100644
> --- a/xinit.c
> +++ b/xinit.c
> @@ -91,6 +91,8 @@ char xserverrcbuf[256];
> 
> #define TRUE 1
> #define FALSE 0
> +#define OK_EXIT 0
> +#define ERR_EXIT 1
> 
> static char *default_server = "X";
> static char *default_display = ":0";        /* choose most efficient */
> @@ -561,6 +563,7 @@ startClient(char *client[])
> {
>     clientpid = fork();
>     if (clientpid == 0) {
> +         int fd;
>         set_environment();
>         setWindowPath();
> 
> @@ -568,7 +571,16 @@ startClient(char *client[])
>             Error("cannot change uid");
>             _exit(EXIT_FAILURE);
>         }
> -        setpgid(0, getpid());
> +         fd = open ("/dev/null", O_RDONLY);
> +
> +         if (fd < 0) {
> +             Error("cannot open /dev/null: %s\n", strerror(errno));
> +             _exit(ERR_EXIT);
> +         }
> +         close (STDIN_FILENO);
> +         dup2 (fd, STDIN_FILENO);
> +         close (fd);
> +         setsid();
>         Execute(client);
>         Error("Unable to run program \"%s\"", client[0]);
> 
> -- 
> 1.7.6
> 
> _______________________________________________
> xorg-devel at lists.x.org: X.Org development
> Archives: http://lists.x.org/archives/xorg-devel
> Info: http://lists.x.org/mailman/listinfo/xorg-devel



More information about the xorg-devel mailing list