[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230331160914.1608208-21-dhowells@redhat.com>
Date: Fri, 31 Mar 2023 17:08:39 +0100
From: David Howells <dhowells@...hat.com>
To: Matthew Wilcox <willy@...radead.org>,
"David S. Miller" <davem@...emloft.net>,
Eric Dumazet <edumazet@...gle.com>,
Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>
Cc: David Howells <dhowells@...hat.com>,
Al Viro <viro@...iv.linux.org.uk>,
Christoph Hellwig <hch@...radead.org>,
Jens Axboe <axboe@...nel.dk>, Jeff Layton <jlayton@...nel.org>,
Christian Brauner <brauner@...nel.org>,
Chuck Lever III <chuck.lever@...cle.com>,
Linus Torvalds <torvalds@...ux-foundation.org>,
netdev@...r.kernel.org, linux-fsdevel@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-mm@...ck.org
Subject: [PATCH v3 20/55] af_unix: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data
If sendmsg() with MSG_SPLICE_PAGES encounters a page that shouldn't be
spliced - a slab page, for instance, or one with a zero count - make
unix_extract_bvec_to_skb() copy it.
Signed-off-by: David Howells <dhowells@...hat.com>
cc: "David S. Miller" <davem@...emloft.net>
cc: Eric Dumazet <edumazet@...gle.com>
cc: Jakub Kicinski <kuba@...nel.org>
cc: Paolo Abeni <pabeni@...hat.com>
cc: Jens Axboe <axboe@...nel.dk>
cc: Matthew Wilcox <willy@...radead.org>
cc: netdev@...r.kernel.org
---
net/unix/af_unix.c | 44 +++++++++++++++++++++++++++++++++-----------
1 file changed, 33 insertions(+), 11 deletions(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index a9ad97f3c57f..88b91005567e 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -2154,12 +2154,12 @@ static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other
/*
* Extract pages from an iterator and add them to the socket buffer.
*/
-static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb,
- struct iov_iter *iter, ssize_t maxsize)
+static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb, struct iov_iter *iter,
+ ssize_t maxsize, gfp_t gfp)
{
struct page *pages[8], **ppages = pages;
unsigned int i, nr;
- ssize_t ret = 0;
+ ssize_t spliced = 0, ret = 0;
while (iter->count > 0) {
size_t off, len;
@@ -2171,31 +2171,52 @@ static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb,
len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off);
if (len <= 0) {
- if (!ret)
- ret = len ?: -EIO;
+ ret = len ?: -EIO;
break;
}
i = 0;
do {
+ struct page *page = pages[i++];
size_t part = min_t(size_t, PAGE_SIZE - off, len);
+ bool put = false;
+
+ if (!sendpage_ok(page)) {
+ const void *p = kmap_local_page(page);
+ void *q;
+
+ q = page_frag_memdup(NULL, p + off, part, gfp,
+ ULONG_MAX);
+ kunmap_local(p);
+ if (!q) {
+ iov_iter_revert(iter, len);
+ ret = -ENOMEM;
+ goto out;
+ }
+ page = virt_to_page(q);
+ off = offset_in_page(q);
+ put = true;
+ }
- if (skb_append_pagefrags(skb, pages[i++], off, part) < 0) {
- if (!ret)
- ret = -EMSGSIZE;
+ ret = skb_append_pagefrags(skb, page, off, part);
+ if (put)
+ put_page(page);
+ if (ret < 0) {
+ iov_iter_revert(iter, len);
goto out;
}
off = 0;
- ret += part;
+ spliced += part;
maxsize -= part;
len -= part;
} while (len > 0);
+
if (maxsize <= 0)
break;
}
out:
- return ret;
+ return spliced ?: ret;
}
static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
@@ -2272,7 +2293,8 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
fds_sent = true;
if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) {
- size = unix_extract_bvec_to_skb(skb, &msg->msg_iter, size);
+ size = unix_extract_bvec_to_skb(skb, &msg->msg_iter, size,
+ sk->sk_allocation);
skb->data_len += size;
skb->len += size;
skb->truesize += size;
Powered by blists - more mailing lists