Skip to content

Commit f6fd5c8

Browse files
author
James Morris
committed
Merge tag 'fw-restrict-3.17' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux into next
2 parents ed3c4f8 + 5a9196d commit f6fd5c8

11 files changed

Lines changed: 104 additions & 8 deletions

File tree

‎Documentation/ABI/testing/ima_policy‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Description:
2626
option: [[appraise_type=]] [permit_directio]
2727

2828
base: func:= [BPRM_CHECK][MMAP_CHECK][FILE_CHECK][MODULE_CHECK]
29+
[FIRMWARE_CHECK]
2930
mask:= [MAY_READ] [MAY_WRITE] [MAY_APPEND] [MAY_EXEC]
3031
fsmagic:= hex value
3132
fsuuid:= file system UUID (e.g 8bcbe394-4f13-4144-be8e-5aa9ea2ce2f6)
@@ -57,7 +58,8 @@ Description:
5758
measure func=BPRM_CHECK
5859
measure func=FILE_MMAP mask=MAY_EXEC
5960
measure func=FILE_CHECK mask=MAY_READ uid=0
60-
measure func=MODULE_CHECK uid=0
61+
measure func=MODULE_CHECK
62+
measure func=FIRMWARE_CHECK
6163
appraise fowner=0
6264

6365
The default policy measures all executables in bprm_check,

‎drivers/base/firmware_class.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/suspend.h>
2929
#include <linux/syscore_ops.h>
3030
#include <linux/reboot.h>
31+
#include <linux/security.h>
3132

3233
#include <generated/utsrelease.h>
3334

@@ -308,12 +309,17 @@ static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
308309
if (rc != size) {
309310
if (rc > 0)
310311
rc = -EIO;
311-
vfree(buf);
312-
return rc;
312+
goto fail;
313313
}
314+
rc = security_kernel_fw_from_file(file, buf, size);
315+
if (rc)
316+
goto fail;
314317
fw_buf->data = buf;
315318
fw_buf->size = size;
316319
return 0;
320+
fail:
321+
vfree(buf);
322+
return rc;
317323
}
318324

319325
static int fw_get_filesystem_firmware(struct device *device,
@@ -617,6 +623,7 @@ static ssize_t firmware_loading_store(struct device *dev,
617623
{
618624
struct firmware_priv *fw_priv = to_firmware_priv(dev);
619625
struct firmware_buf *fw_buf;
626+
ssize_t written = count;
620627
int loading = simple_strtol(buf, NULL, 10);
621628
int i;
622629

@@ -640,6 +647,8 @@ static ssize_t firmware_loading_store(struct device *dev,
640647
break;
641648
case 0:
642649
if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
650+
int rc;
651+
643652
set_bit(FW_STATUS_DONE, &fw_buf->status);
644653
clear_bit(FW_STATUS_LOADING, &fw_buf->status);
645654

@@ -649,10 +658,23 @@ static ssize_t firmware_loading_store(struct device *dev,
649658
* see the mapped 'buf->data' once the loading
650659
* is completed.
651660
* */
652-
if (fw_map_pages_buf(fw_buf))
661+
rc = fw_map_pages_buf(fw_buf);
662+
if (rc)
653663
dev_err(dev, "%s: map pages failed\n",
654664
__func__);
665+
else
666+
rc = security_kernel_fw_from_file(NULL,
667+
fw_buf->data, fw_buf->size);
668+
669+
/*
670+
* Same logic as fw_load_abort, only the DONE bit
671+
* is ignored and we set ABORT only on failure.
672+
*/
655673
list_del_init(&fw_buf->pending_list);
674+
if (rc) {
675+
set_bit(FW_STATUS_ABORT, &fw_buf->status);
676+
written = rc;
677+
}
656678
complete_all(&fw_buf->completion);
657679
break;
658680
}
@@ -666,7 +688,7 @@ static ssize_t firmware_loading_store(struct device *dev,
666688
}
667689
out:
668690
mutex_unlock(&fw_lock);
669-
return count;
691+
return written;
670692
}
671693

672694
static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);

‎include/linux/ima.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern int ima_file_check(struct file *file, int mask);
1919
extern void ima_file_free(struct file *file);
2020
extern int ima_file_mmap(struct file *file, unsigned long prot);
2121
extern int ima_module_check(struct file *file);
22+
extern int ima_fw_from_file(struct file *file, char *buf, size_t size);
2223

2324
#else
2425
static inline int ima_bprm_check(struct linux_binprm *bprm)
@@ -46,6 +47,11 @@ static inline int ima_module_check(struct file *file)
4647
return 0;
4748
}
4849

50+
static inline int ima_fw_from_file(struct file *file, char *buf, size_t size)
51+
{
52+
return 0;
53+
}
54+
4955
#endif /* CONFIG_IMA */
5056

5157
#ifdef CONFIG_IMA_APPRAISE

‎include/linux/security.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,15 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
702702
* @inode points to the inode to use as a reference.
703703
* The current task must be the one that nominated @inode.
704704
* Return 0 if successful.
705+
* @kernel_fw_from_file:
706+
* Load firmware from userspace (not called for built-in firmware).
707+
* @file contains the file structure pointing to the file containing
708+
* the firmware to load. This argument will be NULL if the firmware
709+
* was loaded via the uevent-triggered blob-based interface exposed
710+
* by CONFIG_FW_LOADER_USER_HELPER.
711+
* @buf pointer to buffer containing firmware contents.
712+
* @size length of the firmware contents.
713+
* Return 0 if permission is granted.
705714
* @kernel_module_request:
706715
* Ability to trigger the kernel to automatically upcall to userspace for
707716
* userspace to load a kernel module with the given name.
@@ -1568,6 +1577,7 @@ struct security_operations {
15681577
void (*cred_transfer)(struct cred *new, const struct cred *old);
15691578
int (*kernel_act_as)(struct cred *new, u32 secid);
15701579
int (*kernel_create_files_as)(struct cred *new, struct inode *inode);
1580+
int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
15711581
int (*kernel_module_request)(char *kmod_name);
15721582
int (*kernel_module_from_file)(struct file *file);
15731583
int (*task_fix_setuid) (struct cred *new, const struct cred *old,
@@ -1840,6 +1850,7 @@ int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp);
18401850
void security_transfer_creds(struct cred *new, const struct cred *old);
18411851
int security_kernel_act_as(struct cred *new, u32 secid);
18421852
int security_kernel_create_files_as(struct cred *new, struct inode *inode);
1853+
int security_kernel_fw_from_file(struct file *file, char *buf, size_t size);
18431854
int security_kernel_module_request(char *kmod_name);
18441855
int security_kernel_module_from_file(struct file *file);
18451856
int security_task_fix_setuid(struct cred *new, const struct cred *old,
@@ -2366,6 +2377,12 @@ static inline int security_kernel_create_files_as(struct cred *cred,
23662377
return 0;
23672378
}
23682379

2380+
static inline int security_kernel_fw_from_file(struct file *file,
2381+
char *buf, size_t size)
2382+
{
2383+
return 0;
2384+
}
2385+
23692386
static inline int security_kernel_module_request(char *kmod_name)
23702387
{
23712388
return 0;

‎security/capability.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ static int cap_kernel_create_files_as(struct cred *new, struct inode *inode)
401401
return 0;
402402
}
403403

404+
static int cap_kernel_fw_from_file(struct file *file, char *buf, size_t size)
405+
{
406+
return 0;
407+
}
408+
404409
static int cap_kernel_module_request(char *kmod_name)
405410
{
406411
return 0;
@@ -1015,6 +1020,7 @@ void __init security_fixup_ops(struct security_operations *ops)
10151020
set_to_cap_if_null(ops, cred_transfer);
10161021
set_to_cap_if_null(ops, kernel_act_as);
10171022
set_to_cap_if_null(ops, kernel_create_files_as);
1023+
set_to_cap_if_null(ops, kernel_fw_from_file);
10181024
set_to_cap_if_null(ops, kernel_module_request);
10191025
set_to_cap_if_null(ops, kernel_module_from_file);
10201026
set_to_cap_if_null(ops, task_fix_setuid);

‎security/integrity/ima/ima.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ struct integrity_iint_cache *integrity_iint_insert(struct inode *inode);
158158
struct integrity_iint_cache *integrity_iint_find(struct inode *inode);
159159

160160
/* IMA policy related functions */
161-
enum ima_hooks { FILE_CHECK = 1, MMAP_CHECK, BPRM_CHECK, MODULE_CHECK, POST_SETATTR };
161+
enum ima_hooks { FILE_CHECK = 1, MMAP_CHECK, BPRM_CHECK, MODULE_CHECK, FIRMWARE_CHECK, POST_SETATTR };
162162

163163
int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
164164
int flags);
@@ -171,6 +171,7 @@ void ima_delete_rules(void);
171171
#define IMA_APPRAISE_ENFORCE 0x01
172172
#define IMA_APPRAISE_FIX 0x02
173173
#define IMA_APPRAISE_MODULES 0x04
174+
#define IMA_APPRAISE_FIRMWARE 0x08
174175

175176
#ifdef CONFIG_IMA_APPRAISE
176177
int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,

‎security/integrity/ima/ima_appraise.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
7575
return iint->ima_bprm_status;
7676
case MODULE_CHECK:
7777
return iint->ima_module_status;
78+
case FIRMWARE_CHECK:
79+
return iint->ima_firmware_status;
7880
case FILE_CHECK:
7981
default:
8082
return iint->ima_file_status;
@@ -94,6 +96,9 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
9496
case MODULE_CHECK:
9597
iint->ima_module_status = status;
9698
break;
99+
case FIRMWARE_CHECK:
100+
iint->ima_firmware_status = status;
101+
break;
97102
case FILE_CHECK:
98103
default:
99104
iint->ima_file_status = status;
@@ -113,6 +118,9 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
113118
case MODULE_CHECK:
114119
iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
115120
break;
121+
case FIRMWARE_CHECK:
122+
iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
123+
break;
116124
case FILE_CHECK:
117125
default:
118126
iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);

‎security/integrity/ima/ima_main.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ int ima_module_check(struct file *file)
319319
return process_measurement(file, NULL, MAY_EXEC, MODULE_CHECK);
320320
}
321321

322+
int ima_fw_from_file(struct file *file, char *buf, size_t size)
323+
{
324+
if (!file) {
325+
if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
326+
(ima_appraise & IMA_APPRAISE_ENFORCE))
327+
return -EACCES; /* INTEGRITY_UNKNOWN */
328+
return 0;
329+
}
330+
return process_measurement(file, NULL, MAY_EXEC, FIRMWARE_CHECK);
331+
}
332+
322333
static int __init init_ima(void)
323334
{
324335
int error;

‎security/integrity/ima/ima_policy.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static struct ima_rule_entry default_rules[] = {
8484
{.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID,
8585
.flags = IMA_FUNC | IMA_MASK | IMA_UID},
8686
{.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
87+
{.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
8788
};
8889

8990
static struct ima_rule_entry default_appraise_rules[] = {
@@ -241,6 +242,8 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
241242
return IMA_BPRM_APPRAISE;
242243
case MODULE_CHECK:
243244
return IMA_MODULE_APPRAISE;
245+
case FIRMWARE_CHECK:
246+
return IMA_FIRMWARE_APPRAISE;
244247
case FILE_CHECK:
245248
default:
246249
return IMA_FILE_APPRAISE;
@@ -486,6 +489,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
486489
entry->func = FILE_CHECK;
487490
else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
488491
entry->func = MODULE_CHECK;
492+
else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
493+
entry->func = FIRMWARE_CHECK;
489494
else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
490495
|| (strcmp(args[0].from, "MMAP_CHECK") == 0))
491496
entry->func = MMAP_CHECK;
@@ -636,6 +641,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
636641
result = -EINVAL;
637642
else if (entry->func == MODULE_CHECK)
638643
ima_appraise |= IMA_APPRAISE_MODULES;
644+
else if (entry->func == FIRMWARE_CHECK)
645+
ima_appraise |= IMA_APPRAISE_FIRMWARE;
639646
audit_log_format(ab, "res=%d", !result);
640647
audit_log_end(ab);
641648
return result;

‎security/integrity/integrity.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@
4646
#define IMA_BPRM_APPRAISED 0x00002000
4747
#define IMA_MODULE_APPRAISE 0x00004000
4848
#define IMA_MODULE_APPRAISED 0x00008000
49+
#define IMA_FIRMWARE_APPRAISE 0x00010000
50+
#define IMA_FIRMWARE_APPRAISED 0x00020000
4951
#define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
50-
IMA_BPRM_APPRAISE | IMA_MODULE_APPRAISE)
52+
IMA_BPRM_APPRAISE | IMA_MODULE_APPRAISE | \
53+
IMA_FIRMWARE_APPRAISE)
5154
#define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
52-
IMA_BPRM_APPRAISED | IMA_MODULE_APPRAISED)
55+
IMA_BPRM_APPRAISED | IMA_MODULE_APPRAISED | \
56+
IMA_FIRMWARE_APPRAISED)
5357

5458
enum evm_ima_xattr_type {
5559
IMA_XATTR_DIGEST = 0x01,
@@ -104,6 +108,7 @@ struct integrity_iint_cache {
104108
enum integrity_status ima_mmap_status:4;
105109
enum integrity_status ima_bprm_status:4;
106110
enum integrity_status ima_module_status:4;
111+
enum integrity_status ima_firmware_status:4;
107112
enum integrity_status evm_status:4;
108113
struct ima_digest_data *ima_hash;
109114
};

0 commit comments

Comments
 (0)