On Wed, Sep 02, 2009 at 07:10:35PM +0200, Vladimir Kotal wrote:
>   http://cr.opensolaris.org/~vkotal/daemon_libc-4471189.onnv/

 - Could you make daemon() replace fildes 0, 1 and 2 the way the ssh
   version (from OpenBSD) did it: open /dev/null, dup2() into 0, 1 and
   2, then close the /dev/null fildes.  Also, please check for errors
   when opening /dev/null for this.

   Something like:

        if (noclose == 0) {
                int fd = open("/dev/null", O_RDWR, 0);

                if (fd == -1)
                        return (-1);  /* XXX or maybe 0 would be fine too */

                (void) dup2(fd, STDIN_FILENO);
                (void) dup2(fd, STDOUT_FILENO);
                (void) dup2(fd, STDERR_FILENO);

                if (fd > STDERR_FILENO)
                        (void) close(fd);
        }
        return (0);

   or:

        int fd;
        ...
        if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {

                (void) dup2(fd, STDIN_FILENO);
                (void) dup2(fd, STDOUT_FILENO);
                (void) dup2(fd, STDERR_FILENO);

                if (fd > STDERR_FILENO)
                        (void) close(fd);
        }
        return (0);

   Yes, this is paranoia.  An error opening /dev/null will likely be
   very fatal elsewhere, but it shouldn't be allowed to cause security
   issues _here_.

 - Also, Solaris Kerberos code (from MIT krb5) has a daemon() too:

   $SRC/lib/gss_mechs/mech_krb5/krb5/posix/daemon.c

Aside: interesting that sshd's daemon() didn't fork() again after
setsid().

Nico
-- 

Reply via email to