Skip to content

Commit d3634d0

Browse files
Dmitry KasatkinMimi Zohar
authored andcommitted
ima: read and use signature hash algorithm
All files on the filesystem, currently, are hashed using the same hash algorithm. In preparation for files from different packages being signed using different hash algorithms, this patch adds support for reading the signature hash algorithm from the 'security.ima' extended attribute and calculates the appropriate file data hash based on it. Changelog: - fix scripts Lindent and checkpatch msgs - Mimi - fix md5 support for older version, which occupied 20 bytes in the xattr, not the expected 16 bytes. Fix the comparison to compare only the first 16 bytes. Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com> Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
1 parent c7c8bb2 commit d3634d0

6 files changed

Lines changed: 94 additions & 25 deletions

File tree

‎security/integrity/digsig_asymmetric.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@
1919

2020
#include "integrity.h"
2121

22-
/*
23-
* signature format v2 - for using with asymmetric keys
24-
*/
25-
struct signature_v2_hdr {
26-
uint8_t version; /* signature format version */
27-
uint8_t hash_algo; /* Digest algorithm [enum pkey_hash_algo] */
28-
uint32_t keyid; /* IMA key identifier - not X509/PGP specific*/
29-
uint16_t sig_size; /* signature size */
30-
uint8_t sig[0]; /* signature payload */
31-
} __packed;
32-
3322
/*
3423
* Request an asymmetric key.
3524
*/

‎security/integrity/ima/ima.h‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ static inline unsigned long ima_hash_key(u8 *digest)
9999
int ima_get_action(struct inode *inode, int mask, int function);
100100
int ima_must_measure(struct inode *inode, int mask, int function);
101101
int ima_collect_measurement(struct integrity_iint_cache *iint,
102-
struct file *file);
102+
struct file *file,
103+
struct evm_ima_xattr_data **xattr_value,
104+
int *xattr_len);
103105
void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
104106
const unsigned char *filename);
105107
void ima_audit_measurement(struct integrity_iint_cache *iint,
@@ -132,17 +134,25 @@ void ima_delete_rules(void);
132134

133135
#ifdef CONFIG_IMA_APPRAISE
134136
int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
135-
struct file *file, const unsigned char *filename);
137+
struct file *file, const unsigned char *filename,
138+
struct evm_ima_xattr_data *xattr_value,
139+
int xattr_len);
136140
int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
137141
void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
138142
enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
139143
int func);
144+
void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
145+
struct ima_digest_data *hash);
146+
int ima_read_xattr(struct dentry *dentry,
147+
struct evm_ima_xattr_data **xattr_value);
140148

141149
#else
142150
static inline int ima_appraise_measurement(int func,
143151
struct integrity_iint_cache *iint,
144152
struct file *file,
145-
const unsigned char *filename)
153+
const unsigned char *filename,
154+
struct evm_ima_xattr_data *xattr_value,
155+
int xattr_len)
146156
{
147157
return INTEGRITY_UNKNOWN;
148158
}
@@ -163,6 +173,19 @@ static inline enum integrity_status ima_get_cache_status(struct integrity_iint_c
163173
{
164174
return INTEGRITY_UNKNOWN;
165175
}
176+
177+
static inline void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
178+
int xattr_len,
179+
struct ima_digest_data *hash)
180+
{
181+
}
182+
183+
static inline int ima_read_xattr(struct dentry *dentry,
184+
struct evm_ima_xattr_data **xattr_value)
185+
{
186+
return 0;
187+
}
188+
166189
#endif
167190

168191
/* LSM based policy rules require audit */

‎security/integrity/ima/ima_api.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,27 @@ int ima_must_measure(struct inode *inode, int mask, int function)
139139
* Return 0 on success, error code otherwise
140140
*/
141141
int ima_collect_measurement(struct integrity_iint_cache *iint,
142-
struct file *file)
142+
struct file *file,
143+
struct evm_ima_xattr_data **xattr_value,
144+
int *xattr_len)
143145
{
144146
struct inode *inode = file_inode(file);
145147
const char *filename = file->f_dentry->d_name.name;
146148
int result = 0;
147149

150+
if (xattr_value)
151+
*xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
152+
148153
if (!(iint->flags & IMA_COLLECTED)) {
149154
u64 i_version = file_inode(file)->i_version;
150155

151156
/* use default hash algorithm */
152157
iint->ima_hash.algo = ima_hash_algo;
158+
159+
if (xattr_value)
160+
ima_get_hash_algo(*xattr_value, *xattr_len,
161+
&iint->ima_hash);
162+
153163
result = ima_calc_file_hash(file, &iint->ima_hash);
154164
if (!result) {
155165
iint->version = i_version;

‎security/integrity/ima/ima_appraise.c‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
107107
}
108108
}
109109

110+
void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
111+
struct ima_digest_data *hash)
112+
{
113+
struct signature_v2_hdr *sig;
114+
115+
if (!xattr_value || xattr_len < 0 || xattr_len <= 1 + sizeof(*sig))
116+
return;
117+
118+
sig = (typeof(sig)) xattr_value->digest;
119+
120+
if (xattr_value->type != EVM_IMA_XATTR_DIGSIG || sig->version != 2)
121+
return;
122+
123+
hash->algo = sig->hash_algo;
124+
}
125+
126+
int ima_read_xattr(struct dentry *dentry,
127+
struct evm_ima_xattr_data **xattr_value)
128+
{
129+
struct inode *inode = dentry->d_inode;
130+
131+
if (!inode->i_op->getxattr)
132+
return 0;
133+
134+
return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
135+
0, GFP_NOFS);
136+
}
137+
110138
/*
111139
* ima_appraise_measurement - appraise file measurement
112140
*
@@ -116,23 +144,22 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
116144
* Return 0 on success, error code otherwise
117145
*/
118146
int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
119-
struct file *file, const unsigned char *filename)
147+
struct file *file, const unsigned char *filename,
148+
struct evm_ima_xattr_data *xattr_value,
149+
int xattr_len)
120150
{
121151
struct dentry *dentry = file->f_dentry;
122152
struct inode *inode = dentry->d_inode;
123-
struct evm_ima_xattr_data *xattr_value = NULL;
124153
enum integrity_status status = INTEGRITY_UNKNOWN;
125154
const char *op = "appraise_data";
126155
char *cause = "unknown";
127-
int rc;
156+
int rc = xattr_len;
128157

129158
if (!ima_appraise)
130159
return 0;
131160
if (!inode->i_op->getxattr)
132161
return INTEGRITY_UNKNOWN;
133162

134-
rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
135-
0, GFP_NOFS);
136163
if (rc <= 0) {
137164
if (rc && rc != -ENODATA)
138165
goto out;
@@ -159,7 +186,10 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
159186
status = INTEGRITY_FAIL;
160187
break;
161188
}
162-
if (rc - 1 == iint->ima_hash.length)
189+
if (xattr_len - 1 >= iint->ima_hash.length)
190+
/* xattr length may be longer. md5 hash in previous
191+
version occupied 20 bytes in xattr, instead of 16
192+
*/
163193
rc = memcmp(xattr_value->digest,
164194
iint->ima_hash.digest,
165195
iint->ima_hash.length);
@@ -207,7 +237,6 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
207237
ima_cache_flags(iint, func);
208238
}
209239
ima_set_cache_status(iint, func, status);
210-
kfree(xattr_value);
211240
return status;
212241
}
213242

@@ -223,7 +252,7 @@ void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
223252
if (iint->flags & IMA_DIGSIG)
224253
return;
225254

226-
rc = ima_collect_measurement(iint, file);
255+
rc = ima_collect_measurement(iint, file, NULL, NULL);
227256
if (rc < 0)
228257
return;
229258

‎security/integrity/ima/ima_main.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ static int process_measurement(struct file *file, const char *filename,
149149
char *pathbuf = NULL;
150150
const char *pathname = NULL;
151151
int rc = -ENOMEM, action, must_appraise, _func;
152+
struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
153+
int xattr_len = 0;
152154

153155
if (!ima_initialized || !S_ISREG(inode->i_mode))
154156
return 0;
@@ -187,7 +189,10 @@ static int process_measurement(struct file *file, const char *filename,
187189
goto out_digsig;
188190
}
189191

190-
rc = ima_collect_measurement(iint, file);
192+
if (action & IMA_APPRAISE_SUBMASK)
193+
xattr_ptr = &xattr_value;
194+
195+
rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
191196
if (rc != 0)
192197
goto out_digsig;
193198

@@ -198,7 +203,8 @@ static int process_measurement(struct file *file, const char *filename,
198203
if (action & IMA_MEASURE)
199204
ima_store_measurement(iint, file, pathname);
200205
if (action & IMA_APPRAISE_SUBMASK)
201-
rc = ima_appraise_measurement(_func, iint, file, pathname);
206+
rc = ima_appraise_measurement(_func, iint, file, pathname,
207+
xattr_value, xattr_len);
202208
if (action & IMA_AUDIT)
203209
ima_audit_measurement(iint, pathname);
204210
kfree(pathbuf);
@@ -207,6 +213,7 @@ static int process_measurement(struct file *file, const char *filename,
207213
rc = -EACCES;
208214
out:
209215
mutex_unlock(&inode->i_mutex);
216+
kfree(xattr_value);
210217
if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
211218
return -EACCES;
212219
return 0;

‎security/integrity/integrity.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ struct ima_digest_data {
7070
u8 digest[IMA_MAX_DIGEST_SIZE];
7171
} __packed;
7272

73+
/*
74+
* signature format v2 - for using with asymmetric keys
75+
*/
76+
struct signature_v2_hdr {
77+
uint8_t version; /* signature format version */
78+
uint8_t hash_algo; /* Digest algorithm [enum pkey_hash_algo] */
79+
uint32_t keyid; /* IMA key identifier - not X509/PGP specific */
80+
uint16_t sig_size; /* signature size */
81+
uint8_t sig[0]; /* signature payload */
82+
} __packed;
83+
7384
/* integrity data associated with an inode */
7485
struct integrity_iint_cache {
7586
struct rb_node rb_node; /* rooted in integrity_iint_tree */

0 commit comments

Comments
 (0)