Re: [PATCH v2] binfmt_elf: Update READ_IMPLIES_EXEC logic for modern CPUs

From: Jason Gunthorpe
Date: Fri Apr 26 2019 - 11:02:51 EST


On Thu, Apr 25, 2019 at 10:07:25PM +0200, Ingo Molnar wrote:

> > But yes, your above diff for "has NX" is roughly correct. I'll walk
> > through each piece I'm thinking about. Here is the current state:
> >
> > CPU: | lacks NX* | has NX, ia32 | has NX, x86_64 |
> > ELF: | | | |
> > missing GNU_STACK | exec-all | exec-all | exec-all |
> > GNU_STACK == RWX | exec-all | exec-all | exec-all |
> > GNU_STACK == RW | exec-none | exec-none | exec-none |
> >
> > *this column has no architecture effect: NX markings are ignored by
> > hardware, but may have behavioral effects when "wants X" collides with
> > "cannot be X" constraints in memory permission flags, as in [1].
>
> So [1] appears to be device driver mapping a BAR that isn't intended to
> be excutable:
>
> https://lore.kernel.org/netdev/20190418055759.GA3155@xxxxxxxxxxxx/
>
> and the question is, do we reject this at the device driver mmap() level
> already, right?

No, we wanted to reject it at the driver mmap() level, but if an
executable is marked with GNU_STACK=RWX then the core mm code always
calls the driver with VM_EXEC (even though the mmap isn't a stack) and
the driver becomes incompatible with userspace using GNU_STACK=RWX (ie
some Fortran programs, apparently)

> I suspect the best behavior is to reject as early as possible, so I agree
> with your change here - even though !NX systems tend to become less and
> less relevant these days.

I suggested the idea of adding a flag in either the struct file or the
file_operations flag that says mmap is never to be executable for this
file with the idea that most/all cdev users would set it.

Does that seem reasonable?

Jason