[PATCH] Re: mmap() of O_WRONLY files, and Quake2

Rafael Reilova (rreilova@ececs.uc.edu)
Sun, 29 Nov 1998 02:24:45 -0500 (EST)


Hi Glynn,

On Sat, 28 Nov 1998, Glynn Clements wrote:

> > mmap(0, 65536, PROT_WRITE, MAP_SHARED, 5, 0) = -1 EACCES (Permission denied)
>
> My first reaction was: Huh? However, looking at mm/mmap.c reveals that
> we refuse to mmap() anything that isn't open for reading (due to
> limitations of the x86 architecture, it would seem):
>
> > if (!(file->f_mode & 1))
> > return -EACCES;

Great you found out why it doesn't work! This thing had me stumped. I
was writting a simple sound player and the mmap call would always fail.

> Given that the latest sound drivers use the open() flags to determine
> whether to use 8/16-bit duplex, this seems to pretty much screw the
> possibility of 16-bit write-only sound using mmap().

Yep.

> 2. Change mmap() to allow mapping files which were open()ed O_WRONLY,
> provided that the process has read permission on the inode. Presumably
> this will break if the program is relying on a segfault if the process
> tries to read the mmap()d region (is this a realistic possibility?).

I choose #2. Is the patch at the end correct? It solves the problem for
me, but I don't know enough about the fs layer to judge it correctness.
Also, I don't think letting the mmap proceed breaks anything, since
according to the comments at the top of mmap.c, PROT_WRITE allows all
types of accesses already (i.e. in your example the program wouldn't
segfault anyways).

> 3. Leave things as they are, and forego the possibility of using
> mmap() for 16-bit sound output.

That would suck :-/ Right now mmap fails for *any* sound output if the dsp
device file is opened O_WRONLY, not just for the 16-bit Sound Blaster
case. It is true that this doesn't make much sense for regular files, but
still failing the mmap seems wrong, unless POSIX says othewise ;-)

Cheers,

Rafael

PS. Sorry if by the time people receive this, it has alredy been answered,
but the list seems to be having trouble again, so I can't tell :-/

--- /usr/src/linux-2.1.129/mm/mmap.c Sun Nov 8 20:07:24 1998
+++ /usr/src/linux/mm/mmap.c Sun Nov 29 02:54:20 1998
@@ -220,7 +220,11 @@

/* fall through */
case MAP_PRIVATE:
- if (!(file->f_mode & 1))
+ /* due to x86 limitations, any mmap will effectively
+ * give read access to the mmaped file.
+ */
+ if (!(file->f_mode & 1) &&
+ permission(file->f_dentry->d_inode, MAY_READ))
return -EACCES;
break;

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