Bug 238781 - sys.netinet.socket_afinet.socket_afinet_bind_zero does not work when mac_portacl(4) loaded
Summary: sys.netinet.socket_afinet.socket_afinet_bind_zero does not work when mac_port...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: tests (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Mark Johnston
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-23 19:26 UTC by Li-Wen Hsu
Modified: 2026-05-07 20:41 UTC (History)
6 users (show)

See Also:


Attachments
[PATCH] tests/socket_afinet: unskip socket_afinet_bind_zero testcase (961 bytes, patch)
2025-09-08 17:28 UTC, Siva Mahadevan
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Li-Wen Hsu freebsd_committer freebsd_triage 2019-06-23 19:26:15 UTC
sys.netinet.socket_afinet.socket_afinet_bind_zero does not work when mac_portacl(4) loaded, while mac_portacl is needed for tests under /usr/tests/sys/mac/portacl.

For this simplified program:

#include <sys/errno.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
        int sd, rc;
        struct sockaddr_in sin;

        sd = socket(PF_INET, SOCK_DGRAM, 0);
        bzero(&sin, sizeof(sin));
        //sin.sin_family = AF_INET;
        rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
        if (rc < 0) {
                perror("bind failed");
        } else {
                printf("bind ok.\n");
        }
        close(sd);
        return 0;
}


When mac_portacl(4) is loaded, it outputs:

bind failed: Invalid argument

And we found that uncomment line 16, sin.sin_family = AF_INET, will make bind(2) work. 

From bz:

it could be that portacl enforces the error because of the missing sin_family which we by policy do not enforce for AF_INET (per comment in in_pcb.c [1]).

[1] https://svnweb.freebsd.org/base/head/sys/netinet/in_pcb.c?annotate=346677#l816
Comment 1 commit-hook freebsd_committer freebsd_triage 2019-06-23 19:37:38 UTC
A commit references this bug:

Author: lwhsu
Date: Sun Jun 23 19:37:12 UTC 2019
New revision: 349322
URL: https://svnweb.freebsd.org/changeset/base/349322

Log:
  Skip sys.netinet.socket_afinet.socket_afinet_bind_zero temporarily because it
  doesn't work when mac_portacl(4) loaded

  PR:		238781
  Sponsored by:	The FreeBSD Foundation

Changes:
  head/tests/sys/netinet/socket_afinet.c
Comment 2 Robert Watson freebsd_committer freebsd_triage 2019-08-12 19:31:08 UTC
The (sin_family == 0) thing is unfortunate, but presumably baked in for decades. Does Linux also allow that behavior? If so, I suspect we're pretty stuck with it, and should teach mac_portacl(4) that '0' means AF_INET. If not .. maybe it is still possible to change.
Comment 3 Ed Maste freebsd_committer freebsd_triage 2019-08-14 19:32:06 UTC
(In reply to Robert Watson from comment #2)
It looks like Linux does have many cases allowing AF_UNSPEC instead of AF_INET.
Comment 4 Ed Maste freebsd_committer freebsd_triage 2019-08-14 20:55:57 UTC
Perhaps we should add a security.mac.portacl.family_0_inet sysctl?
Comment 5 Robert Watson freebsd_committer freebsd_triage 2019-08-15 08:21:11 UTC
Am I right in assuming that even when sin_family is set to 0, the sockaddr is always interpreted as a sockaddr_in for PF_INET sockets? If so, then, given that we check for PF_INET earlier in mac_portacl's socket_check_bind() function, we can probably safely allow 0 as well as AF_INET and AF_INET6.
Comment 6 Ed Maste freebsd_committer freebsd_triage 2019-08-15 21:29:37 UTC
(In reply to Robert Watson from comment #5)
I believe that is indeed the case, although I did not do a comprehensive survey.

Anyhow, I wonder how common it is for software (excluding the failing test under discussion here) to fail to set sin_family?
Comment 7 Robert Watson freebsd_committer freebsd_triage 2019-08-21 09:07:05 UTC
Given that it appears to be a portable behavior, I'm not against adding AF_UNSPEC support to mac_portacl, as long as PF_INET sockets are in use. This causes me to wonder: is there AF_UNSPEC behavior for PF_INET6 on FreeBSD or Linux, however?
Comment 8 Rin 2025-04-09 20:23:03 UTC
https://github.com/freebsd/freebsd-src/pull/1653

It seems to be fixed on CURRENT.

$ sudo kyua test socket_afinet:socket_afinet_bind_zero
socket_afinet:socket_afinet_bind_zero  ->  passed  [0.001s]
Comment 9 Rin 2025-04-10 06:03:57 UTC
(In reply to Rin from comment #8)
Sorry, it's my fault.
This bug haven't been fixed at all.
Comment 10 Rin 2025-04-12 03:18:47 UTC
https://github.com/freebsd/freebsd-src/pull/1659

mac_portacl: do not reject unspecific family directly.

I think this commit could fix this bug.

However, I am not sure if there's some potential security issue.
Comment 11 Siva Mahadevan freebsd_committer freebsd_triage 2025-09-08 17:28:30 UTC
Created attachment 263614 [details]
[PATCH] tests/socket_afinet: unskip socket_afinet_bind_zero testcase

I can confirm the 'socket_afinet_bind_zero' testcase passes consistently now and can be unskipped. I have attached a patch to unskip the test, and this bug can be closed afterwards.
Comment 12 commit-hook freebsd_committer freebsd_triage 2026-04-17 12:49:08 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9f6041fb579106aa6fa358071a14eddabd581998

commit 9f6041fb579106aa6fa358071a14eddabd581998
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-04-17 12:48:39 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-04-17 12:48:39 +0000

    tests/socket_afinet: Remove a conditional skip

    The test runs without any unexpected results when mac_portacl is loaded.

    PR:             238781
    MFC after:      1 week

 tests/sys/netinet/socket_afinet.c | 3 ---
 1 file changed, 3 deletions(-)
Comment 13 commit-hook freebsd_committer freebsd_triage 2026-05-03 15:46:50 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ae6c11642b54f304490434ec5fe1eb2b470ff620

commit ae6c11642b54f304490434ec5fe1eb2b470ff620
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-04-17 12:48:39 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-05-03 14:40:10 +0000

    tests/socket_afinet: Remove a conditional skip

    The test runs without any unexpected results when mac_portacl is loaded.

    PR:             238781
    MFC after:      1 week

    (cherry picked from commit 9f6041fb579106aa6fa358071a14eddabd581998)

 tests/sys/netinet/socket_afinet.c | 3 ---
 1 file changed, 3 deletions(-)
Comment 14 commit-hook freebsd_committer freebsd_triage 2026-05-07 20:41:16 UTC
A commit in branch releng/15.1 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5dca0c89c81e2c4c3596e968115ef2170b681a11

commit 5dca0c89c81e2c4c3596e968115ef2170b681a11
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-04-17 12:48:39 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-05-07 20:40:12 +0000

    tests/socket_afinet: Remove a conditional skip

    The test runs without any unexpected results when mac_portacl is loaded.

    Approved by:    re (cperciva)
    PR:             238781
    MFC after:      1 week

    (cherry picked from commit 9f6041fb579106aa6fa358071a14eddabd581998)
    (cherry picked from commit ae6c11642b54f304490434ec5fe1eb2b470ff620)

 tests/sys/netinet/socket_afinet.c | 3 ---
 1 file changed, 3 deletions(-)