Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3a459756 authored by Kirill Korotaev's avatar Kirill Korotaev Committed by Linus Torvalds
Browse files

[PATCH] IA64,sparc: local DoS with corrupted ELFs



This prevents cross-region mappings on IA64 and SPARC which could lead
to system crash.  They were correctly trapped for normal mmap() calls,
but not for the kernel internal calls generated by executable loading.

This code just moves the architecture-specific cross-region checks into
an arch-specific "arch_mmap_check()" macro, and defines that for the
architectures that needed it (ia64, sparc and sparc64).

Architectures that don't have any special requirements can just ignore
the new cross-region check, since the mmap() code will just notice on
its own when the macro isn't defined.

Signed-off-by: default avatarPavel Emelianov <xemul@openvz.org>
Signed-off-by: default avatarKirill Korotaev <dev@openvz.org>
Acked-by: default avatarDavid Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
[ Cleaned up to not affect architectures that don't need it ]
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 10387e5e
Loading
Loading
Loading
Loading
+16 −12
Original line number Original line Diff line number Diff line
@@ -163,10 +163,25 @@ sys_pipe (void)
	return retval;
	return retval;
}
}


int ia64_mmap_check(unsigned long addr, unsigned long len,
		unsigned long flags)
{
	unsigned long roff;

	/*
	 * Don't permit mappings into unmapped space, the virtual page table
	 * of a region, or across a region boundary.  Note: RGN_MAP_LIMIT is
	 * equal to 2^n-PAGE_SIZE (for some integer n <= 61) and len > 0.
	 */
	roff = REGION_OFFSET(addr);
	if ((len > RGN_MAP_LIMIT) || (roff > (RGN_MAP_LIMIT - len)))
		return -EINVAL;
	return 0;
}

static inline unsigned long
static inline unsigned long
do_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, unsigned long pgoff)
do_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, unsigned long pgoff)
{
{
	unsigned long roff;
	struct file *file = NULL;
	struct file *file = NULL;


	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
@@ -188,17 +203,6 @@ do_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, un
		goto out;
		goto out;
	}
	}


	/*
	 * Don't permit mappings into unmapped space, the virtual page table of a region,
	 * or across a region boundary.  Note: RGN_MAP_LIMIT is equal to 2^n-PAGE_SIZE
	 * (for some integer n <= 61) and len > 0.
	 */
	roff = REGION_OFFSET(addr);
	if ((len > RGN_MAP_LIMIT) || (roff > (RGN_MAP_LIMIT - len))) {
		addr = -EINVAL;
		goto out;
	}

	down_write(&current->mm->mmap_sem);
	down_write(&current->mm->mmap_sem);
	addr = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
	addr = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
	up_write(&current->mm->mmap_sem);
	up_write(&current->mm->mmap_sem);
+15 −12
Original line number Original line Diff line number Diff line
@@ -219,6 +219,21 @@ asmlinkage int sys_ipc (uint call, int first, int second, int third, void __user
	return err;
	return err;
}
}


int sparc_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
{
	if (ARCH_SUN4C_SUN4 &&
	    (len > 0x20000000 ||
	     ((flags & MAP_FIXED) &&
	      addr < 0xe0000000 && addr + len > 0x20000000)))
		return -EINVAL;

	/* See asm-sparc/uaccess.h */
	if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
		return -EINVAL;

	return 0;
}

/* Linux version of mmap */
/* Linux version of mmap */
static unsigned long do_mmap2(unsigned long addr, unsigned long len,
static unsigned long do_mmap2(unsigned long addr, unsigned long len,
	unsigned long prot, unsigned long flags, unsigned long fd,
	unsigned long prot, unsigned long flags, unsigned long fd,
@@ -233,25 +248,13 @@ static unsigned long do_mmap2(unsigned long addr, unsigned long len,
			goto out;
			goto out;
	}
	}


	retval = -EINVAL;
	len = PAGE_ALIGN(len);
	len = PAGE_ALIGN(len);
	if (ARCH_SUN4C_SUN4 &&
	    (len > 0x20000000 ||
	     ((flags & MAP_FIXED) &&
	      addr < 0xe0000000 && addr + len > 0x20000000)))
		goto out_putf;

	/* See asm-sparc/uaccess.h */
	if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
		goto out_putf;

	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);


	down_write(&current->mm->mmap_sem);
	down_write(&current->mm->mmap_sem);
	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
	up_write(&current->mm->mmap_sem);
	up_write(&current->mm->mmap_sem);


out_putf:
	if (file)
	if (file)
		fput(file);
		fput(file);
out:
out:
+20 −16
Original line number Original line Diff line number Diff line
@@ -548,6 +548,26 @@ asmlinkage long sparc64_personality(unsigned long personality)
	return ret;
	return ret;
}
}


int sparc64_mmap_check(unsigned long addr, unsigned long len,
		unsigned long flags)
{
	if (test_thread_flag(TIF_32BIT)) {
		if (len >= STACK_TOP32)
			return -EINVAL;

		if ((flags & MAP_FIXED) && addr > STACK_TOP32 - len)
			return -EINVAL;
	} else {
		if (len >= VA_EXCLUDE_START)
			return -EINVAL;

		if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
			return -EINVAL;
	}

	return 0;
}

/* Linux version of mmap */
/* Linux version of mmap */
asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
	unsigned long prot, unsigned long flags, unsigned long fd,
	unsigned long prot, unsigned long flags, unsigned long fd,
@@ -563,27 +583,11 @@ asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
	}
	}
	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
	len = PAGE_ALIGN(len);
	len = PAGE_ALIGN(len);
	retval = -EINVAL;

	if (test_thread_flag(TIF_32BIT)) {
		if (len >= STACK_TOP32)
			goto out_putf;

		if ((flags & MAP_FIXED) && addr > STACK_TOP32 - len)
			goto out_putf;
	} else {
		if (len >= VA_EXCLUDE_START)
			goto out_putf;

		if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
			goto out_putf;
	}


	down_write(&current->mm->mmap_sem);
	down_write(&current->mm->mmap_sem);
	retval = do_mmap(file, addr, len, prot, flags, off);
	retval = do_mmap(file, addr, len, prot, flags, off);
	up_write(&current->mm->mmap_sem);
	up_write(&current->mm->mmap_sem);


out_putf:
	if (file)
	if (file)
		fput(file);
		fput(file);
out:
out:
+8 −0
Original line number Original line Diff line number Diff line
@@ -22,4 +22,12 @@
#define MCL_CURRENT	1		/* lock all current mappings */
#define MCL_CURRENT	1		/* lock all current mappings */
#define MCL_FUTURE	2		/* lock all future mappings */
#define MCL_FUTURE	2		/* lock all future mappings */


#ifdef __KERNEL__
#ifndef __ASSEMBLY__
#define arch_mmap_check	ia64_mmap_check
int ia64_mmap_check(unsigned long addr, unsigned long len,
		unsigned long flags);
#endif
#endif

#endif /* _ASM_IA64_MMAN_H */
#endif /* _ASM_IA64_MMAN_H */
+8 −0
Original line number Original line Diff line number Diff line
@@ -35,4 +35,12 @@


#define MADV_FREE	0x5		/* (Solaris) contents can be freed */
#define MADV_FREE	0x5		/* (Solaris) contents can be freed */


#ifdef __KERNEL__
#ifndef __ASSEMBLY__
#define arch_mmap_check	sparc_mmap_check
int sparc_mmap_check(unsigned long addr, unsigned long len,
		unsigned long flags);
#endif
#endif

#endif /* __SPARC_MMAN_H__ */
#endif /* __SPARC_MMAN_H__ */
Loading