]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
memfd: check for non-NULL file_seals in memfd_create() syscall
authorRoberto Sassu <roberto.sassu@huawei.com>
Wed, 7 Jun 2023 13:24:27 +0000 (15:24 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 28 Jun 2023 09:14:04 +0000 (11:14 +0200)
commit 935d44acf621aa0688fef8312dec3e5940f38f4e upstream.

Ensure that file_seals is non-NULL before using it in the memfd_create()
syscall.  One situation in which memfd_file_seals_ptr() could return a
NULL pointer when CONFIG_SHMEM=n, oopsing the kernel.

Link: https://lkml.kernel.org/r/20230607132427.2867435-1-roberto.sassu@huaweicloud.com
Fixes: 47b9012ecdc7 ("shmem: add sealing support to hugetlb-backed memfd")
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Cc: Marc-Andr Lureau <marcandre.lureau@redhat.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
mm/memfd.c

index a0a7a37e81771b38549f2f0b0d3d5f63cdeed587..0382f001e2a00aa02692d310a4bfaa4584bf0998 100644 (file)
@@ -375,12 +375,15 @@ SYSCALL_DEFINE2(memfd_create,
 
                inode->i_mode &= ~0111;
                file_seals = memfd_file_seals_ptr(file);
-               *file_seals &= ~F_SEAL_SEAL;
-               *file_seals |= F_SEAL_EXEC;
+               if (file_seals) {
+                       *file_seals &= ~F_SEAL_SEAL;
+                       *file_seals |= F_SEAL_EXEC;
+               }
        } else if (flags & MFD_ALLOW_SEALING) {
                /* MFD_EXEC and MFD_ALLOW_SEALING are set */
                file_seals = memfd_file_seals_ptr(file);
-               *file_seals &= ~F_SEAL_SEAL;
+               if (file_seals)
+                       *file_seals &= ~F_SEAL_SEAL;
        }
 
        fd_install(fd, file);