]> git.ipfire.org Git - thirdparty/kernel/stable.git/blobdiff - ipc/shm.c
perf config: Fix an error in the config template documentation
[thirdparty/kernel/stable.git] / ipc / shm.c
index badac463e2c8ea7a035c73acd385d4d58211e6ac..44cca2529a95844055e0da4ceccf1c6a150133ff 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * linux/ipc/shm.c
  * Copyright (C) 1992, 1993 Krishna Balasubramanian
@@ -202,6 +203,12 @@ static int __shm_open(struct vm_area_struct *vma)
        if (IS_ERR(shp))
                return PTR_ERR(shp);
 
+       if (shp->shm_file != sfd->file) {
+               /* ID was reused */
+               shm_unlock(shp);
+               return -EINVAL;
+       }
+
        shp->shm_atim = ktime_get_real_seconds();
        shp->shm_lprid = task_tgid_vnr(current);
        shp->shm_nattch++;
@@ -385,6 +392,17 @@ static int shm_fault(struct vm_fault *vmf)
        return sfd->vm_ops->fault(vmf);
 }
 
+static int shm_split(struct vm_area_struct *vma, unsigned long addr)
+{
+       struct file *file = vma->vm_file;
+       struct shm_file_data *sfd = shm_file_data(file);
+
+       if (sfd->vm_ops && sfd->vm_ops->split)
+               return sfd->vm_ops->split(vma, addr);
+
+       return 0;
+}
+
 #ifdef CONFIG_NUMA
 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
 {
@@ -419,8 +437,9 @@ static int shm_mmap(struct file *file, struct vm_area_struct *vma)
        int ret;
 
        /*
-        * In case of remap_file_pages() emulation, the file can represent
-        * removed IPC ID: propogate shm_lock() error to caller.
+        * In case of remap_file_pages() emulation, the file can represent an
+        * IPC ID that was removed, and possibly even reused by another shm
+        * segment already.  Propagate this case as an error to caller.
         */
        ret = __shm_open(vma);
        if (ret)
@@ -444,6 +463,7 @@ static int shm_release(struct inode *ino, struct file *file)
        struct shm_file_data *sfd = shm_file_data(file);
 
        put_ipc_ns(sfd->ns);
+       fput(sfd->file);
        shm_file_data(file) = NULL;
        kfree(sfd);
        return 0;
@@ -509,6 +529,7 @@ static const struct vm_operations_struct shm_vm_ops = {
        .open   = shm_open,     /* callback for a new vm-area open */
        .close  = shm_close,    /* callback for when the vm-area is released */
        .fault  = shm_fault,
+       .split  = shm_split,
 #if defined(CONFIG_NUMA)
        .set_policy = shm_set_policy,
        .get_policy = shm_get_policy,
@@ -1288,14 +1309,17 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
 
        if (addr) {
                if (addr & (shmlba - 1)) {
-                       /*
-                        * Round down to the nearest multiple of shmlba.
-                        * For sane do_mmap_pgoff() parameters, avoid
-                        * round downs that trigger nil-page and MAP_FIXED.
-                        */
-                       if ((shmflg & SHM_RND) && addr >= shmlba)
-                               addr &= ~(shmlba - 1);
-                       else
+                       if (shmflg & SHM_RND) {
+                               addr &= ~(shmlba - 1);  /* round down */
+
+                               /*
+                                * Ensure that the round-down is non-nil
+                                * when remapping. This can happen for
+                                * cases when addr < shmlba.
+                                */
+                               if (!addr && (shmflg & SHM_REMAP))
+                                       goto out;
+                       } else
 #ifndef __ARCH_FORCE_SHMLBA
                                if (addr & ~PAGE_MASK)
 #endif
@@ -1378,7 +1402,16 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
        file->f_mapping = shp->shm_file->f_mapping;
        sfd->id = shp->shm_perm.id;
        sfd->ns = get_ipc_ns(ns);
-       sfd->file = shp->shm_file;
+       /*
+        * We need to take a reference to the real shm file to prevent the
+        * pointer from becoming stale in cases where the lifetime of the outer
+        * file extends beyond that of the shm segment.  It's not usually
+        * possible, but it can happen during remap_file_pages() emulation as
+        * that unmaps the memory, then does ->mmap() via file reference only.
+        * We'll deny the ->mmap() if the shm segment was since removed, but to
+        * detect shm ID reuse we need to compare the file pointers.
+        */
+       sfd->file = get_file(shp->shm_file);
        sfd->vm_ops = NULL;
 
        err = security_mmap_file(file, prot, flags);