我正在尝试使一个使用原始套接字的程序以具有 Linux 功能的非 root 身份正确运行。程序如下:
#include <netinet/ip.h>
int main()
{
int sd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
if(sd < 0)
{
perror("socket() error");
return 1;
}
return 0;
}
如果我编译它并以非 root 身份运行它,我会得到一个错误,正如预期的那样:
[user@localhost ~]$ make socket
cc socket.c -o socket
[user@localhost ~]$ ./socket
socket() error: Operation not permitted
如果我添加该cap_net_raw
功能,作为有效且允许的功能,它就可以工作。
[user@localhost ~]$ sudo setcap cap_net_raw+ep socket
[sudo] password for user:
[user@localhost ~]$ ./socket
[user@localhost ~]$
现在,我想使用pam_cap.so
它来使只有特定用户才能使用 运行该程序cap_net_raw
,而不是所有人。我/etc/security/capability.conf
的是:
cap_net_raw user
我/etc/pam.d/login
的是(请注意,我也尝试过/etc/pam.d/sshd
,但似乎也不起作用):
#%PAM-1.0
auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so
auth substack system-auth
auth include postlogin
#Added this line to use pam_cap
auth required pam_cap.so
account required pam_nologin.so
account include system-auth
password include system-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
session optional pam_console.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open
session required pam_namespace.so
session optional pam_keyinit.so force revoke
session include system-auth
session include postlogin
-session optional pam_ck_connector.so
我有一个 ssh 会话,然后我注销并重新登录并执行以下命令:
[user@localhost ~]$ sudo setcap cap_net_raw+p socket
[sudo] password for user:
[user@localhost ~]$ getcap socket
socket = cap_net_raw+p
[user@localhost ~]$ ./socket
socket() error: Operation not permitted
[user@localhost ~]$
我的问题是:为什么我不能用 'socket' 程序执行cap_net_raw
?我认为当我登录时,我的用户将获得它作为允许的功能,并且它将允许“用户”使用cap_net_raw
.
这就是我正在运行的内容:
[user@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[user@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)