From patchwork Tue Jan 23 14:53:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13527728 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 56ECAC48260 for ; Tue, 23 Jan 2024 16:01:12 +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=QOtNPJWKYhZqAMOa0wDTZBTm/NC3tZ+dHFl1n2S7Je8=; b=W44cZGO8o33dwqiL8KcIQ/p9Dq 99a6XWEzrcIISETQZJm5gwOVo5tB1Stl6ioLsLzEWorpt0Qbpkse05GMCKVR8wUOrA4KgHyuik/vW yeDMxzrLHoK/MYa02Gq4238TMcsOWyGcaJ7A7l5uHXxfucxubz9/cgHJCPVX3TsESj31geT8lIWkO tsneNFACFsyoiVE7mV+XHoTdfFLO5OpQT1p0KQveb6wmqr8h71NVPXgRJhvzOKh/qPQpqT4zikoss t5ByHTEJyn/yjyZzMlhyR2h67dMpa3pEmuUp4AxZ43Zk6mOjnOqW+O9krEyAB43/6vxd4SYG3q8VO SeTFR/CA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSJCZ-00HAxr-07; Tue, 23 Jan 2024 16:00:39 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSICE-00GuXU-2L for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 14:56:24 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ff817c96a5so65567247b3.3 for ; Tue, 23 Jan 2024 06:56:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706021773; x=1706626573; 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=6ImYLwyRlzjAOW9Qb8r8VqsBk7uC0lvTL8XrDobDD+k=; b=b3ftRPz6xy3NwK++Q6kyJZIpK4Jiw3cn1LR+g9WwW6NOjaR9FcQVdk+GTE1PP04puH zIyajkCYb/1dQixhOb5CTDDLsAFAfMG7+aQNtBumXzab/MJsgqZODXbzq5LUezfpZ2pS BShw2TPZk8Tcpt7HRFJCpSKra1GziXJnxNu+Kdm09WCdbUB2suHyloB7TLgP1pfDvQAB d0s5CJguWrJWL5cg3PhUdXgC1GDzbMjMpI+ozda1q+IzHs5i1fZGkIQRSIYO/SQm9DjV ERoUezvT0jqZsvB98wT/RcrpkfLPTaL7sF+PcMoibW9kh1zhD+xZXH4znoNW5wC85RTH 41Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706021773; x=1706626573; 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=6ImYLwyRlzjAOW9Qb8r8VqsBk7uC0lvTL8XrDobDD+k=; b=kBJQ8BT2/FXOvhkEZzeC4SUn7wTvOlWlIuqxHstuf1nuOVSaAhdO/CLvHcpZ6NI+l3 PzAeYDYRKIyf+z4d0FOqs/JwoKtCzyB2ViZufvZ3VHBI9C4awS9212v8K/DeTmYpu2qF Uyg50C80iidCDPjDeaApwWB9Q1/N1/tbO6bLss0VMKWtSzxPcEOAJ9bserPDwc3AagFC GJFw8faP2GY0OzOtXTatWwxiejd9K9Q+HBmbbQ4rCmjXzJQVwAYQVL3H+l0WX6pED/CA l/aB0x6PWKw23SAK4vRjpcRyW3+V/LDsPsuS6QfGmeWDwwFbGw+j2S69qPeVq4UclyYN tdJw== X-Gm-Message-State: AOJu0YygZQaaxvHJ3OcXsvfAU1oJMNoMzXtCNULCsYrfMzqmNeplNN5Y AmcKwWj2Ofr4dHKitIVNPTNdBa5DLPV3+itJu9Hhas+YVsgULD8lT5TkYxfgOGnTUjtSaUokXzc vGotDLA7MrnATiSCGnPcjmn4BUzmJAS61696TkwUc3ZbLEUwuoA7qdJnSU1d/iw1Io4Yh5EFDNH vgRKpslmWBe04PouQTYNigXX55AaCbFkggVuh1swHr X-Google-Smtp-Source: AGHT+IF212qQ+sz7tw0/MhvsgaVjN9xcMHISB/KT+pl81RPWNOnRbXxwX2WalkovnUo0nRSFigMkkWCd X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:1b12:b0:db5:4766:e363 with SMTP id eh18-20020a0569021b1200b00db54766e363mr2616552ybb.6.1706021773273; Tue, 23 Jan 2024 06:56:13 -0800 (PST) Date: Tue, 23 Jan 2024 15:53:47 +0100 In-Reply-To: <20240123145258.1462979-52-ardb+git@google.com> Mime-Version: 1.0 References: <20240123145258.1462979-52-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=u8l6jWRluTgDlURmlI8+ytPiGtIliUrIw3crfdugoiM=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX9pf+uLV0yvu7Fyyz0OOIPTn2aW7On3E3q993FVlvnT ley3H62o5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEwkrZaRYfbyB8xrX3xVv20m tb5t2pl3ApvFWrcrHTPhejGfoVdkzXuGv9LCaZZHOBKniv1rtP9enJr8uvOY2M5PHxZa3+h5xel xhQkA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123145258.1462979-100-ardb+git@google.com> Subject: [PATCH v7 48/50] 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-20240123_065614_791216_465BD1A2 X-CRM114-Status: GOOD ( 13.48 ) 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 40d94411d492..f5d8c714b4bd 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 b78e83d351d2..a20871bac792 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;