1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: fcaps.eclass
6 # base-system@gentoo.org
7 # @BLURB: function to set POSIX file-based capabilities
9 # This eclass provides a function to set file-based capabilities on binaries.
10 # This is not the same as USE=caps which controls runtime capability changes,
11 # often via packages like libcap.
13 # Due to probable capability-loss on moving or copying, this happens in
14 # pkg_postinst phase (at least for now).
17 # You can manually set the caps on ping and ping6 by doing:
20 # fcaps cap_net_raw bin/ping bin/ping6
24 # Or set it via the global ebuild var FILECAPS:
27 # cap_net_raw bin/ping bin/ping6
31 if [[ -z ${_FCAPS_ECLASS} ]]; then
36 # We can't use libcap-ng atm due to #471414.
38 [0-6]) DEPEND="filecaps? ( sys-libs/libcap )" ;;
39 *) BDEPEND="filecaps? ( sys-libs/libcap )" ;;
42 # @ECLASS-VARIABLE: FILECAPS
45 # An array of fcap arguments to use to automatically execute fcaps. See that
46 # function for more details.
48 # All args are consumed until the '--' marker is found. So if you have:
50 # FILECAPS=( moo cow -- fat cat -- chubby penguin )
53 # This will end up executing:
57 # fcaps chubby penguin
60 # Note: If you override pkg_postinst, you must call fcaps_pkg_postinst yourself.
63 # @USAGE: [-o <owner>] [-g <group>] [-m <mode>] [-M <caps mode>] <capabilities> <file[s]>
65 # Sets the specified capabilities on the specified files.
67 # The caps option takes the form as expected by the cap_from_text(3) man page.
68 # If no action is specified, then "=ep" will be used as a default.
70 # If the file is a relative path (e.g. bin/foo rather than /bin/foo), then the
71 # appropriate path var ($D/$ROOT/etc...) will be prefixed based on the current
74 # The caps mode (default 711) is used to set the permission on the file if
75 # capabilities were properly set on the file.
77 # If the system is unable to set capabilities, it will use the specified user,
78 # group, and mode (presumably to make the binary set*id). The defaults there
79 # are root:0 and 4711. Otherwise, the ownership and permissions will be
82 debug-print-function ${FUNCNAME} "$@"
84 if [[ ${EUID} != 0 ]] ; then
85 einfo "Insufficient privileges to execute ${FUNCNAME}, skipping."
89 # Process the user options first.
95 while [[ $# -gt 0 ]] ; do
100 -M) caps_mode=$2; shift;;
106 [[ $# -lt 2 ]] && die "${FUNCNAME}: wrong arg count"
109 [[ ${caps} == *[-=+]* ]] || caps+="=ep"
113 case ${EBUILD_PHASE} in
114 compile|install|preinst)
118 root=${EROOT:-${ROOT}}
123 # Process every file!
126 [[ ${file} != /* ]] && file="${root}/${file}"
128 if use filecaps ; then
129 # Try to set capabilities. Ignore errors when the
130 # fs doesn't support it, but abort on all others.
131 debug-print "${FUNCNAME}: setting caps '${caps}' on '${file}'"
133 # If everything goes well, we don't want the file to be readable
135 chmod ${caps_mode} "${file}" || die
137 # Set/verify funcs for sys-libs/libcap.
138 _libcap() { setcap "${caps}" "${file}" ; }
139 _libcap_verify() { setcap -v "${caps}" "${file}" >/dev/null ; }
141 # Set/verify funcs for sys-libs/libcap-ng.
142 # Note: filecap only supports =ep mode.
143 # It also expects a different form:
144 # setcap cap_foo,cap_bar
147 local caps=",${caps%=ep}"
148 filecap "${file}" "${caps//,cap_}"
150 _libcap_ng_verify() {
151 # libcap-ng has a crappy interface
152 local rcaps icaps caps=",${caps%=ep}"
153 rcaps=$(filecap "${file}" | \
155 -e "s:^.{${#file}} +::" \
159 [[ ${PIPESTATUS[0]} -eq 0 ]] || return 1
160 icaps=$(echo "${caps//,cap_}" | LC_ALL=C sort)
161 [[ ${rcaps} == ${icaps} ]]
164 local out cmd notfound=0
165 for cmd in _libcap _libcap_ng ; do
166 if ! out=$(LC_ALL=C ${cmd} 2>&1) ; then
168 *"command not found"*)
172 # ENOTSUP and EOPNOTSUPP might be the same value which means
173 # strerror() on them is unstable -- we can get both. #559608
175 *"Operation not supported"*)
176 local fstype=$(stat -f -c %T "${file}")
177 ewarn "Could not set caps on '${file}' due to missing filesystem support:"
178 ewarn "* enable XATTR support for '${fstype}' in your kernel (if configurable)"
179 ewarn "* mount the fs with the user_xattr option (if not the default)"
180 ewarn "* enable the relevant FS_SECURITY option (if configurable)"
184 eerror "Setting caps '${caps}' on file '${file}' failed:"
186 die "could not set caps"
190 # Sanity check that everything took.
191 ${cmd}_verify || die "Checking caps '${caps}' on '${file}' failed"
193 # Everything worked. Move on to the next file.
197 if [[ ${notfound} -eq 2 ]] && [[ -z ${_FCAPS_WARNED} ]] ; then
199 ewarn "Could not find cap utils; make sure libcap or libcap-ng is available."
203 # If we're still here, setcaps failed.
204 debug-print "${FUNCNAME}: setting owner/mode on '${file}'"
205 chown "${owner}:${group}" "${file}" || die
206 chmod ${mode} "${file}" || die
210 # @FUNCTION: fcaps_pkg_postinst
212 # Process the FILECAPS array.
213 fcaps_pkg_postinst() {
215 for arg in "${FILECAPS[@]}" "--" ; do
216 if [[ ${arg} == "--" ]] ; then
225 EXPORT_FUNCTIONS pkg_postinst