6

In user mode [non-root] on a linux machine, I am trying to bind a socket by using a ioctl(iInterfaceSocket, SIOCSIFADDR, &stCommand). I am getting error 13 -> Permission denied because of user mode. If change from usermode to kernel mode everything works fine.

I need to bind the socket in user mode only. Please suggest a solution while explaining the problem above. If I missed any information please let me know and I will provide more info.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Eswar
  • 201
  • 1
  • 3
  • 5
  • 1
    @user736403 - what precisely are you trying to achieve? `SIOCSIFADDR` doesn't bind a socket, `bind()` does. `SIOCSIFADDR` sets the address of the machine's network interface. – Robᵩ May 03 '11 at 15:50
  • 1
    Just a small nitpick: running a program as root is very different than running in kernel mode. Programs run as root still run in user mode most of the time; they just have elevated privileges. All processes (root or not) switch between user and kernel mode when they make system calls like `ioctl`. – Jay Conrod May 03 '11 at 16:13

1 Answers1

8

You can't set the interface address unless you are root (well, technically, unless you have CAP_NET_ADMIN). See devinet.c.

The solution is to run as root. How to implement that solution, whether to make your entire program SUID, or ask the user to run it via sudo or gksudo, or whether to factor your program into two parts (root and non-root), that choice is up to you.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308