Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 1899824

Summary: netcat (nc) listener fails with "Bad file descriptor" after a connection is made
Product: Red Hat Enterprise Linux 8 Reporter: Pavel Zhukov <pzhukov>
Component: nmapAssignee: Martin Osvald 🛹 <mosvald>
Status: CLOSED ERRATA QA Contact: František Hrdina <fhrdina>
Severity: high Docs Contact:
Priority: high    
Version: 8.2CC: aajith, fhrdina, jbainbri, jorton, ndavids
Target Milestone: rcKeywords: AutoVerified, TestCaseNeeded, Triaged
Target Release: 8.0Flags: pm-rhel: mirror+
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: nmap-7.70-7.el8 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-11-08 09:38:42 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 Pavel Zhukov 2020-11-20 06:11:21 UTC
This bug was initially created as a copy of Bug #1899598

I am copying this bug because: 



Description of problem: netcat (nc) listener fails with "Bad file descriptor" after a connection is made

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

How reproducible: 100% of the time

Steps to Reproduce:
1. disable IPv6
2. reboot
3. nc -l 12345
4. Make connection from some other location

Actual results:

nc -l 12481
close: Bad file descriptor


Expected results:
nc does not fail

Additional info:
The problem can be avoided by using the "-4" argument to open only the ipv4 socket or using "-k" so as not to close the listening sockets after a connection is made.

I am pretty sure that the problem is that with IPv6 disabled the socket call to create the AFINET6 socket fails and the variable holding the FD for the socket is set to -1.  This is not checked when the listening sockets are closed after the connection so the close fails and nc aborts.

Comment 3 Pavel Zhukov 2021-02-08 14:33:07 UTC
(In reply to Pavel Zhukov from comment #0)

> 
> I am pretty sure that the problem is that with IPv6 disabled the socket call
> to create the AFINET6 socket fails and the variable holding the FD for the
> socket is set to -1.  This is not checked when the listening sockets are
> closed after the connection so the close fails and nc aborts.

That's not root cause of the issue. 
The issue is in resolve() [1] and underlying getaddrinfo() call which returns AF_INET6 as a family even if IPv6 
is disabled. Besides of that ncat_listen_stream() has a bug with listen_socket[] array length calculation if wrong (-1) FD
is the very last in the list.

[1] https://github.com/nmap/nmap/blob/master/ncat/ncat_main.c#L1061

Comment 19 errata-xmlrpc 2022-11-08 09:38:42 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 (nmap bug fix and enhancement update), 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-2022:7553

Comment 20 Arun Ajith S 2023-06-15 08:37:57 UTC
There's a problem with the fix.
If AI_ADDRCONFIG is used, it doesn't listen on loopback by default.
So nc -l no longer shows any services listening on loopback address.

Comment 21 Martin Osvald 🛹 2023-06-15 10:17:52 UTC
(In reply to Arun Ajith S from comment #20)
> There's a problem with the fix.
> If AI_ADDRCONFIG is used, it doesn't listen on loopback by default.
> So nc -l no longer shows any services listening on loopback address.

Hi Arun,

please, could you share the steps to reproduce and the current and the expected output? Thanks!

Comment 22 Arun Ajith S 2023-06-15 15:35:21 UTC
Hi Martin,
My comment above is not entirely accurate.

Let me describe what I can repro. We have some tests running in a network isolated environment which just has loopback interface. We run `nc -ltnp <PORT>` in the test to simulate a server and our unit under test talks to the server. In el9 environment, we're seeing that `nc` fails.

This is how you can repro
```
> sudo unshare -n bash
# ip link show
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

# rpm -qf `which nc`
nmap-ncat-7.91-12.el9.x86_64
# nc -ntlp 686
Ncat: Failed to resolve default IPv4 address: Name or service not known. QUITTING.
```

Now if you run this in the el7 environment without the `AI_ADDRCONFIG` patch
```
> sudo unshare -n bash
# ip link show
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# rpm -qf `which nc`
nmap-ncat-2:6.40-19.el7.x86_64
# nc -ntlp 686
^Z
[1]+  Stopped                 nc -ntlp 686
# ss -lnpt
State      Recv-Q Send-Q                                                           Local Address:Port                                                                          Peer Address:Port
LISTEN     0      10                                                                           *:686                                                                                      *:*                   users:(("nc",pid=39676,fd=4))
LISTEN     0      10                                                                        [::]:686                                                                                   [::]:*                   users:(("nc",pid=39676,fd=3))
```

From https://man7.org/linux/man-pages/man3/getaddrinfo.3.html
```
       If hints.ai_flags includes the AI_ADDRCONFIG flag, then IPv4
       addresses are returned in the list pointed to by res only if the
       local system has at least one IPv4 address configured, and IPv6
       addresses are returned only if the local system has at least one
       IPv6 address configured.  The loopback address is not considered
       for this case as valid as a configured address.
```
So it doesn't consider `lo` as a valid interface on which it can listen. Usually servers just set `AI_PASSIVE` in the flags and pass `node` as `NULL`.