Re: mmap successed but SIGBUS generated on access

From: Jamie Lokier (lk@tantalophile.demon.co.uk)
Date: Wed Sep 19 2001 - 17:51:53 EST


Andrew V. Samoilov wrote:
>> It is possible to do something useful with a signal handler sometimes.
>> For example, you can mmap() a zero page into the offending page once
>> you've got the fault address, or read() a zero page if you did
>> MAP_PRIVATE (this produces fewer VMAs), set a flag, and let the program
>> continue until it checks the flag and aborts the parsing or whatever
>> operation it's doing.
>
> So, I must read all of the mapped area and even this
> does not saves me of faulting next time if somebody
> change file permission. Does I understand this situation
> right?

If you use MAP_PRIVATE, then after your process modifies the mapped
page, you have the data for sure. This includes calling read() over a
page that raises a SIGBUS -- the page is "modified" by this operation,
although the contents should hopefully be the correct file contents if
read() succeeds. Any subsequent change to the file, including
permission changes and data changes, won't affect the data you have in
memory.

If you do not modify the pages (and usually, for efficiency, you
wouldn't), then yes a change in file permissions can mean you can read
data one second and will get a SIGBUS later. You're not guaranteed to
get a SIGBUS, but you might get one -- it depends on whether the OS
decides to reclaim the page's memory temporarily in between.

If you want to do something like an Editor's "Load File" operation, then
you need to read the whole file. Either call read(), or call mmap() and
then modify every page by reading one byte from each page and writing
the same value back to the same place. read() is probably quicker, but
I've never checked.

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



This archive was generated by hypermail 2b29 : Sun Sep 23 2001 - 21:00:34 EST