From patchwork Sat Oct 7 20:51:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412498 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 077C5E95A6A for ; Sat, 7 Oct 2023 20:51:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFB2E8D0014; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C36D58D0001; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C4D48D0014; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 877148D0001 for ; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5DEEEA02A6 for ; Sat, 7 Oct 2023 20:51:16 +0000 (UTC) X-FDA: 81319860552.30.8235DB3 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf19.hostedemail.com (Postfix) with ESMTP id 78CF91A0004 for ; Sat, 7 Oct 2023 20:51:14 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZboWskx5; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696711874; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=oseJuOnpTrJkQWthxd/UVI/21p++drYvPfVMtJnMKebwb1zKhWoMhKZSA2SatA88CkmVsE lCdwcz4/PA5XaFDRx7YLnwB8VQtg333bV8z2yNl/9AuAecog3qHTSn3pyxxX8IhyldCCtk Cd9grEGzsTec/2ADWysGN8AiBBmX+Qw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696711874; a=rsa-sha256; cv=none; b=x8MCCM0eNNZTN3bt8lVr7wPbWGg5HVAyo3idA15j5+m0rDTDNiggfNUHFjMq+QzX73hrJQ xXbe/uEcO+NVLU9C/gsJg4QCJkhzccaDwdyOAEtS4M07bDTBo5rF5VJXopEv90Ppik5PLj f99V1a0JzkEXznxtyk1xsP4/gAUtmVA= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZboWskx5; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-313e742a787so1948645f8f.1 for ; Sat, 07 Oct 2023 13:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696711873; x=1697316673; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=ZboWskx50rWOvbG2acGhcgPy7QEkYmPxfXmhMRqfKr8JkfrfKY/2vSK4nboARt7ToG qvHK250rNDZxbiFe7HPhMoXHbhuuO2F7m6pyG6HN/r0pTBPAFudQx2xMqTBvpfbZS3l3 YjiIDqVKQc0nYfl6pz5+fD8KZaBEFpGQm6oukguaunkzOsAyQkHiUIRmHTCZwA8GOHRg QsSOwPqWd3PuoOu1yXWf79EgNDd5RPYWumnQjf6XNotBt6YPUN/04mQso/qkn+/6yVyE SIAncfNnOQc9UVO67LPe235aOoEaNLNcmf2mkSO+8AgNd9xhIomC9/8Zfs2PVI3OCgxr nMlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696711873; x=1697316673; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=uqMbF7W/KZym29X7D8zgQwH6bbuH6FH5Jx+Sb0P5iM85ch8mwLkjr9ahbF8gHAYuxW gYROdKpCKc+TZbEZsImjSm6vOgvLi6zIDB5hzBiIfYhdhBa61n2GRkC90zpS1XNhZnqO HwY7z0YxGeOsXWqHtaEODSIydZ25357RYGlUo2mqrfORQe5pDeKQVYDJEu++Kqmx8lZG AS42HjgcZxnta3dYt1GyeJMXD/UGK2V2aj4D2/gQx+94yeKA08EipQkwEsF2vSEtTFoV pe4cGI1IbEZ64Nw4XQFrboq/RTqvS+Q32cB2lm5DBT7zzyuMuT+m03NLYckf2sGrRNYz jP1g== X-Gm-Message-State: AOJu0Yx8haoxG0R5RzdzieKJPwFkF8nC7XiMlMxNGQd5C3lXIcLhK4nj kbUAV9bGbUwrqVB4fRje/MfeUJuHe7k= X-Google-Smtp-Source: AGHT+IEgDkt6GkwlLJ9aqmEH7QCFmGhtBxHyfp4ayq2b9oWhbpAMnmJ+7jpgidaqVh/T1j6p5PGJPg== X-Received: by 2002:a5d:4805:0:b0:314:3369:df57 with SMTP id l5-20020a5d4805000000b003143369df57mr6970810wrq.5.1696711872784; Sat, 07 Oct 2023 13:51:12 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id g7-20020adfe407000000b003232d122dbfsm5120550wrm.66.2023.10.07.13.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:51:11 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v3 3/3] mm: enforce the mapping_map_writable() check after call_mmap() Date: Sat, 7 Oct 2023 21:51:01 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 78CF91A0004 X-Rspam-User: X-Stat-Signature: x7d839ff98zjinttgyqemij37ag5xox3 X-Rspamd-Server: rspam03 X-HE-Tag: 1696711874-101869 X-HE-Meta: U2FsdGVkX18bLhksPwQI521j7+aPxECG24wzrdNFdROxmQWHW/EqTykkdqP4wxxXzFHnElEY74tu85iHeieiq/NcrXKs9Jxjc418e0emW/If97s8HmrUvcW8NNgad6XVarB0rVtITEmlHN1ZtCPem98W6OgSIKGNgtUXwmpnwzXxVP+OeGs+0wtHFnvOzWhXhhqFPl6G3Hk2L5odkFhirtJuc33rsBTVvmX2DGxlTL6gFxCtNzqyvG8M/XwmMDMJflnwmuuNYlPuOL9rHzIoHH64J7QzeReZ1q8tBxbdfae757YVofTctcC60zmyhkna5UTx0J4ML/3B5dyfLYyGI/GMaLElHR80o7TO+10vW2OkTSxf8yrNnG7kBSkF3l3XBlb8lMVKAIu8vOENg+u/8++9jxZCnNRw34adhY5yGZ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5LdbDo0ee5NAhJnrPU X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In order for an F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE in seal_check_write() we must be able to invoke either the shmem_mmap() or hugetlbfs_file_mmap() f_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to clear VM_MAYWRITE. However, the existing logic in mmap_region() performs this check BEFORE calling call_mmap() (which invokes file->f_ops->mmap()). We must enforce this check AFTER the function call. In order to avoid any risk of breaking call_mmap() handlers which assume this will have been done first, we continue to mark the file writable first, simply deferring enforcement of it failing until afterwards. This enables mmap(..., PROT_READ, MAP_SHARED, fd, 0) mappings for memfd's sealed via F_SEAL_WRITE to succeed, whereas previously they were not permitted. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 6f6856b3267a..9fbee92aaaee 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2767,17 +2767,25 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (is_shared_maywrite(vm_flags)) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + int writable_error = 0; + + if (vma_is_shared_maywrite(vma)) + writable_error = mapping_map_writable(file->f_mapping); vma->vm_file = get_file(file); error = call_mmap(file, vma); if (error) goto unmap_and_free_vma; + /* + * call_mmap() may have changed VMA flags, so retry this check + * if it failed before. + */ + if (writable_error && vma_is_shared_maywrite(vma)) { + error = writable_error; + goto close_and_free_vma; + } + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA.