Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp415881rwr; Wed, 19 Apr 2023 08:10:01 -0700 (PDT) X-Google-Smtp-Source: AKy350ZMoNW1dofBNWXMpbFG79YhMefqIKNzYdKoRLNNBJB7T+NFR0r3sbxb5a79hI1pcrF6mBtu X-Received: by 2002:a17:903:230b:b0:1a0:57dd:b340 with SMTP id d11-20020a170903230b00b001a057ddb340mr6611932plh.64.1681917001217; Wed, 19 Apr 2023 08:10:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681917001; cv=none; d=google.com; s=arc-20160816; b=nQwAjgBgNIEhpyVaMnFBG2LLWLjZu5eKO1cAhUirB66hmSZ6X5UIbVyRGPFzdV33Kt 1m6WdOFf08YDspmK1E/S9FbdfasGuL0XQ567FxhY0Ygj+Gsdju3Dl5XQjHHQkcJ8zT20 huLmQORfyJreZarVGiHZMwLBUW7o44liO+9j3HuILMD600Iya4+Od9Xm/tiom8otvjAF Dkh8EpYgip0iSjwmpVqdmgmlLmICl6yLXpHcYxjy+77QL955d78tDn9bbce34Ruph8mH PR4Hc3wOxuUVbdmTZ0ZaCOuXBw6uIuNPTFIN7q2m5UQeIcqcjQeQlXi8HdgqUyeST7D6 NuLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=teQjVnXlf4lhJ2o/juCXqre3cPfSTFtFwCFRcevwxZI=; b=ETcVcL5ozwjRVI0YQM60wCIqx8IZiyFptWJYH2ub/62okKbZ53UxoQLLnSht1IrBbz z6iecJMHCJvwYkL5wbvQdUBYdjg3fa+z8pbz5z1e8aKvlfSsJ3zttFhbOnHsyo6bC4RA giK6DZwdfDHsTMDtLQmPhCn8UyHgyufx9+5cewOZ+dJnGsOijEoNOcjxhbwO3WvHMPai 23fzFSF3jQf+LqnvGaVx4K8ZS5yKOZ4oxyrtaHk+h45nII9xtE2m7vKxB4NmnEgcIl9g Y1pHf9y85W1Aa0buedR1cdPvNwuljI1Vjf/7Q0IPWVY/eIEu/x3lnD4ZU9RDMU+HGDw4 FUpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=wUQFZqIU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t7-20020a17090aba8700b0024b289164c4si1157005pjr.52.2023.04.19.08.09.45; Wed, 19 Apr 2023 08:10:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=wUQFZqIU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232876AbjDSPJc (ORCPT + 99 others); Wed, 19 Apr 2023 11:09:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232441AbjDSPJb (ORCPT ); Wed, 19 Apr 2023 11:09:31 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E18786EAF for ; Wed, 19 Apr 2023 08:09:08 -0700 (PDT) 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=teQjVnXlf4lhJ2o/juCXqre3cPfSTFtFwCFRcevwxZI=; b=wUQFZqIUf/pqaoiQJG9nvBNX+y Qxvgt50FfQQGBOCCBu+lTnAqjnf9YtuZ9aELBmolZz4qGeQ3g4f4XwoO66H8+/tQqIb3XGfJtolTT Ft+UHLKDj0ZCAHmRXZylE5kheLXu5K00Tgt6iKg+UO4MLmx9eTiUQ/vqUgTjkLzI1z8XzlEtsJouQ wt6SFj1V1gM2ynVtAbTMzY9A9CLM33LayRnCibN+3l0sN3nvGt+tE9YJ8HhQw+k0/XoPQb9Pdw+1J zseT6LRXDg5cTiO6JBjBisZKWm++RndVUl3hYPug39y7rIxiGWZ4sNWQ3z3Fdd8L1NoTCz45JiZuc gNpbzrHw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pp9Qc-00DLXX-HQ; Wed, 19 Apr 2023 15:09:02 +0000 Date: Wed, 19 Apr 2023 16:09:02 +0100 From: Matthew Wilcox To: Waiman Long Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Joe Mario , Barry Marson , Rafael Aquini 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> <9f92d530-1cfc-6e50-a717-321ac64ed1c2@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <9f92d530-1cfc-6e50-a717-321ac64ed1c2@redhat.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 19, 2023 at 11:07:04AM -0400, Waiman Long wrote: > On 4/18/23 23:46, Matthew Wilcox wrote: > > On Tue, Apr 18, 2023 at 09:16:37PM -0400, Waiman Long wrote: > > > ?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. > > By the way, this is a daft policy. The policy you really want is > > EXEC|WRITE is not allowed. A non-writable stack is useless, so it's > > actually a superset of your current policy. Forbidding _simultaneous_ > > write and executable is just good programming. This way, you don't need > > to care about the underlying VMA's current permissions, you just need > > to do: > > > > if ((prot & (PROT_EXEC|PROT_WRITE)) == (PROT_EXEC|PROT_WRITE)) > > return -EACCESS; > > I am not totally sure if the application changes the VMA to read-only first. > Even if it does that, it highlights another possible issue when an anonymous > VMA is merged with a stack VMA. Either the mprotect() to write-protect the > VMA will fail or the application will segfault if it writes stuff to the > stack. This particular issue is not related to SELinux. It provides another > good idea why we should avoid merging stack VMA to anonymous VMA. mprotect will split the VMA into two VMAs, one that is PROT_READ|PROT_WRITE and one the is PROT_READ|PROT_EXEC.