免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1945 | 回复: 3
打印 上一主题 下一主题

mmap()的特权问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-10 13:38 |只看该作者 |倒序浏览
在系统中man capabilities
可以看到:
......
CAP_IPC_LOCK
              Permit memory locking (mlock(2), mlockall(2), mmap(2), shmctl(2)).
......

可见,使用mmap调用是需要特权的!
请问mmap调用中,怎么样的操作需要特权呢??
也就是说,mmap怎么样操作使得只有super user(root)可以使用,而普通用户不能使用呢?

看上述man的内容,需要特权的似乎是mmap带中MAP_LOCKED参数的操作,我的理解对么??

如果是MAP_LOCKED这个参数,那么按照http://blog.chinaunix.net/u/24174/showart_216839.html这个老大的说法:“MAP_LOCKED只用在root权限的进程才能使用,以防止锁定所有可用内存的恶意攻击。”

我写了如下程序并进行如下操作:test.c

[root@localhost ~]# cat test.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main(int argc,char **argv)
{
        char s[]="ChinaUnix is the best !!!\n";
        int fd;
        void *buf;
        struct stat stat_buf;
        off_t len;

        if (argc!=2){
                printf("Usage: %s filename\n",argv[0]);
                exit(1);
        }

        if ((fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0644))==-1){
                perror("Can't create file");
                exit(1);
        }

        if (write(fd,s,sizeof(s))==-1){
                perror("Can't write file");
                exit(1);
        }
        close(fd);

        if ((fd=open(argv[1],O_RDONLY))==-1){
                perror("Can't open file");
                exit(2);
        }

        if (fstat(fd,&stat_buf)==-1){
                perror("Can't fstat file");
                exit(2);
        }
        len=stat_buf.st_size;

        if ((buf=mmap(NULL,len,PROT_READ,MAP_LOCKED|MAP_PRIVATE,fd,0))==MAP_FAILED){
                perror("Can't mmap file");
                exit(3);
        }

        printf("Success!\n");
        munmap(buf,len);
        close(fd);
        return 0;
}
[root@localhost ~]# gcc -o test test.c
[root@localhost ~]# ./test aaa
Success!
[root@localhost ~]# cp test /home/song/
[root@localhost ~]# su - song
[song@localhost ~]$ ls
test
[song@localhost ~]$ ./test aa
Success!
[song@localhost ~]$

貌似无论是root还是普通用户都可以运行??

请指教一下,这个mmap()的所谓特权到底是什么呢?

论坛徽章:
0
2 [报告]
发表于 2008-12-10 13:56 |只看该作者
你为什么不再写个程序朝共享内存中写数据?分别测试不同用户权限的情况,看看那个锁到底起作用了没有

论坛徽章:
0
3 [报告]
发表于 2008-12-10 15:58 |只看该作者
试了,无论是root还是普通用户都可以写啊!

论坛徽章:
0
4 [报告]
发表于 2008-12-10 17:06 |只看该作者
没人有这方面的经验么
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP