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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (hide annotations) (download)
Sat May 18 13:43:20 2013 UTC (19 months ago) by zorry
Branch: MAIN
Changes since 1.20: +2 -2 lines
Fix a typo in pax-utils.eclass

1 blueness 1.19 # Copyright 1999-2013 Gentoo Foundation
2 kevquinn 1.1 # Distributed under the terms of the GNU General Public License v2
3 zorry 1.21 # $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.20 2013/04/09 23:43:53 zorry 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.19 # Modifications for bug #431092: Anthony G. Basile <blueness@gentoo.org>
12 blueness 1.11 # @BLURB: functions to provide pax markings
13     # @DESCRIPTION:
14 blueness 1.19 #
15 blueness 1.11 # This eclass provides support for manipulating PaX markings on ELF binaries,
16 blueness 1.19 # whether the system is using legacy PT_PAX markings or the newer XATTR_PAX.
17     # The eclass wraps the use of paxctl-ng, paxctl, set/getattr and scanelf utilities,
18     # deciding which to use depending on what's installed on the build host, and
19     # whether we're working with PT_PAX, XATTR_PAX or both.
20     #
21     # To control what markings are made, set PAX_MARKINGS in /etc/portage/make.conf
22     # to contain either "PT", "XT" or "none". The default is to attempt both
23     # PT_PAX and XATTR_PAX.
24 blueness 1.11
25 vapier 1.17 if [[ ${___ECLASS_ONCE_PAX_UTILS} != "recur -_+^+_- spank" ]] ; then
26     ___ECLASS_ONCE_PAX_UTILS="recur -_+^+_- spank"
27    
28 blueness 1.19 # @ECLASS-VARIABLE: PAX_MARKINGS
29     # @DESCRIPTION:
30     # Control which markings are made:
31     # PT = PT_PAX markings, XT = XATTR_PAX markings
32 zorry 1.20 # Default to PT markings.
33     PAX_MARKINGS=${PAX_MARKINGS:="PT"}
34 blueness 1.11
35     # @FUNCTION: pax-mark
36     # @USAGE: <flags> {<ELF files>}
37     # @RETURN: Shell true if we succeed, shell false otherwise
38     # @DESCRIPTION:
39     # Marks <ELF files> with provided PaX <flags>
40 kevquinn 1.4 #
41 blueness 1.19 # Flags are passed directly to the utilities unchanged
42 kevquinn 1.4 #
43     # p: disable PAGEEXEC P: enable PAGEEXEC
44 zorry 1.21 # e: disable EMUTRAMP E: enable EMUTRAMP
45 kevquinn 1.4 # m: disable MPROTECT M: enable MPROTECT
46     # r: disable RANDMMAP R: enable RANDMMAP
47     # s: disable SEGMEXEC S: enable SEGMEXEC
48     #
49 blueness 1.11 # Default flags are 'PeMRS', which are the most restrictive settings. Refer
50     # to http://pax.grsecurity.net/ for details on what these flags are all about.
51     #
52     # Please confirm any relaxation of restrictions with the Gentoo Hardened team.
53     # Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
54     # the bug report.
55 kevquinn 1.1 pax-mark() {
56 blueness 1.19
57     local f # loop over paxables
58     local flags # pax flags
59     local pt_fail=0 pt_failures="" # record PT_PAX failures
60     local xt_fail=0 xt_failures="" # record xattr PAX marking failures
61     local ret=0 # overal return code of this function
62    
63     # Only the actual PaX flags and z are accepted
64     # 1. The leading '-' is optional
65     # 2. -C -c only make sense for paxctl, but are unnecessary
66     # because we progressively do -q -qc -qC
67     # 3. z is allowed for the default
68    
69     flags="${1//[!zPpEeMmRrSs]}"
70     [[ "${flags}" ]] || return 0
71 kevquinn 1.2 shift
72 blueness 1.19
73     # z = default. For XATTR_PAX, the default is no xattr field at all
74     local dodefault=""
75     [[ "${flags//[!z]}" ]] && dodefault="yes"
76    
77     if has PT ${PAX_MARKINGS}; then
78    
79     #First try paxctl -> this might try to create/convert program headers
80     if type -p paxctl > /dev/null; then
81     einfo "PT PaX marking -${flags} with paxctl"
82     _pax_list_files einfo "$@"
83     for f in "$@"; do
84     # First, try modifying the existing PAX_FLAGS header
85     paxctl -q${flags} "${f}" && continue
86     # Second, try creating a PT_PAX header (works on ET_EXEC)
87     # Even though this is less safe, most exes need it, eg bug #463170
88     paxctl -qC${flags} "${f}" && continue
89     # Third, try stealing the (unused under PaX) PT_GNU_STACK header
90     paxctl -qc${flags} "${f}" && continue
91     pt_fail=1
92     pt_failures="${pt_failures} ${f}"
93     done
94    
95     #Next try paxctl-ng -> this will not create/convert any program headers
96     elif type -p paxctl-ng > /dev/null && paxctl-ng -L ; then
97     einfo "PT PaX marking -${flags} with paxctl-ng"
98     flags="${flags//z}"
99     _pax_list_files einfo "$@"
100     for f in "$@"; do
101     [[ ${dodefault} == "yes" ]] && paxctl-ng -L -z "${f}"
102     [[ "${flags}" ]] || continue
103     paxctl-ng -L -${flags} "${f}" && continue
104     pt_fail=1
105     pt_failures="${pt_failures} ${f}"
106     done
107    
108     #Finally fall back on scanelf
109     elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
110     einfo "Fallback PaX marking -${flags} with scanelf"
111     _pax_list_files einfo "$@"
112     scanelf -Xxz ${flags} "$@"
113    
114     #We failed to set PT_PAX flags
115     elif [[ ${PAX_MARKINGS} != "none" ]]; then
116     pt_failures="$*"
117     pt_fail=1
118     fi
119    
120     if [[ ${pt_fail} == 1 ]]; then
121 zorry 1.20 elog "Failed to set PT_PAX markings -${flags} for:"
122     _pax_list_files elog ${pt_failures}
123 blueness 1.19 ret=1
124     fi
125 kevquinn 1.1 fi
126 blueness 1.19
127     if has XT ${PAX_MARKINGS}; then
128    
129     flags="${flags//z}"
130    
131     #First try paxctl-ng
132     if type -p paxctl-ng > /dev/null && paxctl-ng -l ; then
133     einfo "XT PaX marking -${flags} with paxctl-ng"
134     _pax_list_files einfo "$@"
135     for f in "$@"; do
136     [[ ${dodefault} == "yes" ]] && paxctl-ng -d "${f}"
137     [[ "${flags}" ]] || continue
138     paxctl-ng -l -${flags} "${f}" && continue
139     xt_fail=1
140     xt_failures="${tx_failures} ${f}"
141     done
142    
143     #Next try setfattr
144     elif type -p setfattr > /dev/null; then
145     [[ "${flags//[!Ee]}" ]] || flags+="e" # bug 447150
146     einfo "XT PaX marking -${flags} with setfattr"
147     _pax_list_files einfo "$@"
148     for f in "$@"; do
149     [[ ${dodefault} == "yes" ]] && setfattr -x "user.pax.flags" "${f}"
150     setfattr -n "user.pax.flags" -v "${flags}" "${f}" && continue
151     xt_fail=1
152     xt_failures="${tx_failures} ${f}"
153     done
154    
155     #We failed to set XATTR_PAX flags
156     elif [[ ${PAX_MARKINGS} != "none" ]]; then
157     xt_failures="$*"
158     xt_fail=1
159     fi
160    
161     if [[ ${xt_fail} == 1 ]]; then
162 zorry 1.20 elog "Failed to set XATTR_PAX markings -${flags} for:"
163     _pax_list_files elog ${xt_failures}
164 blueness 1.19 ret=1
165     fi
166 kevquinn 1.3 fi
167 blueness 1.19
168 zorry 1.20 # [[ ${ret} == 1 ]] && elog "Executables may be killed by PaX kernels."
169 blueness 1.19
170     return ${ret}
171 kevquinn 1.2 }
172    
173 blueness 1.11 # @FUNCTION: list-paxables
174     # @USAGE: {<files>}
175     # @RETURN: Subset of {<files>} which are ELF executables or shared objects
176     # @DESCRIPTION:
177     # Print to stdout all of the <files> that are suitable to have PaX flag
178     # markings, i.e., filter out the ELF executables or shared objects from a list
179     # of files. This is useful for passing wild-card lists to pax-mark, although
180     # in general it is preferable for ebuilds to list precisely which ELFS are to
181     # be marked. Often not all the ELF installed by a package need remarking.
182     # @EXAMPLE:
183     # pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
184 kevquinn 1.4 list-paxables() {
185 kevquinn 1.6 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
186 kevquinn 1.4 }
187    
188 blueness 1.11 # @FUNCTION: host-is-pax
189     # @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
190     # @DESCRIPTION:
191     # This is intended for use where the build process must be modified conditionally
192     # depending on whether the host is PaX enabled or not. It is not intedened to
193     # determine whether the final binaries need PaX markings. Note: if procfs is
194     # not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
195 kevquinn 1.2 host-is-pax() {
196 kevquinn 1.4 grep -qs ^PaX: /proc/self/status
197     }
198    
199    
200     # INTERNAL FUNCTIONS
201     # ------------------
202     #
203     # These functions are for use internally by the eclass - do not use
204     # them elsewhere as they are not supported (i.e. they may be removed
205     # or their function may change arbitratily).
206    
207     # Display a list of things, one per line, indented a bit, using the
208     # display command in $1.
209     _pax_list_files() {
210     local f cmd
211     cmd=$1
212     shift
213     for f in "$@"; do
214     ${cmd} " ${f}"
215     done
216 kevquinn 1.1 }
217 kevquinn 1.4
218 vapier 1.17 fi

  ViewVC Help
Powered by ViewVC 1.1.20