为什么我不能在Ubuntu中创建原始套接字?

Enc*_*ner 5 c sockets networking

我正在学习如何在Linux中使用原始套接字.我正在尝试创建一个这样的套接字:

if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
    perror("socket() failed");
    exit(-1);
}
Run Code Online (Sandbox Code Playgroud)

但我在发布后获得的是:

socket()失败:不允许操作

我知道只有root可以创建原始套接字,但如果我用SUID位或sudo运行它 - 问题是一样的.怎么了?该系统是Ubuntu 11.04.

也许我包括不必要的标题?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
Run Code Online (Sandbox Code Playgroud)

我想知道 - 为什么SUID没用?

NPE*_*NPE 14

我的钱没有正确运行你的代码.

我已将您的确切代码复制并粘贴到空白处main().如果我像我一样运行它,我会得到相同的错误,但它正确运行sudo.这是在Ubuntu上.

代码:

#include <sys/socket.h>
#include <netinet/in.h>

int main()
{ 
  int sd;
  if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
    perror("socket() failed");
    return -1;
  }
  return 0;
} 
Run Code Online (Sandbox Code Playgroud)

像我一样跑:

aix@aix:~$ ./a.out 
socket() failed: Operation not permitted
aix@aix:~$
Run Code Online (Sandbox Code Playgroud)

以root身份运行:

aix@aix:~$ sudo ./a.out 
aix@aix:~$
Run Code Online (Sandbox Code Playgroud)


小智 9

据man说:只允许有效用户ID为0或CAP_NET_RAW能力的进程打开原始套接字

所以你可以按照下面的建议用sudo运行应用程序,或者为它设置CAP_NET_RAW功能(实际上你也需要CAP_NET_ADMIN):

# setcap cap_net_raw,cap_net_admin=eip PATH_TO_YOUR_APPLICATION
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问http://ftp.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.4/capfaq-0.2.txt