Bugzilla – Bug 1134722
ps2epsi tempfile handling is insecure
Last modified: 2020-05-18 07:34:13 UTC
When looking at ps2epsi, I noticed some details in the tempfile creation that should be improved: if which mktemp >/dev/null 2>/dev/null; then tmpfile="`mktemp $TMPDIR/ps2epsi.XXXXXX`" adding || exit 1 might be a good idea so that ps2epsi exits if it can't create a tempfile. (mktemp should print an error message in this case, no need to print another one.) else tmpdir=$TMPDIR/ps2epsi.$$ (umask 077 && mkdir "$tmpdir") if test ! -d "$tmpdir"; then echo "failed: could not create temporary file" exit 1 fi tmpfile="$tmpdir"/ps2epsi$$ fi The fallback (if mktemp can't be found) isn't terribly bad, but still insecure. In theory someone could create /tmp/ps2epsi.$$ before (one for each possible pid) and create a ps2epsi$$ symlink in it to overwrite an attacker-chosen file (aka "symlink attack"). The good thing is that mkdir errors out if a directory already exists, so all you need is to check its exit code: (umask 077 && mkdir "$tmpdir") || { echo "$tmpdir can't be created or unexpectedly already exists, abortingfor security reasons" >&2 ; exit 1 ; } (You can also test for $? != 0 if you don't like the "||" syntax.) In theory checking $? should make the "test ! -d" superfluous, but in practise it can't hurt to keep it. For completeness: "mkdir -p" will not error out on existing directories, so better don't use it in scripts ;-)
I am afraid in the foreseeable future I won't find any time to have a closer look what actually goes on here so all _I_ can do is to close it as "wontfix" which does of course not mean someone else at openSUSE could not help here and continue to work on this issue ;-)
bugtracker_accounts++ ;-) Forwarded to upstream - https://bugs.ghostscript.com/show_bug.cgi?id=702416
Christian Boltz, thank you so much! Even relatively small things like upstream reports help a lot when I must not do them myself. The main thing with upstream reporting is not the initial report but being responsive if upstream has questions and things like that.