From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E295C6FD18 for ; Wed, 19 Apr 2023 03:24:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C24F28E0002; Tue, 18 Apr 2023 23:24:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BD4538E0001; Tue, 18 Apr 2023 23:24:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC3808E0002; Tue, 18 Apr 2023 23:24:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9910C8E0001 for ; Tue, 18 Apr 2023 23:24:41 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 56C561C66DB for ; Wed, 19 Apr 2023 03:24:41 +0000 (UTC) X-FDA: 80696698362.22.9FB8450 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf14.hostedemail.com (Postfix) with ESMTP id E0A6510000F for ; Wed, 19 Apr 2023 03:24:38 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=Fh61qubk; dmarc=none; spf=none (imf14.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681874679; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Y0jC6pG5iyPnBukJ2foYlHjpDWbyen476EaJAQv4Kk0=; b=7yGuS/GKT1KD/pPv3r5ZGhGm4Qi+P1HHvucAA1bGnZPMeE4N5pUg8B9dx1MsKwazn1UFBt +xFa6dbftWpHScQknxerFTcghOKtyjCPdyKVpjONTXqQBYg0GzUO1EJ/O3mXKO3iGiY4Cp efodH+yKuupC9eZWdH1rrH4QJor2Fhw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=Fh61qubk; dmarc=none; spf=none (imf14.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681874679; a=rsa-sha256; cv=none; b=hob+pi0RX6VGwKcsYHeQWdIA+f9MtBJpAHd82roZSh48uFywzEHdtlIkIG037xT6uhu5ou nrJNdf60NK33xBTDfXC58puda0PAwJkk/VcKDqwO7R1hTacdevxlputvkGnx9rsFpMLuvj ZmQfZEgwJkBODbWOtjFThamRLAcmg9g= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=Y0jC6pG5iyPnBukJ2foYlHjpDWbyen476EaJAQv4Kk0=; b=Fh61qubkIA4NQ/t+TuAGmvG6/Z 8ae3FW5/kyHKxUStAxLa7mpvkzLtwMNYgJQbnpb+2CAPczsqmR4FOhqqXBgjrj3RsvK6cBbn2Z23m y1eMdPe58ajYGb7XwBpgtdn2A0zmspDoqqDjOG9EbGqZzF0c2i0KcZMghhQK1Rnk3/CMPeBof4m/8 PNA4p/h1AHaAGp0lUFAhoFL0Uyxc8DKnpiw1DPdsayJ4imPlaJRwJ2XRbzFpJFCymq5GNcX8nyqlY 882ux3LVtdcusDvul2SLTD2YkUiZ6PGrENVpWkanrH7HMAJKUBMiU2CalsAYoac+84Z/fgVGzFWyd 9PPgBDkQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1poyQm-00CvFm-U9; Wed, 19 Apr 2023 03:24:28 +0000 Date: Wed, 19 Apr 2023 04:24:28 +0100 From: Matthew Wilcox To: Waiman Long Cc: Hugh Dickins , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Joe Mario , Barry Marson , Rafael Aquini , Paul Moore , Stephen Smalley , Eric Paris , selinux@vger.kernel.org, James Morris , "Serge E. Hallyn" , linux-security-module@vger.kernel.org Subject: Re: [PATCH] mm/mmap: Map MAP_STACK to VM_STACK Message-ID: References: <20230418210230.3495922-1-longman@redhat.com> <20230418141852.75e551e57e97f4b522957c5c@linux-foundation.org> <6c3c68b1-c4d4-dd82-58e8-f7013fb6c8e5@redhat.com> <22aee5ea-dd6b-ac2b-0b28-a25ee6602b48@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <22aee5ea-dd6b-ac2b-0b28-a25ee6602b48@redhat.com> X-Rspamd-Queue-Id: E0A6510000F X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: tf5q15eny3mrffy13ihdpu835nktay4r X-HE-Tag: 1681874678-977851 X-HE-Meta: U2FsdGVkX1/hRDGBVpAjMgTyHokyTc+lblPuvNrVotxJfq33z9FOPs7zXkBr9e2uaDwRFuZZT6rHNDq0UvKZOIwIW9aBdej/sFwjLSRaMx678E0jE9017p9DaATf3MG/IkHEnEJk/ZOhk458g5ZOABfS8lufuYxr6xU6dOW51kw56YtgmNUeV1g4AzfLJaIecu+XX2BTviaNK5AWr/TSZlwfdwDFZnITR+cVaIwsK95v/9ua3RMEMCRtJui32tQ0g7QtsFwK/jBV5uTDLxuHYwmSHyc8cmpOFR0dLA4iYXK1H8up26B6d6roqGAYTAdwEEuaMSlo0jJj8RGGpGaJkZ3k7VL7IXqQvfx2EcXjK0dFBGiDsWeylUDwKHVFAGfDCcI4tOY6FHF34Tp+jRaQ0n5TRpf1u+Pi80c1Y0aCRw0W7kIctJI6sl2K6dnLxpDoPdl3vgha4ORJDdAyQvjqkoqGJkUfDdG/NG5vQeLnW3c18hQJep24XSMTdbsDuPnfHwSMf9J9lAsv7DHRhIuCnjTR5coNvZpTVZsoaEefQubltBIhk76iGYXpFPX2u5kPtFaG3y4XswdvaMydj0a0DjpXAOirTYv4vc/+bdMwBYHZFl7GT66yEQplFP9j/ywQAxh0NjealMchA5x71YereH71gcc0Ev8JQaZ+87eZT1IXOWV3exOYzGr7/IwwnNcwlU1jHGWhjOp3y2s4UKJ29rqPZoJJOuoEgynkS9jrxGvZo9oj7GAeDu9vzOjtNlGu87H56dQhSR47ImBSZeIvIVDngtW+mYUTRepKixc2lQTddCrmBjX1RS6aW54xGuCcSNQb6nsrQn1BWRb/CQ8qkOvz62tDC+1ubBhjhi3/lz8atDrmPBkMgFfhVjatYiKMvJBFUDzODB+QO00HUyPrrccTj5WuqUpcWXdTPloaI6Dfc1tO2JG2mGmMplfgL4nboO1h/qIGUxDgAeq0Fi9 D8bTeFCp tKAkj1e0M6VYjNVubJqXzGvHEA7mhdordT6OawOkMgfSFrmwBRQnNN2kSz/FnbKzV8tEzf9lhxzU46+WIh8KszUUxC2xERRgkPSDvEMx2TTvhpLDW0HFZ9esRs0fs3+8dYxRsVTD47uejp66H3H4pHEw9T1n7mgNQXM2MRAyUR0FovcmebQjd5wDVu42i1ptDzfSH4LtNEE62ZYUMUZ+UInq3T10ujejsEyehwdGsXOWYTEuZMI5guRBrEvnhHYYqA3GJVIzV2iWQMdryY2ABVFGnp9kLEELC4XlRseBVj+sYYyg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Apr 18, 2023 at 09:45:34PM -0400, Waiman Long wrote: > > On 4/18/23 21:36, Hugh Dickins wrote: > > On Tue, 18 Apr 2023, Waiman Long wrote: > > > On 4/18/23 17:18, Andrew Morton wrote: > > > > On Tue, 18 Apr 2023 17:02:30 -0400 Waiman Long wrote: > > > > > > > > > One of the flags of mmap(2) is MAP_STACK to request a memory segment > > > > > suitable for a process or thread stack. The kernel currently ignores > > > > > this flags. Glibc uses MAP_STACK when mmapping a thread stack. However, > > > > > selinux has an execstack check in selinux_file_mprotect() which disallows > > > > > a stack VMA to be made executable. > > > > > > > > > > Since MAP_STACK is a noop, it is possible for a stack VMA to be merged > > > > > with an adjacent anonymous VMA. With that merging, using mprotect(2) > > > > > to change a part of the merged anonymous VMA to make it executable may > > > > > fail. This can lead to sporadic failure of applications that need to > > > > > make those changes. > > > > "Sporadic failure of applications" sounds quite serious. Can you > > > > provide more details? > > > The problem boils down to the fact that it is possible for user code to mmap a > > > region of memory and then for the kernel to merge the VMA for that memory with > > > the VMA for one of the application's thread stacks. This is causing random > > > SEGVs with one of our large customer application. > > > > > > At a high level, this is what's happening: > > > > > >  1) App runs creating lots of threads. > > >  2) It mmap's 256K pages of anonymous memory. > > >  3) It writes executable code to that memory. > > >  4) It calls mprotect() with PROT_EXEC on that memory so > > >     it can subsequently execute the code. > > > > > > The above mprotect() will fail if the mmap'd region's VMA gets merged with the > > > VMA for one of the thread stacks.  That's because the default RHEL SELinux > > > policy is to not allow executable stacks. > > Then wouldn't the bug be at the SELinux end? VMAs may have been merged > > already, but the mprotect() with PROT_EXEC of the good non-stack range > > will then split that area off from the stack again - maybe the SELinux > > check does not understand that must happen? > > The SELinux check is done per VMA, not a region within a VMA. After VMA > merging, SELinux is probably not able to determine which part of a VMA is a > stack unless we keep that information somewhere and provide an API for > SELinux to query. That can be quite a lot of work. So the easiest way to > prevent this problem is to avoid merging a stack VMA with a regular > anonymous VMA. To paraphrase you, "Yes, SELinux is buggy, but we don't want to fix it". Cc'ing the SELinux people so it can be fixed properly.