Skip to content

Commit f47e650

Browse files
siddhpantNipaLocal
authored andcommitted
nfc: llcp_core: Hold a ref to llcp_local->dev when holding a ref to llcp_local
llcp_sock_sendmsg() calls nfc_llcp_send_ui_frame() which in turn calls nfc_alloc_send_skb(), which accesses the nfc_dev from the llcp_sock for getting the headroom and tailroom needed for skb allocation. Parallelly the nfc_dev can be freed, as the refcount is decreased via nfc_free_device(), leading to a UAF reported by Syzkaller, which can be summarized as follows: (1) llcp_sock_sendmsg() -> nfc_llcp_send_ui_frame() -> nfc_alloc_send_skb() -> Dereference *nfc_dev (2) virtual_ncidev_close() -> nci_free_device() -> nfc_free_device() -> put_device() -> nfc_release() -> Free *nfc_dev When a reference to llcp_local is acquired, we do not acquire the same for the nfc_dev. This leads to freeing even when the llcp_local is in use, and this is the case with the UAF described above too. Thus, when we acquire a reference to llcp_local, we should acquire a reference to nfc_dev, and release the references appropriately later. References for llcp_local is initialized in nfc_llcp_register_device() (which is called by nfc_register_device()). Thus, we should acquire a reference to nfc_dev there. nfc_unregister_device() calls nfc_llcp_unregister_device() which in turn calls nfc_llcp_local_put(). Thus, the reference to nfc_dev is appropriately released later. Reported-and-tested-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=bbe84a4010eeea00982d Fixes: c7aa122 ("NFC: Take a reference on the LLCP local pointer when creating a socket") Signed-off-by: Siddh Raman Pant <[email protected]> Reviewed-by: Suman Ghosh <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 7060663 commit f47e650

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

net/nfc/llcp_core.c

Lines changed: 42 additions & 13 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,
@@ -930,9 +945,7 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
930945

931946
if (sk_acceptq_is_full(parent)) {
932947
reason = LLCP_DM_REJ;
933-
release_sock(&sock->sk);
934-
sock_put(&sock->sk);
935-
goto fail;
948+
goto fail_put_sock;
936949
}
937950

938951
if (sock->ssap == LLCP_SDP_UNBOUND) {
@@ -942,9 +955,7 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
942955

943956
if (ssap == LLCP_SAP_MAX) {
944957
reason = LLCP_DM_REJ;
945-
release_sock(&sock->sk);
946-
sock_put(&sock->sk);
947-
goto fail;
958+
goto fail_put_sock;
948959
}
949960

950961
sock->ssap = ssap;
@@ -953,14 +964,18 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
953964
new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0);
954965
if (new_sk == NULL) {
955966
reason = LLCP_DM_REJ;
956-
release_sock(&sock->sk);
957-
sock_put(&sock->sk);
958-
goto fail;
967+
goto fail_put_sock;
959968
}
960969

961970
new_sock = nfc_llcp_sock(new_sk);
962-
new_sock->dev = local->dev;
971+
963972
new_sock->local = nfc_llcp_local_get(local);
973+
if (!new_sock->local) {
974+
reason = LLCP_DM_REJ;
975+
goto fail_free_new_sock;
976+
}
977+
978+
new_sock->dev = local->dev;
964979
new_sock->rw = sock->rw;
965980
new_sock->miux = sock->miux;
966981
new_sock->nfc_protocol = sock->nfc_protocol;
@@ -1004,8 +1019,13 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
10041019

10051020
return;
10061021

1022+
fail_free_new_sock:
1023+
sock_put(&new_sock->sk);
1024+
nfc_llcp_sock_free(new_sock);
1025+
fail_put_sock:
1026+
release_sock(&sock->sk);
1027+
sock_put(&sock->sk);
10071028
fail:
1008-
/* Send DM */
10091029
nfc_llcp_send_dm(local, dsap, ssap, reason);
10101030
}
10111031

@@ -1597,7 +1617,16 @@ int nfc_llcp_register_device(struct nfc_dev *ndev)
15971617
if (local == NULL)
15981618
return -ENOMEM;
15991619

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

0 commit comments

Comments
 (0)