[comp.unix.ultrix] Ultrix 4.1 "find" command bug triggers /bin/sh bug

D. Allen [CGL]" <idallen@watcgl.waterloo.edu> (05/16/91)

#!/bin/sh
# A script that shows how a bug in FIND triggers a bug in /bin/sh
#
# This bug exists on:
#     Ultrix 4.1 RISC, Ultrix 3.1C RISC, Ultrix 3.1 VAX, etc.
#     Unix 4.3BSD, Sequent DYNIX
# It does not exit on:
#     Irix 3.3.1, RISC/os (UMIPS) 4.51, SunOS 4.x
#
# The FIND program opens directories as it goes down a tree.
# Those descriptors remain open across the -exec of a program.
# If that program happens to be /bin/sh, and the depth is right (8),
# /bin/sh will silently exit (and not run your shell script).
#
# This means if you use FIND to search your file system and execute
# a Bourne shell script to do something, it won't work on directories 
# eight deep.
#
# FIX:
#
# FIND should use fcntl F_SETFD to set the close-on-exec flag on all
# its open directory descriptors.  The "-exec" sub-processes should not
# inherit them.
#
# /bin/sh should be more clever about its file descriptors and not
# exit silently because the descriptor it wants to use is occupied.
#
# Work around:
#
# Change all your FIND commands to -print the file names and use some
# other script to read the names and perform the actions you want.
# Note that using xargs incorrectly may result in security problems.
# (See back issues of comp.unix.shell etc.)
#
# Submit an SPR.
#-----------------------------------------------------------------------

# any name will do here
name=x7x

cd /tmp
rm -rf $name

# nest some directories
for i in 1 2 3 4 5 6 7 ; do
    mkdir $name && cd $name
done

# create a file at the bottom
echo hi >file

# start back at the top
cd /tmp

# create a /bin/sh script
rm -f shscript
cat >shscript <<EOF
#!/bin/sh
echo This will never be seen.
EOF

# make it executable
chmod +x shscript

# try to execute it -- no output from the script will appear
/usr/bin/find $name -type f -print -exec /tmp/shscript \; -print

# change shell interpreters and watch it work
cat >shscript <<EOF
#!/bin/csh -f
echo This works.
EOF

# try to execute it and see it now works.
/usr/bin/find $name -type f -print -exec /tmp/shscript \; -print

# Output:
# % ./showbug
# x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
# x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
# x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
# This works.
# x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
-- 
-IAN! (Ian! D. Allen) idallen@watcgl.uwaterloo.ca idallen@watcgl.waterloo.edu
 [129.97.128.64]  Computer Graphics Lab/University of Waterloo/Ontario/Canada

murphy@burfle.dco.dec.com (Rick Murphy) (05/16/91)

I've run this under V4.2 and it works fine:
burfle.dco.dec.com> foo.sh
x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
This will never be seen.
x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
This works.
x7x/x7x/x7x/x7x/x7x/x7x/x7x/file
burfle.dco.dec.com>
	-Rick
--
Rick Murphy, WA1SPT/4			DEC Washington ULTRIX Resource Center
Domain:  murphy@burfle.dco.dec.com -or- murphy@ufp.enet.dec.com
Bang:    decwrl!ufp.enet!murphy		Ding:  (301) 306-2985
Disclaimer: This nonsense came from an AI program written in TECO. Ignore it.