CVE-2015-5232: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') (CWE-362)
value: Race conditions in opa-fm before 10.4.0.0.196 and opa-ff before 10.4.0.0.197.
CWE description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
CVSS v3 base score: 8.1
Published: 2017-06-07T20:29Z
Repository: https://github.com/01org/opa-ff
Language: Shell
Files changed: IbaTools/FastFabric/opascpall.sh, IbaTools/FastFabric/opatest.sh, IbaTools/disable_ports/opadisablehosts.sh, IbaTools/disable_ports/opaenableports.sh, IbaTools/disable_ports/opaextractbadlinks.sh, IbaTools/disable_ports/opaextractlids.sh, IbaTools/disable_ports/opaextractsellinks.sh, IbaTools/opacabletest/opacabletest.sh, IbaTools/opagenswitches/opagenswitches.sh, IbaTools/opalinkanalysis/opalinkanalysis.sh, IbaTools/opasw/opaswscript/opamanageswitch.sh, IbaTools/opaxlattopology/opaxlattopology.sh, IbaTools/opaxlattopology/opaxlattopology_cust.sh, fm_config/config_diff.sh, fm_config/config_generate.sh
Commit date: 2015-09-16 14:21:46 -0400
Commit message: Fix scripts that use well-known temp files.

Diff: diff --git a/IbaTools/FastFabric/opascpall.sh b/IbaTools/FastFabric/opascpall.sh
index f703a19..b035bab 100755
--- a/IbaTools/FastFabric/opascpall.sh
+++ b/IbaTools/FastFabric/opascpall.sh
@@ -29,11 +29,13 @@
 # END_ICS_COPYRIGHT8   ****************************************
 
 # [ICS VERSION STRING: unknown]
 # copy a file to all hosts
 
-trap "exit 1" SIGHUP SIGTERM SIGINT
+temp="$(mktemp --tmpdir "opascpall.XXXXXX")"
+trap "rm -f $temp; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $temp" EXIT
 
 # optional override of defaults
 if [ -f /etc/sysconfig/opa/opafastfabric.conf ]
 then
 	. /etc/sysconfig/opa/opafastfabric.conf
@@ -41,11 +43,11 @@ fi
 
 . /opt/opa/tools/opafastfabric.conf.def
 
 . /opt/opa/tools/ff_funcs
 
-temp=/tmp/opascpall$$
+temp=`mktemp`
 trap "rm -f $temp" 1 2 3 9 15
 
 Usage_full()
 {
 	echo "Usage: opascpall [-p] [-r] [-f hostfile] [-h 'hosts'] [-u user] source_file ... dest_file" >&2
diff --git a/IbaTools/FastFabric/opatest.sh b/IbaTools/FastFabric/opatest.sh
index 27273d6..4f66d6a 100755
--- a/IbaTools/FastFabric/opatest.sh
+++ b/IbaTools/FastFabric/opatest.sh
@@ -47,11 +47,11 @@ BINDIR=${BINDIR:-/usr/sbin}
 if [ x"$FF_IPOIB_SUFFIX" = xNONE ]
 then
 	export FF_IPOIB_SUFFIX=""
 fi
 
-temp=/tmp/opatest$$
+temp=`mktemp`
 trap "rm -f $temp" 1 2 3 9 15
 
 # identify how we are being run, affects valid options and usage
 mode=opatest
 cmd=`basename $0`
diff --git a/IbaTools/disable_ports/opadisablehosts.sh b/IbaTools/disable_ports/opadisablehosts.sh
index 88b8c95..27f5e42 100755
--- a/IbaTools/disable_ports/opadisablehosts.sh
+++ b/IbaTools/disable_ports/opadisablehosts.sh
@@ -31,12 +31,13 @@
 
 # [ICS VERSION STRING: unknown]
 
 # disable the specified set of hosts
 
-tempfile=/tmp/opadisablehosts$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 Usage_full()
 {
 	echo "Usage: opadisablehosts [-h hfi] [-p port] reason host ..." >&2
 	echo "              or" >&2
diff --git a/IbaTools/disable_ports/opaenableports.sh b/IbaTools/disable_ports/opaenableports.sh
index 2b638a1..82d9891 100755
--- a/IbaTools/disable_ports/opaenableports.sh
+++ b/IbaTools/disable_ports/opaenableports.sh
@@ -31,12 +31,13 @@
 
 # [ICS VERSION STRING: unknown]
 
 # reenable the specified set of ports
 
-tempfile=/tmp/opaenableports$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 # optional override of defaults
 if [ -f /etc/sysconfig/opa/opafastfabric.conf ]
 then
 	. /etc/sysconfig/opa/opafastfabric.conf
diff --git a/IbaTools/disable_ports/opaextractbadlinks.sh b/IbaTools/disable_ports/opaextractbadlinks.sh
index fcb2aa9..d907fb6 100755
--- a/IbaTools/disable_ports/opaextractbadlinks.sh
+++ b/IbaTools/disable_ports/opaextractbadlinks.sh
@@ -34,12 +34,13 @@
 #  links with excessive errors
 
 TOOLSDIR=${TOOLSDIR:-/opt/opa/tools}
 BINDIR=${BINDIR:-/usr/sbin}
 
-tempfile=/tmp/opaextractbadlinks$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 Usage_full()
 {
 	echo "Usage: opaextractbadlinks [opareport options]" >&2
 	echo "              or" >&2
diff --git a/IbaTools/disable_ports/opaextractlids.sh b/IbaTools/disable_ports/opaextractlids.sh
index e6e897f..6165469 100755
--- a/IbaTools/disable_ports/opaextractlids.sh
+++ b/IbaTools/disable_ports/opaextractlids.sh
@@ -33,12 +33,13 @@
 # Run opareport pipe output to opaxmlextract to extract lids to a csv file
 
 TOOLSDIR=${TOOLSDIR:-/opt/opa/tools}
 BINDIR=${BINDIR:-/usr/sbin}
 
-tempfile=/tmp/opaextractlids$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 Usage_full()
 {
 	echo "Usage: opaextractlids [-h hfi] [-p port]" >&2
 	echo "              or" >&2
diff --git a/IbaTools/disable_ports/opaextractsellinks.sh b/IbaTools/disable_ports/opaextractsellinks.sh
index 9cd84f6..16247cf 100755
--- a/IbaTools/disable_ports/opaextractsellinks.sh
+++ b/IbaTools/disable_ports/opaextractsellinks.sh
@@ -34,12 +34,13 @@
 #  links with excessive errors
 
 TOOLSDIR=${TOOLSDIR:-/opt/opa/tools}
 BINDIR=${BINDIR:-/usr/sbin}
 
-tempfile=/tmp/opaextractsellinks$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 Usage_full()
 {
 	echo "Usage: opaextractsellinks [opareport options]" >&2
 	echo "              or" >&2
diff --git a/IbaTools/opacabletest/opacabletest.sh b/IbaTools/opacabletest/opacabletest.sh
index b202d8e..d37b6f5 100755
--- a/IbaTools/opacabletest/opacabletest.sh
+++ b/IbaTools/opacabletest/opacabletest.sh
@@ -30,12 +30,13 @@
 
 # [ICS VERSION STRING: unknown]
 
 # start and stop HFI-SW and/or ISL cable Bit Error Rate tests
 
-tempfile=/tmp/opacabletest$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 # optional override of defaults
 if [ -f /etc/sysconfig/opa/opafastfabric.conf ]
 then
 	. /etc/sysconfig/opa/opafastfabric.conf
diff --git a/IbaTools/opagenswitches/opagenswitches.sh b/IbaTools/opagenswitches/opagenswitches.sh
index 1daf6a2..9c89030 100644
--- a/IbaTools/opagenswitches/opagenswitches.sh
+++ b/IbaTools/opagenswitches/opagenswitches.sh
@@ -37,13 +37,10 @@
 # switches file; optionally, update NodeDesc values in an switches file using
 # NodeDesc values found in a specified topology.xml file and fabric link
 # information obtained via opareport -o links (live fabric or snapshot).
 
 
-## Includes
-trap "exit 1" SIGHUP SIGTERM SIGINT
-
 # optional override of defaults
 if [ -f /etc/sysconfig/opa/opafastfabric.conf ]
 then
 	. /etc/sysconfig/opa/opafastfabric.conf
 fi
@@ -158,10 +155,13 @@ clean_files()
 		rm -f $FILE_LINKS_EDGE_HFI
 		rm -f $FILE_LINKS_EDGE_HFI2
 	fi
 }	# End of clean_files()
 
+trap 'clean_files; exit 1' SIGINT SIGHUP SIGTERM 
+trap clean_files EXIT
+
 Usage_full()
 {
 	echo "Usage: opagenswitches [-t portsfile] [-p ports] [-R]" >&2
 	echo "         [-L switches_file] [-o output_file] [-T topology_file] [-X snapshot_file]" >&2
 	echo "         [-s] [-v level] [-K]" >&2
@@ -219,11 +219,10 @@ Usage()
 	echo "   -T topology_file - use topology_file as topology XML" >&2
 	echo "   --help - produce full help text" >&2
 	echo "for example:" >&2
 	echo "   opagenswitches" >&2
 	echo "   opagenswitches -T topology.0:0.xml" >&2
-	clean_files
 	exit 2
 }	# End of Usage()
 
 # Display progress information (to STDOUT)
 # Inputs:
diff --git a/IbaTools/opalinkanalysis/opalinkanalysis.sh b/IbaTools/opalinkanalysis/opalinkanalysis.sh
index 0685ce1..4029e9d 100755
--- a/IbaTools/opalinkanalysis/opalinkanalysis.sh
+++ b/IbaTools/opalinkanalysis/opalinkanalysis.sh
@@ -30,12 +30,13 @@
 
 # [ICS VERSION STRING: unknown]
 
 # analyzes all the links in the fabric
 
-tempfile=/tmp/opalinkanalysis$$
+tempfile=`mktemp`
 trap "rm -f $tempfile; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $tempfile" EXIT
 
 # optional override of defaults
 if [ -f /etc/sysconfig/opa/opafastfabric.conf ]
 then
 	. /etc/sysconfig/opa/opafastfabric.conf
diff --git a/IbaTools/opasw/opaswscript/opamanageswitch.sh b/IbaTools/opasw/opaswscript/opamanageswitch.sh
index 6df0b3e..886ed48 100755
--- a/IbaTools/opasw/opaswscript/opamanageswitch.sh
+++ b/IbaTools/opasw/opaswscript/opamanageswitch.sh
@@ -364,12 +364,11 @@ fwUpdate)
 		thisdir=`pwd`
 		fn=$thisdir/$fileNameArg
 	else
 		fn=$fileNameArg
 	fi
-	tmpdir=/tmp/12200FW$$
-	mkdir $tmpdir
+	tmpdir=`mktemp -d`
 	cd $tmpdir
 	tar xzf $fn > /dev/null 2>&1
 
 	# set names
 	if [ -f emfwMapFile ]
diff --git a/IbaTools/opaxlattopology/opaxlattopology.sh b/IbaTools/opaxlattopology/opaxlattopology.sh
index c052e1f..1fa3d92 100755
--- a/IbaTools/opaxlattopology/opaxlattopology.sh
+++ b/IbaTools/opaxlattopology/opaxlattopology.sh
@@ -218,10 +218,27 @@ debug_4=
 debug_5=
 debug_6=
 debug_7=
 #echo "DEBUG-x.y: 0:$debug_0: 1:$debug_1: 2:$debug_2: 3:$debug_3: 4:$debug_4: 5:$debug_5: 6:$debug_6: 7:$debug_7:"
 
+function clean_tempfiles() {
+  if [ $fl_clean == 1 ]
+    then
+    rm -f $FILE_TEMP
+    rm -f $FILE_TEMP2
+    rm -f $FILE_LINKSUM
+    rm -f $FILE_LINKSUM_NOCORE
+    rm -f $FILE_LINKSUM_NOCABLE
+    rm -f $FILE_NODEFIS
+    rm -f $FILE_NODESWITCHES
+    rm -f $FILE_NODELEAVES
+    rm -f $FILE_NODECHASSIS
+  fi
+}
+
+trap 'clean_tempfiles; exit 1' SIGINT SIGHUP SIGTERM
+trap 'clean_tempfiles' EXIT
 
 ## Local functions:
 functout=
 
 # Output usage information
@@ -398,22 +415,11 @@ gen_topology()
   echo "</Nodes>" >> $FILE_TOPOLOGY_OUT
 
   echo "</Report>" >> $FILE_TOPOLOGY_OUT
 
   # Clean temporary files
-  if [ $fl_clean == 1 ]
-    then
-    rm -f $FILE_TEMP
-    rm -f $FILE_TEMP2
-    rm -f $FILE_LINKSUM
-    rm -f $FILE_LINKSUM_NOCORE
-    rm -f $FILE_LINKSUM_NOCABLE
-    rm -f $FILE_NODEFIS
-    rm -f $FILE_NODESWITCHES
-    rm -f $FILE_NODELEAVES
-    rm -f $FILE_NODECHASSIS
-  fi
+  clean_tempfiles
 }  # End of gen_topology
 
 # Process rack group name; check for non-null name and find in tb_group[].
 # If present return tb_group[] index, otherwise make entry and return index.
 # Note that tb_group[0] is always null and is the default rack group.
@@ -591,13 +597,10 @@ proc_switch()
 }  # End of proc_switch()
 
 
 ## Main function:
 
-rm -f $FILE_DEBUG
-rm -f $FILE_DEBUG2
-
 # Get options
 while getopts c:d:i:Kv:? option
 do
   case $option in
   c)
diff --git a/IbaTools/opaxlattopology/opaxlattopology_cust.sh b/IbaTools/opaxlattopology/opaxlattopology_cust.sh
index 2107e76..c926a00 100755
--- a/IbaTools/opaxlattopology/opaxlattopology_cust.sh
+++ b/IbaTools/opaxlattopology/opaxlattopology_cust.sh
@@ -175,10 +175,20 @@ debug_7=
 
 
 ## Local functions:
 functout=
 
+function clean_tempfiles() {
+	if [ $fl_clean == 1 ]
+	then
+		rm -f $FILE_TEMP $FILE_TEMP2
+	fi
+}
+
+trap 'clean_tempfiles; exit 1' SIGINT SIGHUP SIGTERM
+trap 'clean_tempfiles' EXIT
+
 # Output usage information
 usage_full()
 {
 	echo "Usage: $PROGRAM_NAME -t topology_prime [-s topology_second] -T topology_out"
 	echo "                     [-v level] [-i level] [-c char] [-K] [-?]"
@@ -258,13 +268,10 @@ proc_coreswitch()
 }	# End of proc_corename
 
 
 ## Main function:
 
-rm -f $FILE_DEBUG
-rm -f $FILE_DEBUG2
-
 # Get options
 while getopts c:i:Ks:t:T:v:? option
 do
 	case $option in
 	c)
@@ -442,14 +449,10 @@ do
 	fi
 done	# End of for (( ix=0 ; $ix <= $max_coreswitch ; ix=$[ix+1] ))
 echo ",,,,,,,,,,,,,," >> ${file_topology_out}
 
 # Clean temporary files
-if [ $fl_clean == 1 ]
-	then
-	rm -f $FILE_TEMP
-	rm -f $FILE_TEMP2
-fi
+clean_tempfiles
 
 display_progress "Done"
 exit 0
 
diff --git a/fm_config/config_diff.sh b/fm_config/config_diff.sh
index 4e5d713..b8443f2 100755
--- a/fm_config/config_diff.sh
+++ b/fm_config/config_diff.sh
@@ -28,13 +28,14 @@
 # 
 # END_ICS_COPYRIGHT8   ****************************************
 
 # [ICS VERSION STRING: unknown]
 
-TEMP1=/tmp/smdiffa$$
-TEMP2=/tmp/smdiffb$$
+TEMP1=`mktemp`
+TEMP2=`mktemp`
 trap "rm -f $TEMP1 $TEMP2; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $TEMP1 $TEMP2" EXIT
 
 Usage()
 {
 	echo "Usage: config_diff [-f][-l] [-d 'diff_args'] file1 file2" >&2
 	echo "      -f  - filter out FM parameters which are not part of consistency check" >&2
diff --git a/fm_config/config_generate.sh b/fm_config/config_generate.sh
index 6c27672..c9db1f5 100755
--- a/fm_config/config_generate.sh
+++ b/fm_config/config_generate.sh
@@ -28,12 +28,13 @@
 # 
 # END_ICS_COPYRIGHT8   ****************************************
 
 # [ICS VERSION STRING: unknown]
 
-TEMP=/tmp/smgen$$
+TEMP=`mktemp`
 trap "rm -f $TEMP; exit 1" SIGHUP SIGTERM SIGINT
+trap "rm -f $TEMP" EXIT
 
 Usage()
 {
 	echo "Usage: config_generate [-e] dest_file" >&2
 	echo "      -e  - generate file for embedded FM, default is to generate for host FM" >&2

Vulnerable code: trap "exit 1" SIGHUP SIGTERM SIGINT
temp=/tmp/opascpall$$
temp=/tmp/opatest$$
tempfile=/tmp/opadisablehosts$$
tempfile=/tmp/opaenableports$$
tempfile=/tmp/opaextractbadlinks$$
tempfile=/tmp/opaextractlids$$
tempfile=/tmp/opaextractsellinks$$
tempfile=/tmp/opacabletest$$
## Includes
trap "exit 1" SIGHUP SIGTERM SIGINT

	clean_files
tempfile=/tmp/opalinkanalysis$$
	tmpdir=/tmp/12200FW$$
	mkdir $tmpdir
  if [ $fl_clean == 1 ]
    then
    rm -f $FILE_TEMP
    rm -f $FILE_TEMP2
    rm -f $FILE_LINKSUM
    rm -f $FILE_LINKSUM_NOCORE
    rm -f $FILE_LINKSUM_NOCABLE
    rm -f $FILE_NODEFIS
    rm -f $FILE_NODESWITCHES
    rm -f $FILE_NODELEAVES
    rm -f $FILE_NODECHASSIS
  fi
rm -f $FILE_DEBUG
rm -f $FILE_DEBUG2

rm -f $FILE_DEBUG
rm -f $FILE_DEBUG2

if [ $fl_clean == 1 ]
	then
	rm -f $FILE_TEMP
	rm -f $FILE_TEMP2
fi
TEMP1=/tmp/smdiffa$$
TEMP2=/tmp/smdiffb$$
TEMP=/tmp/smgen$$
Fixed code: temp="$(mktemp --tmpdir "opascpall.XXXXXX")"
trap "rm -f $temp; exit 1" SIGHUP SIGTERM SIGINT
trap "rm -f $temp" EXIT
temp=`mktemp`
temp=`mktemp`
tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
trap 'clean_files; exit 1' SIGINT SIGHUP SIGTERM 
trap clean_files EXIT

tempfile=`mktemp`
trap "rm -f $tempfile" EXIT
	tmpdir=`mktemp -d`
function clean_tempfiles() {
  if [ $fl_clean == 1 ]
    then
    rm -f $FILE_TEMP
    rm -f $FILE_TEMP2
    rm -f $FILE_LINKSUM
    rm -f $FILE_LINKSUM_NOCORE
    rm -f $FILE_LINKSUM_NOCABLE
    rm -f $FILE_NODEFIS
    rm -f $FILE_NODESWITCHES
    rm -f $FILE_NODELEAVES
    rm -f $FILE_NODECHASSIS
  fi
}

trap 'clean_tempfiles; exit 1' SIGINT SIGHUP SIGTERM
trap 'clean_tempfiles' EXIT
  clean_tempfiles
function clean_tempfiles() {
	if [ $fl_clean == 1 ]
	then
		rm -f $FILE_TEMP $FILE_TEMP2
	fi
}

trap 'clean_tempfiles; exit 1' SIGINT SIGHUP SIGTERM
trap 'clean_tempfiles' EXIT

clean_tempfiles
TEMP1=`mktemp`
TEMP2=`mktemp`
trap "rm -f $TEMP1 $TEMP2" EXIT
TEMP=`mktemp`
trap "rm -f $TEMP" EXIT