Bug 1766415

Summary: selinux blocks fail2ban nftables access
Product: Red Hat Enterprise Linux 8 Reporter: Dan Tucny <d>
Component: selinux-policyAssignee: Lukas Vrabec <lvrabec>
Status: CLOSED ERRATA QA Contact: Milos Malik <mmalik>
Severity: low Docs Contact:
Priority: medium    
Version: 8.0CC: lvrabec, mmalik, plautrba, ssekidde, zpytela
Target Milestone: rcKeywords: Patch
Target Release: 8.2   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-04-28 16:41:25 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Dan Tucny 2019-10-29 01:36:38 UTC
Description of problem:
Attempting to use fail2ban with it's nftables actions results in a failure as a result of selinux blocking access to netfilter socket.

Version-Release number of selected component (if applicable):


How reproducible:
Always when using the nftables action

Steps to Reproduce:
1. Install fail2ban from epel
2. Configure it to use banaction = nftables-multiport
3. Start the service

Actual results:
Fail2ban fails to add it's initial set and rule.

e.g.
2019-10-29 08:17:15,115 fail2ban.utils          [709]: #39-Lev. 7f37701a8d20 -- exec: nft add set inet filter f2b-sshd \{ type ipv4_addr\; \}
nft insert rule inet filter input tcp dport \{ ssh \} ip saddr @f2b-sshd reject
2019-10-29 08:17:15,115 fail2ban.utils          [709]: ERROR   7f37701a8d20 -- stderr: 'netlink.c:62: Unable to initialize Netlink socket: Permission denied'
2019-10-29 08:17:15,116 fail2ban.utils          [709]: ERROR   7f37701a8d20 -- stderr: 'netlink.c:62: Unable to initialize Netlink socket: Permission denied'
2019-10-29 08:17:15,116 fail2ban.utils          [709]: ERROR   7f37701a8d20 -- returned 3


Expected results:
Fail2ban is able to create rules/sets using nftables the same way that it can with iptables and firewall-cmd.

Additional info:
SELinux Enforcing audit log:
type=AVC msg=audit(1572312631.453:443): avc:  denied  { create } for  pid=6442 comm="nft" scontext=system_u:system_r:fail2ban_t:s0 tcontext=system_u:system_r:fail2ban_t:s0 tclass=netlink_netfilter_socket permissive=0
type=SYSCALL msg=audit(1572312631.453:443): arch=c000003e syscall=41 success=no exit=-13 a0=10 a1=3 a2=c a3=560750d5e010 items=0 ppid=6441 pid=6442 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="nft" exe="/usr/sbin/nft" subj=system_u:system_r:fail2ban_t:s0 key=(null)ARCH=x86_64 SYSCALL=socket AUID="unset" UID="root" GID="root" EUID="root" SUID="root" FSUID="root" EGID="root" SGID="root" FSGID="root"
type=AVC msg=audit(1572312631.455:444): avc:  denied  { create } for  pid=6441 comm="nft" scontext=system_u:system_r:fail2ban_t:s0 tcontext=system_u:system_r:fail2ban_t:s0 tclass=netlink_netfilter_socket permissive=0
type=SYSCALL msg=audit(1572312631.455:444): arch=c000003e syscall=41 success=no exit=-13 a0=10 a1=3 a2=c a3=55ec973b5010 items=0 ppid=6435 pid=6441 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="nft" exe="/usr/sbin/nft" subj=system_u:system_r:fail2ban_t:s0 key=(null)ARCH=x86_64 SYSCALL=socket AUID="unset" UID="root" GID="root" EUID="root" SUID="root" FSUID="root" EGID="root" SGID="root" FSGID="root"

SELinux Permissive audit log:
type=AVC msg=audit(1572312672.597:463): avc:  denied  { setopt } for  pid=6480 comm="nft" scontext=system_u:system_r:fail2ban_t:s0 tcontext=system_u:system_r:fail2ban_t:s0 tclass=netlink_netfilter_socket permissive=1
type=SYSCALL msg=audit(1572312672.597:463): arch=c000003e syscall=54 success=yes exit=0 a0=3 a1=1 a2=20 a3=7ffd9825074c items=0 ppid=6479 pid=6480 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="nft" exe="/usr/sbin/nft" subj=system_u:system_r:fail2ban_t:s0 key=(null)ARCH=x86_64 SYSCALL=setsockopt AUID="unset" UID="root" GID="root" EUID="root" SUID="root" FSUID="root" EGID="root" SGID="root" FSGID="root"

Audit2allow generated rule from audit log:
#============= fail2ban_t ==============
allow fail2ban_t self:netlink_netfilter_socket { create setopt };

Comment 1 Lukas Vrabec 2019-10-29 09:44:14 UTC
Fixes from Fedora: 
commit 0f1e997af97e443afea394e896e8a368ad80c32b (HEAD -> rawhide)
Author: Lukas Vrabec <lvrabec>
Date:   Tue Oct 29 10:43:34 2019 +0100

    Allow fail2ban_t domain to create netlink netfilter sockets.

Comment 4 Milos Malik 2019-10-29 15:19:04 UTC
Please run the following commands and attach their output here:

# rpm -qa selinux\* | sort
# ls -Z `which nft`
# matchpathcon `which nft`

Thank you.

Comment 11 Dan Tucny 2020-02-27 12:04:33 UTC
I had some challenges reproducing this later when trying and found that this can only currently be reproduced in 8.0, either by installing 8.0 and not updating to 8.1 or by switching to the 8.0 track.

Updates included in 8.1 appear to have resolved the specifically reported issue.

On an 8.0 fresh install:
[root@localhost ~]# rpm -qa selinux\* | sort
selinux-policy-3.14.1-61.el8.noarch
selinux-policy-targeted-3.14.1-61.el8.noarch
[root@localhost ~]# ls -Z `which nft`
system_u:object_r:bin_t:s0 /usr/sbin/nft
[root@localhost ~]# matchpathcon `which nft`
/usr/sbin/nft   system_u:object_r:bin_t:s0

On a host updated to 8.1:
[root@r8-test1 ~]# rpm -qa selinux\* | sort
selinux-policy-3.14.3-20.el8.noarch
selinux-policy-targeted-3.14.3-20.el8.noarch
[root@r8-test1 ~]# ls -Z `which nft`
system_u:object_r:iptables_exec_t:s0 /usr/sbin/nft
[root@r8-test1 ~]# matchpathcon `which nft`
/usr/sbin/nft   system_u:object_r:iptables_exec_t:s0

Comment 12 Milos Malik 2020-02-27 13:11:21 UTC
(In reply to Dan Tucny from comment #11)

> Updates included in 8.1 appear to have resolved the specifically reported
> issue.

Most likely result of https://bugzilla.redhat.com/show_bug.cgi?id=1656891

Comment 14 errata-xmlrpc 2020-04-28 16:41:25 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2020:1773