Ultrix 4.1 "find" command bug triggers /bin/sh bug
Ian! D. Allen [CGL]
idallen at watcgl.waterloo.edu
Thu May 16 04:59:03 AEST 1991
#!/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 at watcgl.uwaterloo.ca idallen at watcgl.waterloo.edu
[129.97.128.64] Computer Graphics Lab/University of Waterloo/Ontario/Canada
More information about the Comp.unix.ultrix
mailing list