PF_RING icon indicating copy to clipboard operation
PF_RING copied to clipboard

PF_Ring ZC usable as non-root ?

Open desbma opened this issue 9 years ago • 20 comments

Quick question:

Is possible to use PF_Ring ZC as non-root user?

In the code, there is a check for the CAP_NET_ADMIN capability, however even with this capability and CAP_NET_RAW the socket system call returns -EPERM.

Thank you

desbma avatar Jul 28 '17 14:07 desbma

@desbma for standard PF_RING you just need to setcap cap_net_admin+eip, for PF_RING ZC you need to setcap cap_net_admin,cap_ipc_lock,cap_sys_admin+eip

cardigliano avatar Jul 28 '17 17:07 cardigliano

Sorry for the late reply, but it does not seem to work as non-root with the capabilities you suggested:

$ sudo setcap 'cap_net_admin,cap_ipc_lock,cap_sys_admin=eip' ~/PF_RING-6.6.0/userland/examples_zc/zcount

$ sudo getcap ~/PF_RING-6.6.0/userland/examples_zc/zcount
~/PF_RING-6.6.0/userland/examples_zc/zcount = cap_net_admin,cap_ipc_lock,cap_sys_admin+eip

$ ~/PF_RING-6.6.0/userland/examples_zc/zcount -i zc:enp15s0f0 -c 0 -g 4
pfring_zc_create_cluster error [No buffer space available] Please check that pf_ring.ko is loaded and hugetlb fs is mounted

$ sudo ~/PF_RING-6.6.0/userland/examples_zc/zcount -i zc:enp15s0f0 -c 0 -g 4
=========================
Absolute Stats: 0 pkts (0 drops) - 0 bytes
=========================

=========================
Absolute Stats: 0 pkts (0 drops) - 0 bytes
Actual Stats: 0.00 pps (0.00 drops) - 0.00 Gbps
=========================

...

desbma avatar Aug 08 '17 09:08 desbma

@cardigliano Using strace, I can see it is the socket system call, that is returning -EPERM, even with the 3 capabilities: socket(0x1b /* PF_RING */, SOCK_RAW, 768) = -1 EPERM (Operation not permitted)

desbma avatar Aug 11 '17 09:08 desbma

@cardigliano Did you reproduce the issue as non-root with the commands in https://github.com/ntop/PF_RING/issues/245#issuecomment-320901143?

Am I missing another capability?

desbma avatar Aug 16 '17 13:08 desbma

@desbma I am not able to reproduce the socket error, however I am able to reproduce an error with hugepages permissions when using zcount (instead it works with pfcount as it can also use standard kernel memory). I will check what is the best way to configure permissions to the hugetlb fs asap, in the meanwhile please confirm pfcount works for you.

cardigliano avatar Aug 17 '17 08:08 cardigliano

Same error with pfcount, with same socket EPERM error code shown by strace. The same pfcount command works fine as root.

desbma avatar Aug 17 '17 08:08 desbma

I am having the exact same issue. Have there been any progress with this?

onursoyer avatar Aug 27 '18 02:08 onursoyer

@onursoyer could you provide the command/configuration you are using please? Thank you.

cardigliano avatar Aug 27 '18 07:08 cardigliano

I have installed PF_RING ZC by following instructions here.

$ uname -a
'Linux ubuntu 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.4 LTS
Release:	16.04
Codename:	xenial
$ zcount -h
zcount - (C) 2014-2018 ntop.org
Using PFRING_ZC v.7.2.0.180626
A simple packet counter application.
$ /sbin/ethtool -i enp130s0f1
driver: ixgbe
version: 5.3.7
firmware-version: 0x2b2c0001
expansion-rom-version:
bus-info: 0000:82:00.1
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: yes
$ cat /proc/net/pf_ring/dev/enp130s0f1/info | grep ZC
Polling Mode: NAPI/ZC
$ modinfo pf_ring
filename:       /lib/modules/4.4.0-116-generic/updates/dkms/pf_ring.ko
alias:          net-pf-27
version:        7.2.0
description:    Packet capture acceleration and analysis
author:         ntop.org
license:        GPL
srcversion:     0B168ED2BB628A0718E64F9
depends:
vermagic:       4.4.0-116-generic SMP mod_unload modversions retpoline
parm:           min_num_slots:Min number of ring slots (uint)
parm:           perfect_rules_hash_size:Perfect rules hash size (uint)
parm:           enable_tx_capture:Set to 1 to capture outgoing packets (uint)
parm:           enable_frag_coherence:Set to 1 to handle fragments (flow coherence) in clusters (uint)
parm:           enable_ip_defrag:Set to 1 to enable IP defragmentation(only rx traffic is defragmentead) (uint)
parm:           quick_mode:Set to 1 to run at full speed but with upto one socket per interface (uint)
parm:           force_ring_lock:Set to 1 to force ring locking (automatically enable with rss) (uint)
parm:           enable_debug:Set to 1 to enable PF_RING debug tracing into the syslog, 2 for more verbosity (uint)
parm:           transparent_mode:(deprecated) (uint)
$ cat /sys/devices/system/node/node*/meminfo | grep Huge
Node 0 AnonHugePages:      8192 kB
Node 0 HugePages_Total:  2048
Node 0 HugePages_Free:   2048
Node 0 HugePages_Surp:      0
Node 1 AnonHugePages:     22528 kB
Node 1 HugePages_Total:     0
Node 1 HugePages_Free:      0
Node 1 HugePages_Surp:      0
$ sudo setcap 'cap_net_admin,cap_ipc_lock,cap_sys_admin=eip' /usr/bin/zcount

$ sudo getcap /usr/bin/zcount
/usr/bin/zcount = cap_net_admin,cap_ipc_lock,cap_sys_admin+eip

$ zcount -i zc:enp130s0f1 -c 0 -g 4
pfring_zc_create_cluster error [Socket operation on non-socket] Please check that pf_ring.ko is loaded and hugetlb fs is mounted

$ sudo zcount -i zc:enp130s0f1 -c 0 -g 4
=========================
Absolute Stats: 172 pkts (0 drops) - 34'552 bytes
=========================

Leaving...
=========================
Absolute Stats: 274 pkts (0 drops) - 45'931 bytes
Actual Stats: 142.74 pps (0.00 drops) - 0.00 Gbps
=========================

=========================
Absolute Stats: 274 pkts (0 drops) - 45'931 bytes
Actual Stats: 0.00 pps (0.00 drops) - 0.00 Gbps
=========================

onursoyer avatar Aug 28 '18 07:08 onursoyer

Please note that cap_net_admin has been replaced by cap_net_raw, please use: sudo setcap 'cap_net_raw,cap_ipc_lock,cap_sys_admin=eip' Please also note that if you want to use zcount you should also configure hugepages permissions (instead pfcount that does not require hugepages).

cardigliano avatar Aug 28 '18 09:08 cardigliano

@onursoyer please find the documentation about capabilities at: https://github.com/ntop/PF_RING/blob/dev/doc/capabilities.rst

cardigliano avatar Aug 28 '18 10:08 cardigliano

Unfortunately, I wasn't able to run it for non-root user.

$ id -g
1003

$ mount |grep huge
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
none on /dev/hugepages type hugetlbfs (rw,relatime,gid=1003)

Also added the following lines to /etc/sysctl.conf and rebooted server.

vm.nr_hugepages=1024
vm.hugetlb_shm_group=1003

Re-set setcap as the way you have suggested.

$ sudo setcap 'cap_net_raw,cap_ipc_lock,cap_sys_admin=eip' /usr/bin/zcount

$ sudo getcap /usr/bin/zcount
/usr/bin/zcount = cap_net_raw,cap_ipc_lock,cap_sys_admin+eip

But zcount returns the same error when executing without sudo.

$ zcount -i zc:enp130s0f1 -c 0 -g 4
pfring_zc_create_cluster error [No buffer space available] Please check that pf_ring.ko is loaded and hugetlb fs is mounted

$ strace zcount -i zc:enp130s0f1 -c 0 -g 4
execve("/usr/bin/zcount", ["zcount", "-i", "zc:enp130s0f1", "-c", "0", "-g", "4"], [/* 49 vars */]) = 0
brk(NULL)                               = 0x14f0000
fcntl(0, F_GETFD)                       = 0
fcntl(1, F_GETFD)                       = 0
fcntl(2, F_GETFD)                       = 0
access("/etc/suid-debug", F_OK)         = -1 ENOENT (No such file or directory)
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=52660, ...}) = 0
mmap(NULL, 52660, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2ea2ffa000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/librt.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0!\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=31712, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2ea2ff9000
mmap(NULL, 2128832, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2ea2bda000
mprotect(0x7f2ea2be1000, 2093056, PROT_NONE) = 0
mmap(0x7f2ea2de0000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x7f2ea2de0000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\r\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=14608, ...}) = 0
mmap(NULL, 2109680, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2ea29d6000
mprotect(0x7f2ea29d9000, 2093056, PROT_NONE) = 0
mmap(0x7f2ea2bd8000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f2ea2bd8000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260`\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=138696, ...}) = 0
mmap(NULL, 2212904, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2ea27b9000
mprotect(0x7f2ea27d1000, 2093056, PROT_NONE) = 0
mmap(0x7f2ea29d0000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x7f2ea29d0000
mmap(0x7f2ea29d2000, 13352, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2ea29d2000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\t\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2ea2ff8000
mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2ea23ef000
mprotect(0x7f2ea25af000, 2097152, PROT_NONE) = 0
mmap(0x7f2ea27af000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f2ea27af000
mmap(0x7f2ea27b5000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2ea27b5000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2ea2ff7000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2ea2ff6000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2ea2ff5000
arch_prctl(ARCH_SET_FS, 0x7f2ea2ff6700) = 0
mprotect(0x7f2ea27af000, 16384, PROT_READ) = 0
mprotect(0x7f2ea29d0000, 4096, PROT_READ) = 0
mprotect(0x7f2ea2bd8000, 4096, PROT_READ) = 0
mprotect(0x7f2ea2de0000, 4096, PROT_READ) = 0
mprotect(0x6af000, 8192, PROT_READ)     = 0
mprotect(0x7f2ea3007000, 4096, PROT_READ) = 0
munmap(0x7f2ea2ffa000, 52660)           = 0
set_tid_address(0x7f2ea2ff69d0)         = 87162
set_robust_list(0x7f2ea2ff69e0, 24)     = 0
rt_sigaction(SIGRTMIN, {0x7f2ea27beb50, [], SA_RESTORER|SA_SIGINFO, 0x7f2ea27ca390}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {0x7f2ea27bebe0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x7f2ea27ca390}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
brk(NULL)                               = 0x14f0000
brk(0x1511000)                          = 0x1511000
access("/proc/net/pf_ring/dev/enp130s0f1/info", F_OK) = 0
socket(0x1b /* PF_??? */, SOCK_RAW, 768) = -1 EPERM (Operation not permitted)
stat("/sys/devices/system/node/node0/cpumap", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
stat("/sys/devices/system/node/node1/cpumap", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
stat("/sys/devices/system/node/node2/cpumap", 0x7fff0f5be440) = -1 ENOENT (No such file or directory)
open("/sys/devices/system/node/node0/cpumap", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
read(3, "00000000,00000000,00000000,00000"..., 4096) = 54
close(3)                                = 0
stat("/sys/devices/system/node/node0/cpumap", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
stat("/sys/devices/system/node/node1/cpumap", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
stat("/sys/devices/system/node/node2/cpumap", 0x7fff0f5be4f0) = -1 ENOENT (No such file or directory)
stat("/sys/devices/system/node/node0/cpumap", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
stat("/sys/devices/system/node/node1/cpumap", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
stat("/sys/devices/system/node/node2/cpumap", 0x7fff0f5be4d0) = -1 ENOENT (No such file or directory)
set_mempolicy(MPOL_BIND 0x14f00b0, 4)   = 0
socket(0x1b /* PF_??? */, SOCK_RAW, 768) = -1 EPERM (Operation not permitted)
write(2, "pfring_zc_create_cluster error ["..., 129pfring_zc_create_cluster error [Socket operation on non-socket] Please check that pf_ring.ko is loaded and hugetlb fs is mounted
) = 129
exit_group(-1)                          = ?
+++ exited with 255 +++

Is there something I'm missing?

onursoyer avatar Aug 30 '18 05:08 onursoyer

It seems that you are getting EPERM opening the pf_ring socket, which is not due to hugepages permissions. This is strange as with the capabilities you are setting it should work. Are you running kernel 4.4 here?

cardigliano avatar Aug 30 '18 07:08 cardigliano

Yes.

$ uname -a
Linux ubuntu 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

onursoyer avatar Aug 30 '18 08:08 onursoyer

Please provide cat /proc/net/pf_ring/info

cardigliano avatar Aug 30 '18 09:08 cardigliano

$ cat /proc/net/pf_ring/info
PF_RING Version          : 7.2.0 (7.2.0-stable:758f764cf6243c81d61d7b6d7cd1ccee04aaa1b6)
Total rings              : 11

Standard (non ZC) Options
Ring slots               : 4096
Slot version             : 17
Capture TX               : Yes [RX+TX]
IP Defragment            : No
Socket Mode              : Standard
Cluster Fragment Queue   : 0
Cluster Fragment Discard : 0

onursoyer avatar Aug 31 '18 06:08 onursoyer

@onursoyer if you look at the command without strace, you can see that it fails due to hugepages (No buffer space available), this happens because your kernel does not allow ZC to map physical pages. This should not happen in 4.4 as stated below, however it seems this is the case with your kernel revision. (Note that when runing strace, it fails opening the socket, but this is because with strace it does not have capabilities for that)

https://github.com/torvalds/linux/blob/v4.4/Documentation/vm/pagemap.txt "Since Linux 4.0 only users with the CAP_SYS_ADMIN capability can get PFNs. In 4.0 and 4.1 opens by unprivileged fail with -EPERM. Starting from 4.2 the PFN field is zeroed if the user does not have CAP_SYS_ADMIN."

cardigliano avatar Aug 31 '18 07:08 cardigliano

I think I am still missing something.

Say If I set mount -t hugetlbfs -o gid=1002 nodev /dev/hugepages, do I have to restart PF_RING service to make the changes take effects?

If so, I would like add that configuration to hugepages.conf file. I couldn't see any explanation about adding group id to hugepages.conf.

I tried something like echo "gid=1002 node=0 hugepagenumber=1024" > /etc/pf_ring/hugepages.conf to see if gid would be added to mount options. But it didn't work.

onursoyer avatar Sep 04 '18 02:09 onursoyer

@onursoyer unfortunately setting the gid through the pf_ring configuration files is not currently supported (all our users start the application as privileged user), we will add this setting asap

cardigliano avatar Sep 04 '18 06:09 cardigliano

Try setting max locked memory ulimit. Example here for the user unprivusername

/etc/security/limits.d/user.conf

#for PFring ZC hugepages - 10% of mem lockable
unprivusername     -       memlock     26403225

Then log out the shell and restart the session. check with ulimit -a

markabey avatar Oct 18 '18 15:10 markabey