Skip to content

Commit dff90e4

Browse files
committed
Merge branch 'nfc-refcounting'
@ 2023-12-19 17:49 Siddh Raman Pant 2023-12-19 17:49 ` [PATCH net-next v7 1/2] nfc: llcp_core: Hold a ref to llcp_local->dev when holding a ref to llcp_local Siddh Raman Pant 2023-12-19 17:49 ` [PATCH net-next v7 2/2] nfc: Do not send datagram if socket state isn't LLCP_BOUND Siddh Raman Pant 0 siblings, 2 replies; 4+ messages in thread Siddh Raman Pant says: ==================== [PATCH net-next v7 0/2] nfc: Fix UAF during datagram sending caused by missing refcounting Changes in v7: - Stupidly reverted ordering in recv() too, fix that. - Remove redundant call to nfc_llcp_sock_free(). Changes in v6: - Revert label introduction from v4, and thus also v5 entirely. Changes in v5: - Move reason = LLCP_DM_REJ under the fail_put_sock label. - Checkpatch now warns about == NULL check for new_sk, so fix that, and also at other similar places in the same function. Changes in v4: - Fix put ordering and comments. - Separate freeing in recv() into end labels. - Remove obvious comment and add reasoning. - Picked up r-bs by Suman. Changes in v3: - Fix missing freeing statements. Changes in v2: - Add net-next in patch subject. - Removed unnecessary extra lock and hold nfc_dev ref when holding llcp_sock. - Remove last formatting patch. - Picked up r-b from Krzysztof for LLCP_BOUND patch. --- For connectionless transmission, llcp_sock_sendmsg() codepath will eventually call nfc_alloc_send_skb() which takes in an nfc_dev as an argument for calculating the total size for skb allocation. virtual_ncidev_close() codepath eventually releases socket by calling nfc_llcp_socket_release() (which sets the sk->sk_state to LLCP_CLOSED) and afterwards the nfc_dev will be eventually freed. When an ndev gets freed, llcp_sock_sendmsg() will result in an use-after-free as it (1) doesn't have any checks in place for avoiding the datagram sending. (2) calls nfc_llcp_send_ui_frame(), which also has a do-while loop which can race with freeing. This loop contains the call to nfc_alloc_send_skb() where we dereference the nfc_dev pointer. nfc_dev is being freed because we do not hold a reference to it when we hold a reference to llcp_local. Thus, virtual_ncidev_close() eventually calls nfc_release() due to refcount going to 0. Since state has to be LLCP_BOUND for datagram sending, we can bail out early in llcp_sock_sendmsg(). Please review and let me know if any errors are there, and hopefully this gets accepted. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 7c5e046 + 6ec0d75 commit dff90e4

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

net/nfc/llcp_core.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
145145

146146
static struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
147147
{
148+
/* Since using nfc_llcp_local may result in usage of nfc_dev, whenever
149+
* we hold a reference to local, we also need to hold a reference to
150+
* the device to avoid UAF.
151+
*/
152+
if (!nfc_get_device(local->dev->idx))
153+
return NULL;
154+
148155
kref_get(&local->ref);
149156

150157
return local;
@@ -177,10 +184,18 @@ static void local_release(struct kref *ref)
177184

178185
int nfc_llcp_local_put(struct nfc_llcp_local *local)
179186
{
187+
struct nfc_dev *dev;
188+
int ret;
189+
180190
if (local == NULL)
181191
return 0;
182192

183-
return kref_put(&local->ref, local_release);
193+
dev = local->dev;
194+
195+
ret = kref_put(&local->ref, local_release);
196+
nfc_put_device(dev);
197+
198+
return ret;
184199
}
185200

186201
static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
@@ -959,8 +974,17 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
959974
}
960975

961976
new_sock = nfc_llcp_sock(new_sk);
962-
new_sock->dev = local->dev;
977+
963978
new_sock->local = nfc_llcp_local_get(local);
979+
if (!new_sock->local) {
980+
reason = LLCP_DM_REJ;
981+
sock_put(&new_sock->sk);
982+
release_sock(&sock->sk);
983+
sock_put(&sock->sk);
984+
goto fail;
985+
}
986+
987+
new_sock->dev = local->dev;
964988
new_sock->rw = sock->rw;
965989
new_sock->miux = sock->miux;
966990
new_sock->nfc_protocol = sock->nfc_protocol;
@@ -1597,7 +1621,16 @@ int nfc_llcp_register_device(struct nfc_dev *ndev)
15971621
if (local == NULL)
15981622
return -ENOMEM;
15991623

1600-
local->dev = ndev;
1624+
/* As we are going to initialize local's refcount, we need to get the
1625+
* nfc_dev to avoid UAF, otherwise there is no point in continuing.
1626+
* See nfc_llcp_local_get().
1627+
*/
1628+
local->dev = nfc_get_device(ndev->idx);
1629+
if (!local->dev) {
1630+
kfree(local);
1631+
return -ENODEV;
1632+
}
1633+
16011634
INIT_LIST_HEAD(&local->list);
16021635
kref_init(&local->ref);
16031636
mutex_init(&local->sdp_lock);

net/nfc/llcp_sock.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,11 @@ static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg,
796796
}
797797

798798
if (sk->sk_type == SOCK_DGRAM) {
799+
if (sk->sk_state != LLCP_BOUND) {
800+
release_sock(sk);
801+
return -ENOTCONN;
802+
}
803+
799804
DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr,
800805
msg->msg_name);
801806

0 commit comments

Comments
 (0)