Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cant create raw socket in Ubuntu?

I'm learning how to work with raw sockets in Linux. I'm trying to create a socket like that:

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

But all I got after launch is:

socket() failed: Operation not permitted

I know that only root can create raw sockets, but if I run it with SUID bit or sudo - the problem is the same. What's wrong? The system is Ubuntu 11.04.

Maybe I'm including needless headers?

#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>

And I'm wondering - why SUID is useless?

like image 685
Enchantner Avatar asked Jul 26 '11 06:07

Enchantner


People also ask

Is raw sockets can be created by any user?

In order to create a raw socket, a process must have the CAP_NET_RAW capability in the user namespace that governs its network namespace. All packets or errors matching the protocol number specified for the raw socket are passed to this socket.

What is the purpose of raw socket?

A raw socket is a type of socket that allows access to the underlying transport provider. This topic focuses only on raw sockets and the IPv4 and IPv6 protocols. This is because most other protocols with the exception of ATM do not support raw sockets.


1 Answers

My money on you not running your code correctly.

I've copied and pasted your exact code into an empty main(). I get the same error if I run it as myself, but it runs correctly under sudo. This is on Ubuntu.

The code:

#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 as myself:

aix@aix:~$ ./a.out 
socket() failed: Operation not permitted
aix@aix:~$

Run as root:

aix@aix:~$ sudo ./a.out 
aix@aix:~$
like image 66
NPE Avatar answered Oct 31 '22 18:10

NPE