You want to make sure that there are no world-writable directories in
root'
s $PATH
. To see why, read Adding the Current Directory to the $PATH.
Use this simple script to check your $PATH
. Use it in conjunction with su -or
sudo
to
check paths for other users:
#!/usr/bin/env bash # cookbook filename: chkpath.1 # Check your $PATH for world-writable or missing directories exit_code=0 for dir in ${PATH//:/ }; do [ -L "$dir" ] && printf "%b" "symlink, " if [ ! -d "$dir" ]; then printf "%b" "missing " (( exit_code++ )) elif [ "$(ls -lLd $dir | grep '^d.......w. ')" ]; then printf "%b" "world writable " (( exit_code++ )) else printf "%b" "ok " fi printf "%b" "$dir " done exit $exit_code
For example:
# ./chkpath ok /usr/local/sbin ok /usr/local/bin ok /sbinok /bin ok /usr/sbin ok /usr/bin ok /usr/X11R6/bin ok /root/bin missing /does_not_exist world writable /tmp symlink, world writable /tmp/bin symlink, ok /root/sbin
We convert the $PATH
to a
space-delimited list using the technique from Finding a File Using a List of Possible Locations, test for
symbolic links (-L)
, and make sure
the directory actually exists (-d)
.
Then we get a long directory listing (-l)
, dereferencing symbolic links (-L)
, and listing the directory name only
(-d)
, not the directory’s contents.
Then we finally get to grep for world-writable
directories.
As you can see, we spaced out the ok
directories, while directories with a
problem may get a little cluttered. We also broke the usual rule of Unix
tools being quiet unless there’s a problem, because we felt it was a
useful opportunity to see exactly what is in your path and give it a
once-over in addition to the automated check.
We also provide an exit code of zero on success with no problems
detected in the $PATH
, or the count
of errors found. With a little more tweaking, we can add the file’s
mode, owner, and group into the output, which might be even more
valuable to check:
#!/usr/bin/env bash # cookbook filename: chkpath.2 # Check your $PATH for world-writable or missing directories, with 'stat' exit_code=0 for dir in ${PATH//:/ }; do [ -L "$dir" ] && printf "%b" "symlink, " if [ ! -d "$dir" ]; then printf "%b" "missing " (( exit_code++ )) else stat=$(ls -lHd $dir | awk '{print $1, $3, $4}') if [ "$(echo $stat | grep '^d.......w. ')" ]; then printf "%b" "world writable $stat " (( exit_code++ )) else printf "%b" "ok $stat " fi fi printf "%b" "$dir " done exit $exit_code
# ./chkpath ; echo $? ok drwxr-xr-x root root /usr/local/sbin ok drwxr-xr-x root root /usr/local/bin ok drwxr-xr-x root root /sbin ok drwxr-xr-x root root /bin ok drwxr-xr-x root root /usr/sbin ok drwxr-xr-x root root /usr/bin ok drwxr-xr-x root root /usr/X11R6/bin ok drwx------ root root /root/bin missing /does_not_exist world writable drwxrwxrwt root root /tmp symlink, ok drwxr-xr-x root root /root/sbin 2
3.139.83.151