All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xingyuan Mo <hdthky0@gmail.com>
To: Paul Moore <paul@paul-moore.com>, Eric Paris <eparis@redhat.com>
Cc: "open list:AUDIT SUBSYSTEM" <audit@vger.kernel.org>,
	Xingyuan Mo <hdthky0@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] audit: clear borrowed LSM pointers before duplicating rule fields
Date: Fri, 4 Dec 2026 01:21:17 +0800	[thread overview]
Message-ID: <20261203172113.2281383-1-hdthky0@gmail.com> (raw)

audit_dupe_rule() memcpy()s the whole fields[] array of the source rule
into the new entry and then re-owns the pointer-bearing members one
index at a time. If any index fails, the error branch calls
audit_free_rule() on the partial copy, which runs audit_free_lsm_field()
on every field, including the ones after the failing index that were
never duplicated and still hold the lsm_str/lsm_rule pointers of the
original rule. The original rule is removed from the filter lists and
freed via call_rcu() regardless of the error, so those pointers end up
freed twice.

This is reachable without memory pressure: a rule combining an AUDIT_EXE
field with an LSM field placed after it fails in audit_dupe_exe() with
-ENOENT once the exe path no longer resolves, and the failing update is
triggered by writing 1 to /sys/fs/selinux/enforce (avc_ss_reset() ->
audit_update_lsm_rules()).

Clear lsm_str and lsm_rule of the LSM-typed fields in the copy right
after the memcpy(), so that the error path only frees what the copy
actually owns.

KASAN report:
==================================================================
BUG: KASAN: double-free in audit_free_rule_rcu+0xd8/0x140
Free of addr ffff88800bf73700 by task systemd-journal/110
CPU: 1 UID: 0 PID: 110 Comm: systemd-journal Not tainted 7.4.0-rc4-00571-g9437ee8b6cf6 #1 PREEMPT(lazy)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
 <IRQ>
 dump_stack_lvl+0x74/0xb0
 print_report+0xce/0x5f0
 kasan_report_invalid_free+0xa7/0xd0
 check_slab_allocation+0x100/0x150
 __kasan_slab_pre_free+0x10/0x20
 kfree+0x176/0x500
 audit_free_rule_rcu+0xd8/0x140
 rcu_core+0x431/0xe30
 rcu_core_si+0x12/0x20
 handle_softirqs+0x130/0x610
 __irq_exit_rcu+0x10f/0x180
 irq_exit_rcu+0x12/0x20
 sysvec_apic_timer_interrupt+0x7e/0x90
 </IRQ>
 <TASK>
 asm_sysvec_apic_timer_interrupt+0x1f/0x30
RIP: 0010:finish_task_switch.isra.0+0x132/0x500
Code: 00 4c 89 f7 e8 af db 3f 00 48 8d 7b 48 c7 83 40 0d 00 00 00 00 00 00 e8 2c 36 f3 01 e8 57 81 21 00 fb 65 48 8b 1d b6 6f 37 04 <4c> 8d 73 2c 4c 89 f7 e8 d2 da 3f 00 f6 43 2f 20 0f 85 f2 02 00 00
RSP: 0018:ffff88800d407a68 EFLAGS: 00000202
RAX: 0000000000047ed5 RBX: ffff888015ea51c0 RCX: dffffc0000000000
RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffff81379059
RBP: ffff88800d407ab0 R08: 0000000000000001 R09: 0000000000000001
R10: ffffffff84f1a99f R11: 0000000000000001 R12: ffff88800c4a3680
R13: 0000000000000000 R14: ffff88806d139b00 R15: ffff8880e7a30000
 __schedule+0xb69/0x2380
 schedule+0x7b/0x190
 schedule_hrtimeout_range_clock+0x1f2/0x210
 schedule_hrtimeout_range+0x17/0x20
 do_epoll_wait+0x8e3/0xb40
 __x64_sys_epoll_wait+0xd4/0x1a0
 x64_sys_call+0x1d6d/0x21d0
 do_syscall_64+0x11a/0x620
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f54af9b4f36
Code: 10 89 7c 24 0c 89 4c 24 1c e8 06 c9 f7 ff 44 8b 54 24 1c 8b 54 24 18 41 89 c0 48 8b 74 24 10 8b 7c 24 0c b8 e8 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 32 44 89 c7 89 44 24 0c e8 56 c9 f7 ff 8b 44
RSP: 002b:00007ffc424f7da0 EFLAGS: 00000293 ORIG_RAX: 00000000000000e8
RAX: ffffffffffffffda RBX: 0000565255734430 RCX: 00007f54af9b4f36
RDX: 0000000000000020 RSI: 000056525573b630 RDI: 0000000000000008
RBP: 00005652557342a0 R08: 0000000000000000 R09: 00007f54afdf5000
R10: 00000000ffffffff R11: 0000000000000293 R12: ffffffffffffffff
R13: 0000000000000020 R14: 0000000000000014 R15: 00000000000000c8
 </TASK>
Allocated by task 332:
 kasan_save_stack+0x2c/0x60
 kasan_save_track+0x18/0x50
 kasan_save_alloc_info+0x3a/0x50
 __kasan_kmalloc+0x88/0xa0
 __kmalloc_noprof+0x297/0x720
 audit_unpack_string+0x60/0xe0
 audit_data_to_entry+0x7ba/0xed0
 audit_rule_change+0xe3/0x740
 audit_receive_msg+0x26a/0x1bc0
 audit_receive+0x17f/0x2a0
 netlink_unicast+0x3d4/0x500
 netlink_sendmsg+0x451/0x7f0
 __sys_sendto+0x384/0x3a0
 __x64_sys_sendto+0x84/0xa0
 x64_sys_call+0xf18/0x21d0
 do_syscall_64+0x11a/0x620
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Freed by task 332:
 kasan_save_stack+0x2c/0x60
 kasan_save_track+0x18/0x50
 kasan_save_free_info+0x3e/0x60
 __kasan_slab_free+0x5c/0x80
 kfree+0x240/0x500
 audit_dupe_rule+0x4d7/0x580
 audit_update_lsm_rules+0x24c/0x530
 selinux_audit_rule_avc_callback+0x1e/0x30
 avc_ss_reset+0x193/0x290
 sel_write_enforce+0x28a/0x2c0
 vfs_write+0x1d1/0x7f0
 ksys_write+0xd9/0x180
 __x64_sys_write+0x47/0x60
 x64_sys_call+0x16f5/0x21d0
 do_syscall_64+0x11a/0x620
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
The buggy address belongs to the object at ffff88800bf73700
                which belongs to the cache kmalloc-16 of size 16
The buggy address is located 0 bytes inside of
                16-byte region [ffff88800bf73700, ffff88800bf73710)
The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0xffff88800bf735c0 pfn:0xbf73
flags: 0x100000000000200(workingset|node=0|zone=1)
page_type: f5(slab)
raw: 0100000000000200 ffff888007441640 ffffea00001d6dd0 ffffea00002a3990
raw: ffff88800bf735c0 0000000000800064 00000000f5000000 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
 ffff88800bf73600: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
 ffff88800bf73680: fa fb fc fc 00 04 fc fc 00 02 fc fc fa fb fc fc
>ffff88800bf73700: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
                   ^
 ffff88800bf73780: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
 ffff88800bf73800: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
==================================================================

Fixes: 3dc7e3153edd ("[PATCH] support for context based audit filtering, part 2")
Cc: stable@vger.kernel.org
Signed-off-by: Xingyuan Mo <hdthky0@gmail.com>
---
 kernel/auditfilter.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c
index cf91ba6f3617b..218cc64f779a3 100644
--- a/kernel/auditfilter.c
+++ b/kernel/auditfilter.c
@@ -883,6 +883,23 @@ struct audit_entry *audit_dupe_rule(struct audit_krule *old,
 	new->tree = old->tree;
 	memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
 
+	for (i = 0; i < fcount; i++) {
+		switch (new->fields[i].type) {
+		case AUDIT_SUBJ_USER:
+		case AUDIT_SUBJ_ROLE:
+		case AUDIT_SUBJ_TYPE:
+		case AUDIT_SUBJ_SEN:
+		case AUDIT_SUBJ_CLR:
+		case AUDIT_OBJ_USER:
+		case AUDIT_OBJ_ROLE:
+		case AUDIT_OBJ_TYPE:
+		case AUDIT_OBJ_LEV_LOW:
+		case AUDIT_OBJ_LEV_HIGH:
+			new->fields[i].lsm_str = NULL;
+			new->fields[i].lsm_rule = NULL;
+		}
+	}
+
 	/* deep copy this information, updating the lsm_rule fields, because
 	 * the originals will all be freed when the old rule is freed. */
 	for (i = 0; i < fcount; i++) {
-- 
2.43.0


             reply	other threads:[~2026-12-03 17:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 17:21 Xingyuan Mo [this message]
2026-12-03 18:26 ` [PATCH] audit: clear borrowed LSM pointers before duplicating rule fields sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203172113.2281383-1-hdthky0@gmail.com \
    --to=hdthky0@gmail.com \
    --cc=audit@vger.kernel.org \
    --cc=eparis@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.