From patchwork Fri Jul 1 13:04:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 12903287 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 58E3FC433EF for ; Fri, 1 Jul 2022 13:09:35 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=dDUqhgkcapNQLoQyPIpdhafBob6PtH7b5NK3bV3aSGg=; b=ldQhJXr6iiSvSD sD94L7yKaLMep/ApWZaaJK4bUsQJYeUMOsPlugiSsJQBo/gnBrT2sQ1DHXXKU3FZb148EkXxVjKmv 1cbxZ3wvYKTyd2j2w6gWCsdiQIdkhgU4LsBTcPLWAtRSuFUu+nhrXjY9GyZw0ybGX6u7P73a/1OIV cUdM605Y0cEr8ZUI2QkZ2PZ89nIBDIDT8G8ReTgbqAZ31jR55pGGgtEK3sqkAK70jJlqnntkRE5kk MtUZMT6kZqwpBz5CLiqNsdX39h/afuPDGcioiVIxCkxIt2p3Bt+VeUPPsD7zAF4vkDvuBkc+7k+jI UUhYWOUIHco26rCBOSZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7GNk-004xBj-M0; Fri, 01 Jul 2022 13:08:25 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7GKi-004voC-Oj for linux-arm-kernel@lists.infradead.org; Fri, 01 Jul 2022 13:05:18 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4D73D61EBF; Fri, 1 Jul 2022 13:05:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0F39C341C8; Fri, 1 Jul 2022 13:05:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656680715; bh=vSImTKiWEzOluhjdBMlo/HqDlfFAkeGRWjESVXcecOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YfNBfTOmXmRP+DkcBuqLlJAQ55SjFntb3X4GoepaLqTQwp0vqnqzXoVD12KUCF8Sd TpIvhzALiU8mz95FAYfyvEbKznYMG5Tzob7jpZBRGowzmPnl9Yrr2hDpzBdYPmiLoD QUB+DzjVKmWwFtjgsZXPqrABUIqzKVLh91Ehs6gLE4qy1KtWbB69x5vjtk8oYPG6Vq dbO1OUbn52JvIdr9x6eVzomftsSbGVFFfeEe00NSBpCj/DFejv1bNFNXjHFTGwiiRZ 7HSzGMtUdsR9pRM5aPcO+ui72Q8p6LNBgVc45m6457slwz0n2H0sjVhS+x6cgJhaCu cwFrySl+1/1bw== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Marc Zyngier , Will Deacon , Mark Rutland , Kees Cook , Catalin Marinas , Mark Brown , Anshuman Khandual Subject: [PATCH v6 8/9] mm: add arch hook to validate mmap() prot flags Date: Fri, 1 Jul 2022 15:04:43 +0200 Message-Id: <20220701130444.2945106-9-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220701130444.2945106-1-ardb@kernel.org> References: <20220701130444.2945106-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1729; h=from:subject; bh=vSImTKiWEzOluhjdBMlo/HqDlfFAkeGRWjESVXcecOc=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBivvDpIQGjGZTZYWBpUq0SPIGva2/C315poCJG0cvs 6zkIauKJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYr7w6QAKCRDDTyI5ktmPJOe7DA CK6YE8gqgKQNfHZi/RS/Xt1CsBEr6Xe4bcBgY7oNlaIu7Xac55OyvAlDbiyijSLNvFoyltEtzxr/fk 8xDZ2r9p9W3S7NfyfsSDSvIG1GI6V5beytk3KzZycE8AANDMXcdYXE/X+shKlWq7cUa92x4L+07CNs MrxUJqzmaBa0040ypxaOH7d1fAPWwhBQTtxPg4l3kJQ7EjZLZpza8SjGUZRbjingCANzQRgtZ9HWna j/zte4DcoY53+bZAIXphgmAGjZJhV/m1m44pB60+VUG3UyscQiC8BOlSFK1Wj4VN+C7zbLqwBBjiDe G2UKFXklL0Ux8jBD5GGDn/M0Er1DVCoByx0e8AMneBIdS0mT2CkrdJ+uCh2yGbaUH4xteSYor1Cm3X A/zfdeewrW8t3i9++KNp7jj8hk+9+tEQPKxEa88sjp/eWy/IBtS/YSuPZOKVQnxVtblMugJk4adK3s iB6rC2uO7hOF6EtdBiErlzhJyfVyUxhx+1JXdf8sHT0mI= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220701_060516_949136_DC0E7AD8 X-CRM114-Status: GOOD ( 14.79 ) 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 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 58b3abd457a3..53ac72310ce0 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -120,6 +120,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 61e6135c54ef..4a585879937d 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1437,6 +1437,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;