From: Jamie Lokier <jamie@shareable.org>
To: William Lee Irwin III <wli@holomorphy.com>,
linux-kernel@vger.kernel.org,
Michael Kerrisk <michael.kerrisk@gmx.net>
Subject: Re: Testing PROT_NONE and other protections, and a surprise
Date: Thu, 1 Jul 2004 05:01:01 +0100 [thread overview]
Message-ID: <20040701040101.GC1564@mail.shareable.org> (raw)
In-Reply-To: <20040701033518.GT21066@holomorphy.com>
William Lee Irwin III wrote:
> > When running it on i386, I got a *huge* surprise (to me). A
> > PROT_WRITE-only page can sometimes fault on read or exec. This is the
> > output:
>
> This is unsurprising. The permissions can't be represented in pagetables,
> but can opportunistically be enforced when exceptions are taken for other
> reasons (e.g. TLB invalidations related to page replacement).
Sure, it makes sense. I understand exactly why the results are like that.
But it was a surprise because it just hadn't occurred to me.
The big point is that I know other people haven't thought of it
either. I'm sure it's commonly thought that when you specify
PROT_WRITE, you'll get a readable page on architectures that can't
do write-only pages.
Interestingly, on Alpha you _will_ always get a readable page.
The Alpha fault handler has different enforcement rules to i386.
I know why this is done too, but it does indicate that the actual
behaviour of different architectures isn't immediately obvious.
The lessons from this investigation for portable code are:
1. You really must specify all the access protections you need,
i.e. read, write and/or exec if you use them. This was obvious.
2. In general you cannot depend on a kernel to _prevent_ accesses
of a certain kind if you don't enable that in the protection flags,
unless you know that architecture and its kernel well.
This was obvious too.
3. Less obviously, if you find that turning off a flag appears to
prevent access of that type in tests, that is _not_ evidence
that it will always prevent access of that type, even on the
same machine.
This is important if you're hoping to prevent certain accesses
and trap them, e.g. for a garbage collector, virtual machine,
memory access checker etc. It has implications for
autoconf-style tests which check for these properties.
There are two exceptions which you can depend on across all
architectures, at least running Linux:
4. Exception #1: Turning off PROT_WRITE always disables writing.
This is probably portable to every system which supports mprotect().
5. Exception #2: PROT_NONE works. Any access will fail, and you
can trap the signal. Historically this hasn't always been the
case with Linux, though. It is probably much less dependable
among other operating systems than exception #1.
People knew these rules already. It's implicit in a lot of code.
However I've not seen them clearly written down in one place.
Now they are.
Feel free to add any I missed, or provide corrections, thanks!
-- Jamie
next prev parent reply other threads:[~2004-07-01 4:01 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-06-30 2:44 A question about PROT_NONE on ARM and ARM26 Jamie Lokier
2004-06-30 3:38 ` William Lee Irwin III
2004-07-01 3:26 ` Testing PROT_NONE and other protections, and a surprise Jamie Lokier
2004-07-01 3:35 ` William Lee Irwin III
2004-07-01 4:01 ` Jamie Lokier [this message]
2004-07-01 3:44 ` Kyle Moffett
2004-07-01 4:11 ` Jamie Lokier
2004-07-01 4:59 ` Kyle Moffett
2004-07-01 12:39 ` Jamie Lokier
2004-07-01 14:43 ` [OT] " Kyle Moffett
2004-07-01 14:50 ` Jamie Lokier
2004-07-01 15:01 ` Kyle Moffett
2004-07-01 16:37 ` Matt Mackall
2004-07-01 17:26 ` Michael Driscoll
2004-07-02 7:37 ` Gabriel Paubert
2004-07-01 12:52 ` Russell King
2004-07-01 14:26 ` Richard Curnow
2004-06-30 8:16 ` A question about PROT_NONE on ARM and ARM26 Russell King
2004-06-30 14:59 ` Jamie Lokier
2004-06-30 15:22 ` Ian Molton
2004-06-30 18:26 ` Russell King
2004-06-30 19:14 ` Jamie Lokier
2004-06-30 19:23 ` Russell King
2004-06-30 20:15 ` Jamie Lokier
2004-06-30 22:59 ` Russell King
2004-06-30 23:30 ` Jamie Lokier
2004-06-30 23:48 ` Ian Molton
2004-07-01 1:59 ` Jamie Lokier
2004-07-01 1:05 ` Nicolas Pitre
2004-07-01 1:50 ` Jamie Lokier
2004-07-02 18:39 ` Russell King
2004-07-01 15:27 ` Scott Wood
2004-07-01 23:53 ` Jamie Lokier
2004-07-02 14:36 ` Scott Wood
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040701040101.GC1564@mail.shareable.org \
--to=jamie@shareable.org \
--cc=linux-kernel@vger.kernel.org \
--cc=michael.kerrisk@gmx.net \
--cc=wli@holomorphy.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox