From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754578Ab3BFOVf (ORCPT ); Wed, 6 Feb 2013 09:21:35 -0500 Received: from emvm-gh1-uea08.nsa.gov ([63.239.67.9]:50055 "EHLO nsa.gov" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751294Ab3BFOVc (ORCPT ); Wed, 6 Feb 2013 09:21:32 -0500 X-TM-IMSS-Message-ID: <331c497c0005086e@nsa.gov> Message-ID: <5112662D.1010406@tycho.nsa.gov> Date: Wed, 06 Feb 2013 09:18:21 -0500 From: Stephen Smalley Organization: National Security Agency User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Tommi Rantala CC: James Morris , Eric Paris , linux-security-module@vger.kernel.org, Dave Jones , LKML Subject: Re: selinux_msg_queue_msgrcv() oops References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/06/2013 07:56 AM, Tommi Rantala wrote: > Hello, > > I'm hitting an oops in selinux_msg_queue_msgrcv() when fuzzing with > Trinity as the root user (in a qemu VM): NULL msg->security at that point is a bug in the ipc subsystem; SELinux is just the messenger. Normally msg->security is set for every allocated msg by load_msg() -> security_msg_msg_alloc() -> selinux_msg_msg_alloc_security(), and freed/cleared upon free_msg() -> security_msg_msg_free() -> selinux_msg_msg_free_security(). Looking around, I see copy_msg() introduced for checkpoint-restore initializes dst->security to NULL but never sets it properly? > > [12578.053111] BUG: unable to handle kernel NULL pointer dereference > at (null) > [12578.054025] IP: [] selinux_msg_queue_msgrcv+0xda/0x1e0 > [12578.054025] PGD 29961067 PUD 34dc5067 PMD 0 > [12578.054025] Oops: 0000 [#2] SMP > [12578.054025] CPU 1 > [12578.054025] Pid: 23453, comm: trinity-child23 Tainted: G D W > 3.8.0-rc6+ #31 Bochs Bochs > [12578.054025] RIP: 0010:[] [] > selinux_msg_queue_msgrcv+0xda/0x1e0 > [12578.054025] RSP: 0018:ffff88002b6b5e18 EFLAGS: 00010246 > [12578.054025] RAX: 0000000000000000 RBX: ffff88003132d410 RCX: 0000000000000001 > [12578.054025] RDX: ffff88000e8bc560 RSI: 0000000000000001 RDI: 0000000000000246 > [12578.054025] RBP: ffff88002b6b5e68 R08: 0000000000000000 R09: 0000000000000000 > [12578.054025] R10: ffff88000e8bc560 R11: 0000000000000000 R12: 0000000000000001 > [12578.054025] R13: 0000000000000000 R14: ffff880006449500 R15: ffff88003132d410 > [12578.054025] FS: 00007f7385059700(0000) GS:ffff88003e200000(0000) > knlGS:0000000000000000 > [12578.054025] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [12578.054025] CR2: 0000000000000000 CR3: 00000000303a2000 CR4: 00000000000006e0 > [12578.054025] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > [12578.054025] DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400 > [12578.054025] Process trinity-child23 (pid: 23453, threadinfo > ffff88002b6b4000, task ffff88000e8bc560) > [12578.054025] Stack: > [12578.054025] ffffffff8131e105 ffffffff81313f69 ffff88002b6b5e04 > ffffffff00000000 > [12578.054025] ffffffff812fd6f5 ffff88003a89c1c0 0000000000000000 > 0000000000000001 > [12578.054025] 0000000000000000 ffff88003132d4c0 ffff88002b6b5e78 > ffffffff81314086 > [12578.054025] Call Trace: > [12578.054025] [] ? selinux_msg_queue_msgrcv+0x5/0x1e0 > [12578.054025] [] ? security_ipc_permission+0x19/0x20 > [12578.054025] [] ? ipc_lock+0x5/0x1c0 > [12578.054025] [] security_msg_queue_msgrcv+0x16/0x20 > [12578.054025] [] do_msgrcv+0x1ef/0x6e0 > [12578.054025] [] ? load_msg+0x180/0x180 > [12578.054025] [] ? lockdep_sys_exit_thunk+0x35/0x67 > [12578.054025] [] ? trace_hardirqs_on_caller+0x16/0x1a0 > [12578.054025] [] ? trace_hardirqs_on_thunk+0x3a/0x3f > [12578.054025] [] sys_msgrcv+0x15/0x20 > [12578.054025] [] system_call_fastpath+0x16/0x1b > [12578.054025] Code: 4c 8d 45 c0 45 31 c9 b9 10 00 00 00 44 89 e7 4d > 8b 6d 28 c6 45 c0 04 89 55 c8 8b 70 04 ba 1b 00 00 00 e8 fa 7a ff ff > 85 c0 75 1d <41> 8b 75 00 4c 8d 45 c0 45 31 c9 b9 02 00 00 00 ba 1a 00 > 00 00 > [12578.054025] RIP [] selinux_msg_queue_msgrcv+0xda/0x1e0 > [12578.054025] RSP > [12578.054025] CR2: 0000000000000000 > [12578.142292] ---[ end trace 36aee1c7bfea7f83 ]--- > > > After adding: > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c > index 54aaa72..20cec57 100644 > --- a/security/selinux/hooks.c > +++ b/security/selinux/hooks.c > @@ -4982,9 +4982,12 @@ static int selinux_msg_queue_msgrcv(struct > msg_queue *msq, struct msg_msg *msg, > > rc = avc_has_perm(sid, isec->sid, > SECCLASS_MSGQ, MSGQ__READ, &ad); > - if (!rc) > + if (!rc) { > + WARN(msec == NULL, "msec is NULL!"); > + > rc = avc_has_perm(sid, msec->sid, > SECCLASS_MSG, MSG__RECEIVE, &ad); > + } > return rc; > } > > > I see: > > [ 43.103283] ------------[ cut here ]------------ > [ 43.104236] WARNING: at > /home/ttrantal/git/linux-2.6/security/selinux/hooks.c:4986 > selinux_msg_queue_msgrcv+0x1ff/0x210() > [ 43.106088] Hardware name: Bochs > [ 43.106640] msec is NULL!Pid: 2387, comm: trinity-child9 Not > tainted 3.8.0-rc6+ #37 > [ 43.107950] Call Trace: > [ 43.108393] [] ? selinux_msg_queue_msgrcv+0x1ff/0x210 > [ 43.109534] [] warn_slowpath_common+0x7a/0xb0 > [ 43.110565] [] warn_slowpath_fmt+0x46/0x50 > [ 43.111561] [] selinux_msg_queue_msgrcv+0x1ff/0x210 > [ 43.112677] [] ? selinux_msg_queue_msgrcv+0x5/0x210 > [ 43.113808] [] ? security_ipc_permission+0x19/0x20 > [ 43.114919] [] ? ipc_lock+0x5/0x1c0 > [ 43.115817] [] security_msg_queue_msgrcv+0x16/0x20 > [ 43.116929] [] do_msgrcv+0x1ef/0x6e0 > [ 43.117909] [] ? load_msg+0x180/0x180 > [ 43.118850] [] ? trace_hardirqs_on_caller+0x10d/0x1a0 > [ 43.120019] [] ? trace_hardirqs_on_thunk+0x3a/0x3f > [ 43.121126] [] sys_msgrcv+0x15/0x20 > [ 43.122001] [] system_call_fastpath+0x16/0x1b > [ 43.123044] ---[ end trace db5952f0fa3bedc7 ]--- > [ 43.123815] > [ 43.124096] =============================== > [ 43.124804] [ INFO: suspicious RCU usage. ] > [ 43.125531] 3.8.0-rc6+ #37 Tainted: G W > [ 43.126344] ------------------------------- > [ 43.127083] /home/ttrantal/git/linux-2.6/include/linux/rcupdate.h:468 > Illegal context switch in RCU read-side critical section! > [ 43.129015] > [ 43.129015] other info that might help us debug this: > [ 43.129015] > [ 43.130367] > [ 43.130367] rcu_scheduler_active = 1, debug_locks = 0 > [ 43.131481] 3 locks held by trinity-child9/2387: > [ 43.132266] #0: (rcu_read_lock){.+.+..}, at: [] > ipc_lock+0x5/0x1c0 > [ 43.133709] #1: (&(&new->lock)->rlock){+.+...}, at: > [] ipc_lock+0x81/0x1c0 > [ 43.135294] #2: (&mm->mmap_sem){++++++}, at: [] > __do_page_fault+0x114/0x4e0 > [ 43.136864] > [ 43.136864] stack backtrace: > [ 43.137619] Pid: 2387, comm: trinity-child9 Tainted: G W > 3.8.0-rc6+ #37 > [ 43.138897] Call Trace: > [ 43.139338] [] lockdep_rcu_suspicious+0xfd/0x130 > [ 43.140417] [] __schedule+0x543/0x900 > [ 43.141342] [] __cond_resched+0x2a/0x40 > [ 43.142291] [] ? __do_page_fault+0x114/0x4e0 > [ 43.143440] [] _cond_resched+0x2f/0x40 > [ 43.144362] [] __do_page_fault+0x121/0x4e0 > [ 43.145362] [] ? trace_hardirqs_on+0xd/0x10 > [ 43.146316] [] ? trace_hardirqs_off_thunk+0x3a/0x3c > [ 43.147386] [] do_page_fault+0xe/0x10 > [ 43.148254] [] do_async_page_fault+0x2a/0xa0 > [ 43.149239] [] async_page_fault+0x28/0x30 > [ 43.150167] [] ? selinux_msg_queue_msgrcv+0xe7/0x210 > [ 43.151263] [] ? selinux_msg_queue_msgrcv+0x1ff/0x210 > [ 43.152357] [] ? selinux_msg_queue_msgrcv+0x5/0x210 > [ 43.153475] [] ? security_ipc_permission+0x19/0x20 > [ 43.154828] [] ? ipc_lock+0x5/0x1c0 > [ 43.156052] [] security_msg_queue_msgrcv+0x16/0x20 > [ 43.157586] [] do_msgrcv+0x1ef/0x6e0 > [ 43.158830] [] ? load_msg+0x180/0x180 > [ 43.160131] [] ? trace_hardirqs_on_caller+0x10d/0x1a0 > [ 43.161736] [] ? trace_hardirqs_on_thunk+0x3a/0x3f > [ 43.163238] [] sys_msgrcv+0x15/0x20 > [ 43.164453] [] system_call_fastpath+0x16/0x1b > [ 43.176035] BUG: unable to handle kernel NULL pointer dereference > at (null) > [ 43.177016] IP: [] selinux_msg_queue_msgrcv+0xe7/0x210 > [ 43.177016] PGD 3189b067 PUD 3189c067 PMD 0 > [ 43.177016] Oops: 0000 [#1] SMP > [ 43.177016] CPU 0 > [ 43.177016] Pid: 2387, comm: trinity-child9 Tainted: G W > 3.8.0-rc6+ #37 Bochs Bochs > [ 43.177016] RIP: 0010:[] [] > selinux_msg_queue_msgrcv+0xe7/0x210 > [ 43.177016] RSP: 0018:ffff8800318a7e18 EFLAGS: 00010296 > [ 43.177016] RAX: 0000000000000000 RBX: ffff880032e0e810 RCX: 0000000000000006 > [ 43.177016] RDX: 0000000000003e50 RSI: ffff88003b7c4c68 RDI: 0000000000000009 > [ 43.177016] RBP: ffff8800318a7e68 R08: 0000000000000001 R09: 0000000000000000 > [ 43.177016] R10: 0000000000000000 R11: 0000000000000288 R12: 0000000000000001 > [ 43.177016] R13: 0000000000000000 R14: ffff88003b22ae80 R15: ffff880032e0e810 > [ 43.177016] FS: 00007fc6ba864700(0000) GS:ffff88003fc00000(0000) > knlGS:0000000000000000 > [ 43.177016] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 43.177016] CR2: 00007fc6ba6471f0 CR3: 0000000031898000 CR4: 00000000000006f0 > [ 43.177016] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > [ 43.177016] DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400 > [ 43.177016] Process trinity-child9 (pid: 2387, threadinfo > ffff8800318a6000, task ffff88003b7c4560) > [ 43.177016] Stack: > [ 43.177016] ffffffff8131df35 ffffffff81313f99 ffff8800318a7e04 > ffffffff5d6d982a > [ 43.177016] ffffffff812fd725 ffff880039c675c0 0000000000000000 > 0000000000000001 > [ 43.177016] 0000000000000000 ffff880032e0e8c0 ffff8800318a7e78 > ffffffff813140b6 > [ 43.177016] Call Trace: > [ 43.177016] [] ? selinux_msg_queue_msgrcv+0x5/0x210 > [ 43.177016] [] ? security_ipc_permission+0x19/0x20 > [ 43.177016] [] ? ipc_lock+0x5/0x1c0 > [ 43.177016] [] security_msg_queue_msgrcv+0x16/0x20 > [ 43.177016] [] do_msgrcv+0x1ef/0x6e0 > [ 43.177016] [] ? load_msg+0x180/0x180 > [ 43.177016] [] ? trace_hardirqs_on_caller+0x10d/0x1a0 > [ 43.177016] [] ? trace_hardirqs_on_thunk+0x3a/0x3f > [ 43.177016] [] sys_msgrcv+0x15/0x20 > [ 43.177016] [] system_call_fastpath+0x16/0x1b > [ 43.177016] Code: 00 00 00 44 89 e7 4d 8b 6d 28 c6 45 c0 04 89 55 > c8 8b 70 04 ba 1b 00 00 00 e8 f6 7c ff ff 85 c0 75 26 4d 85 ed 0f 84 > 00 01 00 00 <41> 8b 75 00 4c 8d 45 c0 45 31 c9 b9 02 00 00 00 ba 1a 00 > 00 00 > [ 43.177016] RIP [] selinux_msg_queue_msgrcv+0xe7/0x210 > [ 43.177016] RSP > [ 43.177016] CR2: 0000000000000000 > [ 43.228535] ---[ end trace db5952f0fa3bedc8 ]--- > [ 68.106008] BUG: soft lockup - CPU#0 stuck for 22s! [trinity-child8:2382] > > Tommi >