Skip to content

Commit c7c8bb2

Browse files
Dmitry KasatkinMimi Zohar
authored andcommitted
ima: provide support for arbitrary hash algorithms
In preparation of supporting more hash algorithms with larger hash sizes needed for signature verification, this patch replaces the 20 byte sized digest, with a more flexible structure. The new structure includes the hash algorithm, digest size, and digest. Changelog: - recalculate filedata hash for the measurement list, if the signature hash digest size is greater than 20 bytes. - use generic HASH_ALGO_ - make ima_calc_file_hash static - scripts lindent and checkpatch fixes Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com> Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
1 parent 3fe78ca commit c7c8bb2

9 files changed

Lines changed: 100 additions & 35 deletions

File tree

‎crypto/asymmetric_keys/x509_parser.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ struct x509_certificate {
2121
char *authority; /* Authority key fingerprint as hex */
2222
struct tm valid_from;
2323
struct tm valid_to;
24-
enum pkey_algo pkey_algo : 8; /* Public key algorithm */
25-
enum hash_algo sig_hash_algo : 8; /* Signature hash algorithm */
2624
const void *tbs; /* Signed data */
2725
unsigned tbs_size; /* Size of signed data */
2826
unsigned raw_sig_size; /* Size of sigature */

‎crypto/asymmetric_keys/x509_public_key.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
213213
cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
214214
cert->valid_to.tm_mday, cert->valid_to.tm_hour,
215215
cert->valid_to.tm_min, cert->valid_to.tm_sec);
216-
pr_devel("Cert Signature: %s\n",
216+
pr_devel("Cert Signature: %s + %s\n",
217+
pkey_algo_name[cert->sig.pkey_algo],
217218
hash_algo_name[cert->sig.pkey_hash_algo]);
218219

219220
if (!cert->fingerprint) {

‎security/integrity/ima/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config IMA
99
select CRYPTO_HMAC
1010
select CRYPTO_MD5
1111
select CRYPTO_SHA1
12+
select CRYPTO_HASH_INFO
1213
select TCG_TPM if HAS_IOMEM && !UML
1314
select TCG_TIS if TCG_TPM && X86
1415
select TCG_IBMVTPM if TCG_TPM && PPC64

‎security/integrity/ima/ima.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };
3939
/* set during initialization */
4040
extern int ima_initialized;
4141
extern int ima_used_chip;
42-
extern char *ima_hash;
42+
extern int ima_hash_algo;
4343
extern int ima_appraise;
4444

4545
/* IMA inode template definition */
@@ -70,8 +70,9 @@ void ima_fs_cleanup(void);
7070
int ima_inode_alloc(struct inode *inode);
7171
int ima_add_template_entry(struct ima_template_entry *entry, int violation,
7272
const char *op, struct inode *inode);
73-
int ima_calc_file_hash(struct file *file, char *digest);
74-
int ima_calc_buffer_hash(const void *data, int len, char *digest);
73+
int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
74+
int ima_calc_buffer_hash(const void *data, int len,
75+
struct ima_digest_data *hash);
7576
int ima_calc_boot_aggregate(char *digest);
7677
void ima_add_violation(struct inode *inode, const unsigned char *filename,
7778
const char *op, const char *cause);

‎security/integrity/ima/ima_api.c‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,22 @@ int ima_store_template(struct ima_template_entry *entry,
4444
const char *op = "add_template_measure";
4545
const char *audit_cause = "hashing_error";
4646
int result;
47+
struct ima_digest_data hash;
4748

4849
memset(entry->digest, 0, sizeof(entry->digest));
4950
entry->template_name = IMA_TEMPLATE_NAME;
5051
entry->template_len = sizeof(entry->template);
5152

5253
if (!violation) {
5354
result = ima_calc_buffer_hash(&entry->template,
54-
entry->template_len,
55-
entry->digest);
55+
entry->template_len, &hash);
5656
if (result < 0) {
5757
integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
5858
entry->template_name, op,
5959
audit_cause, result, 0);
6060
return result;
6161
}
62+
memcpy(entry->digest, hash.digest, hash.length);
6263
}
6364
result = ima_add_template_entry(entry, violation, op, inode);
6465
return result;
@@ -147,8 +148,9 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
147148
if (!(iint->flags & IMA_COLLECTED)) {
148149
u64 i_version = file_inode(file)->i_version;
149150

150-
iint->ima_xattr.type = IMA_XATTR_DIGEST;
151-
result = ima_calc_file_hash(file, iint->ima_xattr.digest);
151+
/* use default hash algorithm */
152+
iint->ima_hash.algo = ima_hash_algo;
153+
result = ima_calc_file_hash(file, &iint->ima_hash);
152154
if (!result) {
153155
iint->version = i_version;
154156
iint->flags |= IMA_COLLECTED;
@@ -196,7 +198,21 @@ void ima_store_measurement(struct integrity_iint_cache *iint,
196198
return;
197199
}
198200
memset(&entry->template, 0, sizeof(entry->template));
199-
memcpy(entry->template.digest, iint->ima_xattr.digest, IMA_DIGEST_SIZE);
201+
if (iint->ima_hash.algo != ima_hash_algo) {
202+
struct ima_digest_data hash;
203+
204+
hash.algo = ima_hash_algo;
205+
result = ima_calc_file_hash(file, &hash);
206+
if (result)
207+
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
208+
filename, "collect_data", "failed",
209+
result, 0);
210+
else
211+
memcpy(entry->template.digest, hash.digest,
212+
hash.length);
213+
} else
214+
memcpy(entry->template.digest, iint->ima_hash.digest,
215+
iint->ima_hash.length);
200216
strcpy(entry->template.file_name,
201217
(strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
202218
file->f_dentry->d_name.name : filename);
@@ -212,14 +228,14 @@ void ima_audit_measurement(struct integrity_iint_cache *iint,
212228
const unsigned char *filename)
213229
{
214230
struct audit_buffer *ab;
215-
char hash[(IMA_DIGEST_SIZE * 2) + 1];
231+
char hash[(iint->ima_hash.length * 2) + 1];
216232
int i;
217233

218234
if (iint->flags & IMA_AUDITED)
219235
return;
220236

221-
for (i = 0; i < IMA_DIGEST_SIZE; i++)
222-
hex_byte_pack(hash + (i * 2), iint->ima_xattr.digest[i]);
237+
for (i = 0; i < iint->ima_hash.length; i++)
238+
hex_byte_pack(hash + (i * 2), iint->ima_hash.digest[i]);
223239
hash[i * 2] = '\0';
224240

225241
ab = audit_log_start(current->audit_context, GFP_KERNEL,

‎security/integrity/ima/ima_appraise.c‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
4343
}
4444

4545
static int ima_fix_xattr(struct dentry *dentry,
46-
struct integrity_iint_cache *iint)
46+
struct integrity_iint_cache *iint)
4747
{
48-
iint->ima_xattr.type = IMA_XATTR_DIGEST;
48+
iint->ima_hash.type = IMA_XATTR_DIGEST;
4949
return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
50-
(u8 *)&iint->ima_xattr,
51-
sizeof(iint->ima_xattr), 0);
50+
&iint->ima_hash.type,
51+
1 + iint->ima_hash.length, 0);
5252
}
5353

5454
/* Return specific func appraised cached result */
@@ -159,8 +159,12 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
159159
status = INTEGRITY_FAIL;
160160
break;
161161
}
162-
rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
163-
IMA_DIGEST_SIZE);
162+
if (rc - 1 == iint->ima_hash.length)
163+
rc = memcmp(xattr_value->digest,
164+
iint->ima_hash.digest,
165+
iint->ima_hash.length);
166+
else
167+
rc = -EINVAL;
164168
if (rc) {
165169
cause = "invalid-hash";
166170
status = INTEGRITY_FAIL;
@@ -172,8 +176,8 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
172176
iint->flags |= IMA_DIGSIG;
173177
rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
174178
xattr_value->digest, rc - 1,
175-
iint->ima_xattr.digest,
176-
IMA_DIGEST_SIZE);
179+
iint->ima_hash.digest,
180+
iint->ima_hash.length);
177181
if (rc == -EOPNOTSUPP) {
178182
status = INTEGRITY_UNKNOWN;
179183
} else if (rc) {

‎security/integrity/ima/ima_crypto.c‎

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/err.h>
2121
#include <linux/slab.h>
2222
#include <crypto/hash.h>
23+
#include <crypto/hash_info.h>
2324
#include "ima.h"
2425

2526
static struct crypto_shash *ima_shash_tfm;
@@ -28,10 +29,11 @@ int ima_init_crypto(void)
2829
{
2930
long rc;
3031

31-
ima_shash_tfm = crypto_alloc_shash(ima_hash, 0, 0);
32+
ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
3233
if (IS_ERR(ima_shash_tfm)) {
3334
rc = PTR_ERR(ima_shash_tfm);
34-
pr_err("Can not allocate %s (reason: %ld)\n", ima_hash, rc);
35+
pr_err("Can not allocate %s (reason: %ld)\n",
36+
hash_algo_name[ima_hash_algo], rc);
3537
return rc;
3638
}
3739
return 0;
@@ -40,17 +42,19 @@ int ima_init_crypto(void)
4042
/*
4143
* Calculate the MD5/SHA1 file digest
4244
*/
43-
int ima_calc_file_hash(struct file *file, char *digest)
45+
static int ima_calc_file_hash_tfm(struct file *file,
46+
struct ima_digest_data *hash,
47+
struct crypto_shash *tfm)
4448
{
4549
loff_t i_size, offset = 0;
4650
char *rbuf;
4751
int rc, read = 0;
4852
struct {
4953
struct shash_desc shash;
50-
char ctx[crypto_shash_descsize(ima_shash_tfm)];
54+
char ctx[crypto_shash_descsize(tfm)];
5155
} desc;
5256

53-
desc.shash.tfm = ima_shash_tfm;
57+
desc.shash.tfm = tfm;
5458
desc.shash.flags = 0;
5559

5660
rc = crypto_shash_init(&desc.shash);
@@ -85,17 +89,42 @@ int ima_calc_file_hash(struct file *file, char *digest)
8589
}
8690
kfree(rbuf);
8791
if (!rc)
88-
rc = crypto_shash_final(&desc.shash, digest);
92+
rc = crypto_shash_final(&desc.shash, hash->digest);
8993
if (read)
9094
file->f_mode &= ~FMODE_READ;
9195
out:
9296
return rc;
9397
}
9498

99+
int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
100+
{
101+
struct crypto_shash *tfm = ima_shash_tfm;
102+
int rc;
103+
104+
if (hash->algo != ima_hash_algo && hash->algo < HASH_ALGO__LAST) {
105+
tfm = crypto_alloc_shash(hash_algo_name[hash->algo], 0, 0);
106+
if (IS_ERR(tfm)) {
107+
rc = PTR_ERR(tfm);
108+
pr_err("Can not allocate %s (reason: %d)\n",
109+
hash_algo_name[hash->algo], rc);
110+
return rc;
111+
}
112+
}
113+
114+
hash->length = crypto_shash_digestsize(tfm);
115+
116+
rc = ima_calc_file_hash_tfm(file, hash, tfm);
117+
118+
if (tfm != ima_shash_tfm)
119+
crypto_free_shash(tfm);
120+
121+
return rc;
122+
}
123+
95124
/*
96125
* Calculate the hash of a given buffer
97126
*/
98-
int ima_calc_buffer_hash(const void *data, int len, char *digest)
127+
int ima_calc_buffer_hash(const void *buf, int len, struct ima_digest_data *hash)
99128
{
100129
struct {
101130
struct shash_desc shash;
@@ -105,7 +134,11 @@ int ima_calc_buffer_hash(const void *data, int len, char *digest)
105134
desc.shash.tfm = ima_shash_tfm;
106135
desc.shash.flags = 0;
107136

108-
return crypto_shash_digest(&desc.shash, data, len, digest);
137+
/* this function uses default algo */
138+
hash->algo = ima_hash_algo;
139+
hash->length = crypto_shash_digestsize(ima_shash_tfm);
140+
141+
return crypto_shash_digest(&desc.shash, buf, len, hash->digest);
109142
}
110143

111144
static void __init ima_pcrread(int idx, u8 *pcr)

‎security/integrity/ima/ima_main.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/slab.h>
2525
#include <linux/xattr.h>
2626
#include <linux/ima.h>
27+
#include <crypto/hash_info.h>
2728

2829
#include "ima.h"
2930

@@ -35,11 +36,12 @@ int ima_appraise = IMA_APPRAISE_ENFORCE;
3536
int ima_appraise;
3637
#endif
3738

38-
char *ima_hash = "sha1";
39+
int ima_hash_algo = HASH_ALGO_SHA1;
40+
3941
static int __init hash_setup(char *str)
4042
{
4143
if (strncmp(str, "md5", 3) == 0)
42-
ima_hash = "md5";
44+
ima_hash_algo = HASH_ALGO_MD5;
4345
return 1;
4446
}
4547
__setup("ima_hash=", hash_setup);

‎security/integrity/integrity.h‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,29 @@ enum evm_ima_xattr_type {
5959
struct evm_ima_xattr_data {
6060
u8 type;
6161
u8 digest[SHA1_DIGEST_SIZE];
62-
} __attribute__((packed));
62+
} __packed;
63+
64+
#define IMA_MAX_DIGEST_SIZE 64
65+
66+
struct ima_digest_data {
67+
u8 algo;
68+
u8 length;
69+
u8 type;
70+
u8 digest[IMA_MAX_DIGEST_SIZE];
71+
} __packed;
6372

6473
/* integrity data associated with an inode */
6574
struct integrity_iint_cache {
66-
struct rb_node rb_node; /* rooted in integrity_iint_tree */
75+
struct rb_node rb_node; /* rooted in integrity_iint_tree */
6776
struct inode *inode; /* back pointer to inode in question */
6877
u64 version; /* track inode changes */
6978
unsigned long flags;
70-
struct evm_ima_xattr_data ima_xattr;
7179
enum integrity_status ima_file_status:4;
7280
enum integrity_status ima_mmap_status:4;
7381
enum integrity_status ima_bprm_status:4;
7482
enum integrity_status ima_module_status:4;
7583
enum integrity_status evm_status:4;
84+
struct ima_digest_data ima_hash;
7685
};
7786

7887
/* rbtree tree calls to lookup, insert, delete

0 commit comments

Comments
 (0)