From patchwork Thu Mar 14 09:48:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13592284 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB5F6C54E67 for ; Thu, 14 Mar 2024 09:48:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=b0+QLjsnVmZTUYlWXwX39+B6FBXY1u+nqprlM+JBT6Q=; b=wvFCPhDUOJbh9ujEvwIlgstzbd QX4pOYRejW1TYqunFTxVkY3Yp3Nhv7L5IVKA3TY/xFvakP6tbDWrOo0jyYhDfIoeHG53EH3FnZ/PJ a7+cUvkxL+PDacP1Irm94fz+Z5cQYNFMyyCGXhTunc3E6lvZ8xXKyF6ipUhy/WKKDruoVzKRh4ydu 8Lo3S82efUXO6fJHMOfUFxFCt8ImfzhmaLy6jzXDGLXMgJwslmX+tbTMN+06Qi6Jpk7/FRdOaLiIO 8PaJurRSNr2Dh+oU3+xrXYQ0Q/w1aWZ9l5uSd8fwDuiLRAwAI8471iOKatouj9mI/hV3Cao9h5Y4H krPK5tdQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkhhL-0000000DmYu-1dWG; Thu, 14 Mar 2024 09:48:27 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkhhD-0000000DmVD-44Io for linux-arm-kernel@lists.infradead.org; Thu, 14 Mar 2024 09:48:21 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dc6b269686aso1121362276.1 for ; Thu, 14 Mar 2024 02:48:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710409694; x=1711014494; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+4/aLYM4N8JNPb/EJ7wavg2rPVwaNsOMkVCzTG/xURA=; b=EyfQLsQV05I648Iss70J9PW7d+o3F428ci4hxXPf30dd09h6xnAeCRmTiUdjJ0FuuL mK6TlI1iNXSEUSzNP6BnUi7Vgk7bSxrXmWGjMxbpjXLiBHDaZ5zn9OKrFiCJ+9EtWk42 M0nDa8YXNoBqfRRLG+EjpThpa4fk+QJ0NoCZGYDN9Ppd9j7lTBZWjOxgI//Y+rBMzC3e 7B5zRq7+BEFS3/Bu6b4vu7u47YRYNg7pryMJjlJRafwE43mnuVOg+CsjN8v9YG2kPBCt E1LYWec5cdnTFvH/42eP+CYFzUtN/RSsNSh8rYsTnSfpXhxhivkxshxjs1xQJ8rt+F9P Z9ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710409694; x=1711014494; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+4/aLYM4N8JNPb/EJ7wavg2rPVwaNsOMkVCzTG/xURA=; b=pAyGFKCDv30sbOrxfiV/2Qe56rNZDeckHuBA1URn0JkC87pJJTlYImatdqsOds69hr vt0WP80+KsBqChodNYJXWWBRtneieXqYP6O5gmEBsTbHmbyjmMMladAoCf/11974G4gT lRWfsxh4BH+w1kIuILl7+L9QGSpSG9oypdW4DMmO78PtMTugWStTH4f7h7V9++vMjk2p pjct52Pd7r/Y3d48nbp9Ic2NbUzGLGWhf7sHfjDBt5A9459h8sLpUfhKgFqQLpxgoqWB ABrXG9a5kA1wxWsDt1Py9LfUc5Uii9wjWGCmYSLD3cFliyHBmuC46LVeEdb5mrxLn/ag +rsA== X-Gm-Message-State: AOJu0YzsFcIIXvk9kWJ70+TuZQQVgV9APLesbJjiild3g9Tf9XnzQMAn rDitJTFeJWDFRMHaEK3OaDxlAPZMHowq1l//kkwOcxhROO2/9VCgHYp4iua8xThLJZf9kiwx208 kyNJs0/oejKugYMOs+AA8ZNPnmpjZ7DLe07nHgFc3zJ5gsy2hbPER+qS5Vf0UJVnhQ+sGqS+7QK 36GcMbksSmypWaZp0auhu860nT2mjTk8WjwZXnrAhs X-Google-Smtp-Source: AGHT+IETPWni323PP68CZpSF0pMKhFMeIzk91FfATRwIb8x7JZrYtEUbMmvjn1OWGscSUXdCazLs/gSR X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:2207:b0:dcb:c2c0:b319 with SMTP id dm7-20020a056902220700b00dcbc2c0b319mr31876ybb.9.1710409694080; Thu, 14 Mar 2024 02:48:14 -0700 (PDT) Date: Thu, 14 Mar 2024 10:48:06 +0100 In-Reply-To: <20240314094804.3094098-4-ardb+git@google.com> Mime-Version: 1.0 References: <20240314094804.3094098-4-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2180; i=ardb@kernel.org; h=from:subject; bh=1GE6vVhTOFDDzbKAgOO3+WF4jIkCJnmX8AMoDy9PNg4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfXT8WsLOV/FOv/l+hR8tDBMSoh7iXhImuwEoYuS869cm HBpv4dBRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjI1UqGf4Y/GHI237vv87z2 wp/iY05MCYyLm9vm7k+dVyOklcGRepuRYV2ua6rYH8Fn/7rzyrfGbmpemb6W5eqql9sk3l14fo2 7mw0A X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240314094804.3094098-5-ardb+git@google.com> Subject: [PATCH 1/2] mm: Add arch hook to map_deny_write_exec() From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Joey Gouly , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240314_024820_180247_772610DC X-CRM114-Status: GOOD ( 14.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ard Biesheuvel Prepare for WXN support on arm64, which disallows mappings that are both writable and executable at the same time altogether. This is less strict than MemoryDenyWriteExecute [which disallows any transition that adds PROT_EXEC], and is not configurable per-process so it is not a seamless fit. So reorganize the map_deny_write_exec() check so that we can deny W+X mappings based on an arch defined policy, while only disallowing other +X transitions if MDWE is really enabled for the process. Signed-off-by: Ard Biesheuvel --- include/linux/mman.h | 32 ++++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/include/linux/mman.h b/include/linux/mman.h index dc7048824be8..de2e2f8b39da 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -162,6 +162,14 @@ calc_vm_flag_bits(unsigned long flags) unsigned long vm_commit_limit(void); +#ifndef arch_deny_write_exec +static inline bool arch_deny_write_exec(void) +{ + return false; +} +#define arch_deny_write_exec arch_deny_write_exec +#endif + /* * Denies creating a writable executable mapping or gaining executable permissions. * @@ -180,19 +188,25 @@ unsigned long vm_commit_limit(void); * * d) mmap(PROT_READ | PROT_EXEC) * mmap(PROT_READ | PROT_EXEC | PROT_BTI) + * + * In addition, permit the arch to implement a strict policy regarding + * PROT_WRITE|PROT_EXEC even if the MDWE prctl() is not in use. */ static inline bool map_deny_write_exec(struct vm_area_struct *vma, unsigned long vm_flags) { - if (!test_bit(MMF_HAS_MDWE, ¤t->mm->flags)) + switch (vm_flags & (VM_WRITE | VM_EXEC)) { + case VM_EXEC: + if (vma->vm_flags & VM_EXEC) + return false; + break; + case VM_WRITE | VM_EXEC: + if (arch_deny_write_exec()) + return true; + break; + default: return false; - - if ((vm_flags & VM_EXEC) && (vm_flags & VM_WRITE)) - return true; - - if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC)) - return true; - - return false; + } + return test_bit(MMF_HAS_MDWE, ¤t->mm->flags); } #endif /* _LINUX_MMAN_H */