@@ -138,7 +138,7 @@ static struct shash_desc *init_desc(char type)
138
138
* protection.)
139
139
*/
140
140
static void hmac_add_misc (struct shash_desc * desc , struct inode * inode ,
141
- char * digest )
141
+ char type , char * digest )
142
142
{
143
143
struct h_misc {
144
144
unsigned long ino ;
@@ -149,8 +149,13 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
149
149
} hmac_misc ;
150
150
151
151
memset (& hmac_misc , 0 , sizeof (hmac_misc ));
152
- hmac_misc .ino = inode -> i_ino ;
153
- hmac_misc .generation = inode -> i_generation ;
152
+ /* Don't include the inode or generation number in portable
153
+ * signatures
154
+ */
155
+ if (type != EVM_XATTR_PORTABLE_DIGSIG ) {
156
+ hmac_misc .ino = inode -> i_ino ;
157
+ hmac_misc .generation = inode -> i_generation ;
158
+ }
154
159
/* The hmac uid and gid must be encoded in the initial user
155
160
* namespace (not the filesystems user namespace) as encoding
156
161
* them in the filesystems user namespace allows an attack
@@ -163,7 +168,8 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
163
168
hmac_misc .gid = from_kgid (& init_user_ns , inode -> i_gid );
164
169
hmac_misc .mode = inode -> i_mode ;
165
170
crypto_shash_update (desc , (const u8 * )& hmac_misc , sizeof (hmac_misc ));
166
- if (evm_hmac_attrs & EVM_ATTR_FSUUID )
171
+ if ((evm_hmac_attrs & EVM_ATTR_FSUUID ) &&
172
+ type != EVM_XATTR_PORTABLE_DIGSIG )
167
173
crypto_shash_update (desc , & inode -> i_sb -> s_uuid .b [0 ],
168
174
sizeof (inode -> i_sb -> s_uuid ));
169
175
crypto_shash_final (desc , digest );
@@ -189,6 +195,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
189
195
char * xattr_value = NULL ;
190
196
int error ;
191
197
int size ;
198
+ bool ima_present = false;
192
199
193
200
if (!(inode -> i_opflags & IOP_XATTR ))
194
201
return - EOPNOTSUPP ;
@@ -199,11 +206,18 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
199
206
200
207
error = - ENODATA ;
201
208
for (xattrname = evm_config_xattrnames ; * xattrname != NULL ; xattrname ++ ) {
209
+ bool is_ima = false;
210
+
211
+ if (strcmp (* xattrname , XATTR_NAME_IMA ) == 0 )
212
+ is_ima = true;
213
+
202
214
if ((req_xattr_name && req_xattr_value )
203
215
&& !strcmp (* xattrname , req_xattr_name )) {
204
216
error = 0 ;
205
217
crypto_shash_update (desc , (const u8 * )req_xattr_value ,
206
218
req_xattr_value_len );
219
+ if (is_ima )
220
+ ima_present = true;
207
221
continue ;
208
222
}
209
223
size = vfs_getxattr_alloc (dentry , * xattrname ,
@@ -218,9 +232,14 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
218
232
error = 0 ;
219
233
xattr_size = size ;
220
234
crypto_shash_update (desc , (const u8 * )xattr_value , xattr_size );
235
+ if (is_ima )
236
+ ima_present = true;
221
237
}
222
- hmac_add_misc (desc , inode , digest );
238
+ hmac_add_misc (desc , inode , type , digest );
223
239
240
+ /* Portable EVM signatures must include an IMA hash */
241
+ if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present )
242
+ return - EPERM ;
224
243
out :
225
244
kfree (xattr_value );
226
245
kfree (desc );
@@ -232,17 +251,45 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
232
251
char * digest )
233
252
{
234
253
return evm_calc_hmac_or_hash (dentry , req_xattr_name , req_xattr_value ,
235
- req_xattr_value_len , EVM_XATTR_HMAC , digest );
254
+ req_xattr_value_len , EVM_XATTR_HMAC , digest );
236
255
}
237
256
238
257
int evm_calc_hash (struct dentry * dentry , const char * req_xattr_name ,
239
258
const char * req_xattr_value , size_t req_xattr_value_len ,
240
- char * digest )
259
+ char type , char * digest )
241
260
{
242
261
return evm_calc_hmac_or_hash (dentry , req_xattr_name , req_xattr_value ,
243
- req_xattr_value_len , IMA_XATTR_DIGEST , digest );
262
+ req_xattr_value_len , type , digest );
263
+ }
264
+
265
+ static int evm_is_immutable (struct dentry * dentry , struct inode * inode )
266
+ {
267
+ const struct evm_ima_xattr_data * xattr_data = NULL ;
268
+ struct integrity_iint_cache * iint ;
269
+ int rc = 0 ;
270
+
271
+ iint = integrity_iint_find (inode );
272
+ if (iint && (iint -> flags & EVM_IMMUTABLE_DIGSIG ))
273
+ return 1 ;
274
+
275
+ /* Do this the hard way */
276
+ rc = vfs_getxattr_alloc (dentry , XATTR_NAME_EVM , (char * * )& xattr_data , 0 ,
277
+ GFP_NOFS );
278
+ if (rc <= 0 ) {
279
+ if (rc == - ENODATA )
280
+ return 0 ;
281
+ return rc ;
282
+ }
283
+ if (xattr_data -> type == EVM_XATTR_PORTABLE_DIGSIG )
284
+ rc = 1 ;
285
+ else
286
+ rc = 0 ;
287
+
288
+ kfree (xattr_data );
289
+ return rc ;
244
290
}
245
291
292
+
246
293
/*
247
294
* Calculate the hmac and update security.evm xattr
248
295
*
@@ -255,6 +302,16 @@ int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
255
302
struct evm_ima_xattr_data xattr_data ;
256
303
int rc = 0 ;
257
304
305
+ /*
306
+ * Don't permit any transformation of the EVM xattr if the signature
307
+ * is of an immutable type
308
+ */
309
+ rc = evm_is_immutable (dentry , inode );
310
+ if (rc < 0 )
311
+ return rc ;
312
+ if (rc )
313
+ return - EPERM ;
314
+
258
315
rc = evm_calc_hmac (dentry , xattr_name , xattr_value ,
259
316
xattr_value_len , xattr_data .digest );
260
317
if (rc == 0 ) {
@@ -280,7 +337,7 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
280
337
}
281
338
282
339
crypto_shash_update (desc , lsm_xattr -> value , lsm_xattr -> value_len );
283
- hmac_add_misc (desc , inode , hmac_val );
340
+ hmac_add_misc (desc , inode , EVM_XATTR_HMAC , hmac_val );
284
341
kfree (desc );
285
342
return 0 ;
286
343
}
0 commit comments