net: qrtr: fix memory leak in qrtr_tun_read_iter
Subsystems (3)
PATCH 1/1 net: qrtr: fix memory leak in qrtr_tun_read_iter
In qrtr_tun_read_iter we need an error handling path to appropriately release skb in cases of error.
--- net/qrtr/tun.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
net/qrtr/tun.c +9 -4
@@ -54,19 +54,24 @@ static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to)int count;while (!(skb =skb_dequeue (&tun->queue))) { -if (filp->f_flags &O_NONBLOCK ) -return -EAGAIN ; +if (filp->f_flags &O_NONBLOCK ) { + count = -EAGAIN ; + goto out; + }/* Wait until we get data or the endpoint goes away */ if (wait_event_interruptible (tun->readq, - !skb_queue_empty (&tun->queue))) -return -ERESTARTSYS ; + !skb_queue_empty (&tun->queue))) { + count = -ERESTARTSYS ; + goto out; + } } count =min_t (size_t,iov_iter_count (to), skb->len);if (copy_to_iter (skb->data, count, to) != count) count = -EFAULT ; +out:kfree_skb (skb);return count;
Signature · 1 line
2.17.1
Quoted 2 lines · Navid Emamdoost
In
qrtr_tun_read_iterwe need an error handling path to appropriately release skb in cases of error.
Release _what_ skb?
net/qrtr/tun.c static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 56
Quoted 13 lines
Signed-off-by Navid Emamdoost <[email protected]>--- net/qrtr/tun.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)net/qrtr/tun.c +0
-0@@ -54,19 +54,24 @@ static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to)int count;while (!(skb =skb_dequeue (&tun->queue))) {
The body of the loop is entered only if the loop condition has evaluated true. In this case, it means that the value of
!(skb = skb_dequeue(&tun->queue)) had been true, i.e. the value of skb = skb_dequeue(&tun->queue)
has been NULL, i.e. that skb_dequeue() has returned NULL, which had been copied into skb.
In other words, in the body of that loop we have skb equal to NULL.
Quoted 6 lines
net/qrtr/tun.c +4
-2-if (filp->f_flags &O_NONBLOCK ) -return -EAGAIN ; +if (filp->f_flags &O_NONBLOCK ) { + count = -EAGAIN ; + goto out; + }
net/qrtr/tun.c static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 68
Quoted 10 lines
net/qrtr/tun.c +4
-2/* Wait until we get data or the endpoint goes away */ if (wait_event_interruptible (tun->readq, - !skb_queue_empty (&tun->queue))) -return -ERESTARTSYS ; + !skb_queue_empty (&tun->queue))) { + count = -ERESTARTSYS ; + goto out; + } }
The meaning of that loop is fairly clear, isn't it? Keep looking int tun->queue until an skb shows up there. If it's not immediately there, fail with -EAGAIN for non-blocking files and wait on tun->readq until some skb arrives.
Quoted 5 lines · Al Viro
Quoted 2 lines · Navid Emamdoost
In
qrtr_tun_read_iterwe need an error handling path to appropriately release skb in cases of error.Release _what_ skb?
It is not a leak clearly! My bad ...
Quoted 32 lines
Quoted 11 lines
Signed-off-by Navid Emamdoost <[email protected]>--- net/qrtr/tun.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)net/qrtr/tun.c +0
-0net/qrtr/tun.c static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 56
Quoted 3 lines
net/qrtr/tun.c +0
-0@@ -54,19 +54,24 @@ static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to)int count;while (!(skb =skb_dequeue (&tun->queue))) {The body of the loop is entered only if the loop condition has evaluated true. In this case, it means that the value of
!(skb = skb_dequeue(&tun->queue)) had been true, i.e. the value of skb = skb_dequeue(&tun->queue)has been NULL, i.e. that
skb_dequeue()has returned NULL, which had been copied into skb.In other words, in the body of that loop we have skb equal to NULL.
Quoted 7 lines
net/qrtr/tun.c +4
-2-if (filp->f_flags &O_NONBLOCK ) -return -EAGAIN ; +if (filp->f_flags &O_NONBLOCK ) { + count = -EAGAIN ; + goto out; + }
Quoted 14 lines
net/qrtr/tun.c static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 68
Quoted 9 lines
net/qrtr/tun.c +4
-2/* Wait until we get data or the endpoint goes away */ if (wait_event_interruptible (tun->readq, - !skb_queue_empty (&tun->queue))) -return -ERESTARTSYS ; + !skb_queue_empty (&tun->queue))) { + count = -ERESTARTSYS ; + goto out; + } }The meaning of that loop is fairly clear, isn't it? Keep looking int
tun->queueuntil an skb shows up there. If it's not immediately there, fail with-EAGAINfor non-blocking files and wait ontun->readquntil some skb arrives.
Thanks for the explainations.
Navid.