aboutsummaryrefslogtreecommitdiff
blob: fdc7769e014e3e4de8ebd0ae8896a1a83dc47c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.18 2012/04/06 18:03:54 blueness Exp $

# @ECLASS: pax-utils.eclass
# @MAINTAINER:
# The Gentoo Linux Hardened Team <hardened@gentoo.org>
# @AUTHOR:
# Original Author: Kevin F. Quinn <kevquinn@gentoo.org>
# Modifications for bug #365825, @ ECLASS markup: Anthony G. Basile <blueness@gentoo.org>
# Modifications for bug #431092: Anthony G. Basile <blueness@gentoo.org>
# @BLURB: functions to provide pax markings
# @DESCRIPTION:
#
# This eclass provides support for manipulating PaX markings on ELF binaries,
# whether the system is using legacy PT_PAX markings or the newer XATTR_PAX.
# The eclass wraps the use of paxctl-ng, paxctl, set/getattr and scanelf utilities,
# deciding which to use depending on what's installed on the build host, and
# whether we're working with PT_PAX, XATTR_PAX or both.
#
# To control what markings are made, set PAX_MARKINGS in /etc/portage/make.conf
# to contain either "PT", "XT" or "none".  The default is to attempt both
# PT_PAX and XATTR_PAX.

if [[ ${___ECLASS_ONCE_PAX_UTILS} != "recur -_+^+_- spank" ]] ; then
___ECLASS_ONCE_PAX_UTILS="recur -_+^+_- spank"

# Default to PT markings.
PAX_MARKINGS=${PAX_MARKINGS:="PT XT"}

# @FUNCTION: pax-mark
# @USAGE: <flags> {<ELF files>}
# @RETURN: Shell true if we succeed, shell false otherwise
# @DESCRIPTION:
# Marks <ELF files> with provided PaX <flags>
#
# Flags are passed directly to the utilities unchanged
#
#	p: disable PAGEEXEC		P: enable PAGEEXEC
#	e: disable EMUTRMAP		E: enable EMUTRMAP
#	m: disable MPROTECT		M: enable MPROTECT
#	r: disable RANDMMAP		R: enable RANDMMAP
#	s: disable SEGMEXEC		S: enable SEGMEXEC
#
# Default flags are 'PeMRS', which are the most restrictive settings.  Refer
# to http://pax.grsecurity.net/ for details on what these flags are all about.
#
# Please confirm any relaxation of restrictions with the Gentoo Hardened team.
# Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
# the bug report.


sanitize-flags() {
		# Only the actual pax flags and z are accepted
		#
		# 1. The leading '-' is irrelevant since it is santized out
		#
		# 2. Cc only make sense for paxctl, and even there these are
		#    not needed as we progressively try:
		#        paxctl -q${flags}
		#        paxctl -qc${flags}
		#        paxctl -qC${flags}
		#    So we sanitize them out.
		#
		# 3. z is allowed for the default
		#

        local flags="$1"
        local clean=""

        [[ "${flags}" != "${flags/z/}" ]] && clean="${clean}z"

        [[ "${flags}" != "${flags/P/}" ]] && clean="${clean}P"
        [[ "${flags}" != "${flags/p/}" ]] && clean="${clean}p"
        [[ "${flags}" != "${flags/E/}" ]] && clean="${clean}E"
        [[ "${flags}" != "${flags/e/}" ]] && clean="${clean}e"
        [[ "${flags}" != "${flags/M/}" ]] && clean="${clean}M"
        [[ "${flags}" != "${flags/m/}" ]] && clean="${clean}m"
        [[ "${flags}" != "${flags/R/}" ]] && clean="${clean}R"
        [[ "${flags}" != "${flags/r/}" ]] && clean="${clean}r"
        [[ "${flags}" != "${flags/S/}" ]] && clean="${clean}S"
        [[ "${flags}" != "${flags/s/}" ]] && clean="${clean}s"

        echo "$clean"
}


pax-mark() {

	local f								# loop over paxables
	local flags							# pax flags
	local pt_fail=0 pt_failures=""		# record PT_PAX failures
	local xt_fail=0 xt_failures=""		# record xattr PAX marking failures
	local ret=0							# overal return code of this function

	flags="$(sanitize-flags $1)"
	shift

	if has PT ${PAX_MARKINGS}; then

		#First try paxctl -> this might try to create/convert program headers
		if type -p paxctl > /dev/null; then
			einfo "PT PaX marking -${flags} with paxctl"
			_pax_list_files einfo "$@"
			for f in "$@"; do
				# First, try modifying the existing PAX_FLAGS header
				paxctl -q${flags} "${f}" && continue
				# Second, try stealing the (unused under PaX) PT_GNU_STACK header
				paxctl -qc${flags} "${f}" && continue
				# Third, creating a PT_PAX header (works on ET_EXEC)
				paxctl -qC${flags} "${f}" && continue
				pt_fail=1
				pt_failures="${pt_failures} ${f}"
			done

		#Next try paxctl-ng -> this will not create/convert any program headers
		elif type -p paxctl-ng > /dev/null && paxctl-ng -L ; then
			einfo "PT PaX marking -${flags} with paxctl-ng"
			_pax_list_files einfo "$@"
			for f in "$@"; do
				paxctl-ng -L -${flags} "${f}" && continue
				pt_fail=1
				pt_failures="${pt_failures} ${f}"
			done

		#Finally fall back on scanelf
		elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
			einfo "Fallback PaX marking -${flags} with scanelf"
			_pax_list_files einfo "$@"
			scanelf -Xxz ${flags} "$@"

		#We failed to set PT_PAX flags
		elif [[ ${PAX_MARKINGS} != "none" ]]; then
			pt_failures="$*"
			pt_fail=1
		fi

		if [[ ${pt_fail} == 1 ]]; then
			ewarn "Failed to set XT_PAX markings -${flags} for:"
			_pax_list_files ewarn ${pt_failures}
			ret=1
		fi
	fi

	if has XT ${PAX_MARKINGS}; then

		# z = default. For XATTR_PAX, the default is no xattr field at all
		local dodefault=""
		if [[ "${flags}" != "${flags/z/}" ]]; then
			flags="${flags/z/}"
			dodefault="yes"
		fi

		#First try paxctl-ng
		if type -p paxctl-ng > /dev/null && paxctl-ng -l ; then
			einfo "XT PaX marking -${flags} with paxctl-ng"
			_pax_list_files einfo "$@"
			for f in "$@"; do
				[[ ${dodefault} == "yes" ]] && paxctl-ng -d "${f}"
				paxctl-ng -l -${flags} "${f}" && continue
				xt_fail=1
				xt_failures="${tx_failures} ${f}"
			done

		#Next try setfattr
		elif type -p setfattr > /dev/null; then
			einfo "XT PaX marking -${flags} with setfattr"
			_pax_list_files einfo "$@"
			for f in "$@"; do
				[[ ${dodefault} == "yes" ]] && setfattr -x "user.pax.flags" "${f}"
				setfattr -n "user.pax.flags" -v "${flags}" "${f}" && continue
				xt_fail=1
				xt_failures="${tx_failures} ${f}"
			done

		#We failed to set PT_PAX flags
		elif [[ ${PAX_MARKINGS} != "none" ]]; then
			pt_failures="$*"
			pt_fail=1
		fi

		if [[ ${xt_fail} == 1 ]]; then
			ewarn "Failed to set XT_PAX markings -${flags} for:"
			_pax_list_files ewarn ${xt_failures}
			ret=1
		fi
	fi

	[[ ${ret} == 1 ]] && ewarn "Executables may be killed by PaX kernels."

	return ${ret}
}

# @FUNCTION: list-paxables
# @USAGE: {<files>}
# @RETURN: Subset of {<files>} which are ELF executables or shared objects
# @DESCRIPTION:
# Print to stdout all of the <files> that are suitable to have PaX flag
# markings, i.e., filter out the ELF executables or shared objects from a list
# of files.  This is useful for passing wild-card lists to pax-mark, although
# in general it is preferable for ebuilds to list precisely which ELFS are to
# be marked.  Often not all the ELF installed by a package need remarking.
# @EXAMPLE:
# pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
list-paxables() {
	file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
}

# @FUNCTION: host-is-pax
# @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
# @DESCRIPTION:
# This is intended for use where the build process must be modified conditionally
# depending on whether the host is PaX enabled or not.  It is not intedened to
# determine whether the final binaries need PaX markings.  Note: if procfs is
# not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
host-is-pax() {
	grep -qs ^PaX: /proc/self/status
}


# INTERNAL FUNCTIONS
# ------------------
#
# These functions are for use internally by the eclass - do not use
# them elsewhere as they are not supported (i.e. they may be removed
# or their function may change arbitratily).

# Display a list of things, one per line, indented a bit, using the
# display command in $1.
_pax_list_files() {
	local f cmd
	cmd=$1
	shift
	for f in "$@"; do
		${cmd} "     ${f}"
	done
}

fi