]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
iov_iter: Fix iov_iter_get_pages{,_alloc} page fault return value
authorAndreas Gruenbacher <agruenba@redhat.com>
Wed, 21 Jul 2021 17:03:47 +0000 (19:03 +0200)
committerAndrea Righi <andrea.righi@canonical.com>
Tue, 4 Jan 2022 08:48:16 +0000 (09:48 +0100)
BugLink: https://bugs.launchpad.net/bugs/1951822
[ Upstream commit 814a66741b9ffb5e1ba119e368b178edb0b7322d ]

Both iov_iter_get_pages and iov_iter_get_pages_alloc return the number
of bytes of the iovec they could get the pages for.  When they cannot
get any pages, they're supposed to return 0, but when the start of the
iovec isn't page aligned, the calculation goes wrong and they return a
negative value.  Fix both functions.

In addition, change iov_iter_get_pages_alloc to return NULL in that case
to prevent resource leaks.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
lib/iov_iter.c

index 755c10c5138cdb51d5da166ee2900565dfdfde38..60b5e6edfbaa77865bb7ec763293786b6e91b54f 100644 (file)
@@ -1488,7 +1488,7 @@ ssize_t iov_iter_get_pages(struct iov_iter *i,
                res = get_user_pages_fast(addr, n,
                                iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0,
                                pages);
-               if (unlikely(res < 0))
+               if (unlikely(res <= 0))
                        return res;
                return (res == n ? len : res * PAGE_SIZE) - *start;
        }
@@ -1612,8 +1612,9 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
                        return -ENOMEM;
                res = get_user_pages_fast(addr, n,
                                iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0, p);
-               if (unlikely(res < 0)) {
+               if (unlikely(res <= 0)) {
                        kvfree(p);
+                       *pages = NULL;
                        return res;
                }
                *pages = p;