Received: by 10.223.176.46 with SMTP id f43csp1318512wra; Fri, 19 Jan 2018 09:47:50 -0800 (PST) X-Google-Smtp-Source: ACJfBosKW/4eLJ+Y/F8llf/tb+BG+bZXd6BmR3P6revFJkl7yUN2KkiCXOJbGl/WzNcBN/aiftbv X-Received: by 2002:a17:902:2ec1:: with SMTP id r59-v6mr1958928plb.31.1516384070229; Fri, 19 Jan 2018 09:47:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516384070; cv=none; d=google.com; s=arc-20160816; b=HBhocMGPjoJNovEl10/TtZxafIn0DUKIZ+73FUX5Or0h0MB4n1EGOJdVplkxeT2RF0 fxuty4i0P0wkzuvZbH4HW3ul7hUp3rfbT2gCMswDqctEUEUmLNwYIG9/oT72wy1gsZTb FXxwIPHi1fibbb/zmAr3Xo6NicdIsj8SeyM026wNYngzgjCLminjwIQYxgE2cqP24rlg YHGPt6EPR4Ti0HVdkFLc9ZofKqfAesrys7SaejGcMv8EPZoRNShSYLJpAnFIZ4tZSds4 Csk8rYxln1+WH4fmT7O5YSfxeLGhAhkg3Q9w2ujCzG0p6+7iHp8iI0tP28xbNX0KvNQ9 qT5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=bSyZPRrjtu0CuYKiy/LmxewPICs0fAcpC+IMQEauLM4=; b=x3UcDtNVuGRim7qEnTc9bVrRgLrKxF/YE2HrgB/qMlTY7ZCj+/+RRlX5mYUbbBta9v UhXrBKkhKqs39GztUQ+z/uy5LvgeVDJdgkik55NwlNlqYLb6sWtVWP7v4FsdOGpH7Vyv S56s53vpvE3FSuj+PxWIA3aDKARtaiTbeS9Ih3OSXjB28s7igqu/ehGdrHrHWc+ObEKJ 8srunBgxx7lR53PzUOn/ifSWY29rWaXBLCqAzDOfqQfAtUYFjxAvCdSCRyhI471y6TI7 oIs7FPJ8idZ6MHXrNoYD5459rcBLw+vIoQrgSU3paXddz1o0ulWsfreco7eJHtuRLA27 5wcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=KHZmBRCo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i78si9650252pfi.150.2018.01.19.09.47.36; Fri, 19 Jan 2018 09:47:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=KHZmBRCo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932563AbeASRrG (ORCPT + 99 others); Fri, 19 Jan 2018 12:47:06 -0500 Received: from mail-pf0-f177.google.com ([209.85.192.177]:44353 "EHLO mail-pf0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932512AbeASRq5 (ORCPT ); Fri, 19 Jan 2018 12:46:57 -0500 Received: by mail-pf0-f177.google.com with SMTP id m26so1872525pfj.11 for ; Fri, 19 Jan 2018 09:46:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=bSyZPRrjtu0CuYKiy/LmxewPICs0fAcpC+IMQEauLM4=; b=KHZmBRCoh+cbUf89wV8GWb+baO0qfQP0cNsoVMTt9K6UN7Bpp9PxmEmMWT+B1kqtec 4nmfHgoKQGTO19cSqrqAA4val3Jabz38HUDcIF/P53IJwLJo8KG+11ZMzOdG+Hew7UmC n0vcoX3Sy9EvzrFSxa3kbR2hhVnDIGQDVZFp4TadOKOFSc5hYPeblZjgez6+mtNSRyXc B6LKU2iH6WjsqK4DZ6PZOEyU0QUAgKyuIBgN72xRN+Rq0h1Il+QWNUg/z35WcAzkvfJw t5zIXkiQhEamLFuRoh1pqbzuXbLk6rTFKiFsgbhJVuTdpg/ryJ9FeT1LfqELgWTP5xqq 39Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=bSyZPRrjtu0CuYKiy/LmxewPICs0fAcpC+IMQEauLM4=; b=D5qUl5o/pH9KITwaQDVsYDNhtvZlYzqaRcG6iJPy9eHjpVQub6n2lVikT37gZmLp9r SpJxFW2zk0B8EbAxecauZiYZ/ArKm02Efq/0pq1rD9u4sHmeFrpYX5/tuUlEvySVUobW kknhmzctirXOVpgWkrqH1YQftGzQaXgDPHxcVJRB9eYsWr0IfOPeAVifZb87QcnI+1X1 GiUj5IIqBgrupCSlgrGWXiXQUJzmi9F321vQW9YPmioqWnjqylppgI/d4NK17lttdR6+ YU/F1Nza/YEdkgZvH5wCtqHvUv+BWP3idyZqbdE2UmGkZv2+qNMnoVYv406FrcSJr7e8 ecbw== X-Gm-Message-State: AKwxytctOUNY5nt5ORxUeggHy79+HiMLh1Yis1C8N/Y2yn2iAOczfdVi LOa8mVlWwePKfFzfA+HdycAhwg== X-Received: by 10.99.143.84 with SMTP id r20mr5120008pgn.142.1516384016763; Fri, 19 Jan 2018 09:46:56 -0800 (PST) Received: from nebulus.mtv.corp.google.com ([2620:0:1000:1612:b4fb:6752:f21f:3502]) by smtp.googlemail.com with ESMTPSA id y7sm18344794pfy.96.2018.01.19.09.46.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Jan 2018 09:46:56 -0800 (PST) Subject: Re: [PATCH] general protection fault in sock_has_perm To: Paul Moore Cc: linux-kernel@vger.kernel.org, Stephen Smalley , Eric Paris , James Morris , "Serge E. Hallyn" , selinux@tycho.nsa.gov, linux-security-module@vger.kernel.org, stable@vger.kernel.org, Eric Dumazer References: <20180118215853.228182-1-salyzyn@android.com> <1f185712-9136-be88-02c0-5613a7683619@android.com> From: Mark Salyzyn Message-ID: <0cab9b55-b21e-48b6-82f3-3750f470ea5b@android.com> Date: Fri, 19 Jan 2018 09:46:55 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01/19/2018 09:06 AM, Paul Moore wrote: > On Fri, Jan 19, 2018 at 10:49 AM, Mark Salyzyn wrote: >> On 01/18/2018 02:36 PM, Paul Moore wrote: >>> On Thu, Jan 18, 2018 at 4:58 PM, Mark Salyzyn wrote: >>>> general protection fault: 0000 [#1] PREEMPT SMP KASAN >>>> CPU: 1 PID: 14233 Comm: syz-executor2 Not tainted 4.4.112-g5f6325b #28 >>>> . . . >>>> [] selinux_socket_setsockopt+0x4d/0x80 >>>> security/selinux/hooks.c:4338 >>>> [] security_socket_setsockopt+0x7d/0xb0 >>>> security/security.c:1257 >>>> [] SYSC_setsockopt net/socket.c:1757 [inline] >>>> [] SyS_setsockopt+0xe8/0x250 net/socket.c:1746 >>>> [] entry_SYSCALL_64_fastpath+0x16/0x92 >>>> Code: c2 42 9b b6 81 be 01 00 00 00 48 c7 c7 a0 cb 2b 84 e8 >>>> f7 2f 6d ff 49 8d 7d 10 48 b8 00 00 00 00 00 fc ff df 48 89 >>>> fa 48 c1 ea 03 <0f> b6 04 02 84 c0 74 08 3c 03 0f 8e 83 01 00 >>>> 00 41 8b 75 10 31 >>>> RIP [] sock_has_perm+0x1fe/0x3e0 >>>> security/selinux/hooks.c:4069 >>>> RSP >>>> ---[ end trace 7b5aaf788fef6174 ]--- >>>> >>>> In the absence of commit a4298e4522d6 ("net: add SOCK_RCU_FREE socket >>>> flag") and all the associated infrastructure changes to take advantage >>>> of a RCU grace period before freeing, there is a heightened >>>> possibility that a security check is performed while an ill-timed >>>> setsockopt call races in from user space. It then is prudent to null >>>> check sk_security, and if the case, reject the permissions. >>>> >>>> This adjustment is orthogonal to infrastructure improvements that may >>>> nullify the needed check, but should be added as good code hygiene. >>> I'm skeptical that this is the full solution for systems that lack the >>> SOCK_RCU_FREE protection. Is this really limited to just >>> setsockopt()? >> Maybe overstepped in my analysis and assumptions? >> >> This is a result of a fuzzer hitting an android 4.4 KASAN kernel. We (so >> far) have _not_ seen this with an android 4.9 KASAN kernel (which has the >> SOCK_RCU_FREE adjustments). There is no standalone duplication or PoC >> _except_ via the fuzzer. The rest of the statements stands based on this >> tidbit (statements of general good code hygiene, not 100% sure SOCK_RCU_FREE >> usage is completely covered, KISS solution etc). >> >> To be honest, yes, this may be a layer in the onion (swat this NULL check >> does not by itself solve the _problem_), I'd prefer kernel continuing on in >> a rational manner rather than panic ... and I have a gut feeling this could >> be a gratuitous NULL check if all the bugs in the network layer have been >> solved . Programming to solve a problem >> with one's gut is not a good practice, but hygiene is. This is 10 >> characters, and an estimated 1.2ns of added hygiene. >> >> No, I do not think this is limited to setsockopt, but would be willing to >> believe a multithreaded attack of any socket functions or ioctl would drop >> down to the check with sock_has_perm at possibly the wrong time in socket >> teardown. > I'm not necessarily opposed to adding additional safety checks, if > warranted, but I am opposed to adding a single check and declaring > mission accomplished when there is a suspicion that additional checks > may be needed. > > Perhaps in this particular case it really is only setsockopt(), but > from what I can tell from your comments and the SOCK_RCU_FREE commit > message it would appear that there is a race condition here between a > socket's lifetime and its visibility to userspace. I will need to fix my comments to be clearer ... (besides, I got the error return wrong, so I will have to respin it anyways). In later kernels SOCK_RCU_FREE _appears_ to fix the race condition. In earlier kernels there is _no_ SOCK_RCU_FREE infrastructure, and thus the race condition exists. That race conditions _should_ have been solved in ToT. I have evaluated that porting all the SOCK_RCU_FREE refactoring may be too risky an endeavor for stable kernels though (maybe I am wrong about that? The author of those fixes edumazet@google.com has been notified, have yet to receive a response). Because I can not confirm that SOCK_RCU_FREE solves the problem, I am making a case for this to land in 4.9+ to ToT kernels as good hygiene. But maybe this should only go in 4.4- stable ... -- Mark