How is this BSD Auth error possible?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How is this BSD Auth error possible?

William Ahern-2
From /usr/src/lib/libc/gen/auth_subr.c. When using auth_userokay(3) I keep
getting the message "dup of backchannel: Bad file descriptor". Yet, I'm
puzzled how the condition could even occur (the last line is the only place
this message exists in the entire source tree):

                if (socketpair(PF_LOCAL, SOCK_STREAM, 0, pfd) < 0) {
                        syslog(LOG_ERR, "unable to create backchannel %m");
                        _warnx("internal resource failure");
                        goto fail;
                }

                switch (pid = fork()) {
                case -1:
                        syslog(LOG_ERR, "%s: %m", path);
                        _warnx("internal resource failure");
                        close(pfd[0]);
                        close(pfd[1]);
                        goto fail;
                case 0:
        #define COMM_FD 3
        #define AUTH_FD 4
                        if (dup2(pfd[1], COMM_FD) < 0)
                                err(1, "dup of backchannel");


I've duplicate the behavior, but only from within the same program which
exhibits this problem:

        do {
                int pfd[2];
                //if (0 != closefrom(4))
                //      err(1, "closefrom");

                if (0 > socketpair(PF_LOCAL, SOCK_STREAM, 0, pfd))
                        err(1, "socketpair");

                warnx("pfd[1]:%d", pfd[1]);

                // The following never fails (if uncommented)...
                //if (0 != close(3) || 0 != close(3) || 0 != close(3))
                // warnx("close(3)");

                if (0 > dup2(pfd[1], 3))
                        err(1, "dup2");
        } while(0);

        return 0 == auth_userokay(nam, 0, 0, pwd);


Now, what's interesting is that if I call "close(3)" multiple times, close
never fails. So, it seems that descriptor number 3 is somehow pinned down.
I'm baffled.

- Bill

Reply | Threaded
Open this post in threaded view
|

Re: How is this BSD Auth error possible?

Otto Moerbeek
On Fri, 1 Dec 2006, William Ahern wrote:

> >From /usr/src/lib/libc/gen/auth_subr.c. When using auth_userokay(3) I keep
> getting the message "dup of backchannel: Bad file descriptor". Yet, I'm
> puzzled how the condition could even occur (the last line is the only place
> this message exists in the entire source tree):
>
>        if (socketpair(PF_LOCAL, SOCK_STREAM, 0, pfd) < 0) {
>                syslog(LOG_ERR, "unable to create backchannel %m");
>                _warnx("internal resource failure");
>                goto fail;
>        }
>
>        switch (pid = fork()) {
>        case -1:
>                syslog(LOG_ERR, "%s: %m", path);
>                _warnx("internal resource failure");
>                close(pfd[0]);
>                close(pfd[1]);
>                goto fail;
>        case 0:
> #define COMM_FD 3
> #define AUTH_FD 4
>                if (dup2(pfd[1], COMM_FD) < 0)
>                        err(1, "dup of backchannel");
>
>
> I've duplicate the behavior, but only from within the same program which
> exhibits this problem:
>
> do {
> int pfd[2];
> //if (0 != closefrom(4))
> //      err(1, "closefrom");
>
> if (0 > socketpair(PF_LOCAL, SOCK_STREAM, 0, pfd))
> err(1, "socketpair");
>
> warnx("pfd[1]:%d", pfd[1]);
>
> // The following never fails (if uncommented)...
> //if (0 != close(3) || 0 != close(3) || 0 != close(3))
> // warnx("close(3)");
>
> if (0 > dup2(pfd[1], 3))
> err(1, "dup2");
> } while(0);
>
> return 0 == auth_userokay(nam, 0, 0, pwd);
>
>
> Now, what's interesting is that if I call "close(3)" multiple times, close
> never fails. So, it seems that descriptor number 3 is somehow pinned down.
> I'm baffled.

Interesting. Since you didn't post your whole program I have to
guess... It boils down to knowing what fd 3 is referring to.  Was this
fd openened with some special flags? Try to use fstat and/or ktrace to
get a clue what is going on.

        -Otto