Re: [rfc 3/3] prctl: Add PR_SET_MM codes to tune up mm_struct entires

From: Kees Cook
Date: Wed Nov 30 2011 - 13:10:40 EST


On Wed, Nov 30, 2011 at 9:37 AM, Cyrill Gorcunov <gorcunov@xxxxxxxxx> wrote:
> On Tue, Nov 29, 2011 at 12:40:57PM -0800, Kees Cook wrote:
>> >
>> > On the other hands these fields are set up by elf hanlder code, which
>> > does mmap these areas, so we have to check that particular member
>> > belongs to existing VMA and never cross user-space area, and together
>> > with root-only approach would not it be enough? I'm sure missing something
>> > that is why I'm asking.
>>
>> Right, if you verify that the addresses are actually inside valid
>> userspace vmas, that is likely to be right, though there are probably
>> other things I haven't thought of. The trouble is avoiding vdso, stack
>> guard page, vsyscall, and anything else that isn't meant for the mm to
>> have direct access to.
>>
>
> Hi Kees,
>
> what about this one? Note that these mm_struct members don't affect
> kernel much (at least as far as I see, except maybe brk,start_brk and
> start_stack values), so I've added some sanity checks here, hope they
> would fit. Still main protection is root-only access only. The kernel
> itself uses vma_area::start/end members for overlows tests internally
> so I think even passing crazy data here won't crash the kernel itself.

Right, though besides just crashing the kernel, I'm trying to look at
this from the perspective of a paranoid admin that doesn't even trust
the root user. Is there some way this new interface could be used to
provide the building blocks for gaining kernel execute control?
(Imagine a system running with modules disabled, STRICT_DEVMEM
enabled, etc.)

> What do you think?

This looks way better, yes. I have this feeling like these validations
should be more centralized or tied to the mm code more directly to
avoid drift, but I don't have any constructive suggestions
unfortunately. Maybe other folks do?

> +       switch (opt) {
> +       case PR_SET_MM_START_CODE:
> +       case PR_SET_MM_END_CODE:
> +
> +               vm_req_flags = VM_READ | VM_EXEC;
> +               vm_bad_flags = VM_WRITE | VM_MAYSHARE;
> +
> +               if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
> +                   (vma->vm_flags & vm_bad_flags))
> +                       goto out;

Another random thought: given this very regular set of checks you're
doing, perhaps the flags should be part of a data structure instead,
just to reduce the size of this routine?

struct mm_flags {
int req_flags;
int bad_flags;
};

struct mm_flags opt_flags[] = {
...
{ VM_READ | VM_EXEC, VM_WRITE | VM_MAYSHARE }, /* PR_SET_MM_START_CODE */
{ VM_READ | VM_EXEC, VM_WRITE | VM_MAYSHARE }, /* PR_SET_MM_END_CODE */
...

then do validation before the switch statement all in one place, and
leave the switch for more programmatic checks?

-Kees

--
Kees Cook
ChromeOS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/