/[gentoo-x86]/eclass/pax-utils.eclass
Gentoo

Diff of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 Revision 1.12
1# Copyright 1999-2006 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.2 2006/11/15 22:14:25 kevquinn Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.12 2011/07/02 17:03:51 blueness Exp $
4 4
5# Author: 5# @ECLASS: pax-utils.eclass
6# @MAINTAINER:
7# Maintained by
8# The Gentoo Linux Hardened Team <hardened@gentoo.org>
9# Original Author
6# Kevin F. Quinn <kevquinn@gentoo.org> 10# Kevin F. Quinn <kevquinn@gentoo.org>
11# Modifications for bug #365825, @ ECLASS markup
12# Anthony G. Basile <blueness@gentoo.org>
13# @BLURB: functions to provide pax markings
14# @DESCRIPTION:
15# This eclass provides support for manipulating PaX markings on ELF binaries,
16# wrapping the use of the paxctl and scanelf utilities. It decides which to
17# use depending on what is installed on the build host, preferring paxctl to
18# scanelf. If paxctl is not installed, we fall back to scanelf since it is
19# always present. However, currently scanelf doesn't do all that paxctl can.
7# 20#
8# This eclass provides support for manipulating PaX markings on ELF 21# To control what markings are made, set PAX_MARKINGS in /etc/make.conf to
9# binaries, wrapping the use of the chpax and paxctl utilities. 22# contain either "PT" or "none". If PAX_MARKINGS is set to "PT", and the
23# necessary utility is installed, the PT_PAX_FLAGS markings will be made. If
24# PAX_MARKINGS is set to "none", no markings will be made.
10 25
11inherit eutils 26inherit eutils
12 27
13##### pax-mark #### 28IUSE="hardened"
14# Mark a file for PaX, with the provided flags, and log it into 29
15# a PaX database. Returns non-zero if flag marking failed. 30DEPEND="hardened? ( app-misc/pax-utils
31 sys-apps/paxctl )"
32
33# Default to PT markings.
34PAX_MARKINGS=${PAX_MARKINGS:="PT"}
35
36# @FUNCTION: pax-mark
37# @USAGE: <flags> {<ELF files>}
38# @RETURN: Shell true if we succeed, shell false otherwise
39# @DESCRIPTION:
40# Marks <ELF files> with provided PaX <flags>
16# 41#
17# If paxctl is installed, but not chpax, then the legacy 42# Flags are passed directly to the utilities unchanged. Possible flags at the
18# EI flags (which are not strip-safe) will not be set. 43# time of writing, taken from /sbin/paxctl, are:
19# If neither are installed, falls back to scanelf (which 44#
20# is always present, but currently doesn't quite do all 45# p: disable PAGEEXEC P: enable PAGEEXEC
21# that paxctl can do). 46# e: disable EMUTRMAP E: enable EMUTRMAP
47# m: disable MPROTECT M: enable MPROTECT
48# r: disable RANDMMAP R: enable RANDMMAP
49# s: disable SEGMEXEC S: enable SEGMEXEC
50#
51# Default flags are 'PeMRS', which are the most restrictive settings. Refer
52# to http://pax.grsecurity.net/ for details on what these flags are all about.
53# Do not use the obsolete flag 'x'/'X' which has been deprecated.
54#
55# Please confirm any relaxation of restrictions with the Gentoo Hardened team.
56# Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
57# the bug report.
58pax-mark() {
59 # It doesn't make sense to pax-mark on non-hardened systems
60 # so we'll just do nothing.
61 use hardened || return 0;
22 62
23pax-mark() { 63 local f flags fail=0 failures="" zero_load_alignment
24 local flags fail=0 64 # Ignore '-' characters - in particular so that it doesn't matter if
65 # the caller prefixes with -
25 flags=$1 66 flags=${1//-}
26 shift 67 shift
27 if [[ -x /sbin/chpax ]]; then 68 # Try paxctl, then scanelf. paxctl is preferred.
28 einfo "Legacy EI PaX marking $* with ${flags}" 69 if type -p paxctl > /dev/null && hasq PT ${PAX_MARKINGS}; then
29 /sbin/chpax -${flags} $* || fail=1 70 # Try paxctl, the upstream supported tool.
71 elog "PT PaX marking -${flags}"
72 _pax_list_files elog "$@"
73 for f in "$@"; do
74 # First, try modifying the existing PAX_FLAGS header
75 paxctl -q${flags} "${f}" && continue
76 # Second, try stealing the (unused under PaX) PT_GNU_STACK header
77 paxctl -qc${flags} "${f}" && continue
78 # Third, try pulling the base down a page, to create space and
79 # insert a PT_GNU_STACK header (works on ET_EXEC)
80 paxctl -qC${flags} "${f}" && continue
81 #
82 # prelink is masked on hardened so we wont use this method.
83 # We're working on a new utiity to try to do the same safely. See
84 # http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=summary
85 #
86 # Fourth - check if it loads to 0 (probably an ET_DYN) and if so,
87 # try rebasing with prelink first to give paxctl some space to
88 # grow downwards into.
89 #if type -p objdump > /dev/null && type -p prelink > /dev/null; then
90 # zero_load_alignment=$(objdump -p "${f}" | \
91 # grep -E '^[[:space:]]*LOAD[[:space:]]*off[[:space:]]*0x0+[[:space:]]' | \
92 # sed -e 's/.*align\(.*\)/\1/')
93 # if [[ ${zero_load_alignment} != "" ]]; then
94 # prelink -r $(( 2*(${zero_load_alignment}) )) &&
95 # paxctl -qC${flags} "${f}" && continue
96 # fi
97 #fi
98 fail=1
99 failures="${failures} ${f}"
100 done
101 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
102 # Try scanelf, the Gentoo swiss-army knife ELF utility
103 # Currently this sets PT if it can, no option to control what it does.
104 elog "Fallback PaX marking -${flags}"
105 _pax_list_files elog "$@"
106 scanelf -Xxz ${flags} "$@"
107 elif [[ ${PAX_MARKINGS} != "none" ]]; then
108 # Out of options!
109 failures="$*"
110 fail=1
30 fi 111 fi
31 if [[ -x /sbin/paxctl ]]; then 112 if [[ ${fail} == 1 ]]; then
32 einfo "PT PaX marking $* with ${flags}" 113 ewarn "Failed to set PaX markings -${flags} for:"
33 /sbin/paxctl -${flags} $* || 114 _pax_list_files ewarn ${failures}
34 /sbin/paxctl -c${flags} $* || 115 ewarn "Executables may be killed by PaX kernels."
35 /sbin/paxctl -C${flags} $* || fail=1
36 elif [[ -x /usr/bin/scanelf ]]; then
37 einfo "Fallback PaX marking $* with ${flags}"
38 /usr/bin/scanelf -Xxz ${flags} $*
39 else
40 ewarn "Failed to set PaX markings ${flags} for files $*. Executables may be killed by PaX kernels."
41 fail=1
42 fi 116 fi
43 return ${fail} 117 return ${fail}
44} 118}
45 119
46##### host-is-pax 120# @FUNCTION: list-paxables
47# Indicates whether the build machine has PaX or not; intended for use 121# @USAGE: {<files>}
48# where the build process must be modified conditionally in order to satisfy PaX. 122# @RETURN: Subset of {<files>} which are ELF executables or shared objects
123# @DESCRIPTION:
124# Print to stdout all of the <files> that are suitable to have PaX flag
125# markings, i.e., filter out the ELF executables or shared objects from a list
126# of files. This is useful for passing wild-card lists to pax-mark, although
127# in general it is preferable for ebuilds to list precisely which ELFS are to
128# be marked. Often not all the ELF installed by a package need remarking.
129# @EXAMPLE:
130# pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
131list-paxables() {
132 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
133}
134
135# @FUNCTION: host-is-pax
136# @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
137# @DESCRIPTION:
138# This is intended for use where the build process must be modified conditionally
139# depending on whether the host is PaX enabled or not. It is not intedened to
140# determine whether the final binaries need PaX markings. Note: if procfs is
141# not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
49host-is-pax() { 142host-is-pax() {
50 # We need procfs to work this out. PaX is only available on Linux,
51 # so result is always false on non-linux machines (e.g. Gentoo/*BSD)
52 [[ -e /proc/self/status ]] || return 1
53 grep ^PaX: /proc/self/status > /dev/null 143 grep -qs ^PaX: /proc/self/status
54 return $?
55} 144}
145
146
147# INTERNAL FUNCTIONS
148# ------------------
149#
150# These functions are for use internally by the eclass - do not use
151# them elsewhere as they are not supported (i.e. they may be removed
152# or their function may change arbitratily).
153
154# Display a list of things, one per line, indented a bit, using the
155# display command in $1.
156_pax_list_files() {
157 local f cmd
158 cmd=$1
159 shift
160 for f in "$@"; do
161 ${cmd} " ${f}"
162 done
163}
164

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.20