From patchwork Fri Mar 31 16:08:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13196180 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A365FC761A6 for ; Fri, 31 Mar 2023 16:10:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 490006B009B; Fri, 31 Mar 2023 12:10:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 440666B009C; Fri, 31 Mar 2023 12:10:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E1416B009D; Fri, 31 Mar 2023 12:10:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 171C96B009B for ; Fri, 31 Mar 2023 12:10:27 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DAB14160487 for ; Fri, 31 Mar 2023 16:10:26 +0000 (UTC) X-FDA: 80629680852.02.05807A4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 1F8B14001C for ; Fri, 31 Mar 2023 16:10:24 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VxDxDqhh; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680279025; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=b8cNPLuNlp8TF1aeLVa/nfCczmmxDIoFIua9rMiLDvM=; b=tucQCb68tAwIXusKy3ydG2WIcDLSmadAQ9mPA5s4K8ppqyK5ugQVswUyIfjo7Eq+n+YJPP bt98l1S1D9ZgeLog2u6SlfSKzmK9lH1W6jzptIUkT1OiHTb1MOfu2ARqFCUT+/rDIvo5T/ QIHHGYfBZYVMm4J/0mN9f8yFmwTqJMg= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VxDxDqhh; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680279025; a=rsa-sha256; cv=none; b=o7IxCw4Rc4aB/EdrKcJ/51u5zoLGfPZ/61ns5EdmJdm+m2/6GvqtIV6nRQFiBzYA3Kwf+B 86tkp365PpvXG6KwkHkEAkvcDgGFWPEFPnKkTCzJC174S/BoqoxGyAzlnvsphpfxgixyUq 3zyQRGUDUgsYB08jaPhEoe7CNegObNA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680279024; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b8cNPLuNlp8TF1aeLVa/nfCczmmxDIoFIua9rMiLDvM=; b=VxDxDqhhPlk5jcB0SSkoIGUsd0iMf0byZGR8NxzGxDhsP35ZiyNKtAQQjQlIZUwOpHacmz tvS5NYDtuDD4QpK4GEaWgCBJzQDoSzy2vWWANkBYTwxngd74onjq8yk7xqcpc4QdLRlHe1 vHEU8ip287ZUrMxSyWZLn1P0ul+AVTM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-203-cb5mzzjVOOCi4dFlfSrt2Q-1; Fri, 31 Mar 2023 12:10:18 -0400 X-MC-Unique: cb5mzzjVOOCi4dFlfSrt2Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 27F62185A78B; Fri, 31 Mar 2023 16:10:17 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44F731121314; Fri, 31 Mar 2023 16:10:15 +0000 (UTC) From: David Howells To: Matthew Wilcox , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: David Howells , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 20/55] af_unix: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data Date: Fri, 31 Mar 2023 17:08:39 +0100 Message-Id: <20230331160914.1608208-21-dhowells@redhat.com> In-Reply-To: <20230331160914.1608208-1-dhowells@redhat.com> References: <20230331160914.1608208-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1F8B14001C X-Stat-Signature: 9e1efdje89jr1z4dq7a6yaxy97ip4k4z X-HE-Tag: 1680279024-94590 X-HE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FK1LAVjS3LxfFFLYNOIIMkKYqsm4N8rHQQfSnWIL85Q118Kxt+nUCQjKqodRTIPgwALrDZ8hN769N+3kL8Nzm9OovFY8kV58F6Cx5EHCXglFosUhXTZR7CaJJ+BI6ce2RZbyPMNRwlxwLQhHFkbhnCmCGJyJ0CbzMbgwTIwXy7pHzKzZHMjDxmDoXKU1t7FZf3yewDXchBa7xLnpGMTNL88kw2vu5/Q0CrLDh6lU56iF2yvGO7VMyy5OyFzi2AlLoOSIsj6tAbumEh/0bgLkH+CFyopIziy3GR37t4x4GRZgnLzR1KCfOGTYTcsdsmkNuvtUGZC0/z1oSRYg2nGTw7XYlcJtevcMqj+r/IXL+XgigL5oSG6nVaui7zIwXB9JQodxzJhhuB+GJdB1fcjpXqj6/X4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.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;