By default, any write to the extended attributes security.ima will be accepted, even if the xattr value uses a hash algorithm not compiled in the kernel (which doesn't make sense, because the kernel wouldn't be able to appraise that file, as it lacks support for validating the hash). Prevent such writes: only writes using hash algorithms available in the current kernel are now allowed. Any attempt to perform these writes will be denied with an audit message. Note however that CONFIG_IMA depends on CONFIG_CRYPTO_SHA1, which somewhat hampers the security benefits of this measure (but MD4 and MD5 can be disabled, which is already a significant improvement). Signed-off-by: Simon Thoby <simon.thoby@xxxxxxxxxx> --- security/integrity/ima/ima_appraise.c | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c index ef9dcfce45d4..a5e0d3400bd1 100644 --- a/security/integrity/ima/ima_appraise.c +++ b/security/integrity/ima/ima_appraise.c @@ -575,6 +575,53 @@ static void ima_reset_appraise_flags(struct inode *inode, int digsig) clear_bit(IMA_DIGSIG, &iint->atomic_flags); } +/** + * validate_hash_algo() - Block setxattr with invalid digests + * @dentry: object being setxattr()'ed + * @xattr_value: value supplied by userland for the xattr + * @xattr_value_len: length of xattr_value + * + * Context: called when the user tries to write the security.ima xattr. + * The xattr value is mapped to some hash algorithm, and this algorithm + * must be built in the kernel for the setxattr to be allowed. + * + * Emit an audit message when the algorithm is invalid. + * + * Return: 0 on success, else an error. + */ +static int validate_hash_algo(struct dentry *dentry, + const void *xattr_value, + size_t xattr_value_len) +{ + char *path = NULL, *pathbuf = NULL; + enum hash_algo xattr_hash_algo; + + xattr_hash_algo = ima_get_hash_algo((struct evm_ima_xattr_data *)xattr_value, + xattr_value_len); + + if (likely(xattr_hash_algo == ima_hash_algo || + crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))) + return 0; + + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); + if (pathbuf) { + path = dentry_path(dentry, pathbuf, PATH_MAX); + + /* + * disallow xattr writes with algorithms disabled in the + * kernel configuration + */ + integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), + path, "collect_data", + "unavailable-hash-algorithm", + -EACCES, 0); + + kfree(pathbuf); + } + + return -EACCES; +} + int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, const void *xattr_value, size_t xattr_value_len) { @@ -595,6 +642,9 @@ int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, ima_reset_appraise_flags(d_backing_inode(dentry), digsig); if (result == 1) result = 0; + + result = validate_hash_algo(dentry, xattr_value, + xattr_value_len); } return result; } -- 2.31.1