pax-utils.eclass 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright 1999-2016 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: pax-utils.eclass
  4. # @MAINTAINER:
  5. # The Gentoo Linux Hardened Team <hardened@gentoo.org>
  6. # @AUTHOR:
  7. # Author: Kevin F. Quinn <kevquinn@gentoo.org>
  8. # Author: Anthony G. Basile <blueness@gentoo.org>
  9. # @BLURB: functions to provide PaX markings for hardened kernels
  10. # @DESCRIPTION:
  11. #
  12. # This eclass provides support for manipulating PaX markings on ELF binaries,
  13. # whether the system is using legacy PT_PAX markings or the newer XATTR_PAX.
  14. # The eclass wraps the use of paxctl-ng, paxctl, set/getattr and scanelf utilities,
  15. # deciding which to use depending on what's installed on the build host, and
  16. # whether we're working with PT_PAX, XATTR_PAX or both.
  17. #
  18. # To control what markings are made, set PAX_MARKINGS in /etc/portage/make.conf
  19. # to contain either "PT", "XT" or "none". The default is to attempt both
  20. # PT_PAX and XATTR_PAX.
  21. if [[ -z ${_PAX_UTILS_ECLASS} ]]; then
  22. _PAX_UTILS_ECLASS=1
  23. # @ECLASS-VARIABLE: PAX_MARKINGS
  24. # @DESCRIPTION:
  25. # Control which markings are made:
  26. # PT = PT_PAX markings, XT = XATTR_PAX markings
  27. # Default to PT markings.
  28. PAX_MARKINGS=${PAX_MARKINGS:="PT XT"}
  29. # @FUNCTION: pax-mark
  30. # @USAGE: <flags> <ELF files>
  31. # @RETURN: Shell true if we succeed, shell false otherwise
  32. # @DESCRIPTION:
  33. # Marks <ELF files> with provided PaX <flags>
  34. #
  35. # Flags are passed directly to the utilities unchanged.
  36. #
  37. # @CODE
  38. # p: disable PAGEEXEC P: enable PAGEEXEC
  39. # e: disable EMUTRAMP E: enable EMUTRAMP
  40. # m: disable MPROTECT M: enable MPROTECT
  41. # r: disable RANDMMAP R: enable RANDMMAP
  42. # s: disable SEGMEXEC S: enable SEGMEXEC
  43. # @CODE
  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. #
  48. # Please confirm any relaxation of restrictions with the Gentoo Hardened team.
  49. # Either ask on the gentoo-hardened mailing list, or CC/assign hardened@g.o on
  50. # the bug report.
  51. pax-mark() {
  52. local f # loop over paxables
  53. local flags # pax flags
  54. local ret=0 # overall return code of this function
  55. # Only the actual PaX flags and z are accepted
  56. # 1. The leading '-' is optional
  57. # 2. -C -c only make sense for paxctl, but are unnecessary
  58. # because we progressively do -q -qc -qC
  59. # 3. z is allowed for the default
  60. flags="${1//[!zPpEeMmRrSs]}"
  61. [[ "${flags}" ]] || return 0
  62. shift
  63. # z = default. For XATTR_PAX, the default is no xattr field at all
  64. local dodefault=""
  65. [[ "${flags//[!z]}" ]] && dodefault="yes"
  66. if has PT ${PAX_MARKINGS}; then
  67. # Uncomment to list all files to be marked
  68. # _pax_list_files einfo "$@"
  69. for f in "$@"; do
  70. # First try paxctl
  71. if type -p paxctl >/dev/null; then
  72. einfo "PT_PAX marking -${flags} ${f} with paxctl"
  73. # We try modifying the existing PT_PAX_FLAGS header.
  74. paxctl -q${flags} "${f}" >/dev/null 2>&1 && continue
  75. # We no longer try to create/convert a PT_PAX_FLAGS header, bug #590422
  76. # paxctl -qC${flags} "${f}" >/dev/null 2>&1 && continue
  77. # paxctl -qc${flags} "${f}" >/dev/null 2>&1 && continue
  78. fi
  79. # Next try paxctl-ng -> this will not create/convert any program headers.
  80. if type -p paxctl-ng >/dev/null && paxctl-ng -L ; then
  81. einfo "PT_PAX marking -${flags} ${f} with paxctl-ng"
  82. flags="${flags//z}"
  83. [[ ${dodefault} == "yes" ]] && paxctl-ng -L -z "${f}" >/dev/null 2>&1
  84. [[ "${flags}" ]] || continue
  85. paxctl-ng -L -${flags} "${f}" >/dev/null 2>&1 && continue
  86. fi
  87. # Finally fall back on scanelf.
  88. if type -p scanelf >/dev/null && [[ ${PAX_MARKINGS} != "none" ]]; then
  89. einfo "PT_PAX marking -${flags} ${f} with scanelf"
  90. scanelf -Xxz ${flags} "$f" >/dev/null 2>&1
  91. # We failed to set PT_PAX flags.
  92. elif [[ ${PAX_MARKINGS} != "none" ]]; then
  93. elog "Failed to set PT_PAX markings -${flags} ${f}."
  94. ret=1
  95. fi
  96. done
  97. fi
  98. if has XT ${PAX_MARKINGS}; then
  99. # Uncomment to list all files to be marked
  100. # _pax_list_files einfo "$@"
  101. flags="${flags//z}"
  102. for f in "$@"; do
  103. # First try paxctl-ng.
  104. if type -p paxctl-ng >/dev/null && paxctl-ng -l ; then
  105. einfo "XATTR_PAX marking -${flags} ${f} with paxctl-ng"
  106. [[ ${dodefault} == "yes" ]] && paxctl-ng -d "${f}" >/dev/null 2>&1
  107. [[ "${flags}" ]] || continue
  108. paxctl-ng -l -${flags} "${f}" >/dev/null 2>&1 && continue
  109. fi
  110. # Next try setfattr.
  111. if type -p setfattr >/dev/null; then
  112. [[ "${flags//[!Ee]}" ]] || flags+="e" # bug 447150
  113. einfo "XATTR_PAX marking -${flags} ${f} with setfattr"
  114. [[ ${dodefault} == "yes" ]] && setfattr -x "user.pax.flags" "${f}" >/dev/null 2>&1
  115. setfattr -n "user.pax.flags" -v "${flags}" "${f}" >/dev/null 2>&1 && continue
  116. fi
  117. # We failed to set XATTR_PAX flags.
  118. if [[ ${PAX_MARKINGS} != "none" ]]; then
  119. elog "Failed to set XATTR_PAX markings -${flags} ${f}."
  120. ret=1
  121. fi
  122. done
  123. fi
  124. # [[ ${ret} == 1 ]] && elog "Executables may be killed by PaX kernels."
  125. return ${ret}
  126. }
  127. # @FUNCTION: list-paxables
  128. # @USAGE: <files>
  129. # @RETURN: Subset of <files> which are ELF executables or shared objects
  130. # @DESCRIPTION:
  131. # Print to stdout all of the <files> that are suitable to have PaX flag
  132. # markings, i.e., filter out the ELF executables or shared objects from a list
  133. # of files. This is useful for passing wild-card lists to pax-mark, although
  134. # in general it is preferable for ebuilds to list precisely which ELFS are to
  135. # be marked. Often not all the ELF installed by a package need remarking.
  136. # @EXAMPLE:
  137. # pax-mark -m $(list-paxables ${S}/{,usr/}bin/*)
  138. list-paxables() {
  139. file "$@" 2> /dev/null | grep -E 'ELF.*(executable|shared object)' | sed -e 's/: .*$//'
  140. }
  141. # @FUNCTION: host-is-pax
  142. # @RETURN: Shell true if the build process is PaX enabled, shell false otherwise
  143. # @DESCRIPTION:
  144. # This is intended for use where the build process must be modified conditionally
  145. # depending on whether the host is PaX enabled or not. It is not indented to
  146. # determine whether the final binaries need PaX markings. Note: if procfs is
  147. # not mounted on /proc, this returns shell false (e.g. Gentoo/FreeBSD).
  148. host-is-pax() {
  149. grep -qs ^PaX: /proc/self/status
  150. }
  151. # INTERNAL FUNCTIONS
  152. # ------------------
  153. #
  154. # These functions are for use internally by the eclass - do not use
  155. # them elsewhere as they are not supported (i.e. they may be removed
  156. # or their function may change arbitrarily).
  157. # Display a list of things, one per line, indented a bit, using the
  158. # display command in $1.
  159. _pax_list_files() {
  160. local f cmd
  161. cmd=$1
  162. shift
  163. for f in "$@"; do
  164. ${cmd} " ${f}"
  165. done
  166. }
  167. fi