@@ -339,6 +339,33 @@ file_not_exists() {
339339 fi
340340}
341341
342+ # Check if the tests are running in a chroot. Copied from dkms main script.
343+ running_in_chroot () {
344+ # Check for fakechroot usage. Works without root.
345+ # shellcheck disable=SC2076
346+ [[ " ${FAKECHROOT-} " == ' true' && -v FAKECHROOT_BASE && " ${LD_PRELOAD-} " =~ ' libfakechroot.so' ]] && return 0
347+
348+ # Check if our view of /proc/PID/mountinfo differs from the init process's view. Works without root.
349+ if [[ -r /proc/1/mountinfo && -r /proc/self/mountinfo ]]; then
350+ local init_mountinfo
351+ local our_mountinfo
352+ IFS= read -rd ' ' init_mountinfo < ' /proc/1/mountinfo'
353+ IFS= read -rd ' ' our_mountinfo < ' /proc/self/mountinfo'
354+ [[ " ${init_mountinfo} " != " ${our_mountinfo} " ]] && return 0
355+ fi
356+
357+ # Check if our root directory differs from the init process's root directory, using inode and device numbers. Requires root.
358+ if [[ " $( id -u) " = 0 ]]; then
359+ local init_rootdir_info
360+ local our_rootdir_info
361+ init_rootdir_info=" $( stat -L --format=' %d %i' /proc/1/root) "
362+ our_rootdir_info=" $( stat -L --format=' %d %i' /) "
363+ [[ " ${init_rootdir_info} " != " ${our_rootdir_info} " ]] && return 0
364+ fi
365+
366+ return 1
367+ }
368+
342369mod_compression_ext=
343370kernel_config=" /lib/modules/${KERNEL_VER} /build/.config"
344371if [[ -f $kernel_config ]]; then
@@ -353,6 +380,12 @@ if [[ -f $kernel_config ]]; then
353380 fi
354381fi
355382
383+ is_running_in_chroot=false
384+ if running_in_chroot; then
385+ is_running_in_chroot=true
386+ fi
387+ try_sign_modules_file=' /etc/dkms/framework.conf.d/try_sign_modules.conf'
388+
356389# Compute the expected destination module location
357390os_id=" $( sed -n ' s/^ID\s*=\s*\(.*\)$/\1/p' /etc/os-release | tr -d ' "' ) "
358391distro_sign_file_candidates=
@@ -431,6 +464,11 @@ DKMS_VERSION="$(dkms --version)"
431464echo ' Preparing a clean test environment'
432465clean_dkms_env
433466
467+ if [[ $is_running_in_chroot = true ]] && (( NO_SIGNING_TOOL == 0 )) ; then
468+ echo ' Enabling module signing in chroot'
469+ install_framework_conf test/framework/try_sign_modules_true.conf " ${try_sign_modules_file} "
470+ fi
471+
434472echo ' Test that there are no dkms modules installed'
435473run_with_expected_output dkms status -k " ${KERNEL_VER} " << EOF
436474EOF
@@ -967,6 +1005,55 @@ Before uninstall, this module version was ACTIVE on this kernel.
9671005Deleting /lib/modules/${KERNEL_VER} /${expected_dest_loc} /dkms_test.ko${mod_compression_ext}
9681006Running depmod... done.
9691007
1008+ Deleting module dkms_test/1.0 completely from the DKMS tree.
1009+ EOF
1010+ run_status_with_expected_output ' dkms_test' << EOF
1011+ EOF
1012+
1013+ echo ' Adding the test module'
1014+ run_with_expected_output dkms add test/dkms_test-1.0 << EOF
1015+ Creating symlink /var/lib/dkms/dkms_test/1.0/source -> /usr/src/dkms_test-1.0
1016+ EOF
1017+ check_module_source_tree_created /usr/src/dkms_test-1.0
1018+ run_status_with_expected_output ' dkms_test' << EOF
1019+ dkms_test/1.0: added
1020+ EOF
1021+
1022+ echo ' Building the test module with try_sign_modules=false'
1023+ install_framework_conf test/framework/try_sign_modules_false.conf " ${try_sign_modules_file} "
1024+ run_with_expected_output dkms build -k " ${KERNEL_VER} " -m dkms_test -v 1.0 --force << EOF
1025+ Module signing is disabled by policy, modules won't be signed
1026+
1027+ Building module(s)... done.
1028+ EOF
1029+
1030+ echo ' Building the test module with try_sign_modules=true'
1031+ install_framework_conf test/framework/try_sign_modules_true.conf " ${try_sign_modules_file} "
1032+ run_with_expected_output dkms build -k " ${KERNEL_VER} " -m dkms_test -v 1.0 --force << EOF
1033+ ${SIGNING_PROLOGUE_tmp_key_cert}
1034+ Building module(s)... done.${SIGNING_MESSAGE}
1035+ EOF
1036+
1037+ if [[ $is_running_in_chroot = true ]]; then
1038+ echo ' Building the test module in a chroot with try_sign_modules=not_in_chroot'
1039+ install_framework_conf test/framework/try_sign_modules_not_in_chroot.conf " ${try_sign_modules_file} "
1040+ run_with_expected_output dkms build -k " ${KERNEL_VER} " -m dkms_test -v 1.0 --force << EOF
1041+ Running in chroot, modules won't be signed
1042+
1043+ Building module(s)... done.
1044+ EOF
1045+
1046+ echo ' Re-enabling module signing in chroot'
1047+ install_framework_conf test/framework/try_sign_modules_true.conf " ${try_sign_modules_file} "
1048+ else
1049+ echo ' Removing try_sign_modules configuration'
1050+ rm " ${try_sign_modules_file} "
1051+ fi
1052+
1053+ echo ' Removing the test module'
1054+ run_with_expected_output dkms remove -k " ${KERNEL_VER} " -m dkms_test -v 1.0 << EOF
1055+ Module dkms_test/1.0 is not installed for kernel ${KERNEL_VER} (${KERNEL_ARCH} ). Skipping...
1056+
9701057Deleting module dkms_test/1.0 completely from the DKMS tree.
9711058EOF
9721059 run_status_with_expected_output ' dkms_test' << EOF
0 commit comments