toolchain.eclass: fix ada build with lto
[gentoo.git] / eclass / pax-utils.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: pax-utils.eclass
5 # @MAINTAINER:
6 # The Gentoo Linux Hardened Team <hardened@gentoo.org>
7 # @AUTHOR:
8 # Author: Kevin F. Quinn <kevquinn@gentoo.org>
9 # Author: Anthony G. Basile <blueness@gentoo.org>
10 # @BLURB: functions to provide PaX markings for hardened kernels
11 # @DESCRIPTION:
12 #
13 # This eclass provides support for manipulating PaX markings on ELF binaries,
14 # whether the system is using legacy PT_PAX markings or the newer XATTR_PAX.
15 # The eclass wraps the use of paxctl-ng, paxctl, set/getattr and scanelf utilities,
16 # deciding which to use depending on what's installed on the build host, and
17 # whether we're working with PT_PAX, XATTR_PAX or both.
18 # Legacy PT_PAX markings no longer supported.
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 none
22
23 if [[ -z ${_PAX_UTILS_ECLASS} ]]; then
24 _PAX_UTILS_ECLASS=1
25
26 # @ECLASS-VARIABLE: PAX_MARKINGS
27 # @DESCRIPTION:
28 # Control which markings are made:
29 # PT = PT_PAX markings, XT = XATTR_PAX markings
30 # Default to none markings.
31 PAX_MARKINGS=${PAX_MARKINGS:="none"}
32
33 # @FUNCTION: pax-mark
34 # @USAGE: <flags> <ELF files>
35 # @RETURN: Shell true if we succeed, shell false otherwise
36 # @DESCRIPTION:
37 # Marks <ELF files> with provided PaX <flags>
38 #
39 # Flags are passed directly to the utilities unchanged.
40 #
41 # @CODE
42 #       p: disable PAGEEXEC             P: enable PAGEEXEC
43 #       e: disable EMUTRAMP             E: enable EMUTRAMP
44 #       m: disable MPROTECT             M: enable MPROTECT
45 #       r: disable RANDMMAP             R: enable RANDMMAP
46 #       s: disable SEGMEXEC             S: enable SEGMEXEC
47 # @CODE
48 #
49 # Default flags are 'PeMRS', which are the most restrictive settings.  Refer
50 # to https://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
54 # hardened@gentoo.org on the bug report.
55 pax-mark() {
56         local f                                                         # loop over paxables
57         local flags                                                     # pax flags
58         local ret=0                                                     # overall 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         shift
69
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                 # Uncomment to list all files to be marked
76                 # _pax_list_files einfo "$@"
77                 for f in "$@"; do
78
79                         # First try paxctl
80                         if type -p paxctl >/dev/null; then
81                                 einfo "PT_PAX marking -${flags} ${f} with paxctl"
82                                 # We try modifying the existing PT_PAX_FLAGS header.
83                                 paxctl -q${flags} "${f}" >/dev/null 2>&1 && continue
84                                 # We no longer try to create/convert a PT_PAX_FLAGS header, bug #590422
85                                 # paxctl -qC${flags} "${f}" >/dev/null 2>&1 && continue
86                                 # paxctl -qc${flags} "${f}" >/dev/null 2>&1 && continue
87                         fi
88
89                         # Next try paxctl-ng -> this will not create/convert any program headers.
90                         if type -p paxctl-ng >/dev/null && paxctl-ng -L ; then
91                                 einfo "PT_PAX marking -${flags} ${f} with paxctl-ng"
92                                 flags="${flags//z}"
93                                 [[ ${dodefault} == "yes" ]] && paxctl-ng -L -z "${f}" >/dev/null 2>&1
94                                 [[ "${flags}" ]] || continue
95                                 paxctl-ng -L -${flags} "${f}" >/dev/null 2>&1 && continue
96                         fi
97
98                         # Finally fall back on scanelf.
99                         if type -p scanelf >/dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
100                                 einfo "PT_PAX marking -${flags} ${f} with scanelf"
101                                 scanelf -Xxz ${flags} "$f" >/dev/null 2>&1
102                         # We failed to set PT_PAX flags.
103                         elif [[ ${PAX_MARKINGS} != "none" ]]; then
104                                 elog "Failed to set PT_PAX markings -${flags} ${f}."
105                                 ret=1
106                         fi
107                 done
108         fi
109
110         if has XT ${PAX_MARKINGS}; then
111                 # Uncomment to list all files to be marked
112                 # _pax_list_files einfo "$@"
113                 flags="${flags//z}"
114                 for f in "$@"; do
115
116                         # First try paxctl-ng.
117                         if type -p paxctl-ng >/dev/null && paxctl-ng -l ; then
118                                 einfo "XATTR_PAX marking -${flags} ${f} with paxctl-ng"
119                                 [[ ${dodefault} == "yes" ]] && paxctl-ng -d "${f}" >/dev/null 2>&1
120                                 [[ "${flags}" ]] || continue
121                                 paxctl-ng -l -${flags} "${f}" >/dev/null 2>&1 && continue
122                         fi
123
124                         # Next try setfattr.
125                         if type -p setfattr >/dev/null; then
126                                 [[ "${flags//[!Ee]}" ]] || flags+="e" # bug 447150
127                                 einfo "XATTR_PAX marking -${flags} ${f} with setfattr"
128                                 [[ ${dodefault} == "yes" ]] && setfattr -x "user.pax.flags" "${f}" >/dev/null 2>&1
129                                 setfattr -n "user.pax.flags" -v "${flags}" "${f}" >/dev/null 2>&1 && continue
130                         fi
131
132                         # We failed to set XATTR_PAX flags.
133                         if [[ ${PAX_MARKINGS} != "none" ]]; then
134                                 elog "Failed to set XATTR_PAX markings -${flags} ${f}."
135                                 ret=1
136                         fi
137                 done
138         fi
139
140         # [[ ${ret} == 1 ]] && elog "Executables may be killed by PaX kernels."
141
142         return ${ret}
143 }
144
145 # @FUNCTION: list-paxables
146 # @USAGE: <files>
147 # @RETURN: Subset of <files> which are ELF executables or shared objects
148 # @DESCRIPTION:
149 # Print to stdout all of the <files> that are suitable to have PaX flag
150 # markings, i.e., filter out the ELF executables or shared objects from a list
151 # of files.  This is useful for passing wild-card lists to pax-mark, although
152 # in general it is preferable for ebuilds to list precisely which ELFS are to
153 # be marked.  Often not all the ELF installed by a package need remarking.
154 # @EXAMPLE:
155 # pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
156 list-paxables() {
157         file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
158 }
159
160 # @FUNCTION: host-is-pax
161 # @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
162 # @DESCRIPTION:
163 # This is intended for use where the build process must be modified conditionally
164 # depending on whether the host is PaX enabled or not.  It is not indented to
165 # determine whether the final binaries need PaX markings.  Note: if procfs is
166 # not mounted on /proc, this returns shell false (e.g. Gentoo/FreeBSD).
167 host-is-pax() {
168         grep -qs ^PaX: /proc/self/status
169 }
170
171
172 # INTERNAL FUNCTIONS
173 # ------------------
174 #
175 # These functions are for use internally by the eclass - do not use
176 # them elsewhere as they are not supported (i.e. they may be removed
177 # or their function may change arbitrarily).
178
179 # Display a list of things, one per line, indented a bit, using the
180 # display command in $1.
181 _pax_list_files() {
182         local f cmd
183         cmd=$1
184         shift
185         for f in "$@"; do
186                 ${cmd} "     ${f}"
187         done
188 }
189
190 fi