Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net: mctp: Reject duplicate wildcard-peer binds
@ 2026-12-02 16:50 Andrew Stellman
  2026-12-04 11:00 ` Jeremy Kerr
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:50 UTC (permalink / raw)
  To: Jeremy Kerr, Matt Johnston
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel

From: Andrew Stellman <astellman@stellman-greene.com>

mctp_sk_hash() hashes an unconnected socket and one connected to
MCTP_ADDR_ANY into the same bucket, and mctp_lookup_bind_details()
matches both against any source EID. The duplicate check treats them
as different peers, so a second bind() with the same net, local EID
and type succeeds when only one socket is connected to ANY.

Compare effective peers (MCTP_ADDR_ANY if bind_peer_set is clear). A
duplicate bind() now fails with -EADDRINUSE when only one of the two
sockets is connected to ANY; it previously succeeded.

Fixes: 3549eb08e550 ("net: mctp: Allow limiting binds to a peer address")
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
Tested on net-next 19c484e12ed2 (7.4.0-rc4) in arm64 QEMU: a duplicate
bind() succeeded in both orders before the fix and returned EADDRINUSE
with it.
Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude Opus
5.5 and Claude Fable 5.2; the fix was drafted with OpenAI GPT-6 (Codex).
A reproducer is available on request.

 net/mctp/af_mctp.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/net/mctp/af_mctp.c b/net/mctp/af_mctp.c
index aa22c0860712..39e966996c54 100644
--- a/net/mctp/af_mctp.c
+++ b/net/mctp/af_mctp.c
@@ -729,9 +729,8 @@ static int mctp_sk_hash(struct sock *sk)
 		struct mctp_sock *mex =
 			container_of(existing, struct mctp_sock, sk);
 
-		bool same_peer = (mex->bind_peer_set && msk->bind_peer_set &&
-				  mex->bind_peer_addr == msk->bind_peer_addr) ||
-				 (!mex->bind_peer_set && !msk->bind_peer_set);
+		bool same_peer = (mex->bind_peer_set ? mex->bind_peer_addr :
+				  MCTP_ADDR_ANY) == remote;
 
 		if (mex->bind_type == msk->bind_type &&
 		    mex->bind_local_addr == msk->bind_local_addr && same_peer &&
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] net: mctp: Reject duplicate wildcard-peer binds
  2026-12-02 16:50 [PATCH net] net: mctp: Reject duplicate wildcard-peer binds Andrew Stellman
@ 2026-12-04 11:00 ` Jeremy Kerr
  0 siblings, 0 replies; 2+ messages in thread
From: Jeremy Kerr @ 2026-12-04 11:00 UTC (permalink / raw)
  To: Andrew Stellman, Matt Johnston
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel

Hi Andrew,

> mctp_sk_hash() hashes an unconnected socket and one connected to
> MCTP_ADDR_ANY into the same bucket, and mctp_lookup_bind_details()
> matches both against any source EID. The duplicate check treats them
> as different peers, so a second bind() with the same net, local EID
> and type succeeds when only one socket is connected to ANY.

OK, good find. connect(MCTP_ADDR_ANY) is a bit odd, perhaps we should
disallow that entirely...

> diff --git a/net/mctp/af_mctp.c b/net/mctp/af_mctp.c
> index aa22c0860712..39e966996c54 100644
> --- a/net/mctp/af_mctp.c
> +++ b/net/mctp/af_mctp.c
> @@ -729,9 +729,8 @@ static int mctp_sk_hash(struct sock *sk)
>  		struct mctp_sock *mex =
>  			container_of(existing, struct mctp_sock, sk);
>  
> -		bool same_peer = (mex->bind_peer_set && msk->bind_peer_set &&
> -				  mex->bind_peer_addr == msk->bind_peer_addr) ||
> -				 (!mex->bind_peer_set && !msk->bind_peer_set);
> +		bool same_peer = (mex->bind_peer_set ? mex->bind_peer_addr :
> +				  MCTP_ADDR_ANY) == remote;

I think the logic of the change is sound, but might benefit from
breaking it up so that we're not hiding the semantics of setting `remote`.

How about something like:

    mctp_eid_t ex_remote = mex->bind_peer_set ? mex->bind_peer_addr :
                            MCTP_ADDR_ANY;

    bool same_peer = ex_remote == remote;

Which then might make more sense to drop same_peer entirely:

diff --git a/net/mctp/af_mctp.c b/net/mctp/af_mctp.c
index 209a963112e3..145b8fb66f68 100644
--- a/net/mctp/af_mctp.c
+++ b/net/mctp/af_mctp.c
@@ -728,13 +728,13 @@ static int mctp_sk_hash(struct sock *sk)
        sk_for_each(existing, &net->mctp.binds[hash]) {
                struct mctp_sock *mex =
                        container_of(existing, struct mctp_sock, sk);
+               mctp_eid_t ex_remote = mex->bind_peer_set ?
+                       mex->bind_peer_addr : MCTP_ADDR_ANY;
 
-               bool same_peer = (mex->bind_peer_set && msk->bind_peer_set &&
-                                 mex->bind_peer_addr == msk->bind_peer_addr) ||
-                                (!mex->bind_peer_set && !msk->bind_peer_set);
 
                if (mex->bind_type == msk->bind_type &&
-                   mex->bind_local_addr == msk->bind_local_addr && same_peer &&
+                   mex->bind_local_addr == msk->bind_local_addr &&
+                   remote == ex_remote &&
                    mex->bind_net == msk->bind_net) {
                        rc = -EADDRINUSE;
                        goto out;

If that makes it any more clear?

Cheers,


Jeremy

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 11:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:50 [PATCH net] net: mctp: Reject duplicate wildcard-peer binds Andrew Stellman
2026-12-04 11:00 ` Jeremy Kerr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox