From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10952f,b69d377feb5a8a2 X-Google-Attributes: gid10952f,public X-Google-Thread: 1164f8,d4c1f7c0fe672a4 X-Google-Attributes: gid1164f8,public X-Google-Thread: 10872d,d4c1f7c0fe672a4 X-Google-Attributes: gid10872d,public X-Google-Thread: 1020c6,d4c1f7c0fe672a4 X-Google-Attributes: gid1020c6,public X-Google-Thread: fcda7,d4c1f7c0fe672a4 X-Google-Attributes: gidfcda7,public X-Google-Thread: 1070f0,d4c1f7c0fe672a4 X-Google-Attributes: gid1070f0,public From: Chris Calabrese Subject: Re: Yet Another Ultimate TMP-cleaner algorithm! Re: Different UNIX pro's and con's. Date: 1998/09/22 Message-ID: <3607C878.85DA16C1@fpk.hp.com>#1/1 X-Deja-AN: 393675636 Content-Transfer-Encoding: 7bit References: <35F71814.1E0B911C@iakom.com> <36069F96.E8127ED3@llnl.gov> <6u67ug$ccd@web.nmti.com> <36079B1B.2781@s054.aone.net.au> <6u88qm$t5u@web.nmti.com> Content-Type: text/plain; charset=us-ascii Organization: Hewlett Packard Engineering Services Group (Consultant) Mime-Version: 1.0 Newsgroups: comp.unix.admin,comp.sys.hp.hpux,comp.sys.sgi.admin,comp.sys.sun.admin,comp.sys.dec,comp.security.unix OK, I guess I'll throw my $.02 into the hat... As many people have said, we can safely ignore the problem of links to ordinary files because the unlink will remove the link and not the file. Also, doing setuid's to the directory owners is nasty because it's complicated and because (on some systems) you can create something in /tmp and then chown it to the user of your choice. So...what we need is a way to make sure we cd at each point and then check that the cd's went where we wanted them to go. One way that was mentioned by Peter de Silva is to check that pwd returns the path we expect after the cd. This is pretty good but has two problems: o It's slow for deep directory trees o It's not guaranteed to work for pwd returning very long paths (like longer than PATH_MAX). However, we don't actually have to check the whole path, just the last component if we check device/inode of .. vs . instead of names (yes, you could munge . and .. on some systems, but it's not a security concern since you'd need write permissions on the directory already to munge it). One implementation (in pseudo-code in no particular language with error handling missing) is: function descend(device, inode, name) { chdir(name) dot=opendir(".") dotdotstatus=stat("..") if dotdotstatus.device != device error if dotdotstatus.inode != inode error status=fstat(dot) for entry in dot { fentry=open(entry) status=fstat(fentry) if isadirectory(status) descend(dotstatus.device, dotstatus.inode, entry.name) if isaregularfile(status) and isoldenough(status) unlink(entry.name) } One problem with this implementation is that it keeps file descriptors open for each directory you've descended into. That problem can't be fixed because you specifically don't want to rely on '.' and '..' not changing as you descend. Now, if there were an funlink system call, you could even get rid of the race condition of a regular file changing after that stat and before the unlink. -- Christopher J. Calabrese ES Architect and DR/Audit/Security/y2k Coordinator (Consultant) Hewlett Packard Engineering Services Group cjc@fpk.hp.com