RE: Is this system call hazardous for my computers health??
by chromatic (Archbishop) on Nov 15, 2000 at 11:19 UTC
|
Three things stick out in my mind. First, there's no need to use double-quoted interpolation of $tmp_dir. Just say (-e $tmp_dir) and it will work correctly. It seems like half of the attacks against programs have to do with variable interpolation, so avoid it when you can.
Your use of the list form of system is good, that avoids having the shell interpolate (there's that word again) the argument string. I'm not sure you want the asterisk at the end, as it may remove all of the files in the current working directory, which is hardly what you want. I'd also recommend you look into File::Path instead, specifically the rmtree() function.
In general, I trust built-in functions more than system calls.
Update: Upon quick testing with bash, rm -rf -i dirname/ * does just as I suspected -- removing dirname/ recursively AND all of the files in the current directory. It's up to you. | [reply] [d/l] [select] |
|
|
I agree with you trusting built-in functions more than system calls. I was trying to stay away from modules, and understand how to best use system calls without compromising security.
On a side note, I find it interesting that perl has built in functions to chdir, rmdir, mkdir, rename, and symlink but does not have functions to move, copy, or erase files.
So it seems the best solution would be to use a module that properly handles removing files or directories such as your suggestion of File::Path. However, if not using modules the next best would be to use system to rm the directory and files and then recreate the directory such as this code.
Thanks! zzspectrez
| [reply] |
|
|
You move files with link()/unlink() or [unless you have a really old version of Perl] rename(). You delete files with unlink(). None of them work across file systems. The first two have been a basic part of Unix for a long time and rename() is a recent addition. Same for Perl [because Perl is based on Unix].
Unix and C don't have a standard subroutine for copying files and so Perl doesn't either [nor for moving a file across file systems which is just a copy and delete]. There are modules for this.
-
tye
(but my friends call me "Tye")
| [reply] |
RE: Is this system call hazardous for my computers health??
by BastardOperator (Monk) on Nov 15, 2000 at 21:30 UTC
|
Seeing as you seem to be concerned with the security of this.... The first question is, "is this running as root?". If so, I see multiple problems:
Here's the lowdown, I'm a mischievious (sp?) user, and I see the source to this program. Knowing the name of $tmp_dir I do the following:
$ ln -s /etc /tmp/blahblah-MM-DD-YY
In your program you say, "hmm, does /tmp/blahblah-MM-DD-YY exist?". Why yes, it does. You say, "okey dokey rm -f /tmp/blah.../*". I say "goodbye /etc/*!!".
If this isn't run by root, it's far less of a problem (unless they link to your home directory :). You really should always check if it's a link, hard or soft, who the owner is, etc. | [reply] |
|
|
if (-e $tmp_dir) {
if (-l $tmp_dir) {
die "Temporary folder $tmp_dir is a symbolic link!\n"
}else{
system ("rm","-rf",$tmp_dir);
mkdir ($tmp_dir) or
die "Unable to make temp folder: $tmpdir: $!\n";
}
}else{
mkdir ($tmp_dir) or
die "Unable to make temporary folder $tmp_dir: $!\n";
}
Thanks! zzspectrez
| [reply] [d/l] |
|
|
Let me direct you to (and I'm by far not trying to pat myself on the back here, but I searched long and hard to come up with what I have, so consider it a smorgesboard (sp?) of my experiences with these same questions) cksec, in particular check out the verifyfile() sub (must carefully read all of the sub) which makes sure a file is what it should be. Also, check out the updatedb() sub, which actually writes to a file and so ensures that it is what it should be.
I wish I could remember where I found this info, in order to give credit to the author, unfortunately I cannot...wait...ah yes, it's from the book Perl for Systems Administrators, which while having too much Windows stuff for my taste, has some great info.
Hope I've helped, enjoy!
| [reply] |
RE: Is this system call hazardous for my computers health??
by knight (Friar) on Nov 15, 2000 at 20:43 UTC
|
Expanding a little on chromatic's reply...
To remove all the files within the directory,
you'd need to append the '*' to the path name
to expand it to file names just within that directory.
Plus, you'd have to concatenate it into a single
argument so that system() will see the '*' and
pass the whole thing through the shell for expansion:
system("rm -rf $tmp_dir/*");
The '/' separator is UNIX-specific, though,
so if portability is important you'd want to:
use File::Spec;
system("rm -rf " . File::Spec->catfile($tmpdir, "*"));
However, removing files within a temporary
directory this way is a little laborious.
If the whole directory is really temporary,
it's more usual to just blow it away and recreate it.
You can do this very simply
(and without using an external command) as follows:
use File::Path;
my $tmp_dir = "/tmp/blahblah-11-14-2000";
rmtree($tmp_dir); # no error check; doesn't matter if it doesn'
+t exist
mkdir($tmp_dir) or die "Unable to make temporary folder $tmp_dir:
+$!\n";
This works unless you really must re-use the
existing temporary directory itself,
which I can only imagine in the unusual case
where you can't create a new directory in /tmp. | [reply] [d/l] [select] |
|
|
| [reply] |
RE: Is this system call hazardous for my computers health??
by Dominus (Parson) on Nov 16, 2000 at 00:45 UTC
|
I realize this wasn't your question, but what jumped out at me is that
system("rm","-f","$tmp_dir","*")
isn't going to do what you think. Using the list form of system
instructs Perl to avoid using the shell, and to fork
and exec rm directly, with the specified arguments.
Since it's the shell that is responsible for expanding
the *, and you're not using the shell, the *
is never expanded, and is passed to rm literally.
You end up attempting to remove a single file named *.
I suspect that what you should have done was simply
system("rm", "-rf", $tmp_dir);
as I think someone else already pointed out.
| [reply] [d/l] [select] |
RE: Is this system call hazardous for my computers health??
by AgentM (Curate) on Nov 15, 2000 at 22:15 UTC
|
POSIX already provides for temporary file names, namely tmpfile() and tmpnam()- you should be using these- they are implemented in File::Temp which includes mechanisms to create new temporary directories. These are far safer than specifying dates or times in the filenames- and POSIX-compatible! (three cheers for POSIX!)
AgentM Systems nor Nasca Enterprises nor
Bone::Easy nor Macperl is responsible for the
comments made by
AgentM. Remember, you can build any logical system with NOR.
| [reply] |