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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations) (download)
Sun May 22 01:01:40 2011 UTC (3 years, 6 months ago) by blueness
Branch: MAIN
Changes since 1.10: +62 -89 lines
Removed chpax, see bug #365825

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.10 2010/03/09 10:35:33 nyhm 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 # Fourth - check if it loads to 0 (probably an ET_DYN) and if so,
73 # try rebasing with prelink first to give paxctl some space to
74 # grow downwards into.
75 if type -p objdump > /dev/null && type -p prelink > /dev/null; then
76 zero_load_alignment=$(objdump -p "${f}" | \
77 grep -E '^[[:space:]]*LOAD[[:space:]]*off[[:space:]]*0x0+[[:space:]]' | \
78 sed -e 's/.*align\(.*\)/\1/')
79 if [[ ${zero_load_alignment} != "" ]]; then
80 prelink -r $(( 2*(${zero_load_alignment}) )) &&
81 paxctl -qC${flags} "${f}" && continue
82 fi
83 fi
84 fail=1
85 failures="${failures} ${f}"
86 done
87 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
88 # Try scanelf, the Gentoo swiss-army knife ELF utility
89 # Currently this sets PT if it can, no option to control what it does.
90 elog "Fallback PaX marking -${flags}"
91 _pax_list_files elog "$@"
92 scanelf -Xxz ${flags} "$@"
93 elif [[ ${PAX_MARKINGS} != "none" ]]; then
94 # Out of options!
95 failures="$*"
96 fail=1
97 fi
98 if [[ ${fail} == 1 ]]; then
99 ewarn "Failed to set PaX markings -${flags} for:"
100 _pax_list_files ewarn ${failures}
101 ewarn "Executables may be killed by PaX kernels."
102 fi
103 return ${fail}
104 }
105
106 # @FUNCTION: list-paxables
107 # @USAGE: {<files>}
108 # @RETURN: Subset of {<files>} which are ELF executables or shared objects
109 # @DESCRIPTION:
110 # Print to stdout all of the <files> that are suitable to have PaX flag
111 # markings, i.e., filter out the ELF executables or shared objects from a list
112 # of files. This is useful for passing wild-card lists to pax-mark, although
113 # in general it is preferable for ebuilds to list precisely which ELFS are to
114 # be marked. Often not all the ELF installed by a package need remarking.
115 # @EXAMPLE:
116 # pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
117 list-paxables() {
118 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
119 }
120
121 # @FUNCTION: host-is-pax
122 # @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
123 # @DESCRIPTION:
124 # This is intended for use where the build process must be modified conditionally
125 # depending on whether the host is PaX enabled or not. It is not intedened to
126 # determine whether the final binaries need PaX markings. Note: if procfs is
127 # not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
128 host-is-pax() {
129 grep -qs ^PaX: /proc/self/status
130 }
131
132
133 # INTERNAL FUNCTIONS
134 # ------------------
135 #
136 # These functions are for use internally by the eclass - do not use
137 # them elsewhere as they are not supported (i.e. they may be removed
138 # or their function may change arbitratily).
139
140 # Display a list of things, one per line, indented a bit, using the
141 # display command in $1.
142 _pax_list_files() {
143 local f cmd
144 cmd=$1
145 shift
146 for f in "$@"; do
147 ${cmd} " ${f}"
148 done
149 }

  ViewVC Help
Powered by ViewVC 1.1.20