From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.135 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796318516; b=FFn28Y0emlD6j7bueCRcXYlZ9APhyb9NF8XwqMv9ORHtfgXNxquqoZIw9L/0iJm5UE8N Uxuc1ax+JmcEuFGvK9ixjUaum9+3UsINyo0tG0XnnAgAVGWNxzEk3Iqw/zPz5ZjewQDiu 4xCqWqmDB3uOVS5S5oAvnp8FmVJ8S2e4kiP5jja4tBMGsdb5/Bj+h1mbfyLaKzVGe+AfE u1qVOQfrvvszRQ3JHpM0npYj7OxS0bkVLaUt5et86MG5fDYEX545a/+yzBVwm6EUGR/7E dGYVZjYWomWQ6p8LM7jRybw2ZCnA49eRXzP0EKuedlGb+wSLuQYncE+Wkg/G4V90JAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796318516; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Date:From:To:Cc: Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition: X-Mailer; bh=sRRo0v4fZ4OaZJFmMyoDJnHxpi57zOeYZ12rblsbFX0=; b=tUtJliD43ctW/x4eFEUlP+13hK0Ct/XeSG3Ba+xCciDLowF4hFEW/TbUD/eJDuzCqV7l hY0Bbi1wm6cylaYHSkLa+MqqJyurJCOqnjQWsLeHza407PSy4RuA9qAVvp/OAKoWi8yih kGZ9LeJrD1gJlbnAdoHTN8ndFdKxKjphTKT//Xf/j9CnXQ24+tnoz9hm0MA58FiiLq0RP v+hi4JbyRwTrYh6BJMph8t5q2XJRHuBNOb0FNlVg1LhX+ldfiDcPmi8F3kjWI4ha7sJp9 VMSojfpHmja5MqJsU1lzGUgu9AunmGhnvTABvO17xSvGvC13v87AIq2kHFZVvKlfy3Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=r63zAaIW; arc=none smtp.remote-ip=74.125.227.135 Received: from mail-pj2-f7.google.com (mail-pj2-f7.google.com [74.125.227.135]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 234A1606A322 for ; Thu, 3 Dec 2026 17:21:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 234A1606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=r63zAaIW Received: by mail-pj2-f7.google.com with SMTP id 98e67ed59e1d1-3b4be95e0aeso20961a91.1 for ; Thu, 03 Dec 2026 09:21:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796318515; x=1796923315; darn=vger.kernel.org; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=sRRo0v4fZ4OaZJFmMyoDJnHxpi57zOeYZ12rblsbFX0=; b=r63zAaIWxA3gK7SAhLftD/KHBrHI1pEhOOJTK7Rb8Eh2X/9q3dgxTveQusP35XUbtr /YSnh2UPR9PRyNmRU+gzbV7jhtQF72ozxu4bgc/4BRChaVGyoqnZuXGzVdMf4Vmw6r+Y 3KGNM1tv/f3dMKDnI3YYTmWJ+OKxBxVtumXAu0MfKpBBz8lwtxszcF5pYvIJ2JkjPInv NjAmYEW0sGt37cAiP4lwl6p0JaB186cPuJ4VIrWscm7bSAoZrQARfB6PllZ3dB+Q4D3+ k7hBVJcAAAMXG5VYs+4fNJTvFQLo4GnPRqhPBIttAnSwesPiIaV/GEB0W7QsNKSKZnD8 uD3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796318515; x=1796923315; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=sRRo0v4fZ4OaZJFmMyoDJnHxpi57zOeYZ12rblsbFX0=; b=PKWj8RIpy/iVjf96Oty6O+mIpOwajktGc2jOMT9bTrcBxcItIisVt1AEeOQZ0zj8f2 m7dYngftKpOnHK4YcZn6KCO0EpCI8CtZIGeUDd7BCtFR4caLLy2bvZEJaChschEWgG2M zUw1QjdY+PZTREs1w20b6FfcbcYjgTnSnAU5/wZjl0xOUYTMf/eKp7Qqg7iMBu5Llvhp AbKKdxMfx7AgzbBFJFXQQIim7F6Y1L6KjnHT1kQmCMlcMB5zbK+Ks+iaoJalrtH9AIDF mItlJWbQiMaymoZA2xhZfIZ5PkP8J+ExBcN8Y5MsMXk93Jmg0hv/PzYUM2j3MRS7LpK6 FbkA== X-Gm-Message-State: APuU4Z9W0VX9r2qbBI7X+vMxdp8v+akoywb4ZGn93/oZmbvrS7yLFeCS reeqm2JiL6bQz/do95kNdXePnUYZU7E/uMSOmwuGbiPwpGEeOAmxLpb8GcuNOakMIk+rdZcO X-Gm-Gg: AZFdTZdGbV7zpCpFVqWY51lNqDjqFjUOYSvj5YHt+zfA3MNpOLLwGYWo9givtFOWmnz 3BIkuC570D3XVn1tg5vn0Ni3Ng/aQMu0QicUyuagusiZPpX57jBiAELOLRBrKOjCYY82tXUD9LU V3Ozjh8bTLEbRdXzQ/auD5aj7xWoK8ilMkWy89EfYIEYvkj//Fzxle43l//YbZvjIJRhOFS3hjO aKgIYS1pezzy6Vn4jyg0a3AktH6rzpi8vATuW9mR8QxTQHv0jIJu87NNaHgzO4q1FfyJYPcdjRr F9ocMF9YtuC2tUjQsofgee5PT68c4yBMa8aXH3DTScO50yBvkgFSA0OXVKt0C47TDnR1IZZbtRM /bvPAkZTgfnutbTrXVII1r4tkppqGF6afbOxRa4RASvG2WXRziRTZYToUU0tMCB01wP9eutKFT7 3YHrpKFJFBkTrw4rEzSVcJvFaljU28loGLwk9buoKPKWgnaA+/BWFT0Au+7lBNeakrzI8N8NmxL p1Dplg1qtFhdg41Ig0qR7A516En4v8lxs0SKIqXKoo0FuVAXmClTZoTCcT/bNymDI/k5uOUJZce 3aXteS0R9hyfg1KIupbY5A== X-Received: by 2002:a17:90b:394a:b0:3b5:dd3e:91cd with SMTP id 98e67ed59e1d1-3b5dd3eaceamr16720a91.41.1796318515003; Thu, 03 Dec 2026 09:21:55 -0800 (PST) Received: from hhost ([2409:8a55:3161:29f0:7f14:2cb3:33a0:f9a3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b82e624sm31249474a91.4.2026.12.03.09.21.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 09:21:54 -0800 (PST) Date: Fri, 4 Dec 2026 01:21:17 +0800 From: Xingyuan Mo To: Paul Moore , Eric Paris Cc: "open list:AUDIT SUBSYSTEM" , Xingyuan Mo , stable@vger.kernel.org Subject: [PATCH] audit: clear borrowed LSM pointers before duplicating rule fields Message-ID: <20261203172113.2281383-1-hdthky0@gmail.com> X-Mailing-List: audit@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Mailer: git-send-email 2.43.0 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: 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 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 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 --- 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