Create and set permissions of files atomically

Apparently it is not safe to first create a file with touch and then set
permissions with chmod. An unprivileged user could open the file in
between. See
https://github.com/NixOS/nixpkgs/pull/60138#discussion_r623971755.
This commit is contained in:
Martin Puppe 2021-10-02 00:24:14 +02:00
parent fb34a0834c
commit 698fc0c6d3

View file

@ -91,16 +91,13 @@ echo "!*data" >&2
read basedir
if test ! -d "$basedir"; then
mkdir "$basedir"
chown root:root "$basedir"
chmod 755 "$basedir"
install -o root -g root -m 755 -d "$basedir"
fi
rm -rf "$basedir"/*
while read name; do
fullpath="$basedir/$name"
touch "$fullpath"
chmod 600 "$fullpath"
install -o root -g root -m 600 /dev/null "$fullpath"
read size
head --bytes="$size" - > "$fullpath"
read owner