in reply to Insecure dependency in chmod while running setuid

I think the results you get from File::Find are tainted. This is good as you don't want to have unexpected filenames and don't want to change them to (even) 664. Also, operating on $_ works, but I prefer operating on $File::Find::name, as having absolute filenames is much better in error messages. I would look at perlsec and do something like the following:

sub setperms { my $fn = $File::Find::name; my $untained_fn; my $mode; if ($fn =~ m!^(\Q$baseDir\E([/\w.]+)\z!) { $untainted_fn = $1; $mode = 0664; } else { $fn =~ m!^(.*)$!; $untainted_fn = $1; $mode = 0000; warn "File name '$untainted_fn' does not match criteria, setti +ng mode to 0."; }; chmod($untained_fn,$mode) or die "Couldn't change '$untainted_fn' +to $mode: $!"; };

Also, you should really, really consider whether you want to have any script that runs as root accessible from the outside of your computer. I recommend having that script run from a cron job and having as the CGI program a program that just appends filenames to a list.

Replies are listed 'Best First'.
Re^2: Insecure dependency in chmod while running setuid
by afoken (Chancellor) on Jun 12, 2009 at 09:06 UTC

    File::Find has untaint, untaint_pattern, and untaint_skip options for this purpose.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Insecure dependency in chmod while running setuid
by ak4good (Initiate) on Jun 13, 2009 at 04:18 UTC
    Yes, I really, really didn't like the idea of running a CGI script as root. What I decided to do in the end was to just give users sudo rights to run the permission fixing shell script. I think that's good enough. No complaints so far. :P