Skip to content

Commit 3fb7759

Browse files
GUO ZihuaSasha Levin
GUO Zihua
authored and
Sasha Levin
committed
ima: Avoid blocking in RCU read-side critical section
commit 9a95c5b upstream. A panic happens in ima_match_policy: BUG: unable to handle kernel NULL pointer dereference at 0000000000000010 PGD 42f873067 P4D 0 Oops: 0000 [#1] SMP NOPTI CPU: 5 PID: 1286325 Comm: kubeletmonit.sh Kdump: loaded Tainted: P Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015 RIP: 0010:ima_match_policy+0x84/0x450 Code: 49 89 fc 41 89 cf 31 ed 89 44 24 14 eb 1c 44 39 7b 18 74 26 41 83 ff 05 74 20 48 8b 1b 48 3b 1d f2 b9 f4 00 0f 84 9c 01 00 00 <44> 85 73 10 74 ea 44 8b 6b 14 41 f6 c5 01 75 d4 41 f6 c5 02 74 0f RSP: 0018:ff71570009e07a80 EFLAGS: 00010207 RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000200 RDX: ffffffffad8dc7c0 RSI: 0000000024924925 RDI: ff3e27850dea2000 RBP: 0000000000000000 R08: 0000000000000000 R09: ffffffffabfce739 R10: ff3e27810cc42400 R11: 0000000000000000 R12: ff3e2781825ef970 R13: 00000000ff3e2785 R14: 000000000000000c R15: 0000000000000001 FS: 00007f5195b51740(0000) GS:ff3e278b12d40000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000010 CR3: 0000000626d24002 CR4: 0000000000361ee0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: ima_get_action+0x22/0x30 process_measurement+0xb0/0x830 ? page_add_file_rmap+0x15/0x170 ? alloc_set_pte+0x269/0x4c0 ? prep_new_page+0x81/0x140 ? simple_xattr_get+0x75/0xa0 ? selinux_file_open+0x9d/0xf0 ima_file_check+0x64/0x90 path_openat+0x571/0x1720 do_filp_open+0x9b/0x110 ? page_counter_try_charge+0x57/0xc0 ? files_cgroup_alloc_fd+0x38/0x60 ? __alloc_fd+0xd4/0x250 ? do_sys_open+0x1bd/0x250 do_sys_open+0x1bd/0x250 do_syscall_64+0x5d/0x1d0 entry_SYSCALL_64_after_hwframe+0x65/0xca Commit c7423db ("ima: Handle -ESTALE returned by ima_filter_rule_match()") introduced call to ima_lsm_copy_rule within a RCU read-side critical section which contains kmalloc with GFP_KERNEL. This implies a possible sleep and violates limitations of RCU read-side critical sections on non-PREEMPT systems. Sleeping within RCU read-side critical section might cause synchronize_rcu() returning early and break RCU protection, allowing a UAF to happen. The root cause of this issue could be described as follows: | Thread A | Thread B | | |ima_match_policy | | | rcu_read_lock | |ima_lsm_update_rule | | | synchronize_rcu | | | | kmalloc(GFP_KERNEL)| | | sleep | ==> synchronize_rcu returns early | kfree(entry) | | | | entry = entry->next| ==> UAF happens and entry now becomes NULL (or could be anything). | | entry->action | ==> Accessing entry might cause panic. To fix this issue, we are converting all kmalloc that is called within RCU read-side critical section to use GFP_ATOMIC. Fixes: c7423db ("ima: Handle -ESTALE returned by ima_filter_rule_match()") Cc: stable@vger.kernel.org Signed-off-by: GUO Zihua <guozihua@huawei.com> Acked-by: John Johansen <john.johansen@canonical.com> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com> [PM: fixed missing comment, long lines, !CONFIG_IMA_LSM_RULES case] Signed-off-by: Paul Moore <paul@paul-moore.com> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 599697c commit 3fb7759

File tree

11 files changed

+34
-22
lines changed

11 files changed

+34
-22
lines changed

include/linux/lsm_hook_defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer)
382382

383383
#ifdef CONFIG_AUDIT
384384
LSM_HOOK(int, 0, audit_rule_init, u32 field, u32 op, char *rulestr,
385-
void **lsmrule)
385+
void **lsmrule, gfp_t gfp)
386386
LSM_HOOK(int, 0, audit_rule_known, struct audit_krule *krule)
387387
LSM_HOOK(int, 0, audit_rule_match, u32 secid, u32 field, u32 op, void *lsmrule)
388388
LSM_HOOK(void, LSM_RET_VOID, audit_rule_free, void *lsmrule)

include/linux/security.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1921,15 +1921,16 @@ static inline int security_key_getsecurity(struct key *key, char **_buffer)
19211921

19221922
#ifdef CONFIG_AUDIT
19231923
#ifdef CONFIG_SECURITY
1924-
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule);
1924+
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
1925+
gfp_t gfp);
19251926
int security_audit_rule_known(struct audit_krule *krule);
19261927
int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule);
19271928
void security_audit_rule_free(void *lsmrule);
19281929

19291930
#else
19301931

19311932
static inline int security_audit_rule_init(u32 field, u32 op, char *rulestr,
1932-
void **lsmrule)
1933+
void **lsmrule, gfp_t gfp)
19331934
{
19341935
return 0;
19351936
}

kernel/auditfilter.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
529529
entry->rule.buflen += f_val;
530530
f->lsm_str = str;
531531
err = security_audit_rule_init(f->type, f->op, str,
532-
(void **)&f->lsm_rule);
532+
(void **)&f->lsm_rule,
533+
GFP_KERNEL);
533534
/* Keep currently invalid fields around in case they
534535
* become valid after a policy reload. */
535536
if (err == -EINVAL) {
@@ -798,7 +799,7 @@ static inline int audit_dupe_lsm_field(struct audit_field *df,
798799

799800
/* our own (refreshed) copy of lsm_rule */
800801
ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
801-
(void **)&df->lsm_rule);
802+
(void **)&df->lsm_rule, GFP_KERNEL);
802803
/* Keep currently invalid fields around in case they
803804
* become valid after a policy reload. */
804805
if (ret == -EINVAL) {

security/apparmor/audit.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void aa_audit_rule_free(void *vrule)
173173
}
174174
}
175175

176-
int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
176+
int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule, gfp_t gfp)
177177
{
178178
struct aa_audit_rule *rule;
179179

@@ -186,14 +186,14 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
186186
return -EINVAL;
187187
}
188188

189-
rule = kzalloc(sizeof(struct aa_audit_rule), GFP_KERNEL);
189+
rule = kzalloc(sizeof(struct aa_audit_rule), gfp);
190190

191191
if (!rule)
192192
return -ENOMEM;
193193

194194
/* Currently rules are treated as coming from the root ns */
195195
rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
196-
GFP_KERNEL, true, false);
196+
gfp, true, false);
197197
if (IS_ERR(rule->label)) {
198198
int err = PTR_ERR(rule->label);
199199
aa_audit_rule_free(rule);

security/apparmor/include/audit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static inline int complain_error(int error)
186186
}
187187

188188
void aa_audit_rule_free(void *vrule);
189-
int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule);
189+
int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule, gfp_t gfp);
190190
int aa_audit_rule_known(struct audit_krule *rule);
191191
int aa_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule);
192192

security/integrity/ima/ima.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ static inline void ima_free_modsig(struct modsig *modsig)
428428
#else
429429

430430
static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr,
431-
void **lsmrule)
431+
void **lsmrule, gfp_t gfp)
432432
{
433433
return -EINVAL;
434434
}

security/integrity/ima/ima_policy.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ static void ima_free_rule(struct ima_rule_entry *entry)
377377
kfree(entry);
378378
}
379379

380-
static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
380+
static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry,
381+
gfp_t gfp)
381382
{
382383
struct ima_rule_entry *nentry;
383384
int i;
@@ -386,7 +387,7 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
386387
* Immutable elements are copied over as pointers and data; only
387388
* lsm rules can change
388389
*/
389-
nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
390+
nentry = kmemdup(entry, sizeof(*nentry), gfp);
390391
if (!nentry)
391392
return NULL;
392393

@@ -401,7 +402,8 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
401402

402403
ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
403404
nentry->lsm[i].args_p,
404-
&nentry->lsm[i].rule);
405+
&nentry->lsm[i].rule,
406+
gfp);
405407
if (!nentry->lsm[i].rule)
406408
pr_warn("rule for LSM \'%s\' is undefined\n",
407409
nentry->lsm[i].args_p);
@@ -414,7 +416,7 @@ static int ima_lsm_update_rule(struct ima_rule_entry *entry)
414416
int i;
415417
struct ima_rule_entry *nentry;
416418

417-
nentry = ima_lsm_copy_rule(entry);
419+
nentry = ima_lsm_copy_rule(entry, GFP_KERNEL);
418420
if (!nentry)
419421
return -ENOMEM;
420422

@@ -638,7 +640,7 @@ static bool ima_match_rules(struct ima_rule_entry *rule,
638640
}
639641

640642
if (rc == -ESTALE && !rule_reinitialized) {
641-
lsm_rule = ima_lsm_copy_rule(rule);
643+
lsm_rule = ima_lsm_copy_rule(rule, GFP_ATOMIC);
642644
if (lsm_rule) {
643645
rule_reinitialized = true;
644646
goto retry;
@@ -1113,7 +1115,8 @@ static int ima_lsm_rule_init(struct ima_rule_entry *entry,
11131115
entry->lsm[lsm_rule].type = audit_type;
11141116
result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
11151117
entry->lsm[lsm_rule].args_p,
1116-
&entry->lsm[lsm_rule].rule);
1118+
&entry->lsm[lsm_rule].rule,
1119+
GFP_KERNEL);
11171120
if (!entry->lsm[lsm_rule].rule) {
11181121
pr_warn("rule for LSM \'%s\' is undefined\n",
11191122
entry->lsm[lsm_rule].args_p);

security/security.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -2617,9 +2617,11 @@ int security_key_getsecurity(struct key *key, char **_buffer)
26172617

26182618
#ifdef CONFIG_AUDIT
26192619

2620-
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2620+
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
2621+
gfp_t gfp)
26212622
{
2622-
return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
2623+
return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule,
2624+
gfp);
26232625
}
26242626

26252627
int security_audit_rule_known(struct audit_krule *krule)

security/selinux/include/audit.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
* @op: the operator the rule uses
2222
* @rulestr: the text "target" of the rule
2323
* @rule: pointer to the new rule structure returned via this
24+
* @gfp: GFP flag used for kmalloc
2425
*
2526
* Returns 0 if successful, -errno if not. On success, the rule structure
2627
* will be allocated internally. The caller must free this structure with
2728
* selinux_audit_rule_free() after use.
2829
*/
29-
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **rule);
30+
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **rule,
31+
gfp_t gfp);
3032

3133
/**
3234
* selinux_audit_rule_free - free an selinux audit rule structure.

security/selinux/ss/services.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -3563,7 +3563,8 @@ void selinux_audit_rule_free(void *vrule)
35633563
}
35643564
}
35653565

3566-
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3566+
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule,
3567+
gfp_t gfp)
35673568
{
35683569
struct selinux_state *state = &selinux_state;
35693570
struct selinux_policy *policy;
@@ -3604,7 +3605,7 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
36043605
return -EINVAL;
36053606
}
36063607

3607-
tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3608+
tmprule = kzalloc(sizeof(struct selinux_audit_rule), gfp);
36083609
if (!tmprule)
36093610
return -ENOMEM;
36103611

security/smack/smack_lsm.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4552,11 +4552,13 @@ static int smack_post_notification(const struct cred *w_cred,
45524552
* @op: required testing operator (=, !=, >, <, ...)
45534553
* @rulestr: smack label to be audited
45544554
* @vrule: pointer to save our own audit rule representation
4555+
* @gfp: type of the memory for the allocation
45554556
*
45564557
* Prepare to audit cases where (@field @op @rulestr) is true.
45574558
* The label to be audited is created if necessay.
45584559
*/
4559-
static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4560+
static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule,
4561+
gfp_t gfp)
45604562
{
45614563
struct smack_known *skp;
45624564
char **rule = (char **)vrule;

0 commit comments

Comments
 (0)