pwmconfig: bug in temporary file creation
Hi all,
There is a bug in pwmconfig, it uses a fixed file name for the temporary files. As it uses /usr/tmp instead of /tmp, it is not really a security bug, but I think it should be fixed. Moreover /usr/tmp doesn't exists on all distributions, though I haven't checked if it is specified in the FHS.
The attached patch, provided by Javier Fern?ndez-Sanguino Pe?a via the Debian BTS, fixes that.
Bye,
Aur?lien
Signature · 19 lines
.''`. Aurelien Jarno GPG: 1024D/F1BCDB73 : :' : Debian GNU/Linux developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net -------------- next part -------------- Index: prog/pwm/pwmconfig =================================RCS file: /home/cvs/lm_sensors2/prog/pwm/pwmconfig,v retrieving revision 1.13 diff -u -1 -b -p -r1.13 pwmconfig --- prog/pwm/pwmconfig 20 Dec 2004 22:00:10 -0000 1.13 +++ prog/pwm/pwmconfig 24 Jan 2005 12:54:56 -0000 @@ -211,4 +211,5 @@ function pwmdetail() then - TMP1=/usr/tmp/pwmtest$$.1 - TMP2=/usr/tmp/pwmtest$$.2 + TMP1=`tempfile` || { echo "$0: Cannot create temporary file" >&2; exit 1; } + TMP2=`tempfile` || { echo "$0: Cannot create temporary file" >&2; exit 1; } + trap " /bin/rm -f -- \"$TMP1\" \"$TMP2\" " 0 1 2 3 13 15 echo "set xlabel \"PWM: $P\"" > $TMP1
Quoted 1 line · Mark Studebaker
pwmtest$$.1 is not a fixed file name ($$ expands to the PID)
Actually I meant predictable, not fixed. As you now the PID will be between 2 and 33000, a user could do a force attack:
for i in `seq 2 33000 ; do
ln -s /etc/shadow /usr/tmp/pwmtest$i.1 ;
done
Then if somebody run pwmconfig, /etc/shadow is wiped out.
You could find more information on:
http://www.linuxsecurity.com/content/view/115462/151/
Moreover, I checked the FHS, and /usr/tmp is not a standard directory. It is however allowed for compatibility reasons, and in that case should be link to /var/tmp. So /usr/tmp should not used in pwmconfig, /tmp should be used instead.
Quoted 1 line
and my linux system doesn't have the tempfile command...
You're right, it's seems is not a standard command. I have attached a new patch using mktemp.
Signature · 18 lines
.''`. Aurelien Jarno GPG: 1024D/F1BCDB73 : :' : Debian GNU/Linux developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net -------------- next part -------------- Index: pwmconfig =================================RCS file: /home/cvs/lm_sensors2/prog/pwm/pwmconfig,v retrieving revision 1.13 diff -u -1 -b -p -r1.13 pwmconfig --- pwmconfig 20 Dec 2004 22:00:10 -0000 1.13 +++ pwmconfig 25 Jan 2005 10:26:47 -0000 @@ -211,4 +211,4 @@ function pwmdetail() then - TMP1=/usr/tmp/pwmtest$$.1 - TMP2=/usr/tmp/pwmtest$$.2 + TMP1=`mktemp -t pwmtest1.XXXXXXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1; } + TMP2=`mktemp -t pwmtest2.XXXXXXXXXX` || { rm -f $TMP1 ; echo "$0: Cannot create temporary file" >&2; exit 1; } echo "set xlabel \"PWM: $P\"" > $TMP1
Quoted 3 lines · Jean Delvare
Indeed mktmp seems to be more widely available. Any idea why tempfile exists though? I wonder why we have two commands doing basically the same thing.
They differs by the fact tempfile uses tempnam(), whereas mktemp uses mkstemp(). I don't know exactly the difference, but after reading the manpage, it seems that tempnam() only return a valid filename, whereas mkstemp() create a file with 0600 permissions, and return its name.
So, it's seems that mkstemp() is more secure, as it is possible to create a file between the call to tempnam() and actual creation of the file. However, it's only my interpretation, I am not sure about that.
Signature · 4 lines
.''`. Aurelien Jarno GPG: 1024D/F1BCDB73 : :' : Debian GNU/Linux developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net
pwmtest$$.1 is not a fixed file name ($$ expands to the PID) and my linux system doesn't have the tempfile command... so not an improvement IMHO
Quoted 33 lines · Aurelien Jarno
Hi all,
There is a bug in pwmconfig, it uses a fixed file name for the temporary files. As it uses
/usr/tmpinstead of /tmp, it is not really a security bug, but I think it should be fixed. Moreover/usr/tmpdoesn't exists on all distributions, though I haven't checked if it is specified in the FHS.The attached patch, provided by Javier Fern?ndez-Sanguino Pe?a via the Debian BTS, fixes that.
Bye,
Aur?lien------------------------------------------------------------------------
Index: prog/pwm/pwmconfig
=================================> RCS file:/home/cvs/lm_sensors2/prog/pwm/pwmconfig,vretrieving revision 1.13
diff -u -1 -b -p -r1.13 pwmconfigpwm/pwmconfig +3
-2@@ -211,4 +211,5 @@ function pwmdetail() then - TMP1=/usr/tmp/pwmtest$$.1 - TMP2=/usr/tmp/pwmtest$$.2 + TMP1=`tempfile` || { echo "$0: Cannot create temporary file" >&2; exit 1; } + TMP2=`tempfile` || { echo "$0: Cannot create temporary file" >&2; exit 1; } + trap " /bin/rm -f -- \"$TMP1\" \"$TMP2\" " 0 1 2 3 13 15 echo "set xlabel \"PWM: $P\"" > $TMP1
Hi Aurelien,
Quoted 8 lines
They differs by the fact tempfile uses
tempnam(), whereas mktemp usesmkstemp(). I don't know exactly the difference, but after reading the manpage, it seems thattempnam()only return a valid filename, whereasmkstemp()create a file with 0600 permissions, and return its name.So, it's seems that
mkstemp()is more secure, as it is possible to create a file between the call totempnam()and actual creation of the file. However, it's only my interpretation, I am not sure about that.
This page seems to confirm this:
http://www.fr.linuxfromscratch.org/view/lfs-cvs/chapter06/lfs-utils.html
"mktemp creates temporary files in a secure manner. It is used in scripts.
tempfile creates temporary files in a less secure manner than mktemp. It is installed for backwards-compatibility."
They don't give details though.
So let's go with mktemp. Mark, can you confirm that you have mktemp on your system(s)?
Thanks,
--
Jean Delvare
Hi Aurelien,
Quoted 4 lines
Moreover, I checked the FHS, and
/usr/tmpis not a standard directory. It is however allowed for compatibility reasons, and in that case should be link to/var/tmp. So/usr/tmpshould not used in pwmconfig, /tmp should be used instead.
Actually $TMPDIR should be used instead, but your patch is obviously better than that.
Quoted 3 lines
and my linux system doesn't have the tempfile command... You're right, it's seems is not a standard command. I have attached a new patch using mktemp.
Indeed mktmp seems to be more widely available. Any idea why tempfile exists though? I wonder why we have two commands doing basically the same thing.
Thanks,
--
Jean Delvare
actually I don't have mktemp either.
But the redhat man page for it is dated 1996 so I may be a little behind the times... so I'm OK with the patch if everybody else has mktemp
Quoted 32 lines · Jean Delvare
Hi Aurelien,
Quoted 8 lines
They differs by the fact tempfile uses
tempnam(), whereas mktemp usesmkstemp(). I don't know exactly the difference, but after reading the manpage, it seems thattempnam()only return a valid filename, whereasmkstemp()create a file with 0600 permissions, and return its name.So, it's seems that
mkstemp()is more secure, as it is possible to create a file between the call totempnam()and actual creation of the file. However, it's only my interpretation, I am not sure about that.This page seems to confirm this:
http://www.fr.linuxfromscratch.org/view/lfs-cvs/chapter06/lfs-utils.html"mktemp creates temporary files in a secure manner. It is used in scripts.
tempfile creates temporary files in a less secure manner than mktemp. It is installed for backwards-compatibility."
They don't give details though.
So let's go with mktemp. Mark, can you confirm that you have mktemp on your system(s)?
Thanks,
--
Jean Delvare