Skip to content

Commit a38e022

Browse files
GUO Zihuagregkh
authored andcommitted
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 0edae06 commit a38e022

File tree

11 files changed

+34
-22
lines changed

11 files changed

+34
-22
lines changed

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer)
379379

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

include/linux/security.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,15 +1896,16 @@ static inline int security_key_getsecurity(struct key *key, char **_buffer)
18961896

18971897
#ifdef CONFIG_AUDIT
18981898
#ifdef CONFIG_SECURITY
1899-
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule);
1899+
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
1900+
gfp_t gfp);
19001901
int security_audit_rule_known(struct audit_krule *krule);
19011902
int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule);
19021903
void security_audit_rule_free(void *lsmrule);
19031904

19041905
#else
19051906

19061907
static inline int security_audit_rule_init(u32 field, u32 op, char *rulestr,
1907-
void **lsmrule)
1908+
void **lsmrule, gfp_t gfp)
19081909
{
19091910
return 0;
19101911
}

kernel/auditfilter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
521521
entry->rule.buflen += f_val;
522522
f->lsm_str = str;
523523
err = security_audit_rule_init(f->type, f->op, str,
524-
(void **)&f->lsm_rule);
524+
(void **)&f->lsm_rule,
525+
GFP_KERNEL);
525526
/* Keep currently invalid fields around in case they
526527
* become valid after a policy reload. */
527528
if (err == -EINVAL) {
@@ -790,7 +791,7 @@ static inline int audit_dupe_lsm_field(struct audit_field *df,
790791

791792
/* our own (refreshed) copy of lsm_rule */
792793
ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
793-
(void **)&df->lsm_rule);
794+
(void **)&df->lsm_rule, GFP_KERNEL);
794795
/* Keep currently invalid fields around in case they
795796
* become valid after a policy reload. */
796797
if (ret == -EINVAL) {

security/apparmor/audit.c

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ static void ima_free_rule(struct ima_rule_entry *entry)
370370
kfree(entry);
371371
}
372372

373-
static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
373+
static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry,
374+
gfp_t gfp)
374375
{
375376
struct ima_rule_entry *nentry;
376377
int i;
@@ -379,7 +380,7 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
379380
* Immutable elements are copied over as pointers and data; only
380381
* lsm rules can change
381382
*/
382-
nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
383+
nentry = kmemdup(entry, sizeof(*nentry), gfp);
383384
if (!nentry)
384385
return NULL;
385386

@@ -394,7 +395,8 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
394395

395396
ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
396397
nentry->lsm[i].args_p,
397-
&nentry->lsm[i].rule);
398+
&nentry->lsm[i].rule,
399+
gfp);
398400
if (!nentry->lsm[i].rule)
399401
pr_warn("rule for LSM \'%s\' is undefined\n",
400402
nentry->lsm[i].args_p);
@@ -407,7 +409,7 @@ static int ima_lsm_update_rule(struct ima_rule_entry *entry)
407409
int i;
408410
struct ima_rule_entry *nentry;
409411

410-
nentry = ima_lsm_copy_rule(entry);
412+
nentry = ima_lsm_copy_rule(entry, GFP_KERNEL);
411413
if (!nentry)
412414
return -ENOMEM;
413415

@@ -618,7 +620,7 @@ static bool ima_match_rules(struct ima_rule_entry *rule,
618620
}
619621

620622
if (rc == -ESTALE && !rule_reinitialized) {
621-
lsm_rule = ima_lsm_copy_rule(rule);
623+
lsm_rule = ima_lsm_copy_rule(rule, GFP_ATOMIC);
622624
if (lsm_rule) {
623625
rule_reinitialized = true;
624626
goto retry;
@@ -1080,7 +1082,8 @@ static int ima_lsm_rule_init(struct ima_rule_entry *entry,
10801082
entry->lsm[lsm_rule].type = audit_type;
10811083
result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
10821084
entry->lsm[lsm_rule].args_p,
1083-
&entry->lsm[lsm_rule].rule);
1085+
&entry->lsm[lsm_rule].rule,
1086+
GFP_KERNEL);
10841087
if (!entry->lsm[lsm_rule].rule) {
10851088
pr_warn("rule for LSM \'%s\' is undefined\n",
10861089
entry->lsm[lsm_rule].args_p);

security/security.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,9 +2587,11 @@ int security_key_getsecurity(struct key *key, char **_buffer)
25872587

25882588
#ifdef CONFIG_AUDIT
25892589

2590-
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2590+
int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
2591+
gfp_t gfp)
25912592
{
2592-
return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
2593+
return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule,
2594+
gfp);
25932595
}
25942596

25952597
int security_audit_rule_known(struct audit_krule *krule)

security/selinux/include/audit.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
* @op: the operater the rule uses
1919
* @rulestr: the text "target" of the rule
2020
* @rule: pointer to the new rule structure returned via this
21+
* @gfp: GFP flag used for kmalloc
2122
*
2223
* Returns 0 if successful, -errno if not. On success, the rule structure
2324
* will be allocated internally. The caller must free this structure with
2425
* selinux_audit_rule_free() after use.
2526
*/
26-
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **rule);
27+
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **rule,
28+
gfp_t gfp);
2729

2830
/**
2931
* selinux_audit_rule_free - free an selinux audit rule structure.

security/selinux/ss/services.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,7 +3560,8 @@ void selinux_audit_rule_free(void *vrule)
35603560
}
35613561
}
35623562

3563-
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3563+
int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule,
3564+
gfp_t gfp)
35643565
{
35653566
struct selinux_state *state = &selinux_state;
35663567
struct selinux_policy *policy;
@@ -3601,7 +3602,7 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
36013602
return -EINVAL;
36023603
}
36033604

3604-
tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3605+
tmprule = kzalloc(sizeof(struct selinux_audit_rule), gfp);
36053606
if (!tmprule)
36063607
return -ENOMEM;
36073608

0 commit comments

Comments
 (0)