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

Contents of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations) (download)
Sat Dec 17 04:23:13 2011 UTC (3 years ago) by vapier
Branch: MAIN
Changes since 1.15: +1 -3 lines
this eclass does not seem to use eutils.eclass, so drop the inherit

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

  ViewVC Help
Powered by ViewVC 1.1.20