Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

net: mctp: Prevent duplicate binds

Disallow bind() calls that have the same arguments as existing bound
sockets. Previously multiple sockets could bind() to the same
type/local address, with an arbitrary socket receiving matched messages.

This is only a partial fix, a future commit will define precedence order
for MCTP_ADDR_ANY versus specific EID bind(), which are allowed to exist
together.

Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
Link: https://patch.msgid.link/20250710-mctp-bind-v4-2-8ec2f6460c56@codeconstruct.com.au
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Matt Johnston and committed by
Paolo Abeni
39545023 3558ab79

+24 -4
+24 -4
net/mctp/af_mctp.c
··· 73 73 74 74 lock_sock(sk); 75 75 76 - /* TODO: allow rebind */ 77 76 if (sk_hashed(sk)) { 78 77 rc = -EADDRINUSE; 79 78 goto out_release; ··· 610 611 static int mctp_sk_hash(struct sock *sk) 611 612 { 612 613 struct net *net = sock_net(sk); 614 + struct sock *existing; 615 + struct mctp_sock *msk; 616 + int rc; 617 + 618 + msk = container_of(sk, struct mctp_sock, sk); 613 619 614 620 /* Bind lookup runs under RCU, remain live during that. */ 615 621 sock_set_flag(sk, SOCK_RCU_FREE); 616 622 617 623 mutex_lock(&net->mctp.bind_lock); 618 - sk_add_node_rcu(sk, &net->mctp.binds); 619 - mutex_unlock(&net->mctp.bind_lock); 620 624 621 - return 0; 625 + /* Prevent duplicate binds. */ 626 + sk_for_each(existing, &net->mctp.binds) { 627 + struct mctp_sock *mex = 628 + container_of(existing, struct mctp_sock, sk); 629 + 630 + if (mex->bind_type == msk->bind_type && 631 + mex->bind_addr == msk->bind_addr && 632 + mex->bind_net == msk->bind_net) { 633 + rc = -EADDRINUSE; 634 + goto out; 635 + } 636 + } 637 + 638 + sk_add_node_rcu(sk, &net->mctp.binds); 639 + rc = 0; 640 + 641 + out: 642 + mutex_unlock(&net->mctp.bind_lock); 643 + return rc; 622 644 } 623 645 624 646 static void mctp_sk_unhash(struct sock *sk)