Last active
July 29, 2026 15:25
-
-
Save achilleas-k/32c03f118ea10ba5a5e4742ac46685da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/bash | |
| set -euo pipefail | |
| print_selinux_labels() { | |
| # ls -Z and getfattr will show unlabeled_t because the kernel gets in the way | |
| # and maps the security attributes with the database. | |
| # Read the raw bytes instead using debugfs | |
| fpath=$(realpath "${1}") | |
| partition="/dev/sda4" # update to match the fs partition for the current directory | |
| echo ":: Getting security.selinux attributes for $fpath via debugfs on $partition" | |
| sudo debugfs -R "ea_get $fpath security.selinux" "${partition}" | |
| } | |
| tmpdir=$(mktemp -d -p .) | |
| cleanup() { | |
| rm -rf "${tmpdir}" | |
| } | |
| trap cleanup EXIT | |
| # This file will receive an arbitrary selinux label | |
| targetfile="${tmpdir}/somefile" | |
| touch "${targetfile}" | |
| labelscript="${tmpdir}/label.sh" | |
| # Little script that sets the label (so we can give it install_exec_t) | |
| cat > "${labelscript}" << EOF | |
| #!/usr/bin/bash | |
| # Needs sudo for CAP_MAC_ADMIN | |
| sudo chcon -t some_arbitrary_label "${targetfile}" | |
| EOF | |
| chmod +x "${labelscript}" | |
| # label.sh can't set arbitrary labels | |
| echo ":: Running ${labelscript} without install_exec_t" | |
| if ! ${labelscript}; then | |
| echo ":: Failed successfully \\o/" | |
| fi | |
| print_selinux_labels "${targetfile}" | |
| # Give label.sh install_exec_t | |
| echo ":: Setting install_exec_t on ${labelscript}" | |
| sudo chcon -t install_exec_t "${labelscript}" | |
| echo ":: Running ${labelscript} with install_exec_t" | |
| ${labelscript} | |
| print_selinux_labels "${targetfile}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.