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 ABA0CC77B75 for ; Wed, 19 Apr 2023 01:45:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 174BD8E0002; Tue, 18 Apr 2023 21:45:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 125388E0001; Tue, 18 Apr 2023 21:45:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F2EB18E0002; Tue, 18 Apr 2023 21:45:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E51AB8E0001 for ; Tue, 18 Apr 2023 21:45:41 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AAF8E4051D for ; Wed, 19 Apr 2023 01:45:41 +0000 (UTC) X-FDA: 80696448882.26.0FBBF7D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf10.hostedemail.com (Postfix) with ESMTP id BFD62C0016 for ; Wed, 19 Apr 2023 01:45:39 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h75yaWRv; spf=pass (imf10.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681868739; 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=uxbho8QxMR+zxIIpkOmO13HkVCORBZijDHQqdGD2kSE=; b=eOAZlC/UutmiuDVB6LozmoOF28gFQ6En/C2H+Bf4LxZauCMXG+jBY5WHoio39ubDQeur73 M84REJjYWVH7qZEQ2CYvVNv/RGBmUXlINkONRk2gSH4RGCtWilKrFfU2+cUKeowJ2enMlL SyIxNf2MDvuLRarlaMBvD4U99Ppux4E= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h75yaWRv; spf=pass (imf10.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681868739; a=rsa-sha256; cv=none; b=idbepLfk0vn7fJg6bGW6aAFk3YlfVACUK/9q6y+5++Rz6sQHWFhqKfKTlQFYMH1S2tLo99 a6KiP2OvoSDwB5vGdc02C2inoV2P6R36Ux+9HEH+1tHXCNFPMWsO7LsKMraQz4zXk8NA3z hyGlQz2goLFqAIJIo9s255oNHVWdY0s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681868739; h=from:from: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; bh=uxbho8QxMR+zxIIpkOmO13HkVCORBZijDHQqdGD2kSE=; b=h75yaWRvDFZkOF1sv3V1ukD5DEuz9EBAPTkWDmoGLB93orQyCTaWXKybOWtES7u3XFJTUB 2rejyChz3RAlEkb7fxeyqzns9JYXHngoqLFuztJt8smtzXeiNVgAcPQbnqmK8m5REhvp1v 2PNbGmsksSFGHG4kdkTqtDWlOq3v15w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-499-nt_oy41jNOyHJiCmN5TsSg-1; Tue, 18 Apr 2023 21:45:35 -0400 X-MC-Unique: nt_oy41jNOyHJiCmN5TsSg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5E2E68996E0; Wed, 19 Apr 2023 01:45:35 +0000 (UTC) Received: from [10.22.34.98] (unknown [10.22.34.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED1485AB7A; Wed, 19 Apr 2023 01:45:34 +0000 (UTC) Message-ID: <22aee5ea-dd6b-ac2b-0b28-a25ee6602b48@redhat.com> Date: Tue, 18 Apr 2023 21:45:34 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: [PATCH] mm/mmap: Map MAP_STACK to VM_STACK Content-Language: en-US To: Hugh Dickins Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Joe Mario , Barry Marson , Rafael Aquini References: <20230418210230.3495922-1-longman@redhat.com> <20230418141852.75e551e57e97f4b522957c5c@linux-foundation.org> <6c3c68b1-c4d4-dd82-58e8-f7013fb6c8e5@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Stat-Signature: aeskgboqht4k3su54pkfuyghhoaedqni X-Rspam-User: X-Rspamd-Queue-Id: BFD62C0016 X-Rspamd-Server: rspam06 X-HE-Tag: 1681868739-928194 X-HE-Meta: U2FsdGVkX1/ASS86Azq6ga7lOEATwzKmXfta6/ZwdHl5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tofTmLbx0e9BoHYyp8QDIdI8fN8RDzh5BZp8lP7A0RSRodGtiifA2ELpsQO1jGh8IH+EcMvnS/eDjleFeb09saEUw/fHVLliTJJfbGM15KzlwXWYZDQ== 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 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. Cheers, Longman