2021-05-31 15:35:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2014-01-07 04:58:19 +08:00
|
|
|
/*
|
|
|
|
* Marvell NFC driver: major functions
|
|
|
|
*
|
2015-10-26 17:27:37 +08:00
|
|
|
* Copyright (C) 2014-2015 Marvell International Ltd.
|
2014-01-07 04:58:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
2015-06-11 17:25:46 +08:00
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/delay.h>
|
2015-06-11 20:00:19 +08:00
|
|
|
#include <linux/of_gpio.h>
|
2014-01-07 04:58:19 +08:00
|
|
|
#include <linux/nfc.h>
|
|
|
|
#include <net/nfc/nci.h>
|
|
|
|
#include <net/nfc/nci_core.h>
|
|
|
|
#include "nfcmrvl.h"
|
|
|
|
|
|
|
|
static int nfcmrvl_nci_open(struct nci_dev *ndev)
|
|
|
|
{
|
|
|
|
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
|
|
|
|
return 0;
|
|
|
|
|
2015-10-26 17:27:41 +08:00
|
|
|
/* Reset possible fault of previous session */
|
|
|
|
clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
|
|
|
|
|
2014-01-07 04:58:19 +08:00
|
|
|
err = priv->if_ops->nci_open(priv);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nfcmrvl_nci_close(struct nci_dev *ndev)
|
|
|
|
{
|
|
|
|
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
|
|
|
|
|
|
|
|
if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
priv->if_ops->nci_close(priv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
|
|
|
|
|
|
|
|
nfc_info(priv->dev, "send entry, len %d\n", skb->len);
|
|
|
|
|
|
|
|
skb->dev = (void *)ndev;
|
|
|
|
|
2015-06-11 20:00:19 +08:00
|
|
|
if (priv->config.hci_muxed) {
|
2015-06-11 17:25:43 +08:00
|
|
|
unsigned char *hdr;
|
|
|
|
unsigned char len = skb->len;
|
|
|
|
|
networking: make skb_push & __skb_push return void pointers
It seems like a historic accident that these return unsigned char *,
and in many places that means casts are required, more often than not.
Make these functions return void * and remove all the casts across
the tree, adding a (u8 *) cast only where the unsigned char pointer
was used directly, all done with the following spatch:
@@
expression SKB, LEN;
typedef u8;
identifier fn = { skb_push, __skb_push, skb_push_rcsum };
@@
- *(fn(SKB, LEN))
+ *(u8 *)fn(SKB, LEN)
@@
expression E, SKB, LEN;
identifier fn = { skb_push, __skb_push, skb_push_rcsum };
type T;
@@
- E = ((T *)(fn(SKB, LEN)))
+ E = fn(SKB, LEN)
@@
expression SKB, LEN;
identifier fn = { skb_push, __skb_push, skb_push_rcsum };
@@
- fn(SKB, LEN)[0]
+ *(u8 *)fn(SKB, LEN)
Note that the last part there converts from push(...)[0] to the
more idiomatic *(u8 *)push(...).
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 20:29:23 +08:00
|
|
|
hdr = skb_push(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
|
2015-06-11 17:25:43 +08:00
|
|
|
hdr[0] = NFCMRVL_HCI_COMMAND_CODE;
|
|
|
|
hdr[1] = NFCMRVL_HCI_OGF;
|
|
|
|
hdr[2] = NFCMRVL_HCI_OCF;
|
|
|
|
hdr[3] = len;
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:58:19 +08:00
|
|
|
return priv->if_ops->nci_send(priv, skb);
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:58:20 +08:00
|
|
|
static int nfcmrvl_nci_setup(struct nci_dev *ndev)
|
|
|
|
{
|
2015-06-12 21:35:54 +08:00
|
|
|
__u8 val = 1;
|
|
|
|
|
|
|
|
nci_set_config(ndev, NFCMRVL_PB_BAIL_OUT, 1, &val);
|
2014-01-07 04:58:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:27:39 +08:00
|
|
|
static int nfcmrvl_nci_fw_download(struct nci_dev *ndev,
|
|
|
|
const char *firmware_name)
|
|
|
|
{
|
|
|
|
return nfcmrvl_fw_dnld_start(ndev, firmware_name);
|
|
|
|
}
|
|
|
|
|
2021-07-25 05:47:33 +08:00
|
|
|
static const struct nci_ops nfcmrvl_nci_ops = {
|
2014-01-07 04:58:19 +08:00
|
|
|
.open = nfcmrvl_nci_open,
|
|
|
|
.close = nfcmrvl_nci_close,
|
|
|
|
.send = nfcmrvl_nci_send,
|
2014-01-07 04:58:20 +08:00
|
|
|
.setup = nfcmrvl_nci_setup,
|
2015-10-26 17:27:39 +08:00
|
|
|
.fw_download = nfcmrvl_nci_fw_download,
|
2014-01-07 04:58:19 +08:00
|
|
|
};
|
|
|
|
|
2015-10-26 17:27:40 +08:00
|
|
|
struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
|
|
|
|
void *drv_data,
|
2021-07-29 18:42:41 +08:00
|
|
|
const struct nfcmrvl_if_ops *ops,
|
2015-06-11 20:00:19 +08:00
|
|
|
struct device *dev,
|
2021-07-29 18:40:21 +08:00
|
|
|
const struct nfcmrvl_platform_data *pdata)
|
2014-01-07 04:58:19 +08:00
|
|
|
{
|
|
|
|
struct nfcmrvl_private *priv;
|
|
|
|
int rc;
|
2015-10-26 17:27:40 +08:00
|
|
|
int headroom;
|
|
|
|
int tailroom;
|
2014-01-07 04:58:19 +08:00
|
|
|
u32 protocols;
|
|
|
|
|
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
|
|
|
if (!priv)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
priv->drv_data = drv_data;
|
|
|
|
priv->if_ops = ops;
|
|
|
|
priv->dev = dev;
|
2015-10-26 17:27:40 +08:00
|
|
|
priv->phy = phy;
|
2015-06-11 17:25:46 +08:00
|
|
|
|
2015-06-11 20:00:19 +08:00
|
|
|
memcpy(&priv->config, pdata, sizeof(*pdata));
|
|
|
|
|
2017-03-30 18:15:42 +08:00
|
|
|
if (gpio_is_valid(priv->config.reset_n_io)) {
|
2017-03-30 18:15:37 +08:00
|
|
|
rc = gpio_request_one(priv->config.reset_n_io,
|
|
|
|
GPIOF_OUT_INIT_LOW,
|
|
|
|
"nfcmrvl_reset_n");
|
|
|
|
if (rc < 0) {
|
2017-03-30 18:15:42 +08:00
|
|
|
priv->config.reset_n_io = -EINVAL;
|
2015-06-11 17:25:46 +08:00
|
|
|
nfc_err(dev, "failed to request reset_n io\n");
|
2017-03-30 18:15:37 +08:00
|
|
|
}
|
2015-06-11 17:25:46 +08:00
|
|
|
}
|
2015-06-11 17:25:43 +08:00
|
|
|
|
2015-10-26 17:27:44 +08:00
|
|
|
if (phy == NFCMRVL_PHY_SPI) {
|
|
|
|
headroom = NCI_SPI_HDR_LEN;
|
|
|
|
tailroom = 1;
|
|
|
|
} else
|
|
|
|
headroom = tailroom = 0;
|
2015-10-26 17:27:40 +08:00
|
|
|
|
2015-06-11 20:00:19 +08:00
|
|
|
if (priv->config.hci_muxed)
|
2015-10-26 17:27:40 +08:00
|
|
|
headroom += NFCMRVL_HCI_EVENT_HEADER_SIZE;
|
2014-01-07 04:58:19 +08:00
|
|
|
|
|
|
|
protocols = NFC_PROTO_JEWEL_MASK
|
2015-06-11 20:00:19 +08:00
|
|
|
| NFC_PROTO_MIFARE_MASK
|
|
|
|
| NFC_PROTO_FELICA_MASK
|
2014-01-07 04:58:19 +08:00
|
|
|
| NFC_PROTO_ISO14443_MASK
|
|
|
|
| NFC_PROTO_ISO14443_B_MASK
|
2015-06-12 21:35:52 +08:00
|
|
|
| NFC_PROTO_ISO15693_MASK
|
2014-01-07 04:58:19 +08:00
|
|
|
| NFC_PROTO_NFC_DEP_MASK;
|
|
|
|
|
2015-06-11 17:25:43 +08:00
|
|
|
priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols,
|
2015-10-26 17:27:40 +08:00
|
|
|
headroom, tailroom);
|
2014-01-07 04:58:19 +08:00
|
|
|
if (!priv->ndev) {
|
2015-04-07 15:17:00 +08:00
|
|
|
nfc_err(dev, "nci_allocate_device failed\n");
|
2014-01-09 02:52:27 +08:00
|
|
|
rc = -ENOMEM;
|
2017-03-30 18:15:37 +08:00
|
|
|
goto error_free_gpio;
|
2014-01-07 04:58:19 +08:00
|
|
|
}
|
|
|
|
|
2017-03-30 18:15:39 +08:00
|
|
|
rc = nfcmrvl_fw_dnld_init(priv);
|
|
|
|
if (rc) {
|
|
|
|
nfc_err(dev, "failed to initialize FW download %d\n", rc);
|
|
|
|
goto error_free_dev;
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:58:19 +08:00
|
|
|
nci_set_drvdata(priv->ndev, priv);
|
|
|
|
|
|
|
|
rc = nci_register_device(priv->ndev);
|
|
|
|
if (rc) {
|
2015-04-07 15:17:00 +08:00
|
|
|
nfc_err(dev, "nci_register_device failed %d\n", rc);
|
2017-03-30 18:15:39 +08:00
|
|
|
goto error_fw_dnld_deinit;
|
2015-10-26 17:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that controller is powered off */
|
|
|
|
nfcmrvl_chip_halt(priv);
|
|
|
|
|
2014-01-07 04:58:19 +08:00
|
|
|
nfc_info(dev, "registered with nci successfully\n");
|
|
|
|
return priv;
|
2014-01-09 02:52:27 +08:00
|
|
|
|
2017-03-30 18:15:39 +08:00
|
|
|
error_fw_dnld_deinit:
|
|
|
|
nfcmrvl_fw_dnld_deinit(priv);
|
2015-10-26 17:27:39 +08:00
|
|
|
error_free_dev:
|
|
|
|
nci_free_device(priv->ndev);
|
2017-03-30 18:15:37 +08:00
|
|
|
error_free_gpio:
|
2017-03-30 18:15:42 +08:00
|
|
|
if (gpio_is_valid(priv->config.reset_n_io))
|
2017-03-30 18:15:37 +08:00
|
|
|
gpio_free(priv->config.reset_n_io);
|
2014-01-09 02:52:27 +08:00
|
|
|
kfree(priv);
|
|
|
|
return ERR_PTR(rc);
|
2014-01-07 04:58:19 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev);
|
|
|
|
|
|
|
|
void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
|
|
|
|
{
|
|
|
|
struct nci_dev *ndev = priv->ndev;
|
|
|
|
|
nfc: nfcmrvl: main: reorder destructive operations in nfcmrvl_nci_unregister_dev to avoid bugs
There are destructive operations such as nfcmrvl_fw_dnld_abort and
gpio_free in nfcmrvl_nci_unregister_dev. The resources such as firmware,
gpio and so on could be destructed while the upper layer functions such as
nfcmrvl_fw_dnld_start and nfcmrvl_nci_recv_frame is executing, which leads
to double-free, use-after-free and null-ptr-deref bugs.
There are three situations that could lead to double-free bugs.
The first situation is shown below:
(Thread 1) | (Thread 2)
nfcmrvl_fw_dnld_start |
... | nfcmrvl_nci_unregister_dev
release_firmware() | nfcmrvl_fw_dnld_abort
kfree(fw) //(1) | fw_dnld_over
| release_firmware
... | kfree(fw) //(2)
| ...
The second situation is shown below:
(Thread 1) | (Thread 2)
nfcmrvl_fw_dnld_start |
... |
mod_timer |
(wait a time) |
fw_dnld_timeout | nfcmrvl_nci_unregister_dev
fw_dnld_over | nfcmrvl_fw_dnld_abort
release_firmware | fw_dnld_over
kfree(fw) //(1) | release_firmware
... | kfree(fw) //(2)
The third situation is shown below:
(Thread 1) | (Thread 2)
nfcmrvl_nci_recv_frame |
if(..->fw_download_in_progress)|
nfcmrvl_fw_dnld_recv_frame |
queue_work |
|
fw_dnld_rx_work | nfcmrvl_nci_unregister_dev
fw_dnld_over | nfcmrvl_fw_dnld_abort
release_firmware | fw_dnld_over
kfree(fw) //(1) | release_firmware
| kfree(fw) //(2)
The firmware struct is deallocated in position (1) and deallocated
in position (2) again.
The crash trace triggered by POC is like below:
BUG: KASAN: double-free or invalid-free in fw_dnld_over
Call Trace:
kfree
fw_dnld_over
nfcmrvl_nci_unregister_dev
nci_uart_tty_close
tty_ldisc_kill
tty_ldisc_hangup
__tty_hangup.part.0
tty_release
...
What's more, there are also use-after-free and null-ptr-deref bugs
in nfcmrvl_fw_dnld_start. If we deallocate firmware struct, gpio or
set null to the members of priv->fw_dnld in nfcmrvl_nci_unregister_dev,
then, we dereference firmware, gpio or the members of priv->fw_dnld in
nfcmrvl_fw_dnld_start, the UAF or NPD bugs will happen.
This patch reorders destructive operations after nci_unregister_device
in order to synchronize between cleanup routine and firmware download
routine.
The nci_unregister_device is well synchronized. If the device is
detaching, the firmware download routine will goto error. If firmware
download routine is executing, nci_unregister_device will wait until
firmware download routine is finished.
Fixes: 3194c6870158 ("NFC: nfcmrvl: add firmware download support")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-04-29 20:45:51 +08:00
|
|
|
nci_unregister_device(ndev);
|
2015-10-26 17:27:39 +08:00
|
|
|
if (priv->ndev->nfc_dev->fw_download_in_progress)
|
|
|
|
nfcmrvl_fw_dnld_abort(priv);
|
|
|
|
|
|
|
|
nfcmrvl_fw_dnld_deinit(priv);
|
|
|
|
|
2017-03-30 18:15:42 +08:00
|
|
|
if (gpio_is_valid(priv->config.reset_n_io))
|
2017-03-30 18:15:37 +08:00
|
|
|
gpio_free(priv->config.reset_n_io);
|
2015-11-04 02:19:34 +08:00
|
|
|
|
2014-01-07 04:58:19 +08:00
|
|
|
nci_free_device(ndev);
|
|
|
|
kfree(priv);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev);
|
|
|
|
|
2015-06-11 17:25:44 +08:00
|
|
|
int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, struct sk_buff *skb)
|
2014-01-07 04:58:19 +08:00
|
|
|
{
|
2015-06-11 20:00:19 +08:00
|
|
|
if (priv->config.hci_muxed) {
|
2015-06-11 17:25:43 +08:00
|
|
|
if (skb->data[0] == NFCMRVL_HCI_EVENT_CODE &&
|
|
|
|
skb->data[1] == NFCMRVL_HCI_NFC_EVENT_CODE) {
|
|
|
|
/* Data packet, let's extract NCI payload */
|
|
|
|
skb_pull(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
|
|
|
|
} else {
|
|
|
|
/* Skip this packet */
|
|
|
|
kfree_skb(skb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:27:39 +08:00
|
|
|
if (priv->ndev->nfc_dev->fw_download_in_progress) {
|
|
|
|
nfcmrvl_fw_dnld_recv_frame(priv, skb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-11 17:25:44 +08:00
|
|
|
if (test_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
|
|
|
|
nci_recv_frame(priv->ndev, skb);
|
|
|
|
else {
|
|
|
|
/* Drop this packet since nobody wants it */
|
|
|
|
kfree_skb(skb);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-07 04:58:19 +08:00
|
|
|
|
2015-06-11 17:25:44 +08:00
|
|
|
return 0;
|
2014-01-07 04:58:19 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame);
|
|
|
|
|
2015-06-11 17:25:46 +08:00
|
|
|
void nfcmrvl_chip_reset(struct nfcmrvl_private *priv)
|
|
|
|
{
|
2015-10-26 17:27:41 +08:00
|
|
|
/* Reset possible fault of previous session */
|
|
|
|
clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
|
2015-06-11 17:25:46 +08:00
|
|
|
|
2019-08-05 18:00:55 +08:00
|
|
|
if (gpio_is_valid(priv->config.reset_n_io)) {
|
2015-06-11 17:25:46 +08:00
|
|
|
nfc_info(priv->dev, "reset the chip\n");
|
2015-06-11 20:00:19 +08:00
|
|
|
gpio_set_value(priv->config.reset_n_io, 0);
|
2015-06-11 17:25:46 +08:00
|
|
|
usleep_range(5000, 10000);
|
2015-06-11 20:00:19 +08:00
|
|
|
gpio_set_value(priv->config.reset_n_io, 1);
|
2015-06-11 17:25:46 +08:00
|
|
|
} else
|
|
|
|
nfc_info(priv->dev, "no reset available on this interface\n");
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:27:39 +08:00
|
|
|
void nfcmrvl_chip_halt(struct nfcmrvl_private *priv)
|
|
|
|
{
|
2019-08-05 18:00:55 +08:00
|
|
|
if (gpio_is_valid(priv->config.reset_n_io))
|
2015-10-26 17:27:39 +08:00
|
|
|
gpio_set_value(priv->config.reset_n_io, 0);
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:00:19 +08:00
|
|
|
int nfcmrvl_parse_dt(struct device_node *node,
|
|
|
|
struct nfcmrvl_platform_data *pdata)
|
|
|
|
{
|
|
|
|
int reset_n_io;
|
|
|
|
|
|
|
|
reset_n_io = of_get_named_gpio(node, "reset-n-io", 0);
|
|
|
|
if (reset_n_io < 0) {
|
|
|
|
pr_info("no reset-n-io config\n");
|
|
|
|
} else if (!gpio_is_valid(reset_n_io)) {
|
|
|
|
pr_err("invalid reset-n-io GPIO\n");
|
|
|
|
return reset_n_io;
|
|
|
|
}
|
|
|
|
pdata->reset_n_io = reset_n_io;
|
|
|
|
|
|
|
|
if (of_find_property(node, "hci-muxed", NULL))
|
|
|
|
pdata->hci_muxed = 1;
|
|
|
|
else
|
|
|
|
pdata->hci_muxed = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nfcmrvl_parse_dt);
|
|
|
|
|
2014-01-07 04:58:19 +08:00
|
|
|
MODULE_AUTHOR("Marvell International Ltd.");
|
2015-10-26 17:27:37 +08:00
|
|
|
MODULE_DESCRIPTION("Marvell NFC driver");
|
2014-01-07 04:58:19 +08:00
|
|
|
MODULE_LICENSE("GPL v2");
|