Skip to content

Instantly share code, notes, and snippets.

@toonetown
Created March 14, 2019 17:46
Show Gist options
  • Save toonetown/a82af9c92d58fe0491f2a2aa82e53aab to your computer and use it in GitHub Desktop.
Save toonetown/a82af9c92d58fe0491f2a2aa82e53aab to your computer and use it in GitHub Desktop.
Checks the linkage of a kext
###########
# Copies a kext to a temporary location and test loading it (to make sure that all
# library linkages are in place). This is a standalone script because in order
# for a kext to be loaded, it must have ownership of root:wheel - which requires
# root to change.
#
# This script will call sudo on itself if it is run as non-root.
#
# In order to prevent this script for prompting for a password (i.e. for use in
# continuous integration or for other cases where you are too lazy to type in
# your password), you can add a file to /etc/sudoers.d by running the following
# command:
# > sudo visudo -f /etc/sudoers.d/check-kext-linkage
#
# And adding the following lines to the file (assuming you install this script
# to /usr/local/bin/check-kext-linkage):
# %admin ALL=(root) NOPASSWD: /usr/local/bin/check-kext-linkage
# Defaults env_keep += "TMPDIR"
#
#!/bin/bash
[ -d "${1}" ] || {
echo "Usage: ${0} <path/to/test.kext>" >&2
exit 1
}
# Make sure we are running as root
if [ "$(id -u)" != "0" ]; then /usr/bin/sudo "${0}" "$@"; exit $?; fi
# Copy to our temporary location - set it to clean up whenever the script exits
TMPKEXT="${TMPDIR:-/tmp}/check-kext.$$.kext"
/bin/cp -r "${1}" "${TMPKEXT}" || exit $?
trap 'rm -rf "${TMPKEXT}"' EXIT
# Change permissions - kext ownership should be root:wheel in order to work
/usr/sbin/chown -R root:wheel "${TMPKEXT}" || exit $?
# Test load the kext - this should fail if we are missing dependencies, or if there is
# something else wrong with the kext
/usr/bin/kextutil -nt "${TMPKEXT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment