What is the purpose of the call to access() in localtime.c?

Jonathan Leffler jonathan.leffler at gmail.com
Mon Dec 3 04:40:15 UTC 2007


In localtime.c, there is the following stanza of code that opens a time zone
file to file descriptor fid:

    {
        register int    doaccess;
        /*
        ** Section 4.9.1 of the C standard says that
        ** "FILENAME_MAX expands to an integral constant expression
        ** that is the size needed for an array of char large enough
        ** to hold the longest file name string that the implementation
        ** guarantees can be opened."
        */
        char        fullname[FILENAME_MAX + 1];

        if (name[0] == ':')
            ++name;
        doaccess = name[0] == '/';
        if (!doaccess) {
            if ((p = TZDIR) == NULL)
                return -1;
            if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
                return -1;
            (void) strcpy(fullname, p);
            (void) strcat(fullname, "/");
            (void) strcat(fullname, name);
            /*
            ** Set doaccess if '.' (as in "../") shows up in name.
            */
            if (strchr(name, '.') != NULL)
                doaccess = TRUE;
            name = fullname;
        }
        if (doaccess && access(name, R_OK) != 0)
            return -1;
        if ((fid = open(name, OPEN_MODE)) == -1)
            return -1;
    }

I'm puzzled about the benefit of calling access().
Won't the open() call work, or not, pretty much the same?

The only reason I've thought of for why access() would fail where open()
might succeed is in a programming running with setuid (or setgid)
privileges, where access() would check the permissions using the real UID
(and GID) but open would be controlled by the effective UID (and GID).
However, it seems incorrect to prevent a setuid or setgid program from
working like that -- maybe the reason the program is setuid or setgid is to
permit it to access the time zone files.  In terms of performance, a call to
access() is only slightly less expensive than a call to open().  My concern
is more about correctness than performance, though.  Is there a system where
using access makes a difference?

-- 
Jonathan Leffler <jonathan.leffler at gmail.com>  #include <disclaimer.h>
Guardian of DBD::Informix - v2007.0914 - http://dbi.perl.org
"Blessed are we who can laugh at ourselves, for we shall never cease to be
amused."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm.icann.org/pipermail/tz/attachments/20071202/5e4f8460/attachment-0001.html 


More information about the tz mailing list