/[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.2 Revision 1.16
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.2 2006/11/15 22:14:25 kevquinn Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/pax-utils.eclass,v 1.16 2011/12/17 04:23:13 vapier Exp $
4 4
5# Author: 5# @ECLASS: pax-utils.eclass
6# @MAINTAINER:
7# The Gentoo Linux Hardened Team <hardened@gentoo.org>
8# @AUTHOR:
6# Kevin F. Quinn <kevquinn@gentoo.org> 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.
7# 18#
8# This eclass provides support for manipulating PaX markings on ELF 19# To control what markings are made, set PAX_MARKINGS in /etc/make.conf to
9# binaries, wrapping the use of the chpax and paxctl utilities. 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.
10 23
11inherit eutils 24# Default to PT markings.
25PAX_MARKINGS=${PAX_MARKINGS:="PT"}
12 26
13##### pax-mark #### 27# @FUNCTION: pax-mark
14# Mark a file for PaX, with the provided flags, and log it into 28# @USAGE: <flags> {<ELF files>}
15# a PaX database. Returns non-zero if flag marking failed. 29# @RETURN: Shell true if we succeed, shell false otherwise
30# @DESCRIPTION:
31# Marks <ELF files> with provided PaX <flags>
16# 32#
17# If paxctl is installed, but not chpax, then the legacy 33# Flags are passed directly to the utilities unchanged. Possible flags at the
18# EI flags (which are not strip-safe) will not be set. 34# time of writing, taken from /sbin/paxctl, are:
19# If neither are installed, falls back to scanelf (which 35#
20# is always present, but currently doesn't quite do all 36# p: disable PAGEEXEC P: enable PAGEEXEC
21# that paxctl can do). 37# e: disable EMUTRMAP E: enable EMUTRMAP
22 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.
23pax-mark() { 49pax-mark() {
24 local flags fail=0 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 -
25 flags=$1 53 flags=${1//-}
26 shift 54 shift
27 if [[ -x /sbin/chpax ]]; then 55 # Try paxctl, then scanelf. paxctl is preferred.
28 einfo "Legacy EI PaX marking $* with ${flags}" 56 if type -p paxctl > /dev/null && has PT ${PAX_MARKINGS}; then
29 /sbin/chpax -${flags} $* || fail=1 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
30 fi 98 fi
31 if [[ -x /sbin/paxctl ]]; then 99 if [[ ${fail} == 1 ]]; then
32 einfo "PT PaX marking $* with ${flags}" 100 ewarn "Failed to set PaX markings -${flags} for:"
33 /sbin/paxctl -${flags} $* || 101 _pax_list_files ewarn ${failures}
34 /sbin/paxctl -c${flags} $* || 102 ewarn "Executables may be killed by PaX kernels."
35 /sbin/paxctl -C${flags} $* || fail=1
36 elif [[ -x /usr/bin/scanelf ]]; then
37 einfo "Fallback PaX marking $* with ${flags}"
38 /usr/bin/scanelf -Xxz ${flags} $*
39 else
40 ewarn "Failed to set PaX markings ${flags} for files $*. Executables may be killed by PaX kernels."
41 fail=1
42 fi 103 fi
43 return ${fail} 104 return ${fail}
44} 105}
45 106
46##### host-is-pax 107# @FUNCTION: list-paxables
47# Indicates whether the build machine has PaX or not; intended for use 108# @USAGE: {<files>}
48# where the build process must be modified conditionally in order to satisfy PaX. 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/*)
118list-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).
49host-is-pax() { 129host-is-pax() {
50 # We need procfs to work this out. PaX is only available on Linux,
51 # so result is always false on non-linux machines (e.g. Gentoo/*BSD)
52 [[ -e /proc/self/status ]] || return 1
53 grep ^PaX: /proc/self/status > /dev/null 130 grep -qs ^PaX: /proc/self/status
54 return $?
55} 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}
151

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.16

  ViewVC Help
Powered by ViewVC 1.1.20