From nobody Mon Feb 9 00:19:43 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 922344A1D for ; Fri, 27 Dec 2024 01:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735264338; cv=none; b=a/AgZqNTE9UbX+TwxkZkFVpducI4eo0peVVSsfOkBxY2APUkHPY7MJbG8u4V95y7rl05yeKrPLoRgBYZEdiJsaBzr2cGFn5xG/kuf2VY61KLqxx5HD+/cRMmvpjOL4rEPBT0wXkAXlBXBSanWwQFHvbBS9HRrKi3hwuCt+RD+aM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735264338; c=relaxed/simple; bh=M2C2mYblq5WwfPR3JTQ7zjObWujAbnG0RbSo75nAPyc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LzeEGef+POHLWCPLobWmgNw0p9ZFlXX5c3cZYgEbSBS6EEWbuRowTGe0NBBUXCV+P4qzqfBy3rlmg/8NDkb2Iiy82JydCaNGg/Jq3Ksq2etZVUZYp0I8YsQzxnWmMfhtTsBspt4qsLFTGWwX7+CVWHLsFGJxYiCDYBY09D65Vko= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--isaacmanjarres.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OiYyyiS4; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--isaacmanjarres.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OiYyyiS4" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21648ddd461so110350755ad.0 for ; Thu, 26 Dec 2024 17:52:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735264336; x=1735869136; darn=vger.kernel.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=v6yaTDfkaNJVYvN9IEtl+N1Z6cSe/++H2abnIIHWux0=; b=OiYyyiS4QJIGLPWwuz59mWA05dVmPpPuPJOPDm14cxV+pV/nouFlMbxV7WozTBJXzZ /7PYWlcadiVgopQjw7ZF6iaVmvR+CVLbbO+kMlwSe2rR+Tjm95XwQ+PREA8OQ7Cc1lxm +CF9h+PwAYSSshrkIFrx2czIsLbSz8RBrPAVnckItoUZEwbr3JXUUr3edt2uLVMV4iyY f1/gVei0U5wzKJBcIyj5DWpg68g7wlM/PVkx/g8ThIjZeAhuuu/Nu8Ch2mmbwR3IXj2I DYpbD9VMqzRTaJlzaLyCo29NKV18OlakNeW8PM7xTa0LklIipq5KFYkkntYyqj4B+nkC jGzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735264336; x=1735869136; 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=v6yaTDfkaNJVYvN9IEtl+N1Z6cSe/++H2abnIIHWux0=; b=isDAPxWLRQN00zqrcnxFQ5a9sHqyARCzLo6ipD6/W34t6vXJQNAKPspB4mfFn/MUJO 64LtHQXD8BqytWmv2LPritSwOl4Z4WYyY1nGGcpeJyDP8iAsO51b4/pytzv1sFzDa1Uh bP23XFkU5MjXFtgw1FoaMaceuaLCW5VicrSRmk74o+D96HMtq+ZMt1yH8bDvcTn1ZMXr t4qWzHZWHCnzpib6XR+2mAoTGy2uDZ/UjwTHC7AJOKWJL4isMenFrVtXmKbomF+geSat ET/Og8JkpLTg2VVrJVz76VMlJAki4u33LpV4ofUeUC2/JxLM9Tij/Q14wxiDmxkobr4P Ilag== X-Forwarded-Encrypted: i=1; AJvYcCUYbovLn6ToSTWapLUkBjDPcoMNTd0/km8PL0/aSeermDv1zPUdEBVvsHKZSXNCPaHQOAqc/STUlvW6eYk=@vger.kernel.org X-Gm-Message-State: AOJu0YzLITadXIgGQNw/RhUtNmzAB5YDJXzJ37APdBODWQF5IEytRlTD 5fGCOodTgpIe0abiLpv+yoduX2EVI8f/BVg9wOupn0sUxLZ+eVt9dWqCR3Rz2JXqygoljYYjQOP LVNz3H+/f0bcN7+usOlGzVjM1bz3ilWMPYA== X-Google-Smtp-Source: AGHT+IHse4XovH7OCFMIjVWOPD1SQgxTAQ/h5NRfnQWPd1gX69b7atGrDkgoCdZnGb8GfH754+jN0PTv1D/JC0qrH+J2xw== X-Received: from pgjg7.prod.google.com ([2002:a63:dd47:0:b0:801:9858:ef95]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f68f:b0:215:63a0:b58c with SMTP id d9443c01a7336-219e6f25f6bmr358562925ad.46.1735264335985; Thu, 26 Dec 2024 17:52:15 -0800 (PST) Date: Thu, 26 Dec 2024 17:51:59 -0800 In-Reply-To: <20241227015205.1375680-1-isaacmanjarres@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241227015205.1375680-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241227015205.1375680-2-isaacmanjarres@google.com> Subject: [RFC PATCH v2 1/2] mm/memfd: Add support for F_SEAL_FUTURE_EXEC to memfd From: "Isaac J. Manjarres" To: Jeff Layton , Chuck Lever , Alexander Aring , Andrew Morton , Shuah Khan Cc: surenb@google.com, kaleshsingh@google.com, jstultz@google.com, aliceryhl@google.com, jeffxu@google.com, kees@kernel.org, "Isaac J. Manjarres" , kernel-team@android.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Android currently uses the ashmem driver [1] for creating shared memory regions between processes. Ashmem buffers can initially be mapped with PROT_READ, PROT_WRITE, and PROT_EXEC. Processes can then use the ASHMEM_SET_PROT_MASK ioctl command to restrict--never add--the permissions that the buffer can be mapped with. Processes can remove the ability to map ashmem buffers as executable to ensure that those buffers cannot be exploited to run unintended code. For instance, suppose process A allocates a memfd that is meant to be read and written by itself and another process, call it B. Process A shares the buffer with process B, but process B injects code into the buffer, and compromises process A, such that it makes A map the buffer with PROT_EXEC. This provides an opportunity for process A to run the code that process B injected into the buffer. If process A had the ability to seal the buffer against future executable mappings before sharing the buffer with process B, this attack would not be possible. Android is currently trying to replace ashmem with memfd. However, memfd does not have a provision to permanently remove the ability to map a buffer as executable, and leaves itself open to the type of attack described earlier. However, this should be something that can be achieved via a new file seal. There are known usecases (e.g. CursorWindow [2]) where a process maps a buffer with read/write permissions before restricting the buffer to being mapped as read-only for future mappings. The resulting VMA from the writable mapping has VM_MAYEXEC set, meaning that mprotect() can change the mapping to be executable. Therefore, implementing the seal similar to F_SEAL_WRITE would not be appropriate, since it would not work with the CursorWindow usecase. This is because the CursorWindow process restricts the mapping permissions to read-only after the writable mapping is created. So, adding a file seal for executable mappings that operates like F_SEAL_WRITE would fail. Therefore, add support for F_SEAL_FUTURE_EXEC, which is handled similarly to F_SEAL_FUTURE_WRITE. This ensures that CursorWindow can continue to create a writable mapping initially, and then restrict the permissions on the buffer to be mappable as read-only by using both F_SEAL_FUTURE_WRITE and F_SEAL_FUTURE_EXEC. After the seal is applied, any calls to mmap() with PROT_EXEC will fail. [1] https://cs.android.com/android/kernel/superproject/+/common-android-mai= nline:common/drivers/staging/android/ashmem.c [2] https://developer.android.com/reference/android/database/CursorWindow Signed-off-by: Isaac J. Manjarres --- include/uapi/linux/fcntl.h | 1 + mm/memfd.c | 39 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index 6e6907e63bfc..ef066e524777 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -49,6 +49,7 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped = */ #define F_SEAL_EXEC 0x0020 /* prevent chmod modifying exec bits */ +#define F_SEAL_FUTURE_EXEC 0x0040 /* prevent future executable mappings */ /* (1U << 31) is reserved for signed error codes */ =20 /* diff --git a/mm/memfd.c b/mm/memfd.c index 5f5a23c9051d..cfd62454df5e 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -184,6 +184,7 @@ static unsigned int *memfd_file_seals_ptr(struct file *= file) } =20 #define F_ALL_SEALS (F_SEAL_SEAL | \ + F_SEAL_FUTURE_EXEC |\ F_SEAL_EXEC | \ F_SEAL_SHRINK | \ F_SEAL_GROW | \ @@ -357,14 +358,50 @@ static int check_write_seal(unsigned long *vm_flags_p= tr) return 0; } =20 +static inline bool is_exec_sealed(unsigned int seals) +{ + return seals & F_SEAL_FUTURE_EXEC; +} + +static int check_exec_seal(unsigned long *vm_flags_ptr) +{ + unsigned long vm_flags =3D *vm_flags_ptr; + unsigned long mask =3D vm_flags & (VM_SHARED | VM_EXEC); + + /* Executability is not a concern for private mappings. */ + if (!(mask & VM_SHARED)) + return 0; + + /* + * New PROT_EXEC and MAP_SHARED mmaps are not allowed when exec seal + * is active. + */ + if (mask & VM_EXEC) + return -EPERM; + + /* + * Prevent mprotect() from making an exec-sealed mapping executable in + * the future. + */ + *vm_flags_ptr &=3D ~VM_MAYEXEC; + + return 0; +} + int memfd_check_seals_mmap(struct file *file, unsigned long *vm_flags_ptr) { int err =3D 0; unsigned int *seals_ptr =3D memfd_file_seals_ptr(file); unsigned int seals =3D seals_ptr ? *seals_ptr : 0; =20 - if (is_write_sealed(seals)) + if (is_write_sealed(seals)) { err =3D check_write_seal(vm_flags_ptr); + if (err) + return err; + } + + if (is_exec_sealed(seals)) + err =3D check_exec_seal(vm_flags_ptr); =20 return err; } --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Mon Feb 9 00:19:43 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C798C3597B for ; Fri, 27 Dec 2024 01:52:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735264343; cv=none; b=T55Gev9N60ZyBZTtY+jGkIlZ7gFz+y4J/t2G/5bdHszBZUpAMzMRFwsNhu8K9w43smyxZtCp23RNs23rxecapd6Ko+ytQnoVZC54nLSpAltq/7vU4WjkLBkNMixdQa3+fQ6N13ZHtcjPeGbgzGRAhPjUqIiga6taTztpgdyzyyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735264343; c=relaxed/simple; bh=4DBEFnoWxm6wOsHhIgJEisd7g7YgHjqynQfwaJjdS5Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=buWLUD2rJDdFH3FlyBs0yoqY9Zb4b2r8x0XQmcgMU94I3P0fgDHSMmlP1ScRZgem4O2IyRCMUb5NOPjqZTummwWrUG9hCgE4qxXqf2idF6PDARIxUqLTo7NVWCUrNWF0budjj36uIhOIlGhIjIUP+jt0pLQDy0OwUOdFGAAbLOE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--isaacmanjarres.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=o/o187Iu; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--isaacmanjarres.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o/o187Iu" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21661949f23so144687085ad.3 for ; Thu, 26 Dec 2024 17:52:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735264341; x=1735869141; darn=vger.kernel.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=9DO6BLM5CMOo/wBOU4R18TkGC6gkh5CUUEjeVIIwslA=; b=o/o187Iu1ToUUq4Cm+3BwHS/jGEQlNRsv3eZh3ng5BAPovHS4HwCfcQ0CoAAbe1/eb FAEXj1MvhwgArIBGLvJUrQNKGUVXcStny9CnXXmAHH3jDba4V2k+lMFOBI6cP7KsGaZi nHVvHFZ7l6z5ukeFU8nFeg2YPzglwO8frhvBzPfH3mVQGrGBLZrQLbmJj2ty/HCoA0n6 aFkvDi5ub0x6jY41i7pGctA06xW/KtRCjWEWYPqanxYQZB+35/sAay4zNPrFrKropeeV dFEMtr8VNyr8JQpCtqcY91yLFgKOgFBSsZEic27M9fUmPvfRCJv1isJUZahJ8MfY/Bpp xhVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735264341; x=1735869141; 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=9DO6BLM5CMOo/wBOU4R18TkGC6gkh5CUUEjeVIIwslA=; b=MWIDrr6IdoqKY0IBQcov1NxEFPihvBehJYPsEXP3ItvKqgInD78PAZrPzRSAr1PpLY Q9nl3g4QEJ0W919bIW2bvJmnOXdZ0AznEEyw7F329iMS+4IAVKTn0nohKTJ4CIqNsdwH nTmJlJf784hWyZ3ZRs2iciRr4STtt6EUT/4+NcvNHqS71HXOG5acLvBQFdJAlTmIJU4o 9/bD9kibJ4LonI/ukUIe5QboduUtITbJ61wIPRqrYYmM4P0VDtGG1Le5NUjrRQK0vxeC JPheBGAGNTcYl+sh4EZB9RE5qnwA4mOio/3F/oVFBHybrypYekKweVDGz/NOBswy18FR FkLA== X-Forwarded-Encrypted: i=1; AJvYcCXHfJB+G3k1Ea2ruT3eH9pIYy1xrZQxurQgfY0xFPQZTuDx2ES5Ar56oTQn65OFVEde/1inb97XQMBazb4=@vger.kernel.org X-Gm-Message-State: AOJu0YxsVK+WZNIi4Kso/GeZbx5xe0v4njVkzVfymH9cvUPkHSgF8zwS o3yq4FqkMatWrbpunP+GdMJdTQwv2K+vRJDiK/A/qz0OI7LrCEmQlhZbiYWhcP9M8+POZVE29WE BF1jEs7UDDTtWQfXFq94DwN91e/ba+fr+Zw== X-Google-Smtp-Source: AGHT+IH4sl3E4JdQ3Zzj9j9rlWOFgu6cDoaF1zsaqwb/U//n/TSWDC0MOEarXx8wBC5QemWNASyhP4U5bzLV21KlRPbd6w== X-Received: from pfbbx13.prod.google.com ([2002:a05:6a00:428d:b0:725:a760:4c72]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:680b:b0:1e1:a0b6:9872 with SMTP id adf61e73a8af0-1e5e045a333mr37386543637.11.1735264341098; Thu, 26 Dec 2024 17:52:21 -0800 (PST) Date: Thu, 26 Dec 2024 17:52:00 -0800 In-Reply-To: <20241227015205.1375680-1-isaacmanjarres@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241227015205.1375680-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241227015205.1375680-3-isaacmanjarres@google.com> Subject: [RFC PATCH v2 2/2] selftests/memfd: Add tests for F_SEAL_FUTURE_EXEC From: "Isaac J. Manjarres" To: Jeff Layton , Chuck Lever , Alexander Aring , Andrew Morton , Shuah Khan Cc: surenb@google.com, kaleshsingh@google.com, jstultz@google.com, aliceryhl@google.com, jeffxu@google.com, kees@kernel.org, "Isaac J. Manjarres" , kernel-team@android.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add tests to ensure that F_SEAL_FUTURE_EXEC behaves as expected. Signed-off-by: Isaac J. Manjarres Reviewed-by: Alice Ryhl --- tools/testing/selftests/memfd/memfd_test.c | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/sel= ftests/memfd/memfd_test.c index c0c53451a16d..abc213a5ce99 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -31,6 +31,7 @@ #define STACK_SIZE 65536 =20 #define F_SEAL_EXEC 0x0020 +#define F_SEAL_FUTURE_EXEC 0x0040 =20 #define F_WX_SEALS (F_SEAL_SHRINK | \ F_SEAL_GROW | \ @@ -318,6 +319,37 @@ static void *mfd_assert_mmap_private(int fd) return p; } =20 +static void *mfd_fail_mmap_exec(int fd) +{ + void *p; + + p =3D mmap(NULL, + mfd_def_size, + PROT_EXEC, + MAP_SHARED, + fd, + 0); + if (p !=3D MAP_FAILED) { + printf("mmap() didn't fail as expected\n"); + abort(); + } + + return p; +} + +static void mfd_fail_mprotect_exec(void *p) +{ + int ret; + + ret =3D mprotect(p, + mfd_def_size, + PROT_EXEC); + if (!ret) { + printf("mprotect didn't fail as expected\n"); + abort(); + } +} + static int mfd_assert_open(int fd, int flags, mode_t mode) { char buf[512]; @@ -998,6 +1030,52 @@ static void test_seal_future_write(void) close(fd); } =20 +/* + * Test SEAL_FUTURE_EXEC_MAPPING + * Test whether SEAL_FUTURE_EXEC_MAPPING actually prevents executable mapp= ings. + */ +static void test_seal_future_exec_mapping(void) +{ + int fd; + void *p; + + + printf("%s SEAL-FUTURE-EXEC-MAPPING\n", memfd_str); + + fd =3D mfd_assert_new("kern_memfd_seal_future_exec_mapping", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + + /* + * PROT_READ | PROT_WRITE mappings create VMAs with VM_MAYEXEC set. + * However, F_SEAL_FUTURE_EXEC applies to subsequent mappings, + * so it should still succeed even if this mapping is active when the + * seal is applied. + */ + p =3D mfd_assert_mmap_shared(fd); + + mfd_assert_has_seals(fd, 0); + + mfd_assert_add_seals(fd, F_SEAL_FUTURE_EXEC); + mfd_assert_has_seals(fd, F_SEAL_FUTURE_EXEC); + + mfd_fail_mmap_exec(fd); + + munmap(p, mfd_def_size); + + /* Ensure that new mappings without PROT_EXEC work. */ + p =3D mfd_assert_mmap_shared(fd); + + /* + * Ensure that mappings created after the seal was applied cannot be + * made executable via mprotect(). + */ + mfd_fail_mprotect_exec(p); + + munmap(p, mfd_def_size); + close(fd); +} + static void test_seal_write_map_read_shared(void) { int fd; @@ -1639,6 +1717,7 @@ int main(int argc, char **argv) test_seal_shrink(); test_seal_grow(); test_seal_resize(); + test_seal_future_exec_mapping(); =20 if (pid_ns_supported()) { test_sysctl_simple(); --=20 2.47.1.613.gc27f4b7a9f-goog