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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (hide annotations) (download)
Sat Dec 17 04:23:53 2011 UTC (3 years ago) by vapier
Branch: MAIN
Changes since 1.16: +5 -1 lines
avoid multiple inclusions when possible to speed caching up

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

  ViewVC Help
Powered by ViewVC 1.1.20