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

Diff of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.18 Revision 1.19
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2013 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.18 2012/04/06 18:03:54 blueness Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.19 2013/04/05 02:08:36 blueness Exp $
4 4
5# @ECLASS: pax-utils.eclass 5# @ECLASS: pax-utils.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# The Gentoo Linux Hardened Team <hardened@gentoo.org> 7# The Gentoo Linux Hardened Team <hardened@gentoo.org>
8# @AUTHOR: 8# @AUTHOR:
9# Original Author: Kevin F. Quinn <kevquinn@gentoo.org> 9# Original Author: Kevin F. Quinn <kevquinn@gentoo.org>
10# Modifications for bug #365825, @ ECLASS markup: Anthony G. Basile <blueness@gentoo.org> 10# Modifications for bug #365825, @ ECLASS markup: Anthony G. Basile <blueness@gentoo.org>
11# Modifications for bug #431092: Anthony G. Basile <blueness@gentoo.org>
11# @BLURB: functions to provide pax markings 12# @BLURB: functions to provide pax markings
12# @DESCRIPTION: 13# @DESCRIPTION:
14#
13# This eclass provides support for manipulating PaX markings on ELF binaries, 15# 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 16# whether the system is using legacy PT_PAX markings or the newer XATTR_PAX.
15# use depending on what is installed on the build host, preferring paxctl to 17# The eclass wraps the use of paxctl-ng, paxctl, set/getattr and scanelf utilities,
16# scanelf. If paxctl is not installed, we fall back to scanelf since it is 18# deciding which to use depending on what's installed on the build host, and
17# always present. However, currently scanelf doesn't do all that paxctl can. 19# whether we're working with PT_PAX, XATTR_PAX or both.
18# 20#
19# To control what markings are made, set PAX_MARKINGS in /etc/make.conf to 21# To control what markings are made, set PAX_MARKINGS in /etc/portage/make.conf
20# contain either "PT" or "none". If PAX_MARKINGS is set to "PT", and the 22# to contain either "PT", "XT" or "none". The default is to attempt both
21# necessary utility is installed, the PT_PAX_FLAGS markings will be made. If 23# PT_PAX and XATTR_PAX.
22# PAX_MARKINGS is set to "none", no markings will be made.
23 24
24if [[ ${___ECLASS_ONCE_PAX_UTILS} != "recur -_+^+_- spank" ]] ; then 25if [[ ${___ECLASS_ONCE_PAX_UTILS} != "recur -_+^+_- spank" ]] ; then
25___ECLASS_ONCE_PAX_UTILS="recur -_+^+_- spank" 26___ECLASS_ONCE_PAX_UTILS="recur -_+^+_- spank"
26 27
28# @ECLASS-VARIABLE: PAX_MARKINGS
29# @DESCRIPTION:
30# Control which markings are made:
31# PT = PT_PAX markings, XT = XATTR_PAX markings
27# Default to PT markings. 32# Default to both PT and XT markings.
28PAX_MARKINGS=${PAX_MARKINGS:="PT"} 33PAX_MARKINGS=${PAX_MARKINGS:="PT XT"}
29 34
30# @FUNCTION: pax-mark 35# @FUNCTION: pax-mark
31# @USAGE: <flags> {<ELF files>} 36# @USAGE: <flags> {<ELF files>}
32# @RETURN: Shell true if we succeed, shell false otherwise 37# @RETURN: Shell true if we succeed, shell false otherwise
33# @DESCRIPTION: 38# @DESCRIPTION:
34# Marks <ELF files> with provided PaX <flags> 39# Marks <ELF files> with provided PaX <flags>
35# 40#
36# Flags are passed directly to the utilities unchanged. Possible flags at the 41# Flags are passed directly to the utilities unchanged
37# time of writing, taken from /sbin/paxctl, are:
38# 42#
39# p: disable PAGEEXEC P: enable PAGEEXEC 43# p: disable PAGEEXEC P: enable PAGEEXEC
40# e: disable EMUTRMAP E: enable EMUTRMAP 44# e: disable EMUTRMAP E: enable EMUTRMAP
41# m: disable MPROTECT M: enable MPROTECT 45# m: disable MPROTECT M: enable MPROTECT
42# r: disable RANDMMAP R: enable RANDMMAP 46# r: disable RANDMMAP R: enable RANDMMAP
43# s: disable SEGMEXEC S: enable SEGMEXEC 47# s: disable SEGMEXEC S: enable SEGMEXEC
44# 48#
45# Default flags are 'PeMRS', which are the most restrictive settings. Refer 49# 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. 50# 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# 51#
49# Please confirm any relaxation of restrictions with the Gentoo Hardened team. 52# 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 53# Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
51# the bug report. 54# the bug report.
52pax-mark() { 55pax-mark() {
53 local f flags fail=0 failures="" zero_load_alignment 56
54 # Ignore '-' characters - in particular so that it doesn't matter if 57 local f # loop over paxables
55 # the caller prefixes with - 58 local flags # pax flags
56 flags=${1//-} 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
57 shift 71 shift
58 # Try paxctl, then scanelf. paxctl is preferred. 72
59 if type -p paxctl > /dev/null && has PT ${PAX_MARKINGS}; then 73 # z = default. For XATTR_PAX, the default is no xattr field at all
60 # Try paxctl, the upstream supported tool. 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
61 einfo "PT PaX marking -${flags}" 81 einfo "PT PaX marking -${flags} with paxctl"
62 _pax_list_files einfo "$@" 82 _pax_list_files einfo "$@"
63 for f in "$@"; do 83 for f in "$@"; do
64 # First, try modifying the existing PAX_FLAGS header 84 # First, try modifying the existing PAX_FLAGS header
65 paxctl -q${flags} "${f}" && continue 85 paxctl -q${flags} "${f}" && continue
66 # Second, try stealing the (unused under PaX) PT_GNU_STACK header 86 # Second, try creating a PT_PAX header (works on ET_EXEC)
67 paxctl -qc${flags} "${f}" && continue 87 # Even though this is less safe, most exes need it, eg bug #463170
68 # Third, try pulling the base down a page, to create space and
69 # insert a PT_GNU_STACK header (works on ET_EXEC)
70 paxctl -qC${flags} "${f}" && continue 88 paxctl -qC${flags} "${f}" && continue
71 # 89 # Third, try stealing the (unused under PaX) PT_GNU_STACK header
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 # 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 #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 90 paxctl -qc${flags} "${f}" && continue
86 # fi
87 #fi
88 fail=1 91 pt_fail=1
89 failures="${failures} ${f}" 92 pt_failures="${pt_failures} ${f}"
90 done 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
91 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then 109 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
92 # Try scanelf, the Gentoo swiss-army knife ELF utility
93 # Currently this sets PT if it can, no option to control what it does.
94 einfo "Fallback PaX marking -${flags}" 110 einfo "Fallback PaX marking -${flags} with scanelf"
95 _pax_list_files einfo "$@" 111 _pax_list_files einfo "$@"
96 scanelf -Xxz ${flags} "$@" 112 scanelf -Xxz ${flags} "$@"
113
114 #We failed to set PT_PAX flags
97 elif [[ ${PAX_MARKINGS} != "none" ]]; then 115 elif [[ ${PAX_MARKINGS} != "none" ]]; then
98 # Out of options!
99 failures="$*" 116 pt_failures="$*"
100 fail=1 117 pt_fail=1
118 fi
119
120 if [[ ${pt_fail} == 1 ]]; then
121 ewarn "Failed to set PT_PAX markings -${flags} for:"
122 _pax_list_files ewarn ${pt_failures}
123 ret=1
124 fi
101 fi 125 fi
126
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
102 if [[ ${fail} == 1 ]]; then 161 if [[ ${xt_fail} == 1 ]]; then
103 ewarn "Failed to set PaX markings -${flags} for:" 162 ewarn "Failed to set XATTR_PAX markings -${flags} for:"
104 _pax_list_files ewarn ${failures} 163 _pax_list_files ewarn ${xt_failures}
105 ewarn "Executables may be killed by PaX kernels." 164 ret=1
165 fi
106 fi 166 fi
167
168 [[ ${ret} == 1 ]] && ewarn "Executables may be killed by PaX kernels."
169
107 return ${fail} 170 return ${ret}
108} 171}
109 172
110# @FUNCTION: list-paxables 173# @FUNCTION: list-paxables
111# @USAGE: {<files>} 174# @USAGE: {<files>}
112# @RETURN: Subset of {<files>} which are ELF executables or shared objects 175# @RETURN: Subset of {<files>} which are ELF executables or shared objects

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.19

  ViewVC Help
Powered by ViewVC 1.1.20