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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations) (download)
Sat Jul 2 17:03:51 2011 UTC (3 years ago) by blueness
Branch: MAIN
Changes since 1.11: +24 -10 lines
Make dependency on pax-utils and paxctl explicit

1 # Copyright 1999-2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.11 2011/05/22 01:01:40 blueness Exp $
4
5 # @ECLASS: pax-utils.eclass
6 # @MAINTAINER:
7 # Maintained by
8 # The Gentoo Linux Hardened Team <hardened@gentoo.org>
9 # Original Author
10 # Kevin F. Quinn <kevquinn@gentoo.org>
11 # Modifications for bug #365825, @ ECLASS markup
12 # Anthony G. Basile <blueness@gentoo.org>
13 # @BLURB: functions to provide pax markings
14 # @DESCRIPTION:
15 # This eclass provides support for manipulating PaX markings on ELF binaries,
16 # wrapping the use of the paxctl and scanelf utilities. It decides which to
17 # use depending on what is installed on the build host, preferring paxctl to
18 # scanelf. If paxctl is not installed, we fall back to scanelf since it is
19 # always present. However, currently scanelf doesn't do all that paxctl can.
20 #
21 # To control what markings are made, set PAX_MARKINGS in /etc/make.conf to
22 # contain either "PT" or "none". If PAX_MARKINGS is set to "PT", and the
23 # necessary utility is installed, the PT_PAX_FLAGS markings will be made. If
24 # PAX_MARKINGS is set to "none", no markings will be made.
25
26 inherit eutils
27
28 IUSE="hardened"
29
30 DEPEND="hardened? ( app-misc/pax-utils
31 sys-apps/paxctl )"
32
33 # Default to PT markings.
34 PAX_MARKINGS=${PAX_MARKINGS:="PT"}
35
36 # @FUNCTION: pax-mark
37 # @USAGE: <flags> {<ELF files>}
38 # @RETURN: Shell true if we succeed, shell false otherwise
39 # @DESCRIPTION:
40 # Marks <ELF files> with provided PaX <flags>
41 #
42 # Flags are passed directly to the utilities unchanged. Possible flags at the
43 # time of writing, taken from /sbin/paxctl, are:
44 #
45 # p: disable PAGEEXEC P: enable PAGEEXEC
46 # e: disable EMUTRMAP E: enable EMUTRMAP
47 # m: disable MPROTECT M: enable MPROTECT
48 # r: disable RANDMMAP R: enable RANDMMAP
49 # s: disable SEGMEXEC S: enable SEGMEXEC
50 #
51 # Default flags are 'PeMRS', which are the most restrictive settings. Refer
52 # to http://pax.grsecurity.net/ for details on what these flags are all about.
53 # Do not use the obsolete flag 'x'/'X' which has been deprecated.
54 #
55 # Please confirm any relaxation of restrictions with the Gentoo Hardened team.
56 # Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
57 # the bug report.
58 pax-mark() {
59 # It doesn't make sense to pax-mark on non-hardened systems
60 # so we'll just do nothing.
61 use hardened || return 0;
62
63 local f flags fail=0 failures="" zero_load_alignment
64 # Ignore '-' characters - in particular so that it doesn't matter if
65 # the caller prefixes with -
66 flags=${1//-}
67 shift
68 # Try paxctl, then scanelf. paxctl is preferred.
69 if type -p paxctl > /dev/null && hasq PT ${PAX_MARKINGS}; then
70 # Try paxctl, the upstream supported tool.
71 elog "PT PaX marking -${flags}"
72 _pax_list_files elog "$@"
73 for f in "$@"; do
74 # First, try modifying the existing PAX_FLAGS header
75 paxctl -q${flags} "${f}" && continue
76 # Second, try stealing the (unused under PaX) PT_GNU_STACK header
77 paxctl -qc${flags} "${f}" && continue
78 # Third, try pulling the base down a page, to create space and
79 # insert a PT_GNU_STACK header (works on ET_EXEC)
80 paxctl -qC${flags} "${f}" && continue
81 #
82 # prelink is masked on hardened so we wont use this method.
83 # We're working on a new utiity to try to do the same safely. See
84 # http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=summary
85 #
86 # Fourth - check if it loads to 0 (probably an ET_DYN) and if so,
87 # try rebasing with prelink first to give paxctl some space to
88 # grow downwards into.
89 #if type -p objdump > /dev/null && type -p prelink > /dev/null; then
90 # zero_load_alignment=$(objdump -p "${f}" | \
91 # grep -E '^[[:space:]]*LOAD[[:space:]]*off[[:space:]]*0x0+[[:space:]]' | \
92 # sed -e 's/.*align\(.*\)/\1/')
93 # if [[ ${zero_load_alignment} != "" ]]; then
94 # prelink -r $(( 2*(${zero_load_alignment}) )) &&
95 # paxctl -qC${flags} "${f}" && continue
96 # fi
97 #fi
98 fail=1
99 failures="${failures} ${f}"
100 done
101 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
102 # Try scanelf, the Gentoo swiss-army knife ELF utility
103 # Currently this sets PT if it can, no option to control what it does.
104 elog "Fallback PaX marking -${flags}"
105 _pax_list_files elog "$@"
106 scanelf -Xxz ${flags} "$@"
107 elif [[ ${PAX_MARKINGS} != "none" ]]; then
108 # Out of options!
109 failures="$*"
110 fail=1
111 fi
112 if [[ ${fail} == 1 ]]; then
113 ewarn "Failed to set PaX markings -${flags} for:"
114 _pax_list_files ewarn ${failures}
115 ewarn "Executables may be killed by PaX kernels."
116 fi
117 return ${fail}
118 }
119
120 # @FUNCTION: list-paxables
121 # @USAGE: {<files>}
122 # @RETURN: Subset of {<files>} which are ELF executables or shared objects
123 # @DESCRIPTION:
124 # Print to stdout all of the <files> that are suitable to have PaX flag
125 # markings, i.e., filter out the ELF executables or shared objects from a list
126 # of files. This is useful for passing wild-card lists to pax-mark, although
127 # in general it is preferable for ebuilds to list precisely which ELFS are to
128 # be marked. Often not all the ELF installed by a package need remarking.
129 # @EXAMPLE:
130 # pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
131 list-paxables() {
132 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
133 }
134
135 # @FUNCTION: host-is-pax
136 # @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
137 # @DESCRIPTION:
138 # This is intended for use where the build process must be modified conditionally
139 # depending on whether the host is PaX enabled or not. It is not intedened to
140 # determine whether the final binaries need PaX markings. Note: if procfs is
141 # not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
142 host-is-pax() {
143 grep -qs ^PaX: /proc/self/status
144 }
145
146
147 # INTERNAL FUNCTIONS
148 # ------------------
149 #
150 # These functions are for use internally by the eclass - do not use
151 # them elsewhere as they are not supported (i.e. they may be removed
152 # or their function may change arbitratily).
153
154 # Display a list of things, one per line, indented a bit, using the
155 # display command in $1.
156 _pax_list_files() {
157 local f cmd
158 cmd=$1
159 shift
160 for f in "$@"; do
161 ${cmd} " ${f}"
162 done
163 }

  ViewVC Help
Powered by ViewVC 1.1.20