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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations) (download)
Fri Apr 6 18:03:54 2012 UTC (2 years, 5 months ago) by blueness
Branch: MAIN
Changes since 1.17: +5 -5 lines
Downgrade from elog to einfo to avoid flooding logs

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.17 2011/12/17 04:23:53 vapier Exp $
4
5 # @ECLASS: pax-utils.eclass
6 # @MAINTAINER:
7 # The Gentoo Linux Hardened Team <hardened@gentoo.org>
8 # @AUTHOR:
9 # Original Author: Kevin F. Quinn <kevquinn@gentoo.org>
10 # Modifications for bug #365825, @ ECLASS markup: Anthony G. Basile <blueness@gentoo.org>
11 # @BLURB: functions to provide pax markings
12 # @DESCRIPTION:
13 # 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
15 # use depending on what is installed on the build host, preferring paxctl to
16 # scanelf. If paxctl is not installed, we fall back to scanelf since it is
17 # always present. However, currently scanelf doesn't do all that paxctl can.
18 #
19 # To control what markings are made, set PAX_MARKINGS in /etc/make.conf to
20 # contain either "PT" or "none". If PAX_MARKINGS is set to "PT", and the
21 # necessary utility is installed, the PT_PAX_FLAGS markings will be made. If
22 # PAX_MARKINGS is set to "none", no markings will be made.
23
24 if [[ ${___ECLASS_ONCE_PAX_UTILS} != "recur -_+^+_- spank" ]] ; then
25 ___ECLASS_ONCE_PAX_UTILS="recur -_+^+_- spank"
26
27 # Default to PT markings.
28 PAX_MARKINGS=${PAX_MARKINGS:="PT"}
29
30 # @FUNCTION: pax-mark
31 # @USAGE: <flags> {<ELF files>}
32 # @RETURN: Shell true if we succeed, shell false otherwise
33 # @DESCRIPTION:
34 # Marks <ELF files> with provided PaX <flags>
35 #
36 # Flags are passed directly to the utilities unchanged. Possible flags at the
37 # time of writing, taken from /sbin/paxctl, are:
38 #
39 # p: disable PAGEEXEC P: enable PAGEEXEC
40 # e: disable EMUTRMAP E: enable EMUTRMAP
41 # m: disable MPROTECT M: enable MPROTECT
42 # r: disable RANDMMAP R: enable RANDMMAP
43 # s: disable SEGMEXEC S: enable SEGMEXEC
44 #
45 # 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.
47 # Do not use the obsolete flag 'x'/'X' which has been deprecated.
48 #
49 # 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
51 # the bug report.
52 pax-mark() {
53 local f flags fail=0 failures="" zero_load_alignment
54 # Ignore '-' characters - in particular so that it doesn't matter if
55 # the caller prefixes with -
56 flags=${1//-}
57 shift
58 # Try paxctl, then scanelf. paxctl is preferred.
59 if type -p paxctl > /dev/null && has PT ${PAX_MARKINGS}; then
60 # Try paxctl, the upstream supported tool.
61 einfo "PT PaX marking -${flags}"
62 _pax_list_files einfo "$@"
63 for f in "$@"; do
64 # First, try modifying the existing PAX_FLAGS header
65 paxctl -q${flags} "${f}" && continue
66 # Second, try stealing the (unused under PaX) PT_GNU_STACK header
67 paxctl -qc${flags} "${f}" && continue
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
71 #
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
86 # fi
87 #fi
88 fail=1
89 failures="${failures} ${f}"
90 done
91 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}"
95 _pax_list_files einfo "$@"
96 scanelf -Xxz ${flags} "$@"
97 elif [[ ${PAX_MARKINGS} != "none" ]]; then
98 # Out of options!
99 failures="$*"
100 fail=1
101 fi
102 if [[ ${fail} == 1 ]]; then
103 ewarn "Failed to set PaX markings -${flags} for:"
104 _pax_list_files ewarn ${failures}
105 ewarn "Executables may be killed by PaX kernels."
106 fi
107 return ${fail}
108 }
109
110 # @FUNCTION: list-paxables
111 # @USAGE: {<files>}
112 # @RETURN: Subset of {<files>} which are ELF executables or shared objects
113 # @DESCRIPTION:
114 # Print to stdout all of the <files> that are suitable to have PaX flag
115 # markings, i.e., filter out the ELF executables or shared objects from a list
116 # of files. This is useful for passing wild-card lists to pax-mark, although
117 # in general it is preferable for ebuilds to list precisely which ELFS are to
118 # be marked. Often not all the ELF installed by a package need remarking.
119 # @EXAMPLE:
120 # pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
121 list-paxables() {
122 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
123 }
124
125 # @FUNCTION: host-is-pax
126 # @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
127 # @DESCRIPTION:
128 # This is intended for use where the build process must be modified conditionally
129 # depending on whether the host is PaX enabled or not. It is not intedened to
130 # determine whether the final binaries need PaX markings. Note: if procfs is
131 # not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
132 host-is-pax() {
133 grep -qs ^PaX: /proc/self/status
134 }
135
136
137 # INTERNAL FUNCTIONS
138 # ------------------
139 #
140 # These functions are for use internally by the eclass - do not use
141 # them elsewhere as they are not supported (i.e. they may be removed
142 # or their function may change arbitratily).
143
144 # Display a list of things, one per line, indented a bit, using the
145 # display command in $1.
146 _pax_list_files() {
147 local f cmd
148 cmd=$1
149 shift
150 for f in "$@"; do
151 ${cmd} " ${f}"
152 done
153 }
154
155 fi

  ViewVC Help
Powered by ViewVC 1.1.20