Access Control

TOCTTOU

 In early versions of Unix there was no mkdir() system call. here's
 the source for /bin/mkdir (from v7 unix):
   if ((mknod(d, 040777, 0)) < 0) {
       fprintf(stderr,"mkdir: cannot make directory %s\n", d);
       ++Errors;
       return;
   }
   chown(d, getuid(), getgid());
 mkdir was setuid to root -- it ran as root even if started by
   an ordinary user, since mknod() is a privileged call.
 what's the problem?
 how to exploit it?
   cd /tmp
   mkdir foo & ./exploit
   exploit waits for foo to appear, then removes it,
     substitutes a link to e.g. /etc/passwd

unix used to be full of such bugs. e.g. mail delivery command
appended to /usr/spool/mail/cs123456, running as root:
 stat(/usr/spool/mail/cs123456)
 if owned by cs123456
   open and write

how do people fix tocttou bugs?
 atomic mkdir() call
 f-calls:
   fd = open(file)
   fstat(fd)
   if(ok)
     fchown(fd)
 don't run as root: structure s/w to drop privileges when possible
   e.g. setuid(cs123456) before delivering mail

rest of course