Skip to content

Commit 50f742d

Browse files
nightmaredmimizohar
authored andcommitted
IMA: block writes of the security.ima xattr with unsupported algorithms
By default, writes to the extended attributes security.ima will be allowed even if the hash algorithm used for the xattr is not compiled in the kernel (which does not make sense because the kernel would not be able to appraise that file as it lacks support for validating the hash). Prevent and audit writes to the security.ima xattr if the hash algorithm used in the new value is not available in the current kernel. Signed-off-by: THOBY Simon <Simon.THOBY@viveris.fr> Reviewed-by: Lakshmi Ramasubramanian <nramas@linux.microsoft.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 8510505 commit 50f742d

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

security/integrity/ima/ima.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
319319
void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
320320
enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
321321
enum ima_hooks func);
322-
enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
322+
enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
323323
int xattr_len);
324324
int ima_read_xattr(struct dentry *dentry,
325325
struct evm_ima_xattr_data **xattr_value);

security/integrity/ima/ima_appraise.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static void ima_cache_flags(struct integrity_iint_cache *iint,
171171
}
172172
}
173173

174-
enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
174+
enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
175175
int xattr_len)
176176
{
177177
struct signature_v2_hdr *sig;
@@ -575,6 +575,47 @@ static void ima_reset_appraise_flags(struct inode *inode, int digsig)
575575
clear_bit(IMA_DIGSIG, &iint->atomic_flags);
576576
}
577577

578+
/**
579+
* validate_hash_algo() - Block setxattr with unsupported hash algorithms
580+
* @dentry: object of the setxattr()
581+
* @xattr_value: userland supplied xattr value
582+
* @xattr_value_len: length of xattr_value
583+
*
584+
* The xattr value is mapped to its hash algorithm, and this algorithm
585+
* must be built in the kernel for the setxattr to be allowed.
586+
*
587+
* Emit an audit message when the algorithm is invalid.
588+
*
589+
* Return: 0 on success, else an error.
590+
*/
591+
static int validate_hash_algo(struct dentry *dentry,
592+
const struct evm_ima_xattr_data *xattr_value,
593+
size_t xattr_value_len)
594+
{
595+
char *path = NULL, *pathbuf = NULL;
596+
enum hash_algo xattr_hash_algo;
597+
598+
xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
599+
600+
if (likely(xattr_hash_algo == ima_hash_algo ||
601+
crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0)))
602+
return 0;
603+
604+
pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
605+
if (!pathbuf)
606+
return -EACCES;
607+
608+
path = dentry_path(dentry, pathbuf, PATH_MAX);
609+
610+
integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
611+
"set_data", "unavailable-hash-algorithm",
612+
-EACCES, 0);
613+
614+
kfree(pathbuf);
615+
616+
return -EACCES;
617+
}
618+
578619
int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
579620
const void *xattr_value, size_t xattr_value_len)
580621
{
@@ -592,9 +633,11 @@ int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
592633
digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
593634
}
594635
if (result == 1 || evm_revalidate_status(xattr_name)) {
636+
result = validate_hash_algo(dentry, xvalue, xattr_value_len);
637+
if (result)
638+
return result;
639+
595640
ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
596-
if (result == 1)
597-
result = 0;
598641
}
599642
return result;
600643
}

0 commit comments

Comments
 (0)