Skip to content

Commit c95f919

Browse files
siddhpantdavem330
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: syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=bbe84a4010eeea00982d Fixes: c7aa122 ("NFC: Take a reference on the LLCP local pointer when creating a socket") Reviewed-by: Suman Ghosh <sumang@marvell.com> Signed-off-by: Siddh Raman Pant <code@siddh.me> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7c5e046 commit c95f919

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

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);

0 commit comments

Comments
 (0)