From patchwork Wed Feb 14 12:29:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13556557 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 C3B18C48BC1 for ; Wed, 14 Feb 2024 13:46:13 +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=E8O7y9DabZnbMNBDtN5KSVbARFSDbuxYxNMnwl8zgKc=; b=Cwd+94GPDeNeuGSEAB28c2Lh4Y 9bQ2CGYV3UxmlYJo2TZunvOfmdg9HmWh64ksjnXchddc64/aGw3GKoz0jYVt1HWPJ1hoKmmpNHp6A bGRl7vr1fe7B66rhryH5p6JkqIUyufnou82e7i8uoRyB+VgWw/Lh2qagESlwH2ptVA10wT2VCCv1X gDXHsHfmxjMLw6+VNMpcM1crWVS2f+cnP+wu3WtkrN8qFicncAOQK5yvt23CtfeUdyCVwfnoyNEcr 6eZHm2w3PeNOR8QZM+QoLYPNqwzKSIFjpXcKwtygVOTOZ8IB/ieD0XM2jhVv8fF++O3pnkr1a6sUY +WnybBUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1raFaI-0000000D3q1-3kav; Wed, 14 Feb 2024 13:45:58 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1raEQ0-0000000Cp9w-3zeC for linux-arm-kernel@lists.infradead.org; Wed, 14 Feb 2024 12:31:29 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-6077e501cecso36096957b3.2 for ; Wed, 14 Feb 2024 04:31:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707913875; x=1708518675; 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=TB0o3w4wUdMFbdJIxgmVT97gCq8dSmpzSBeokdjbF68=; b=UsVR6UVsXXpclykt2BKf/piuVRdxAXjRW0UKelwlckY+GYEpnWl5Ai4u/EKInqCHIN X4GSqUHuc+ID3kxxvS/OuQMro5MM2FgUfjZUelDKCAiU2B37HlfE+qG90PvxL8014LqY eWLxzCtZ2FQk0DqVHCegS+xp6kmNvkm+vjIKAoS+IqqrO0zaCQJH4gH2+1NC2ItX1reE iwaSDJQTaB4EdZzzqt6kzNP7Xq6rY6yPuU5YFO9/XBtKlFQj8bLx7RvNqS6fCTZsoVpg DixcKQC04eRtatPpq0DXRzQsNhufmz3ssIk3gJKKhOLRHNvcSPB+95Lan7YZMt9k1Vti W/Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707913875; x=1708518675; 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=TB0o3w4wUdMFbdJIxgmVT97gCq8dSmpzSBeokdjbF68=; b=raFboNmaAOGl21hp860ZJ1NnVuSt7KEJEYkA56JMmpd86uEPBshzzXpvJAymbC9gjF GKqnynY7LcGlq647GTLJHCIPZU4CV5kh/ALzWLnVgUITgF7nTFpDfWPcQ5h2y5Mswef2 V/Wh9C9ZKUQ0yoBK/xqRggtaGMHivNYMNz56Djlbfp+vRZOfuA3V2haFaYbBU74xbiIc 60680BYMMpTdSmXFGjXHxECZM3ez0tPqgLP+rqrIna7qvh4M2Z4y2R9bTWxV4MsRPpoP jTNN/XboXDvTQZwUGaV0tCAQ1iSyxUQRt4QQIU+PI0aST+QGwEAI1ARbm1ACdIAv4ZiN CYcw== X-Gm-Message-State: AOJu0YzGnQjBJfXbqF/qgL0SwVkVd+UJGk7akU0d5HamDcTEEK6R2cxz MZbdCSdGIamncb54YeClW1fKkrzdsJC7zPlL21J8Z+zhtl5Uyq3l+vEP5VPZmPL2Rfsp9OCb4Ib CS9dNnEH0LNITamgw91IbHy7SSXDZuGA+Ja6zxeJNN7uViZculxObyYSKIp3RDmrHatq5i2F9oc KBj080pC2ITbz4D92Kt+YYClejmyaBPQK9zrfiC3eT X-Google-Smtp-Source: AGHT+IF2VE0jRm3VfVnsHLjnA0z07HJPbfI+JkMBEAUN+5QvUp4ZA6LVsMsAGXxVOhuWZUfakfvLkEoY X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a0d:e60c:0:b0:607:8baf:836 with SMTP id p12-20020a0de60c000000b006078baf0836mr544417ywe.4.1707913875616; Wed, 14 Feb 2024 04:31:15 -0800 (PST) Date: Wed, 14 Feb 2024 13:29:28 +0100 In-Reply-To: <20240214122845.2033971-45-ardb+git@google.com> Mime-Version: 1.0 References: <20240214122845.2033971-45-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1750; i=ardb@kernel.org; h=from:subject; bh=BWZDj4BWSzj9d+Ke8suXqeVvJtUPu6Dg0TXbGvgFUNk=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfXMJu1IjuOaN433dK9V5oxbO1tlDpu0qqiaW1uPUcJMX of6tX0dpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCJ6jxkZDnKqc07P/3GR797E 5E9Sr9876Cj/ZHK3lxN/en6pi4jxLkaG8+0q8plbc1iuWR43SV+4Ivrix6KjKdst78xUOLJs84e 7zAA= X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214122845.2033971-87-ardb+git@google.com> Subject: [PATCH v8 42/43] mm: add arch hook to validate mmap() prot flags From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , 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-20240214_043117_205407_72545679 X-CRM114-Status: GOOD ( 13.49 ) 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 Add a hook to permit architectures to perform validation on the prot flags passed to mmap(), like arch_validate_prot() does for mprotect(). This will be used by arm64 to reject PROT_WRITE+PROT_EXEC mappings on configurations that run with WXN enabled. Reviewed-by: Kees Cook Signed-off-by: Ard Biesheuvel --- include/linux/mman.h | 15 +++++++++++++++ mm/mmap.c | 3 +++ 2 files changed, 18 insertions(+) diff --git a/include/linux/mman.h b/include/linux/mman.h index dc7048824be8..ec5e7f606e43 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -124,6 +124,21 @@ static inline bool arch_validate_flags(unsigned long flags) #define arch_validate_flags arch_validate_flags #endif +#ifndef arch_validate_mmap_prot +/* + * This is called from mmap(), which ignores unknown prot bits so the default + * is to accept anything. + * + * Returns true if the prot flags are valid + */ +static inline bool arch_validate_mmap_prot(unsigned long prot, + unsigned long addr) +{ + return true; +} +#define arch_validate_mmap_prot arch_validate_mmap_prot +#endif + /* * Optimisation macro. It is equivalent to: * (x & bit1) ? bit2 : 0 diff --git a/mm/mmap.c b/mm/mmap.c index d89770eaab6b..977a8c3fd9f5 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1229,6 +1229,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!(file && path_noexec(&file->f_path))) prot |= PROT_EXEC; + if (!arch_validate_mmap_prot(prot, addr)) + return -EACCES; + /* force arch specific MAP_FIXED handling in get_unmapped_area */ if (flags & MAP_FIXED_NOREPLACE) flags |= MAP_FIXED;