diff options
author | 2012-11-10 19:18:35 -0500 | |
---|---|---|
committer | 2012-11-10 19:18:35 -0500 | |
commit | eb3a2e198c926aca7063aa036793bb94bfbec1ef (patch) | |
tree | c08bdf210b4913d238928068e1b1256cd18447bd | |
parent | Clear out the branch in case it causes problems (diff) | |
download | hardened-dev-XT_PAX.tar.gz hardened-dev-XT_PAX.tar.bz2 hardened-dev-XT_PAX.zip |
pax-utils.eclass: updated eclass to handle PT_PAX and XT_PAXXT_PAX
-rw-r--r-- | profiles/eclass/pax-utils.eclass | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/profiles/eclass/pax-utils.eclass b/profiles/eclass/pax-utils.eclass new file mode 100644 index 00000000..1bab9ff5 --- /dev/null +++ b/profiles/eclass/pax-utils.eclass @@ -0,0 +1,198 @@ +# Copyright 1999-2011 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.18 2012/04/06 18:03:54 blueness Exp $ + +# @ECLASS: pax-utils.eclass +# @MAINTAINER: +# The Gentoo Linux Hardened Team <hardened@gentoo.org> +# @AUTHOR: +# Original Author: Kevin F. Quinn <kevquinn@gentoo.org> +# Modifications for bug #365825, @ ECLASS markup: Anthony G. Basile <blueness@gentoo.org> +# Modifications for bug #431092: Anthony G. Basile <blueness@gentoo.org> +# @BLURB: functions to provide pax markings +# @DESCRIPTION: +# This eclass provides support for manipulating PaX markings on ELF binaries, +# wrapping the use of the paxctl and scanelf utilities. It decides which to +# use depending on what is installed on the build host, preferring paxctl to +# scanelf. If paxctl is not installed, we fall back to scanelf since it is +# always present. However, currently scanelf doesn't do all that paxctl can. +# +# To control what markings are made, set PAX_MARKINGS in /etc/portage/make.conf +# to contain either "PT", "XT" or "none". If PAX_MARKINGS contains "PT", and +# the necessary utility is installed, then PT_PAX_FLAGS markings will be made. +# Similarly, if PAX_MARKINGS contains "XT", then xattr markings will be made. +# If PAX_MARKINGS is set to "none", no markings will be made. + +if [[ ${___ECLASS_ONCE_PAX_UTILS} != "recur -_+^+_- spank" ]] ; then +___ECLASS_ONCE_PAX_UTILS="recur -_+^+_- spank" + +# Default to PT markings. +PAX_MARKINGS=${PAX_MARKINGS:="PT XT"} + +# @FUNCTION: pax-mark +# @USAGE: <flags> {<ELF files>} +# @RETURN: Shell true if we succeed, shell false otherwise +# @DESCRIPTION: +# Marks <ELF files> with provided PaX <flags> +# +# Flags are passed directly to the utilities unchanged. Possible flags at the +# time of writing, taken from /sbin/paxctl, are: +# +# p: disable PAGEEXEC P: enable PAGEEXEC +# e: disable EMUTRMAP E: enable EMUTRMAP +# m: disable MPROTECT M: enable MPROTECT +# r: disable RANDMMAP R: enable RANDMMAP +# s: disable SEGMEXEC S: enable SEGMEXEC +# +# Default flags are 'PeMRS', which are the most restrictive settings. Refer +# to http://pax.grsecurity.net/ for details on what these flags are all about. +# Do not use the obsolete flag 'x'/'X' which has been deprecated. +# +# Please confirm any relaxation of restrictions with the Gentoo Hardened team. +# Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on +# the bug report. + +pax-mark() { + + local f # loop over paxables + local flags # pax flags + local pt_fail=0 pt_failures="" # record PT_PAX failures + local xt_fail=0 xt_failures="" # record xattr PAX marking failures + local ret=0 # overal return code of this function + + # You can call pax-mark with/out leading '-' on flags + flags=${1//-} + shift + + if has PT ${PAX_MARKINGS}; then + + #First try paxctl-ng + if type -p paxctl-ng > /dev/null; then + einfo "PT PaX marking -${flags}" + _pax_list_files einfo "$@" + for f in "$@"; do + paxctl-ng -L -${flags} "${f}" && continue + pt_fail=1 + pt_failures="${pt_failures} ${f}" + done + + #Next try paxctl + elif type -p paxctl > /dev/null; then + einfo "PT PaX marking -${flags}" + _pax_list_files einfo "$@" + for f in "$@"; do + # First, try modifying the existing PAX_FLAGS header + paxctl -q${flags} "${f}" && continue + # Second, try stealing the (unused under PaX) PT_GNU_STACK header + paxctl -qc${flags} "${f}" && continue + # Third, creating a PT_PAX header (works on ET_EXEC) + paxctl -qC${flags} "${f}" && continue + pt_fail=1 + pt_failures="${pt_failures} ${f}" + done + + #Finally fall back on scanelf + elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then + einfo "Fallback PaX marking -${flags}" + _pax_list_files einfo "$@" + scanelf -Xxz ${flags} "$@" + + #We failed to set PT_PAX flags + elif [[ ${PAX_MARKINGS} != "none" ]]; then + pt_failures="$*" + pt_fail=1 + fi + + if [[ ${pt_fail} == 1 ]]; then + ewarn "Failed to set XT_PAX markings -${flags} for:" + _pax_list_files ewarn ${pt_failures} + ret=1 + fi + fi + + if has XT ${PAX_MARKINGS}; then + + #First try paxctl-ng + if type -p paxctl-ng > /dev/null; then + einfo "XT PaX marking -${flags}" + _pax_list_files einfo "$@" + for f in "$@"; do + paxctl-ng -l -${flags} "${f}" && continue + xt_fail=1 + xt_failures="${tx_failures} ${f}" + done + + #Next try set/get + elif type -p setfattr > /dev/null; then + einfo "XT PaX marking -${flags}" + _pax_list_files einfo "$@" + for f in "$@"; do + setfattr -n "user.pax.flags" -v "${flags}" "${f}" && continue + xt_fail=1 + xt_failures="${tx_failures} ${f}" + done + + #We failed to set PT_PAX flags + elif [[ ${PAX_MARKINGS} != "none" ]]; then + pt_failures="$*" + pt_fail=1 + fi + + if [[ ${xt_fail} == 1 ]]; then + ewarn "Failed to set XT_PAX markings -${flags} for:" + _pax_list_files ewarn ${xt_failures} + ret=1 + fi + fi + + [[ ${ret} == 1 ]] && ewarn "Executables may be killed by PaX kernels." + + return ${ret} +} + +# @FUNCTION: list-paxables +# @USAGE: {<files>} +# @RETURN: Subset of {<files>} which are ELF executables or shared objects +# @DESCRIPTION: +# Print to stdout all of the <files> that are suitable to have PaX flag +# markings, i.e., filter out the ELF executables or shared objects from a list +# of files. This is useful for passing wild-card lists to pax-mark, although +# in general it is preferable for ebuilds to list precisely which ELFS are to +# be marked. Often not all the ELF installed by a package need remarking. +# @EXAMPLE: +# pax-mark -m $(list-paxables ${S}/{,usr/}bin/*) +list-paxables() { + file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//' +} + +# @FUNCTION: host-is-pax +# @RETURN: Shell true if the build process is PaX enabled, shell false otherwise +# @DESCRIPTION: +# This is intended for use where the build process must be modified conditionally +# depending on whether the host is PaX enabled or not. It is not intedened to +# determine whether the final binaries need PaX markings. Note: if procfs is +# not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD). +host-is-pax() { + grep -qs ^PaX: /proc/self/status +} + + +# INTERNAL FUNCTIONS +# ------------------ +# +# These functions are for use internally by the eclass - do not use +# them elsewhere as they are not supported (i.e. they may be removed +# or their function may change arbitratily). + +# Display a list of things, one per line, indented a bit, using the +# display command in $1. +_pax_list_files() { + local f cmd + cmd=$1 + shift + for f in "$@"; do + ${cmd} " ${f}" + done +} + +fi |