From patchwork Sat Oct 7 20:51:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412497 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 17DD2E95A62 for ; Sat, 7 Oct 2023 20:51:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58B678D0013; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E3908D0001; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BEC08D0013; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0D04A8D0001 for ; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C9A8BB469B for ; Sat, 7 Oct 2023 20:51:14 +0000 (UTC) X-FDA: 81319860468.10.CC8113A Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by imf29.hostedemail.com (Postfix) with ESMTP id F15AC120003 for ; Sat, 7 Oct 2023 20:51:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AvvUmf7r; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696711873; 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=OgUbWzhgbQTh2Tsrre/VRjjQqlOhd22889mYfV2Y6PM=; b=0HnDHFgU/mUz3RDKM5lx+RryB1mTShaQIO2JfPyhw7H7O3Hcp6IMnaiG8T/xHTBjdOJliu bgDBnMKB6P7eyirybbLNd29CG/rB53QbA8qaaXarb0kgsuhNc1WJG1+VuGxRvgCjhOgHdF XwZqqwm/dcqSQUjR+/q+Abpdx/Sow+M= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AvvUmf7r; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696711873; a=rsa-sha256; cv=none; b=YQ02eC0Ep3Un194ykFcyw8fw46lPsDzwePOGniqU6wF4UCudjVqzCVmt0yxFiFQ1Yh7cbA dj4GIJDiHbSPbibOQERgFlw+VhjiyjPl6muhyYNfE+EQNXtkb8886j27ZdfC5PL5IlS9S7 7JKWcSdLR/1+A8cH+V1ObBKSojJW0mE= Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-3231d6504e1so3016983f8f.2 for ; Sat, 07 Oct 2023 13:51:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696711871; x=1697316671; 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=OgUbWzhgbQTh2Tsrre/VRjjQqlOhd22889mYfV2Y6PM=; b=AvvUmf7rrV2/pWxViW3ezxfmXuk/yUS92gdBh1+Bxp1okJND3dPw44hRSec+9rjqYO dquar6zAvuoeXmGyz2gGTReQqvg/4tLtGQh9WznkbSCB1614u/iwrwr8GFUf9/PXj/S9 /ed07//r00pdl3j1baLhUsr5ip0QZJN5aD1P9w/QZb/H5xKYSFNSvT31JCSggOFF3zDL DUsOuiiNZsDY0XCl/fTJJ/0OgJjPRJBhD2KdLvaRG7EGnK6JFzehu2u2DDX300XbSvdo XTUvnVMD3mMU0/rL2aDU49o14JhjPpXs12H8jgPl50wWaV/ssoRJJvw0IgDltC63RI+k 9+YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696711871; x=1697316671; 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=OgUbWzhgbQTh2Tsrre/VRjjQqlOhd22889mYfV2Y6PM=; b=GgomrMi78az4hZl5zp6Vwn804TPt2ywnmfJlOG3CeZEZ8fbqf0i+tcNcdumM//8Mma OC2rLWbjhWYGwhZ8IrH0LqLqW7GBW1+Se7AN/5MTmwHJX6FKzGbfdUYYjCoA+h1nqgh6 E0iEW8mR5jFVzjL2LcBzfbT1wqJR1/sb/uBcEmd0bZ6DZ+z3wtszfVgN1GJS6gSDZyfS bVzyyFkbgYEX+Fxf0CiuGb5vCfAw6uEYrNcjgc8jLcJOuf6qg0FxM44m6veEWkKBmJJz +lmRWVZ3JIQTJLv3MTt9oVCAoteyjjxvNXcThBzo34i2CbS9Te0Ty0F96tcnC20V/qHX Z0XQ== X-Gm-Message-State: AOJu0Yz/KwNl9wBgzCjbeT/4U3aUN828JT+QiInBcQ4c7M+AnH9hWSsX XjEtLlCVC4uCTrAoDD/8bWBAQBRBxfY= X-Google-Smtp-Source: AGHT+IEXmITz4RMxLO9GqG7a0wyR3fIBUWZzSpv/XJYSngX65HmU/52/H9zjUYWovmJKhPLVZlC06A== X-Received: by 2002:adf:f005:0:b0:326:c623:3bbf with SMTP id j5-20020adff005000000b00326c6233bbfmr11078774wro.26.1696711871254; Sat, 07 Oct 2023 13:51:11 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:51:10 -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 2/3] mm: update memfd seal write check to include F_SEAL_WRITE Date: Sat, 7 Oct 2023 21:51:00 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: F15AC120003 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: f6a8cfat6kndqbpyrmnebyf9giutt657 X-HE-Tag: 1696711872-684908 X-HE-Meta: U2FsdGVkX1+GDm1xu4U0cSjc24EL5qXnBuf2sEny8Ufjx0G+dQ0oAqzZ6XK7FFGzWTEG1bUffoV2+VVZGfS6IG4Fn/ovjfO9Luev10oCUD5ohtEokD7use9A4N2A/qk5s1dlr4mWUR66m6BBZ3Noy/obrEIpyaOlVg9pv6yR2zeglfXkD7VvGBrUEloAFYQ2K1sCPYeOE6b0NKLgDM2a5nUwrmiBC3g/8l//Us5jg8uNbXX6txRVUkj7mHFxq/wG2J0q7HFYLOU52K/JdJyzTMDnbcFscf0B6HEXJmZpTQl6XO86dn/dZ/rLZ6RJ9WchK8X5+2Lw0KXOoXU6TP07oBT3wcV79fwhtW0jb252PtfojT/L3Er6M8HzIv4sDjHSbNWgeuZD1jjyHog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ycTQHJ8 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: The seal_check_future_write() function is called by shmem_mmap() or hugetlbfs_file_mmap() to disallow any future writable mappings of an memfd sealed this way. The F_SEAL_WRITE flag is not checked here, as that is handled via the mapping->i_mmap_writable mechanism and so any attempt at a mapping would fail before this could be run. However we intend to change this, meaning this check can be performed for F_SEAL_WRITE mappings also. The logic here is equally applicable to both flags, so update this function to accommodate both and rename it accordingly. Signed-off-by: Lorenzo Stoakes --- fs/hugetlbfs/inode.c | 2 +- include/linux/mm.h | 15 ++++++++------- mm/shmem.c | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 06693bb1153d..5c333373dcc9 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -112,7 +112,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND); vma->vm_ops = &hugetlb_vm_ops; - ret = seal_check_future_write(info->seals, vma); + ret = seal_check_write(info->seals, vma); if (ret) return ret; diff --git a/include/linux/mm.h b/include/linux/mm.h index c9e9628addc4..51a217ed4d1b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4027,25 +4027,26 @@ static inline void mem_dump_obj(void *object) {} #endif /** - * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it + * seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and + * handle them. * @seals: the seals to check * @vma: the vma to operate on * - * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on - * the vma flags. Return 0 if check pass, or <0 for errors. + * Check whether F_SEAL_WRITE or F_SEAL_FUTURE_WRITE are set; if so, do proper + * check/handling on the vma flags. Return 0 if check pass, or <0 for errors. */ -static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) +static inline int seal_check_write(int seals, struct vm_area_struct *vma) { - if (seals & F_SEAL_FUTURE_WRITE) { + if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { /* * New PROT_WRITE and MAP_SHARED mmaps are not allowed when - * "future write" seal active. + * write seals are active. */ if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) return -EPERM; /* - * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as + * Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as * MAP_SHARED and read-only, take care to not allow mprotect to * revert protections on such mappings. Do this only for shared * mappings. For private mappings, don't need to mask diff --git a/mm/shmem.c b/mm/shmem.c index 6503910b0f54..cab053831fea 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2405,7 +2405,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma) struct shmem_inode_info *info = SHMEM_I(inode); int ret; - ret = seal_check_future_write(info->seals, vma); + ret = seal_check_write(info->seals, vma); if (ret) return ret;