Skip to content

Commit 273df86

Browse files
naynajainmpe
authored andcommitted
ima: Check against blacklisted hashes for files with modsig
Asymmetric private keys are used to sign multiple files. The kernel currently supports checking against blacklisted keys. However, if the public key is blacklisted, any file signed by the blacklisted key will automatically fail signature verification. Blacklisting the public key is not fine enough granularity, as we might want to only blacklist a particular file. This patch adds support for checking against the blacklisted hash of the file, without the appended signature, based on the IMA policy. It defines a new policy option "appraise_flag=check_blacklist". In addition to the blacklisted binary hashes stored in the firmware "dbx" variable, the Linux kernel may be configured to load blacklisted binary hashes onto the .blacklist keyring as well. The following example shows how to blacklist a specific kernel module hash. $ sha256sum kernel/kheaders.ko 77fa889b35a05338ec52e51591c1b89d4c8d1c99a21251d7c22b1a8642a6bad3 kernel/kheaders.ko $ grep BLACKLIST .config CONFIG_SYSTEM_BLACKLIST_KEYRING=y CONFIG_SYSTEM_BLACKLIST_HASH_LIST="blacklist-hash-list" $ cat certs/blacklist-hash-list "bin:77fa889b35a05338ec52e51591c1b89d4c8d1c99a21251d7c22b1a8642a6bad3" Update the IMA custom measurement and appraisal policy rules (/etc/ima-policy): measure func=MODULE_CHECK template=ima-modsig appraise func=MODULE_CHECK appraise_flag=check_blacklist appraise_type=imasig|modsig After building, installing, and rebooting the kernel: 545660333 ---lswrv 0 0 \_ blacklist: bin:77fa889b35a05338ec52e51591c1b89d4c8d1c99a21251d7c22b1a8642a6bad3 measure func=MODULE_CHECK template=ima-modsig appraise func=MODULE_CHECK appraise_flag=check_blacklist appraise_type=imasig|modsig modprobe: ERROR: could not insert 'kheaders': Permission denied 10 0c9834db5a0182c1fb0cdc5d3adcf11a11fd83dd ima-sig sha256:3bc6ed4f0b4d6e31bc1dbc9ef844605abc7afdc6d81a57d77a1ec9407997c40 2 /usr/lib/modules/5.4.0-rc3+/kernel/kernel/kheaders.ko 10 82aad2bcc3fa8ed94762356b5c14838f3bcfa6a0 ima-modsig sha256:3bc6ed4f0b4d6e31bc1dbc9ef844605abc7afdc6d81a57d77a1ec9407997c40 2 /usr/lib/modules/5.4.0rc3+/kernel/kernel/kheaders.ko sha256:77fa889b3 5a05338ec52e51591c1b89d4c8d1c99a21251d7c22b1a8642a6bad3 3082029a06092a864886f70d010702a082028b30820287020101310d300b0609608648 016503040201300b06092a864886f70d01070131820264.... 10 25b72217cc1152b44b134ce2cd68f12dfb71acb3 ima-buf sha256:8b58427fedcf8f4b20bc8dc007f2e232bf7285d7b93a66476321f9c2a3aa132 b blacklisted-hash 77fa889b35a05338ec52e51591c1b89d4c8d1c99a21251d7c22b1a8642a6bad3 Signed-off-by: Nayna Jain <nayna@linux.ibm.com> [zohar@linux.ibm.com: updated patch description] Signed-off-by: Mimi Zohar <zohar@linux.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/1572492694-6520-8-git-send-email-zohar@linux.ibm.com
1 parent 2434f7d commit 273df86

6 files changed

Lines changed: 64 additions & 6 deletions

File tree

‎Documentation/ABI/testing/ima_policy‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Description:
2525
lsm: [[subj_user=] [subj_role=] [subj_type=]
2626
[obj_user=] [obj_role=] [obj_type=]]
2727
option: [[appraise_type=]] [template=] [permit_directio]
28+
[appraise_flag=]
2829
base: func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK]
2930
[FIRMWARE_CHECK]
3031
[KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK]
@@ -38,6 +39,9 @@ Description:
3839
fowner:= decimal value
3940
lsm: are LSM specific
4041
option: appraise_type:= [imasig] [imasig|modsig]
42+
appraise_flag:= [check_blacklist]
43+
Currently, blacklist check is only for files signed with appended
44+
signature.
4145
template:= name of a defined IMA template type
4246
(eg, ima-ng). Only valid when action is "measure".
4347
pcr:= decimal value

‎security/integrity/ima/ima.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ int ima_policy_show(struct seq_file *m, void *v);
256256
#define IMA_APPRAISE_KEXEC 0x40
257257

258258
#ifdef CONFIG_IMA_APPRAISE
259+
int ima_check_blacklist(struct integrity_iint_cache *iint,
260+
const struct modsig *modsig, int pcr);
259261
int ima_appraise_measurement(enum ima_hooks func,
260262
struct integrity_iint_cache *iint,
261263
struct file *file, const unsigned char *filename,
@@ -271,6 +273,12 @@ int ima_read_xattr(struct dentry *dentry,
271273
struct evm_ima_xattr_data **xattr_value);
272274

273275
#else
276+
static inline int ima_check_blacklist(struct integrity_iint_cache *iint,
277+
const struct modsig *modsig, int pcr)
278+
{
279+
return 0;
280+
}
281+
274282
static inline int ima_appraise_measurement(enum ima_hooks func,
275283
struct integrity_iint_cache *iint,
276284
struct file *file,

‎security/integrity/ima/ima_appraise.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/magic.h>
1313
#include <linux/ima.h>
1414
#include <linux/evm.h>
15+
#include <keys/system_keyring.h>
1516

1617
#include "ima.h"
1718

@@ -303,6 +304,38 @@ static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
303304
return rc;
304305
}
305306

307+
/*
308+
* ima_check_blacklist - determine if the binary is blacklisted.
309+
*
310+
* Add the hash of the blacklisted binary to the measurement list, based
311+
* on policy.
312+
*
313+
* Returns -EPERM if the hash is blacklisted.
314+
*/
315+
int ima_check_blacklist(struct integrity_iint_cache *iint,
316+
const struct modsig *modsig, int pcr)
317+
{
318+
enum hash_algo hash_algo;
319+
const u8 *digest = NULL;
320+
u32 digestsize = 0;
321+
int rc = 0;
322+
323+
if (!(iint->flags & IMA_CHECK_BLACKLIST))
324+
return 0;
325+
326+
if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
327+
ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
328+
329+
rc = is_binary_blacklisted(digest, digestsize);
330+
if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
331+
process_buffer_measurement(digest, digestsize,
332+
"blacklisted-hash", NONE,
333+
pcr);
334+
}
335+
336+
return rc;
337+
}
338+
306339
/*
307340
* ima_appraise_measurement - appraise file measurement
308341
*

‎security/integrity/ima/ima_main.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,14 @@ static int process_measurement(struct file *file, const struct cred *cred,
335335
xattr_value, xattr_len, modsig, pcr,
336336
template_desc);
337337
if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
338-
inode_lock(inode);
339-
rc = ima_appraise_measurement(func, iint, file, pathname,
340-
xattr_value, xattr_len, modsig);
341-
inode_unlock(inode);
338+
rc = ima_check_blacklist(iint, modsig, pcr);
339+
if (rc != -EPERM) {
340+
inode_lock(inode);
341+
rc = ima_appraise_measurement(func, iint, file,
342+
pathname, xattr_value,
343+
xattr_len, modsig);
344+
inode_unlock(inode);
345+
}
342346
if (!rc)
343347
rc = mmap_violation_check(func, file, &pathbuf,
344348
&pathname, filename);

‎security/integrity/ima/ima_policy.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,8 @@ enum {
765765
Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
766766
Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
767767
Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
768-
Opt_appraise_type, Opt_permit_directio,
769-
Opt_pcr, Opt_template, Opt_err
768+
Opt_appraise_type, Opt_appraise_flag,
769+
Opt_permit_directio, Opt_pcr, Opt_template, Opt_err
770770
};
771771

772772
static const match_table_t policy_tokens = {
@@ -798,6 +798,7 @@ static const match_table_t policy_tokens = {
798798
{Opt_euid_lt, "euid<%s"},
799799
{Opt_fowner_lt, "fowner<%s"},
800800
{Opt_appraise_type, "appraise_type=%s"},
801+
{Opt_appraise_flag, "appraise_flag=%s"},
801802
{Opt_permit_directio, "permit_directio"},
802803
{Opt_pcr, "pcr=%s"},
803804
{Opt_template, "template=%s"},
@@ -1172,6 +1173,11 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
11721173
else
11731174
result = -EINVAL;
11741175
break;
1176+
case Opt_appraise_flag:
1177+
ima_log_string(ab, "appraise_flag", args[0].from);
1178+
if (strstr(args[0].from, "blacklist"))
1179+
entry->flags |= IMA_CHECK_BLACKLIST;
1180+
break;
11751181
case Opt_permit_directio:
11761182
entry->flags |= IMA_PERMIT_DIRECTIO;
11771183
break;
@@ -1500,6 +1506,8 @@ int ima_policy_show(struct seq_file *m, void *v)
15001506
else
15011507
seq_puts(m, "appraise_type=imasig ");
15021508
}
1509+
if (entry->flags & IMA_CHECK_BLACKLIST)
1510+
seq_puts(m, "appraise_flag=check_blacklist ");
15031511
if (entry->flags & IMA_PERMIT_DIRECTIO)
15041512
seq_puts(m, "permit_directio ");
15051513
rcu_read_unlock();

‎security/integrity/integrity.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define EVM_IMMUTABLE_DIGSIG 0x08000000
3333
#define IMA_FAIL_UNVERIFIABLE_SIGS 0x10000000
3434
#define IMA_MODSIG_ALLOWED 0x20000000
35+
#define IMA_CHECK_BLACKLIST 0x40000000
3536

3637
#define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \
3738
IMA_HASH | IMA_APPRAISE_SUBMASK)

0 commit comments

Comments
 (0)