Blame SOURCES/mod-denylist.sh

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