[syzbot] [keyrings?] [lsm?] KASAN: slab-use-after-free Read in key_put

36 views
Skip to first unread message

syzbot

unread,
Nov 18, 2024, 11:27:26 AM11/18/24
to dhow...@redhat.com, jar...@kernel.org, jmo...@namei.org, keyr...@vger.kernel.org, linux-...@vger.kernel.org, linux-secu...@vger.kernel.org, pa...@paul-moore.com, se...@hallyn.com, syzkall...@googlegroups.com
Hello,

syzbot found the following issue on:

HEAD commit: adc218676eef Linux 6.12
git tree: upstream
console+strace: https://syzkaller.appspot.com/x/log.txt?x=16c672e8580000
kernel config: https://syzkaller.appspot.com/x/.config?x=55f8591b98dd132
dashboard link: https://syzkaller.appspot.com/bug?extid=6105ffc1ded71d194d6d
compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=12dbbb5f980000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=11c672e8580000

Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/2362200b664b/disk-adc21867.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/866b8b236466/vmlinux-adc21867.xz
kernel image: https://storage.googleapis.com/syzbot-assets/607680582dad/bzImage-adc21867.xz

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+6105ff...@syzkaller.appspotmail.com

trusted_key: encrypted_key: insufficient parameters specified
==================================================================
BUG: KASAN: slab-use-after-free in key_put security/keys/key.c:657 [inline]
BUG: KASAN: slab-use-after-free in key_put+0x288/0x2a0 security/keys/key.c:646
Read of size 8 at addr ffff888079173b00 by task syz-executor356/7162

CPU: 0 UID: 0 PID: 7162 Comm: syz-executor356 Not tainted 6.12.0-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/30/2024
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x116/0x1f0 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:377 [inline]
print_report+0xc3/0x620 mm/kasan/report.c:488
kasan_report+0xd9/0x110 mm/kasan/report.c:601
key_put security/keys/key.c:657 [inline]
key_put+0x288/0x2a0 security/keys/key.c:646
__key_create_or_update+0x92b/0xe10 security/keys/key.c:940
key_create_or_update+0x42/0x60 security/keys/key.c:1018
__do_sys_add_key+0x29c/0x460 security/keys/keyctl.c:134
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f410659a399
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 51 18 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f4106552218 EFLAGS: 00000246 ORIG_RAX: 00000000000000f8
RAX: ffffffffffffffda RBX: 00007f4106621328 RCX: 00007f410659a399
RDX: 0000000020000100 RSI: 0000000020000180 RDI: 0000000020000140
RBP: 00007f4106621320 R08: 00000000fffffffe R09: 0000000000000000
R10: 00000000000000ca R11: 0000000000000246 R12: 00007f41065ee074
R13: 0072736d2f232f75 R14: 7570632f7665642f R15: 6574707972636e65
</TASK>

Allocated by task 7162:
kasan_save_stack+0x33/0x60 mm/kasan/common.c:47
kasan_save_track+0x14/0x30 mm/kasan/common.c:68
unpoison_slab_object mm/kasan/common.c:319 [inline]
__kasan_slab_alloc+0x89/0x90 mm/kasan/common.c:345
kasan_slab_alloc include/linux/kasan.h:247 [inline]
slab_post_alloc_hook mm/slub.c:4085 [inline]
slab_alloc_node mm/slub.c:4134 [inline]
kmem_cache_alloc_noprof+0x121/0x2f0 mm/slub.c:4141
key_alloc+0x3e3/0x13a0 security/keys/key.c:277
__key_create_or_update+0x71f/0xe10 security/keys/key.c:930
key_create_or_update+0x42/0x60 security/keys/key.c:1018
__do_sys_add_key+0x29c/0x460 security/keys/keyctl.c:134
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 46:
kasan_save_stack+0x33/0x60 mm/kasan/common.c:47
kasan_save_track+0x14/0x30 mm/kasan/common.c:68
kasan_save_free_info+0x3b/0x60 mm/kasan/generic.c:579
poison_slab_object mm/kasan/common.c:247 [inline]
__kasan_slab_free+0x51/0x70 mm/kasan/common.c:264
kasan_slab_free include/linux/kasan.h:230 [inline]
slab_free_hook mm/slub.c:2342 [inline]
slab_free mm/slub.c:4579 [inline]
kmem_cache_free+0x152/0x4b0 mm/slub.c:4681
key_gc_unused_keys.constprop.0+0x134/0x480 security/keys/gc.c:167
key_garbage_collector+0x432/0x990 security/keys/gc.c:300
process_one_work+0x9c5/0x1ba0 kernel/workqueue.c:3229
process_scheduled_works kernel/workqueue.c:3310 [inline]
worker_thread+0x6c8/0xf00 kernel/workqueue.c:3391
kthread+0x2c1/0x3a0 kernel/kthread.c:389
ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244

The buggy address belongs to the object at ffff888079173a40
which belongs to the cache key_jar of size 336
The buggy address is located 192 bytes inside of
freed 336-byte region [ffff888079173a40, ffff888079173b90)

The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x79172
head: order:1 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000000040(head|node=0|zone=1|lastcpupid=0x7ff)
page_type: f5(slab)
raw: 00fff00000000040 ffff88801c2ba140 dead000000000122 0000000000000000
raw: 0000000000000000 0000000080120012 00000001f5000000 0000000000000000
head: 00fff00000000040 ffff88801c2ba140 dead000000000122 0000000000000000
head: 0000000000000000 0000000080120012 00000001f5000000 0000000000000000
head: 00fff00000000001 ffffea0001e45c81 ffffffffffffffff 0000000000000000
head: 0000000000000002 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 1, migratetype Unmovable, gfp_mask 0xd20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 7134, tgid 7131 (syz-executor356), ts 90591707414, free_ts 84975549277
set_page_owner include/linux/page_owner.h:32 [inline]
post_alloc_hook+0x2d1/0x350 mm/page_alloc.c:1556
prep_new_page mm/page_alloc.c:1564 [inline]
get_page_from_freelist+0xfce/0x2f80 mm/page_alloc.c:3474
__alloc_pages_noprof+0x223/0x25a0 mm/page_alloc.c:4751
alloc_pages_mpol_noprof+0x2c9/0x610 mm/mempolicy.c:2265
alloc_slab_page mm/slub.c:2412 [inline]
allocate_slab mm/slub.c:2578 [inline]
new_slab+0x2c9/0x410 mm/slub.c:2631
___slab_alloc+0xdac/0x1880 mm/slub.c:3818
__slab_alloc.constprop.0+0x56/0xb0 mm/slub.c:3908
__slab_alloc_node mm/slub.c:3961 [inline]
slab_alloc_node mm/slub.c:4122 [inline]
kmem_cache_alloc_noprof+0x2a7/0x2f0 mm/slub.c:4141
key_alloc+0x3e3/0x13a0 security/keys/key.c:277
keyring_alloc+0x44/0xc0 security/keys/keyring.c:526
install_process_keyring_to_cred security/keys/process_keys.c:275 [inline]
install_process_keyring security/keys/process_keys.c:300 [inline]
lookup_user_key+0xa34/0x12f0 security/keys/process_keys.c:653
__do_sys_add_key+0x25a/0x460 security/keys/keyctl.c:126
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 6961 tgid 6958 stack trace:
reset_page_owner include/linux/page_owner.h:25 [inline]
free_pages_prepare mm/page_alloc.c:1127 [inline]
free_unref_page+0x661/0x1080 mm/page_alloc.c:2657
qlink_free mm/kasan/quarantine.c:163 [inline]
qlist_free_all+0x4e/0x120 mm/kasan/quarantine.c:179
kasan_quarantine_reduce+0x192/0x1e0 mm/kasan/quarantine.c:286
__kasan_slab_alloc+0x69/0x90 mm/kasan/common.c:329
kasan_slab_alloc include/linux/kasan.h:247 [inline]
slab_post_alloc_hook mm/slub.c:4085 [inline]
slab_alloc_node mm/slub.c:4134 [inline]
__kmalloc_cache_node_noprof+0x173/0x350 mm/slub.c:4303
kmalloc_node_noprof include/linux/slab.h:901 [inline]
alloc_user_cpus_ptr kernel/sched/sched.h:2614 [inline]
sched_setaffinity+0x252/0x430 kernel/sched/syscalls.c:1282
__do_sys_sched_setaffinity kernel/sched/syscalls.c:1331 [inline]
__se_sys_sched_setaffinity kernel/sched/syscalls.c:1320 [inline]
__x64_sys_sched_setaffinity+0x101/0x170 kernel/sched/syscalls.c:1320
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f

Memory state around the buggy address:
ffff888079173a00: fc fc fc fc fc fc fc fc fa fb fb fb fb fb fb fb
ffff888079173a80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff888079173b00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff888079173b80: fb fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff888079173c00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzk...@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.

If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

Suraj Sonawane

unread,
Nov 18, 2024, 4:27:58 PM11/18/24
to syzbot, dhow...@redhat.com, jar...@kernel.org, jmo...@namei.org, keyr...@vger.kernel.org, linux-...@vger.kernel.org, linux-secu...@vger.kernel.org, pa...@paul-moore.com, se...@hallyn.com, syzkall...@googlegroups.com
#syz test

--
You received this message because you are subscribed to the Google Groups "syzkaller-bugs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to syzkaller-bug...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/syzkaller-bugs/673b6aec.050a0220.87769.004a.GAE%40google.com.
0001-fix-slab-after-free-read.patch

syzbot

unread,
Nov 18, 2024, 4:37:05 PM11/18/24
to dhow...@redhat.com, jar...@kernel.org, jmo...@namei.org, keyr...@vger.kernel.org, linux-...@vger.kernel.org, linux-secu...@vger.kernel.org, pa...@paul-moore.com, se...@hallyn.com, surajson...@gmail.com, syzkall...@googlegroups.com
Hello,

syzbot tried to test the proposed patch but the build/boot failed:

security/keys/gc.c:146:40: error: 'KEY_IS_DEAD' undeclared (first use in this function); did you mean 'KEY_USR_READ'?


Tested on:

commit: 9fb2cfa4 Merge tag 'pull-ufs' of git://git.kernel.org/..
git tree: upstream
kernel config: https://syzkaller.appspot.com/x/.config?x=55f8591b98dd132
dashboard link: https://syzkaller.appspot.com/bug?extid=6105ffc1ded71d194d6d
compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
patch: https://syzkaller.appspot.com/x/patch.diff?x=1781eac0580000

syzbot

unread,
Nov 19, 2024, 5:36:04 AM11/19/24
to dhow...@redhat.com, jarkko....@kernel.org, jar...@kernel.org, jmo...@namei.org, keyr...@vger.kernel.org, lhenr...@suse.de, linux-...@vger.kernel.org, linux-secu...@vger.kernel.org, pa...@paul-moore.com, se...@hallyn.com, surajson...@gmail.com, syzkall...@googlegroups.com
syzbot has bisected this issue to:

commit 9578e327b2b4935a25d49e3891b8fcca9b6c10c6
Author: Luis Henriques <lhenr...@suse.de>
Date: Tue Jan 30 10:13:44 2024 +0000

keys: update key quotas in key_put()

bisection log: https://syzkaller.appspot.com/x/bisect.txt?x=10821bf7980000
start commit: adc218676eef Linux 6.12
git tree: upstream
final oops: https://syzkaller.appspot.com/x/report.txt?x=12821bf7980000
console output: https://syzkaller.appspot.com/x/log.txt?x=14821bf7980000
Reported-by: syzbot+6105ff...@syzkaller.appspotmail.com
Fixes: 9578e327b2b4 ("keys: update key quotas in key_put()")

For information about bisection process see: https://goo.gl/tpsmEJ#bisection

Hillf Danton

unread,
Nov 20, 2024, 5:42:37 AM11/20/24
to syzbot, linux-...@vger.kernel.org, syzkall...@googlegroups.com
On Mon, 18 Nov 2024 08:27:24 -0800
> syzbot found the following issue on:
>
> HEAD commit: adc218676eef Linux 6.12
> git tree: upstream
> C reproducer: https://syzkaller.appspot.com/x/repro.c?x=11c672e8580000

#syz test

--- x/security/keys/key.c
+++ y/security/keys/key.c
@@ -645,21 +645,30 @@ EXPORT_SYMBOL(key_reject_and_link);
*/
void key_put(struct key *key)
{
+ int quota_flag;
+ unsigned short len;
+ struct key_user *user;
+
if (key) {
key_check(key);

+ quota_flag = test_bit(KEY_FLAG_IN_QUOTA, &key->flags);
+ len = key->quotalen;
+ user = key->user;
+ refcount_inc(&user->usage);
if (refcount_dec_and_test(&key->usage)) {
unsigned long flags;

/* deal with the user's key tracking and quota */
- if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
- spin_lock_irqsave(&key->user->lock, flags);
- key->user->qnkeys--;
- key->user->qnbytes -= key->quotalen;
- spin_unlock_irqrestore(&key->user->lock, flags);
+ if (quota_flag) {
+ spin_lock_irqsave(&user->lock, flags);
+ user->qnkeys--;
+ user->qnbytes -= len;
+ spin_unlock_irqrestore(&user->lock, flags);
}
schedule_work(&key_gc_work);
}
+ key_user_put(user);
}
}
EXPORT_SYMBOL(key_put);
--

syzbot

unread,
Nov 20, 2024, 6:15:06 AM11/20/24
to hda...@sina.com, linux-...@vger.kernel.org, syzkall...@googlegroups.com
Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Reported-by: syzbot+6105ff...@syzkaller.appspotmail.com
Tested-by: syzbot+6105ff...@syzkaller.appspotmail.com

Tested on:

commit: bf9aa14f Merge tag 'timers-core-2024-11-18' of git://g..
git tree: upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=16a2e930580000
kernel config: https://syzkaller.appspot.com/x/.config?x=8b2ddebc25a60ddb
dashboard link: https://syzkaller.appspot.com/bug?extid=6105ffc1ded71d194d6d
compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
patch: https://syzkaller.appspot.com/x/patch.diff?x=12ad4ae8580000

Note: testing is done by a robot and is best-effort only.

David Howells

unread,
Mar 19, 2025, 11:20:33 AM3/19/25
to syzbot, dhow...@redhat.com, jar...@kernel.org, jmo...@namei.org, Oleg Nesterov, keyr...@vger.kernel.org, linux-...@vger.kernel.org, linux-secu...@vger.kernel.org, pa...@paul-moore.com, se...@hallyn.com, syzkall...@googlegroups.com
#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

commit 91286ccf56bb5030c0c84b11684f06142f50e681
Author: David Howells <dhow...@redhat.com>
Date: Tue Mar 18 09:54:54 2025 +0000

keys: Fix UAF in key_put()

Once a key's reference count has been reduced to 0, the garbage collector
thread may destroy it at any time and so key_put() is not allowed to touch
the key after that point. The most key_put() is normally allowed to do is
to touch key_gc_work as that's a static global variable.

However, in an effort to speed up the reclamation of quota, this is now
done in key_put() once the key's usage is reduced to 0 - but now the code
is looking at the key after the deadline, which is forbidden.

Fix this by using a flag to indicate that a key can be gc'd now rather than
looking at the key's refcount in the garbage collector.

Fixes: 9578e327b2b4 ("keys: update key quotas in key_put()")
Reported-by: syzbot+6105ff...@syzkaller.appspotmail.com
Signed-off-by: David Howells <dhow...@redhat.com>
cc: Jarkko Sakkinen <jar...@kernel.org>
cc: Oleg Nesterov <ol...@redhat.com>
cc: Kees Cook <ke...@kernel.org>
cc: Hillf Danton <hda...@sina.com>,
cc: keyr...@vger.kernel.org
Cc: sta...@vger.kernel.org # v6.10+

diff --git a/include/linux/key.h b/include/linux/key.h
index 074dca3222b9..ba05de8579ec 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -236,6 +236,7 @@ struct key {
#define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */
#define KEY_FLAG_KEEP 8 /* set if key should not be removed */
#define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */
+#define KEY_FLAG_FINAL_PUT 10 /* set if final put has happened on key */

/* the key type and key description string
* - the desc is used to match a key against search criteria
diff --git a/security/keys/gc.c b/security/keys/gc.c
index 7d687b0962b1..f27223ea4578 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -218,8 +218,10 @@ static void key_garbage_collector(struct work_struct *work)
key = rb_entry(cursor, struct key, serial_node);
cursor = rb_next(cursor);

- if (refcount_read(&key->usage) == 0)
+ if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) {
+ smp_mb(); /* Clobber key->user after FINAL_PUT seen. */
goto found_unreferenced_key;
+ }

if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
if (key->type == key_gc_dead_keytype) {
diff --git a/security/keys/key.c b/security/keys/key.c
index 3d7d185019d3..7198cd2ac3a3 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -658,6 +658,8 @@ void key_put(struct key *key)
key->user->qnbytes -= key->quotalen;
spin_unlock_irqrestore(&key->user->lock, flags);
}
+ smp_mb(); /* key->user before FINAL_PUT set. */
+ set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
schedule_work(&key_gc_work);
}
}

syzbot

unread,
Mar 19, 2025, 11:49:04 AM3/19/25
to dhow...@redhat.com, jar...@kernel.org, jmo...@namei.org, keyr...@vger.kernel.org, linux-...@vger.kernel.org, linux-secu...@vger.kernel.org, ol...@redhat.com, pa...@paul-moore.com, se...@hallyn.com, syzkall...@googlegroups.com
Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Reported-by: syzbot+6105ff...@syzkaller.appspotmail.com
Tested-by: syzbot+6105ff...@syzkaller.appspotmail.com

Tested on:

commit: 81e4f8d6 Merge tag 'pmdomain-v6.14-rc4' of git://git.k..
git tree: upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=1216744c580000
kernel config: https://syzkaller.appspot.com/x/.config?x=14213ea05709bce7
dashboard link: https://syzkaller.appspot.com/bug?extid=6105ffc1ded71d194d6d
compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
patch: https://syzkaller.appspot.com/x/patch.diff?x=16bb93b0580000
Reply all
Reply to author
Forward
0 new messages