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

Diff of /eclass/pax-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.7 Revision 1.12
1# Copyright 1999-2006 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.7 2007/05/07 09:24:16 kevquinn Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.12 2011/07/02 17:03:51 blueness Exp $
4 4
5# Author: 5# @ECLASS: pax-utils.eclass
6# @MAINTAINER:
7# Maintained by
8# The Gentoo Linux Hardened Team <hardened@gentoo.org>
9# Original Author
6# Kevin F. Quinn <kevquinn@gentoo.org> 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.
7# 20#
8# This eclass provides support for manipulating PaX markings on ELF 21# To control what markings are made, set PAX_MARKINGS in /etc/make.conf to
9# binaries, wrapping the use of the chpaxi, paxctl and scanelf utilities. 22# contain either "PT" or "none". If PAX_MARKINGS is set to "PT", and the
10# Currently it decides which to use depending on what is installed on the 23# necessary utility is installed, the PT_PAX_FLAGS markings will be made. If
11# build host; this may change in the future to use a control variable 24# PAX_MARKINGS is set to "none", no markings will be made.
12# (which would also mean modifying DEPEND to bring in sys-apps/paxctl etc). 25
26inherit eutils
27
28IUSE="hardened"
29
30DEPEND="hardened? ( app-misc/pax-utils
31 sys-apps/paxctl )"
32
33# Default to PT markings.
34PAX_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>
13# 41#
14#
15# CONTROL
16# -------
17#
18# To control what markings are set, assign PAX_MARKINGS in
19# /etc/make.conf to contain the strings "EI" and/or "PT".
20# If EI is present in PAX_MARKINGS (and the chpax utility
21# is present), the legacy 'chpax' style markings will be
22# set. If PT is present in PAX_MARKINGS (and the paxctl
23# utility is present), the 'paxctl' markings will be set.
24# Default is to try to do both. Set it to "NONE" to prevent
25# any markings being made.
26#
27#
28# PROVIDED FUNCTIONS
29# ------------------
30#
31#### pax-mark <flags> {<ELF files>}
32# Marks files <files> with provided PaX flags <flags>
33#
34# Please confirm any relaxation of restrictions with the
35# Gentoo Hardened team; either ask on the gentoo-hardened
36# mailing list, or CC/assign hardened@g.o on a bug.
37#
38# Flags are passed directly to the utilities unchanged. Possible 42# Flags are passed directly to the utilities unchanged. Possible flags at the
39# flags at the time of writing, taken from /sbin/paxctl, are: 43# time of writing, taken from /sbin/paxctl, are:
40# 44#
41# p: disable PAGEEXEC P: enable PAGEEXEC 45# p: disable PAGEEXEC P: enable PAGEEXEC
42# e: disable EMUTRMAP E: enable EMUTRMAP 46# e: disable EMUTRMAP E: enable EMUTRMAP
43# m: disable MPROTECT M: enable MPROTECT 47# m: disable MPROTECT M: enable MPROTECT
44# r: disable RANDMMAP R: enable RANDMMAP 48# r: disable RANDMMAP R: enable RANDMMAP
45# s: disable SEGMEXEC S: enable SEGMEXEC 49# s: disable SEGMEXEC S: enable SEGMEXEC
46# 50#
47# Default flags are 'PeMRS', which are the most restrictive 51# Default flags are 'PeMRS', which are the most restrictive settings. Refer
48# settings. Refer to http://pax.grsecurity.net/ for details 52# to http://pax.grsecurity.net/ for details on what these flags are all about.
49# on what these flags are all about. There is an obsolete 53# Do not use the obsolete flag 'x'/'X' which has been deprecated.
50# flag 'x'/'X' which has been removed from PaX.
51# 54#
52# If chpax is not installed, the legacy EI flags (which are 55# Please confirm any relaxation of restrictions with the Gentoo Hardened team.
53# not strip-safe, and strictly speaking violate the ELF spec) 56# Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
54# will not be set. If paxctl is not installed, it falls back 57# the bug report.
55# to scanelf. scanelf is always present, but currently doesn't 58pax-mark() {
56# quite do all that paxctl can do. 59 # It doesn't make sense to pax-mark on non-hardened systems
57# Returns fail if one or more files could not be marked. 60 # so we'll just do nothing.
58# 61 use hardened || return 0;
59#
60#### list-paxables {<files>}
61# Prints to stdout all of <files> that are suitable to having PaX
62# flags (i.e. filter to just ELF files). Useful for passing wild-card
63# lists of files to pax-mark, although in general it is preferable
64# for ebuilds to list precisely which executables are to be marked.
65# Use like:
66# pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
67#
68#
69#### host-is-pax
70# Returns true if the host has a PaX-enabled kernel, false otherwise.
71# Intended for use where the build process must be modified conditionally
72# in order to satisfy PaX. Note; it is _not_ intended to indicate
73# whether the final executables should satisfy PaX - executables should
74# always be marked appropriately even if they're only going to be
75# installed on a non-PaX system.
76 62
77inherit eutils
78
79# Default to both EI and PT markings.
80PAX_MARKINGS=${PAX_MARKINGS:="EI PT"}
81
82# pax-mark <flags> {<ELF files>}
83pax-mark() {
84 local f flags fail=0 failures="" zero_load_alignment 63 local f flags fail=0 failures="" zero_load_alignment
85 # Ignore '-' characters - in particular so that it doesn't matter if 64 # Ignore '-' characters - in particular so that it doesn't matter if
86 # the caller prefixes with - 65 # the caller prefixes with -
87 flags=${1//-} 66 flags=${1//-}
88 shift 67 shift
89 # Try chpax, for (deprecated) EI legacy marking.
90 if type -p chpax > /dev/null && hasq EI ${PAX_MARKINGS}; then
91 elog "Legacy EI PaX marking -${flags}"
92 _pax_list_files elog "$@"
93 for f in "$@"; do
94 chpax -${flags} "${f}" && continue
95 fail=1
96 failures="${failures} ${f}"
97 done
98 fi
99 # Try paxctl, then scanelf - paxctl takes precedence 68 # Try paxctl, then scanelf. paxctl is preferred.
100 # over scanelf.
101 if type -p paxctl > /dev/null && hasq PT ${PAX_MARKINGS}; then 69 if type -p paxctl > /dev/null && hasq PT ${PAX_MARKINGS}; then
102 # Try paxctl, the upstream supported tool. 70 # Try paxctl, the upstream supported tool.
103 elog "PT PaX marking -${flags}" 71 elog "PT PaX marking -${flags}"
104 _pax_list_files elog "$@" 72 _pax_list_files elog "$@"
105 for f in "$@"; do 73 for f in "$@"; do
108 # Second, try stealing the (unused under PaX) PT_GNU_STACK header 76 # Second, try stealing the (unused under PaX) PT_GNU_STACK header
109 paxctl -qc${flags} "${f}" && continue 77 paxctl -qc${flags} "${f}" && continue
110 # Third, try pulling the base down a page, to create space and 78 # Third, try pulling the base down a page, to create space and
111 # insert a PT_GNU_STACK header (works on ET_EXEC) 79 # insert a PT_GNU_STACK header (works on ET_EXEC)
112 paxctl -qC${flags} "${f}" && continue 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 #
113 # Fourth - check if it loads to 0 (probably an ET_DYN) and if so, 86 # Fourth - check if it loads to 0 (probably an ET_DYN) and if so,
114 # try rebasing with prelink first to give paxctl some space to 87 # try rebasing with prelink first to give paxctl some space to
115 # grow downwards into. 88 # grow downwards into.
116 if type -p objdump > /dev/null && type -p prelink > /dev/null; then 89 #if type -p objdump > /dev/null && type -p prelink > /dev/null; then
117 zero_load_alignment=$(objdump -p "${f}" | \ 90 # zero_load_alignment=$(objdump -p "${f}" | \
118 grep -E '^[[:space:]]*LOAD[[:space:]]*off[[:space:]]*0x0+[[:space:]]' | \ 91 # grep -E '^[[:space:]]*LOAD[[:space:]]*off[[:space:]]*0x0+[[:space:]]' | \
119 sed -e 's/.*align\(.*\)/\1/') 92 # sed -e 's/.*align\(.*\)/\1/')
120 if [[ ${zero_load_alignment} != "" ]]; then 93 # if [[ ${zero_load_alignment} != "" ]]; then
121 prelink -r $(( 2*(${zero_load_alignment}) )) && 94 # prelink -r $(( 2*(${zero_load_alignment}) )) &&
122 paxctl -qC${flags} "${f}" && continue 95 # paxctl -qC${flags} "${f}" && continue
123 fi 96 # fi
124 fi 97 #fi
125 fail=1 98 fail=1
126 failures="${failures} ${f}" 99 failures="${failures} ${f}"
127 done 100 done
128 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then 101 elif type -p scanelf > /dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
129 # Try scanelf, the Gentoo swiss-army knife ELF utility 102 # Try scanelf, the Gentoo swiss-army knife ELF utility
130 # Currently this sets EI and PT if it can, no option to 103 # Currently this sets PT if it can, no option to control what it does.
131 # control what it does.
132 elog "Fallback PaX marking -${flags}" 104 elog "Fallback PaX marking -${flags}"
133 _pax_list_files elog "$@" 105 _pax_list_files elog "$@"
134 scanelf -Xxz ${flags} "$@" 106 scanelf -Xxz ${flags} "$@"
135 elif [[ ${PAX_MARKINGS} != "none" ]]; then 107 elif [[ ${PAX_MARKINGS} != "none" ]]; then
136 # Out of options! 108 # Out of options!
143 ewarn "Executables may be killed by PaX kernels." 115 ewarn "Executables may be killed by PaX kernels."
144 fi 116 fi
145 return ${fail} 117 return ${fail}
146} 118}
147 119
148# list-paxables {<files>} 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/*)
149list-paxables() { 131list-paxables() {
150 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//' 132 file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
151} 133}
152 134
153# host-is-pax 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
154# Note: if procfs is not on /proc, this returns False (e.g. Gentoo/FBSD). 141# not mounted on /proc, this returns shell false (e.g. Gentoo/FBSD).
155host-is-pax() { 142host-is-pax() {
156 grep -qs ^PaX: /proc/self/status 143 grep -qs ^PaX: /proc/self/status
157} 144}
158 145
159 146

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.20