- 论坛徽章:
- 0
|
在系统中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()的所谓特权到底是什么呢?
[ 本帖最后由 basten54188 于 2008-12-10 13:26 编辑 ] |
|