mirror of
https://git.proxmox.com/git/pve-common
synced 2025-08-16 01:18:37 +00:00
harden file_set_contents against symlink attacks
This commit is contained in:
parent
c9cb574660
commit
aa757172c6
@ -2,7 +2,7 @@ package PVE::Tools;
|
|||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use POSIX qw(EINTR);
|
use POSIX qw(EINTR EEXIST);
|
||||||
use IO::Socket::INET;
|
use IO::Socket::INET;
|
||||||
use IO::Select;
|
use IO::Select;
|
||||||
use File::Basename;
|
use File::Basename;
|
||||||
@ -175,7 +175,13 @@ sub file_set_contents {
|
|||||||
my $tmpname = "$filename.tmp.$$";
|
my $tmpname = "$filename.tmp.$$";
|
||||||
|
|
||||||
eval {
|
eval {
|
||||||
my $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT, $perm);
|
my ($fh, $tries) = (undef, 0);
|
||||||
|
while (!$fh && $tries++ < 3) {
|
||||||
|
$fh = IO::File->new($tmpname, O_WRONLY|O_CREAT|O_EXCL, $perm);
|
||||||
|
if (!$fh && $! == EEXIST) {
|
||||||
|
unlink($tmpname) or die "unable to delete old temp file: $!\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
die "unable to open file '$tmpname' - $!\n" if !$fh;
|
die "unable to open file '$tmpname' - $!\n" if !$fh;
|
||||||
die "unable to write '$tmpname' - $!\n" unless print $fh $data;
|
die "unable to write '$tmpname' - $!\n" unless print $fh $data;
|
||||||
die "closing file '$tmpname' failed - $!\n" unless close $fh;
|
die "closing file '$tmpname' failed - $!\n" unless close $fh;
|
||||||
|
Loading…
Reference in New Issue
Block a user