mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-14 14:34:28 +08:00
e840d8f4a1
skb should be free in virtual_nci_send(), otherwise kmemleak will report
memleak.
Steps for reproduction (simulated in qemu):
cd tools/testing/selftests/nci
make
./nci_dev
BUG: memory leak
unreferenced object 0xffff888107588000 (size 208):
comm "nci_dev", pid 206, jiffies 4294945376 (age 368.248s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
[<000000008d94c8fd>] __alloc_skb+0x1da/0x290
[<00000000278bc7f8>] nci_send_cmd+0xa3/0x350
[<0000000081256a22>] nci_reset_req+0x6b/0xa0
[<000000009e721112>] __nci_request+0x90/0x250
[<000000005d556e59>] nci_dev_up+0x217/0x5b0
[<00000000e618ce62>] nfc_dev_up+0x114/0x220
[<00000000981e226b>] nfc_genl_dev_up+0x94/0xe0
[<000000009bb03517>] genl_family_rcv_msg_doit.isra.14+0x228/0x2d0
[<00000000b7f8c101>] genl_rcv_msg+0x35c/0x640
[<00000000c94075ff>] netlink_rcv_skb+0x11e/0x350
[<00000000440cfb1e>] genl_rcv+0x24/0x40
[<0000000062593b40>] netlink_unicast+0x43f/0x640
[<000000001d0b13cc>] netlink_sendmsg+0x73a/0xbf0
[<000000003272487f>] __sys_sendto+0x324/0x370
[<00000000ef9f1747>] __x64_sys_sendto+0xdd/0x1b0
[<000000001e437841>] do_syscall_64+0x3f/0x90
Fixes: e624e6c3e7
("nfc: Add a virtual nci device driver")
Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Link: https://lore.kernel.org/r/20221020030505.15572-1-shangxiaojing@huawei.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
224 lines
4.6 KiB
C
224 lines
4.6 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
* Virtual NCI device simulation driver
|
|
*
|
|
* Copyright (C) 2020 Samsung Electrnoics
|
|
* Bongsu Jeon <bongsu.jeon@samsung.com>
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/miscdevice.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/wait.h>
|
|
#include <net/nfc/nci_core.h>
|
|
|
|
enum virtual_ncidev_mode {
|
|
virtual_ncidev_enabled,
|
|
virtual_ncidev_disabled,
|
|
virtual_ncidev_disabling,
|
|
};
|
|
|
|
#define IOCTL_GET_NCIDEV_IDX 0
|
|
#define VIRTUAL_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
|
|
NFC_PROTO_MIFARE_MASK | \
|
|
NFC_PROTO_FELICA_MASK | \
|
|
NFC_PROTO_ISO14443_MASK | \
|
|
NFC_PROTO_ISO14443_B_MASK | \
|
|
NFC_PROTO_ISO15693_MASK)
|
|
|
|
static enum virtual_ncidev_mode state;
|
|
static DECLARE_WAIT_QUEUE_HEAD(wq);
|
|
static struct miscdevice miscdev;
|
|
static struct sk_buff *send_buff;
|
|
static struct nci_dev *ndev;
|
|
static DEFINE_MUTEX(nci_mutex);
|
|
|
|
static int virtual_nci_open(struct nci_dev *ndev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int virtual_nci_close(struct nci_dev *ndev)
|
|
{
|
|
mutex_lock(&nci_mutex);
|
|
kfree_skb(send_buff);
|
|
send_buff = NULL;
|
|
mutex_unlock(&nci_mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
|
|
{
|
|
mutex_lock(&nci_mutex);
|
|
if (state != virtual_ncidev_enabled) {
|
|
mutex_unlock(&nci_mutex);
|
|
kfree_skb(skb);
|
|
return 0;
|
|
}
|
|
|
|
if (send_buff) {
|
|
mutex_unlock(&nci_mutex);
|
|
kfree_skb(skb);
|
|
return -1;
|
|
}
|
|
send_buff = skb_copy(skb, GFP_KERNEL);
|
|
mutex_unlock(&nci_mutex);
|
|
wake_up_interruptible(&wq);
|
|
consume_skb(skb);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct nci_ops virtual_nci_ops = {
|
|
.open = virtual_nci_open,
|
|
.close = virtual_nci_close,
|
|
.send = virtual_nci_send
|
|
};
|
|
|
|
static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
|
|
size_t count, loff_t *ppos)
|
|
{
|
|
size_t actual_len;
|
|
|
|
mutex_lock(&nci_mutex);
|
|
while (!send_buff) {
|
|
mutex_unlock(&nci_mutex);
|
|
if (wait_event_interruptible(wq, send_buff))
|
|
return -EFAULT;
|
|
mutex_lock(&nci_mutex);
|
|
}
|
|
|
|
actual_len = min_t(size_t, count, send_buff->len);
|
|
|
|
if (copy_to_user(buf, send_buff->data, actual_len)) {
|
|
mutex_unlock(&nci_mutex);
|
|
return -EFAULT;
|
|
}
|
|
|
|
skb_pull(send_buff, actual_len);
|
|
if (send_buff->len == 0) {
|
|
consume_skb(send_buff);
|
|
send_buff = NULL;
|
|
}
|
|
mutex_unlock(&nci_mutex);
|
|
|
|
return actual_len;
|
|
}
|
|
|
|
static ssize_t virtual_ncidev_write(struct file *file,
|
|
const char __user *buf,
|
|
size_t count, loff_t *ppos)
|
|
{
|
|
struct sk_buff *skb;
|
|
|
|
skb = alloc_skb(count, GFP_KERNEL);
|
|
if (!skb)
|
|
return -ENOMEM;
|
|
|
|
if (copy_from_user(skb_put(skb, count), buf, count)) {
|
|
kfree_skb(skb);
|
|
return -EFAULT;
|
|
}
|
|
|
|
nci_recv_frame(ndev, skb);
|
|
return count;
|
|
}
|
|
|
|
static int virtual_ncidev_open(struct inode *inode, struct file *file)
|
|
{
|
|
int ret = 0;
|
|
|
|
mutex_lock(&nci_mutex);
|
|
if (state != virtual_ncidev_disabled) {
|
|
mutex_unlock(&nci_mutex);
|
|
return -EBUSY;
|
|
}
|
|
|
|
ndev = nci_allocate_device(&virtual_nci_ops, VIRTUAL_NFC_PROTOCOLS,
|
|
0, 0);
|
|
if (!ndev) {
|
|
mutex_unlock(&nci_mutex);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
ret = nci_register_device(ndev);
|
|
if (ret < 0) {
|
|
nci_free_device(ndev);
|
|
mutex_unlock(&nci_mutex);
|
|
return ret;
|
|
}
|
|
state = virtual_ncidev_enabled;
|
|
mutex_unlock(&nci_mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int virtual_ncidev_close(struct inode *inode, struct file *file)
|
|
{
|
|
mutex_lock(&nci_mutex);
|
|
|
|
if (state == virtual_ncidev_enabled) {
|
|
state = virtual_ncidev_disabling;
|
|
mutex_unlock(&nci_mutex);
|
|
|
|
nci_unregister_device(ndev);
|
|
nci_free_device(ndev);
|
|
|
|
mutex_lock(&nci_mutex);
|
|
}
|
|
|
|
state = virtual_ncidev_disabled;
|
|
mutex_unlock(&nci_mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static long virtual_ncidev_ioctl(struct file *flip, unsigned int cmd,
|
|
unsigned long arg)
|
|
{
|
|
const struct nfc_dev *nfc_dev = ndev->nfc_dev;
|
|
void __user *p = (void __user *)arg;
|
|
|
|
if (cmd != IOCTL_GET_NCIDEV_IDX)
|
|
return -ENOTTY;
|
|
|
|
if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct file_operations virtual_ncidev_fops = {
|
|
.owner = THIS_MODULE,
|
|
.read = virtual_ncidev_read,
|
|
.write = virtual_ncidev_write,
|
|
.open = virtual_ncidev_open,
|
|
.release = virtual_ncidev_close,
|
|
.unlocked_ioctl = virtual_ncidev_ioctl
|
|
};
|
|
|
|
static int __init virtual_ncidev_init(void)
|
|
{
|
|
state = virtual_ncidev_disabled;
|
|
miscdev.minor = MISC_DYNAMIC_MINOR;
|
|
miscdev.name = "virtual_nci";
|
|
miscdev.fops = &virtual_ncidev_fops;
|
|
miscdev.mode = 0600;
|
|
|
|
return misc_register(&miscdev);
|
|
}
|
|
|
|
static void __exit virtual_ncidev_exit(void)
|
|
{
|
|
misc_deregister(&miscdev);
|
|
}
|
|
|
|
module_init(virtual_ncidev_init);
|
|
module_exit(virtual_ncidev_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("Virtual NCI device simulation driver");
|
|
MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");
|