Commit 48e50dcb authored by Matthieu Baerts (NGI0)'s avatar Matthieu Baerts (NGI0) Committed by Jakub Kicinski

mptcp: pm: avoid possible UaF when selecting endp

select_local_address() and select_signal_address() both select an
endpoint entry from the list inside an RCU protected section, but return
a reference to it, to be read later on. If the entry is dereferenced
after the RCU unlock, reading info could cause a Use-after-Free.

A simple solution is to copy the required info while inside the RCU
protected section to avoid any risk of UaF later. The address ID might
need to be modified later to handle the ID0 case later, so a copy seems
OK to deal with.
Reported-by: default avatarPaolo Abeni <pabeni@redhat.com>
Closes: https://lore.kernel.org/45cd30d3-7710-491c-ae4d-a1368c00beb1@redhat.com
Fixes: 01cacb00 ("mptcp: add netlink-based PM")
Cc: stable@vger.kernel.org
Reviewed-by: default avatarMat Martineau <martineau@kernel.org>
Signed-off-by: default avatarMatthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/20240819-net-mptcp-pm-reusing-id-v1-14-38035d40de5b@kernel.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 4878f9f8
...@@ -143,11 +143,13 @@ static bool lookup_subflow_by_daddr(const struct list_head *list, ...@@ -143,11 +143,13 @@ static bool lookup_subflow_by_daddr(const struct list_head *list,
return false; return false;
} }
static struct mptcp_pm_addr_entry * static bool
select_local_address(const struct pm_nl_pernet *pernet, select_local_address(const struct pm_nl_pernet *pernet,
const struct mptcp_sock *msk) const struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *new_entry)
{ {
struct mptcp_pm_addr_entry *entry, *ret = NULL; struct mptcp_pm_addr_entry *entry;
bool found = false;
msk_owned_by_me(msk); msk_owned_by_me(msk);
...@@ -159,17 +161,21 @@ select_local_address(const struct pm_nl_pernet *pernet, ...@@ -159,17 +161,21 @@ select_local_address(const struct pm_nl_pernet *pernet,
if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap)) if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
continue; continue;
ret = entry; *new_entry = *entry;
found = true;
break; break;
} }
rcu_read_unlock(); rcu_read_unlock();
return ret;
return found;
} }
static struct mptcp_pm_addr_entry * static bool
select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk) select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *new_entry)
{ {
struct mptcp_pm_addr_entry *entry, *ret = NULL; struct mptcp_pm_addr_entry *entry;
bool found = false;
rcu_read_lock(); rcu_read_lock();
/* do not keep any additional per socket state, just signal /* do not keep any additional per socket state, just signal
...@@ -184,11 +190,13 @@ select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk) ...@@ -184,11 +190,13 @@ select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk)
if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
continue; continue;
ret = entry; *new_entry = *entry;
found = true;
break; break;
} }
rcu_read_unlock(); rcu_read_unlock();
return ret;
return found;
} }
unsigned int mptcp_pm_get_add_addr_signal_max(const struct mptcp_sock *msk) unsigned int mptcp_pm_get_add_addr_signal_max(const struct mptcp_sock *msk)
...@@ -512,9 +520,10 @@ __lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info) ...@@ -512,9 +520,10 @@ __lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info)
static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
{ {
struct mptcp_pm_addr_entry *local, *signal_and_subflow = NULL;
struct sock *sk = (struct sock *)msk; struct sock *sk = (struct sock *)msk;
struct mptcp_pm_addr_entry local;
unsigned int add_addr_signal_max; unsigned int add_addr_signal_max;
bool signal_and_subflow = false;
unsigned int local_addr_max; unsigned int local_addr_max;
struct pm_nl_pernet *pernet; struct pm_nl_pernet *pernet;
unsigned int subflows_max; unsigned int subflows_max;
...@@ -565,23 +574,22 @@ static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) ...@@ -565,23 +574,22 @@ static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
if (msk->pm.addr_signal & BIT(MPTCP_ADD_ADDR_SIGNAL)) if (msk->pm.addr_signal & BIT(MPTCP_ADD_ADDR_SIGNAL))
return; return;
local = select_signal_address(pernet, msk); if (!select_signal_address(pernet, msk, &local))
if (!local)
goto subflow; goto subflow;
/* If the alloc fails, we are on memory pressure, not worth /* If the alloc fails, we are on memory pressure, not worth
* continuing, and trying to create subflows. * continuing, and trying to create subflows.
*/ */
if (!mptcp_pm_alloc_anno_list(msk, &local->addr)) if (!mptcp_pm_alloc_anno_list(msk, &local.addr))
return; return;
__clear_bit(local->addr.id, msk->pm.id_avail_bitmap); __clear_bit(local.addr.id, msk->pm.id_avail_bitmap);
msk->pm.add_addr_signaled++; msk->pm.add_addr_signaled++;
mptcp_pm_announce_addr(msk, &local->addr, false); mptcp_pm_announce_addr(msk, &local.addr, false);
mptcp_pm_nl_addr_send_ack(msk); mptcp_pm_nl_addr_send_ack(msk);
if (local->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) if (local.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
signal_and_subflow = local; signal_and_subflow = true;
} }
subflow: subflow:
...@@ -592,26 +600,22 @@ static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) ...@@ -592,26 +600,22 @@ static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
bool fullmesh; bool fullmesh;
int i, nr; int i, nr;
if (signal_and_subflow) { if (signal_and_subflow)
local = signal_and_subflow; signal_and_subflow = false;
signal_and_subflow = NULL; else if (!select_local_address(pernet, msk, &local))
} else {
local = select_local_address(pernet, msk);
if (!local)
break; break;
}
fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH); fullmesh = !!(local.flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
msk->pm.local_addr_used++; msk->pm.local_addr_used++;
__clear_bit(local->addr.id, msk->pm.id_avail_bitmap); __clear_bit(local.addr.id, msk->pm.id_avail_bitmap);
nr = fill_remote_addresses_vec(msk, &local->addr, fullmesh, addrs); nr = fill_remote_addresses_vec(msk, &local.addr, fullmesh, addrs);
if (nr == 0) if (nr == 0)
continue; continue;
spin_unlock_bh(&msk->pm.lock); spin_unlock_bh(&msk->pm.lock);
for (i = 0; i < nr; i++) for (i = 0; i < nr; i++)
__mptcp_subflow_connect(sk, &local->addr, &addrs[i]); __mptcp_subflow_connect(sk, &local.addr, &addrs[i]);
spin_lock_bh(&msk->pm.lock); spin_lock_bh(&msk->pm.lock);
} }
mptcp_pm_nl_check_work_pending(msk); mptcp_pm_nl_check_work_pending(msk);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment