Blame SOURCES/mod-blacklist.sh

b1b780
#! /bin/bash
b1b780
b1b780
RpmDir=$1
b1b780
ModDir=$2
b1b780
Dir="$1/$2"
b1b780
# Note the list filename must have the format mod-[PACKAGE].list, for example,
b1b780
# mod-internal.list or mod-extra.list.  The PACKAGE is used to create a
b1b780
# override directory for the modules.
b1b780
List=$3
b1b780
Dest="$4"
b1b780
b1b780
blacklist()
b1b780
{
b1b780
	cat > "$RpmDir/etc/modprobe.d/$1-blacklist.conf" <<-__EOF__
b1b780
	# This kernel module can be automatically loaded by non-root users. To
b1b780
	# enhance system security, the module is blacklisted by default to ensure
b1b780
	# system administrators make the module available for use as needed.
b1b780
	# See https://access.redhat.com/articles/3760101 for more details.
b1b780
	#
b1b780
	# Remove the blacklist by adding a comment # at the start of the line.
b1b780
	blacklist $1
b1b780
__EOF__
b1b780
}
b1b780
b1b780
check_blacklist()
b1b780
{
b1b780
	mod=$(find $RpmDir/$ModDir -name "$1")
b1b780
	[ ! "$mod" ] && return 0
b1b780
	if modinfo $mod | grep -q '^alias:\s\+net-'; then
b1b780
		mod="${1##*/}"
b1b780
		mod="${mod%.ko*}"
b1b780
		echo "$mod has an alias that allows auto-loading. Blacklisting."
b1b780
		blacklist "$mod"
b1b780
	fi
b1b780
}
b1b780
b1b780
find_depends()
b1b780
{
b1b780
	dep=$1
b1b780
	depends=`modinfo $dep | sed -n -e "/^depends/ s/^depends:[ \t]*//p"`
b1b780
	[ -z "$depends" ] && exit
b1b780
	for mod in ${depends//,/ }
b1b780
	do
b1b780
		match=$(grep "^$mod.ko" "$ListName")
b1b780
		[ -z "$match" ] && continue
b1b780
		# check if the module we are looking at is in mod-* too.
b1b780
		# if so we do not need to mark the dep as required.
b1b780
		mod2=${dep##*/}  # same as `basename $dep`, but faster
b1b780
		match2=$(grep "^$mod2" "$ListName")
b1b780
		if [ -n "$match2" ]
b1b780
		then
b1b780
			#echo $mod2 >> notreq.list
b1b780
			continue
b1b780
		fi
b1b780
		echo $mod.ko >> req.list
b1b780
	done
b1b780
}
b1b780
b1b780
foreachp()
b1b780
{
b1b780
	P=$(nproc)
b1b780
	bgcount=0
b1b780
	while read mod; do
b1b780
		$1 "$mod" &
b1b780
b1b780
		bgcount=$((bgcount + 1))
b1b780
		if [ $bgcount -eq $P ]; then
b1b780
			wait -n
b1b780
			bgcount=$((bgcount - 1))
b1b780
		fi
b1b780
	done
b1b780
b1b780
	wait
b1b780
}
b1b780
b1b780
# Destination was specified on the command line
b1b780
test -n "$4" && echo "$0: Override Destination $Dest has been specified."
b1b780
b1b780
pushd $Dir
b1b780
b1b780
OverrideDir=$(basename $List)
b1b780
OverrideDir=${OverrideDir%.*}
b1b780
OverrideDir=${OverrideDir#*-}
b1b780
mkdir -p $OverrideDir
b1b780
b1b780
rm -rf modnames
b1b780
find . -name "*.ko" -type f > modnames
b1b780
# Look through all of the modules, and throw any that have a dependency in
b1b780
# our list into the list as well.
b1b780
rm -rf dep.list dep2.list
b1b780
rm -rf req.list req2.list
b1b780
touch dep.list req.list
b1b780
cp "$List" .
b1b780
b1b780
# This variable needs to be exported because it is used in sub-script
b1b780
# executed by xargs
b1b780
export ListName=$(basename "$List")
b1b780
b1b780
foreachp find_depends < modnames
b1b780
b1b780
sort -u req.list > req2.list
b1b780
sort -u "$ListName" > modules2.list
b1b780
join -v 1 modules2.list req2.list > modules3.list
b1b780
b1b780
for mod in $(cat modules3.list)
b1b780
do
b1b780
  # get the path for the module
b1b780
  modpath=`grep /$mod modnames`
b1b780
  [ -z "$modpath" ] && continue
b1b780
  echo $modpath >> dep.list
b1b780
done
b1b780
b1b780
sort -u dep.list > dep2.list
b1b780
b1b780
if [ -n "$Dest" ]; then
b1b780
	# now move the modules into the $Dest directory
b1b780
	for mod in `cat dep2.list`
b1b780
	do
b1b780
	  newpath=`dirname $mod | sed -e "s/kernel\\//$Dest\//"`
b1b780
	  mkdir -p $newpath
b1b780
	  mv $mod $newpath
b1b780
	  echo $mod | sed -e "s/kernel\\//$Dest\//" | sed -e "s|^.|${ModDir}|g" >> $RpmDir/$ListName
b1b780
	done
b1b780
fi
b1b780
b1b780
popd
b1b780
b1b780
# If we're signing modules, we can't leave the .mod files for the .ko files
b1b780
# we've moved in .tmp_versions/.  Remove them so the Kbuild 'modules_sign'
b1b780
# target doesn't try to sign a non-existent file.  This is kinda ugly, but
b1b780
# so are the modules-* packages.
b1b780
b1b780
for mod in `cat ${Dir}/dep2.list`
b1b780
do
b1b780
  modfile=`basename $mod | sed -e 's/.ko/.mod/'`
b1b780
  rm .tmp_versions/$modfile
b1b780
done
b1b780
b1b780
if [ ! -n "$Dest" ]; then
b1b780
	sed -e "s|^.|${ModDir}|g" ${Dir}/dep2.list > $RpmDir/$ListName
b1b780
	echo "./$RpmDir/$ListName created."
b1b780
	[ -d "$RpmDir/etc/modprobe.d/" ] || mkdir -p "$RpmDir/etc/modprobe.d/"
b1b780
	foreachp check_blacklist < $List
b1b780
fi
b1b780
b1b780
# avoid an empty kernel-extra package
b1b780
echo "$ModDir/$OverrideDir" >> $RpmDir/$ListName
b1b780
b1b780
pushd $Dir
b1b780
rm modnames dep.list dep2.list req.list req2.list
b1b780
rm "$ListName" modules2.list modules3.list
b1b780
popd