我问,因为在Linux 4.0.4 x86-64系统上失败了(strace log):
mkdir("test",0700) = 0 open("test/foo",O_WRONLY|O_CREAT,0666) = 3 ftruncate(3,11) = 0 mmap(NULL,11,PROT_WRITE,MAP_SHARED,3,0) = -1 EACCES (Permission denied)
errno等于EACCESS.
用O_RDWR替换open-flag O_WRONLY会产生成功的映射.
Linux mmap手册页将errno记录为:
06001
因此,用第二句记录了这种行为.
但它背后的原因是什么?
是POSIX允许的吗?
它是内核还是库限制? (快速浏览一下,我在Linux / mm / mmap.c中找不到任何明显的东西)
解决方法
IEEE Std 1003.1,2004 Edition (POSIX.1 2004)似乎禁止它.
An implementation may permit accesses other than those specified by
prot
; however,if the Memory Protection option is supported,the implementation shall not permit a write to succeed wherePROT_WRITE
has not been set or shall not permit any access wherePROT_NONE
alone has been set. The implementation shall support at least the following values ofprot
:PROT_NONE
,PROT_READ
,PROT_WRITE
,and the bitwise-inclusive OR ofPROT_READ
andPROT_WRITE
. If the Memory Protection option is not supported,the result of any access that conflicts with the specified protection is undefined. The file descriptorfildes
shall have been opened with read permission,regardless of the protection options specified. IfPROT_WRITE
is specified,the application shall ensure that it has opened the file descriptorfildes
with write permission unlessMAP_PRIVATE
is specified in theflags
parameter as described below.
(重点补充)
此外,在x86上,不可能具有只写内存,这是页表条目的限制.页面可以标记为只读或读写,并且可以是可执行的或不可执行的,但不能是只写的.此外,mprotect()的手册页说:
Whether
PROT_EXEC
has any effect different fromPROT_READ
is architecture- and kernel version-dependent. On some hardware architectures (e.g.,i386),PROT_WRITE
impliesPROT_READ
.
在这种情况下,您已经打开了一个没有读取权限的文件描述符,但mmap()将通过为您提供PROT_READ权限来绕过O_WRONLY.相反,它将完全拒绝EACCESS.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。