20.04 - 在/dev/shm/中,对共享内存段的权限被拒绝

分享于 

2分钟阅读

ubuntu

  繁體

问题:

使用shm_open()O_CREATO_RDWR标志创建,并放在/dev/shm中,我有以下rxample程序:


#include <stdio.h>


#include <sys/mman.h>


#include <sys/stat.h>


#include <fcntl.h>


#include <string.h>


#include <errno.h>



int main()


{


 int ret = 0;


 errno = 0;


 ret = shm_open("/testshm", O_CREAT | O_RDWR, 00666);


 fprintf(stderr, "return value %dn", ret);


 fprintf(stderr, "result %d: %sn", errno, strerror(errno));



 return 0;


}



当我第一次运行它时,它按预期工作,并且创建/dev/shm/testshm


$ ls -la /dev/shm


total 0


drwxrwxrwt 2 root root 60 Aug 3 16:34 .


drwxr-xr-x 18 root root 4580 Aug 3 12:41 ..


-rw-rw-r-- 1 krejci krejci 0 Aug 3 14:37 testshm



以相同的用户运行程序可以正常工作,但当我尝试以root运行程序时,我得到" Permission denied" :


# id


uid=0(root) gid=0(root) groups=0(root)


# ./test


return value -1


result 13: Permission denied




答案1:

代码在完成共享内存时缺少调用shm_unlink()语句


答案2:


./untitled1; ls -la /dev/shm; sudo su; ./untitled1 



结果是:


return value 3


result 0: Success


total 0


drwxrwxrwt 2 root root 60 Aug 7 13:19 .


drwxr-xr-x 20 root root 4640 Aug 6 13:26 ..


-rw-r--r-- 1 richard richard 0 Aug 6 18:17 testshm


[sudo] password for richard: 


root@richard-desktop:/home/richard/Documents/forum# ./untitled1


return value 3


result 0: Success



注:我已经升级到20,如:


uname -a 


Linux richard-desktop 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux





SHA  分享  MEMO  内存  permission  Shared