LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 903|回复: 1

mmap()中的特权问题

[复制链接]
发表于 2008-12-10 20:04:29 | 显示全部楼层 |阅读模式
在系统中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/2417 ... 恶意攻击。”

我写了如下程序并进行如下操作: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还是普通用户都可以运行??
[color="red"]
请指教一下,这个mmap()的所谓特权到底是什么呢?
发表于 2008-12-19 16:32:17 | 显示全部楼层
和文件有关系,如果是自己实现的设备节点,可以根据参数自定义
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表