[PATCH v6 0/5] IMA: restrict the accepted digest algorithms for the security.ima xattr

THOBY Simon
2021-08-04T09:20:24Z

IMA protects files by storing a hash (or a signature thereof) of their
content in the security.ima xattr. While the security.ima xattr itself
is protected by EVM with either a HMAC or a digital signature, no
mechanism is currently in place to ensure that the security.ima xattr
was generated with a strong digest algorithm, and any hash defined
in the kernel will be accepted, even obsolete formats like MD4 and MD5.

The kernel itself will only write this xattr with the 'ima_hash' parameter,
fixed at init, but it will also happily accept userland writes for said
xattr, and those writes may use arbitrary hash algorithms as long as the
kernel have support for it.

One important point is safeguarding users from mislabelling their
files when using userland utilities to update their files, as this
is the kind of behavior one can observe with evmctl (`evmctl ima_hash`
defaults to sha1). Another group that may be interested is those
that have deployed IMA years ago, possibly using algorithms that
was then deemed sufficiently collision-resistant, but that proved
to be weak with the passage of time (note that this could also
happen in the future with algorithms considered safe today).
This patch provides a migration path of sorts for these users.

This patch series gives users the ability to restrict the algorithms
accepted by their system, both when writing/updating xattrs, and
when appraising files, while retaining a permissive behavior by default
to preserve backward compatibility.

To provide these features, alter the behavior of setxattr to
only accept hashes built in the kernel, instead of any hash listed
in the kernel (complete list crypto/hash_info.c). In addition, the
user can define in his IMA policy the list of digest algorithms
allowed for writing to the security.ima xattr. In that case,
only algorithms present in that list are accepted for writing.

In addition, users may opt-in to allowlist hash algorithms for
ppraising thanks to the new "appraise_hash" IMA policy option.
By default IMA will keep accepting any hash algorithm, but specifying
that option will make appraisal of files hashed with another algorithm
fail.

Even when using this option to restrict accepted hashes, a migration
to a new algorithm is still possible. Suppose your policy states you
must migrate from 'old_algo' (e.g. sha1) to 'new_algo' (e.g. one of
sha256/384/512). You can upgrade without relaxing the hash requirements:
alter your policy rules from 'appraise_hash=old_algo' to
'appraise_hash=old_algo,new_algo', load a new SETXATTR_CHECK policy
rule that accept writes using 'new_algo', reboot, relabel
all your files with 'new_algo', alter your policy rules from
'appraise_hash=old_algo,new_algo' to 'appraise_hash=new_algo',
and you're done.
While this represent a significant amount of work, it is important to
showcase that this patchset is flexible enough to let users upgrade
if needed.

Here is also a short description of the new audit messages, but I can
send it in a followup mail if that is not the proper place:

When writing the xattr with an algorithm not built in the kernel (here the
kernel was built with CONFIG_CRYPTO_MD5 unset), e.g. with
"evmctl ima_hash -a md5 /usr/bin/strace":
audit(1628066120.418:121): pid=1344 uid=0 auid=0 ses=1 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 op=collect_data cause=unavailable-hash-algorithm comm="evmctl" name="/usr/bin/strace" dev="dm-0" ino=2632657 res=0 errno=0

With the same command and the policy rule
"appraise func=SETXATTR_CHECK appraise_hash=sha256,sha384,sha512", we get:
audit(1628066210.141:127): pid=1362 uid=0 auid=0 ses=1 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 op=collect_data cause=denied-hash-algorithm comm="evmctl" name="/usr/bin/strace" dev="dm-0" ino=2632657 res=0 errno=0

Note that the cause is now 'denied-hash-algorithm' instead of
'unavailable-hash-algorithm'. We get that audit message for any algorithm
outside of sha256/384/512 (including MD5). In a sense, 'denied-hash-algorithm'
take predecence over 'unavailable-hash-algorithm'.

When appraising files, e.g. trying to execute a file whose xattr was hashed
with sha1 while the policy rule
"appraise func=BPRM_CHECK fowner=0 appraise_hash=sha256" is enabled:
audit(1628066349.230:130): pid=1369 uid=0 auid=0 ses=1 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 op=collect_data cause=denied-hash-algorithm comm="bash" name="/usr/bin/strace" dev="dm-0" ino=2632657 res=0 errno=0

This yields the same error message when appraising a file and updating a
xattr with an invalid algorithm. Maybe that is not a desired behavior ?

This series is based on the following repo/branch:
repo: https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git
branch: next-integrity-testing
commit e37be5343ae2b9419aea1442b07e5d2428b437b4 ("Merge branch 'ima-buffer-measurement-changes-v4' into next-integrity")

Changelog since v5:
- Adapt a few places where lines were longer than 80 characters (suggested by
Mimi Zohar)
- Rebase on top on next-integrity-testing (suggested by Mimi Zohar)
- Replace "whitelist" with "allowlist" (suggested by both Mimi Zohar and
Lakshmi Ramasubramanian)
- Fix a broken kernel-doc (suggested by Mimi Zohar)
- Prune the feature that updated "func=SETXATTR_CHECK" (suggested by Mimi
Zohar)
- Update the commit messages with suggestions from Lakshmi Ramasubramanian
- Short-circuit a bit the evaluation in ima_appraise.c/validate_hash_algo
to return earlier in case of memory exhaustion (suggested by Lakshmi
Ramasubramanian)
- Renamed "forbidden-hash-algorithm" in ima_main.c to reduce the number of

Changelog since v4:
- Deleting the ability to remove SETXATTR_CHECK rules, as it added
a lot of concurrency troubles while creating a special case for
SETXATTR_CHECK rules only, which is rarely a good idea (suggested by Mimi
Zohar)
- Change from #ifdef CONFIG_... to IS_ENABLED (suggested by Mimi Zohar)
- Various fixes (code style, english grammar errors, double initialization
to zero) reported by Mimi Zohar
- Fixed a logic inversion error introduced in v4 where checks where
performed when no SETXATTR_CHECK rule was enabled.
- Do not log partial audit messages under memory pressure (suggested by
Mimi Zohar)

Changelog since v3:
- fixed an issue where the first write to the policy would ignore the
SETXATTR_CHECK attribute
- fixed potential concurrency issues (I would greatly like external
opinions on this, because I clearly don't know much about RCU. Beside
maybe it's better to completely ignore the duplicates SETXATTR_CHECK
issue and not update the IMA policy in any case)
- remove the CONFIG_CRYPTO_MD5 requirement for IMA (suggested by Mimi Zohar)
- updated commit messages to follow more closely the kernel style guide
(suggested by Mimi Zohar)
- moved the hash verification code on appraisal a bit later, to prevent
issues when using the code with IMA in a disable/auditing mode
(suggested by Mimi Zohar)
- limit the 'appraise_hash' parameter to the 'appraise' action
(suggested by Mimi Zohar)

Changelog since v2:
- remove the SecureBoot-specific behavior (suggested by Mimi Zohar)
- users can now tweak through policy both the algorithms for
appraising files (a feature already present in v2) and for writing
with the new SETXATTR_CHECK value for the 'func' ima policy flag
- updating 'forbidden-hash-algorithm' to 'denied-hash-algorithm' and
'unsupported-hash-algorithm' to disambiguate cases when the user
asked for an algorithm not present in the kernel and when the system
vendor explicitly opted in to a restricted list of accepted
algorithms (suggested by Mimi Zohar)
- change the order of the patches to be bisect-safe while retaining
the guarantee that a policy cannot be accepted but not enforced
(suggested by Mimi Zohar)

Changelog since v1:
- Remove the two boot parameters (suggested by Mimi Zohar)
- filter out hash algorithms not compiled in the kernel
on xattr writes (suggested by Mimi Zohar)
- add a special case when secure boot is enabled: only the
ima_hash algorithm is accepted on userland writes
- add a policy option to opt-in to restricting digest algorithms
at a per-rule granularity (suggested by Mimi Zohar)

Simon Thoby (5):
IMA: remove the dependency on CRYPTO_MD5
IMA: block writes of the security.ima xattr with unsupported
algorithms
IMA: add support to restrict the hash algorithms used for file
appraisal
IMA: add a policy option to restrict xattr hash algorithms on
appraisal
IMA: introduce a new policy option func=SETXATTR_CHECK

Documentation/ABI/testing/ima_policy | 15 ++-
security/integrity/ima/Kconfig | 1 -
security/integrity/ima/ima.h | 14 ++-
security/integrity/ima/ima_api.c | 6 +-
security/integrity/ima/ima_appraise.c | 73 ++++++++++-
security/integrity/ima/ima_main.c | 23 +++-
security/integrity/ima/ima_policy.c | 169 +++++++++++++++++++++++---
7 files changed, 267 insertions(+), 34 deletions(-)

--
2.31.1