i2c: jz4780: prevent potential division by zero
[cascardo/linux.git] / mm / mprotect.c
index f7cb3d4..b650c54 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/migrate.h>
 #include <linux/perf_event.h>
 #include <linux/ksm.h>
+#include <linux/pkeys.h>
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
 #include <asm/cacheflush.h>
@@ -354,10 +355,13 @@ fail:
 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
                unsigned long, prot)
 {
-       unsigned long vm_flags, nstart, end, tmp, reqprot;
+       unsigned long nstart, end, tmp, reqprot;
        struct vm_area_struct *vma, *prev;
        int error = -EINVAL;
        const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
+       const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
+                               (prot & PROT_READ);
+
        prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
        if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
                return -EINVAL;
@@ -374,13 +378,6 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
                return -EINVAL;
 
        reqprot = prot;
-       /*
-        * Does the application expect PROT_READ to imply PROT_EXEC:
-        */
-       if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
-               prot |= PROT_EXEC;
-
-       vm_flags = calc_vm_prot_bits(prot);
 
        down_write(&current->mm->mmap_sem);
 
@@ -411,10 +408,15 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
 
        for (nstart = start ; ; ) {
                unsigned long newflags;
+               int pkey = arch_override_mprotect_pkey(vma, prot, -1);
 
                /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
 
-               newflags = vm_flags;
+               /* Does the application expect PROT_READ to imply PROT_EXEC */
+               if (rier && (vma->vm_flags & VM_MAYEXEC))
+                       prot |= PROT_EXEC;
+
+               newflags = calc_vm_prot_bits(prot, pkey);
                newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
 
                /* newflags >> 4 shift VM_MAY% in place of VM_% */
@@ -445,6 +447,7 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
                        error = -ENOMEM;
                        goto out;
                }
+               prot = reqprot;
        }
 out:
        up_write(&current->mm->mmap_sem);