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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (hide annotations) (download)
Sat Aug 30 14:06:04 2014 UTC (2 weeks, 3 days ago) by blueness
Branch: MAIN
CVS Tags: HEAD
Changes since 1.22: +45 -72 lines
Update pax-utils.eclass according to bug #520198

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

  ViewVC Help
Powered by ViewVC 1.1.20