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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations) (download)
Sun Jul 3 21:17:10 2011 UTC (3 years, 3 months ago) by blueness
Branch: MAIN
Changes since 1.12: +1 -10 lines
Reverted last change

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

  ViewVC Help
Powered by ViewVC 1.1.20