From patchwork Wed Apr 5 16:53: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: 13202273 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 93485C7619A for ; Wed, 5 Apr 2023 16:54:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DD006B0093; Wed, 5 Apr 2023 12:54:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 28EF46B0095; Wed, 5 Apr 2023 12:54:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 105E46B0096; Wed, 5 Apr 2023 12:54:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EB0766B0093 for ; Wed, 5 Apr 2023 12:54:48 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A872DAC2FB for ; Wed, 5 Apr 2023 16:54:48 +0000 (UTC) X-FDA: 80647936656.09.5F6ED0F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id DDEF640023 for ; Wed, 5 Apr 2023 16:54:46 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NT9H3O18; spf=pass (imf11.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680713686; 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=QZshbeXwoidsAPUStOu/ElI81f+RaVvzRteHlRNDwSs=; b=kJJpqnikmh9s1TNGWB96k9LLTBX3kUdD5yy7AXg0WPUY3tTKvR3MZbpZFmo/0qbL4RExVQ J4l9vKtzMXtMzSQcO4r43XG5Gp8fDzShmtQz1yrACqNWGUFoPGu7boRLMqLjZ3GnSdAqLU HV3BnqXSx6e6z7Ryew1hEwGY5XQuwiQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NT9H3O18; spf=pass (imf11.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680713686; a=rsa-sha256; cv=none; b=spJVtaV6t4WYeUW2krD14s5MSo15HfV59egADm+htzbsH7xq03FBwbpyhuZDGysOH9P3at IZFGXGVmvvObG3Vc3Fz5BS+PI6J34MiFeYR/nGae+KNyOEWDXNahqsqF0cdNTpcIfnoEDd gJ6Uz1Olbu8gMf7s51MayCb3SMWRGO0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713686; 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=QZshbeXwoidsAPUStOu/ElI81f+RaVvzRteHlRNDwSs=; b=NT9H3O187YCBRnISQk6rhHmHWRnbVy6/BCCiAyVNHF4JVQ4zoSP5Cj7gzv20EdU8AqS2DX lzAuJAptmPLtaEnYrw3iaXchKFy1b0PcD/XYKn/Ymlj/KCkmkluz+/qKnAD07QZzt4QSeW QxPAPLkW8lzyR3OrJonWk0DFdbCY9ok= 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-659-7ntbeM6RPUWZ_fvl6pOJLw-1; Wed, 05 Apr 2023 12:54:43 -0400 X-MC-Unique: 7ntbeM6RPUWZ_fvl6pOJLw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1C59F8996E4; Wed, 5 Apr 2023 16:54:42 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 19E08140EBF4; Wed, 5 Apr 2023 16:54:40 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 20/20] af_unix: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data Date: Wed, 5 Apr 2023 17:53:39 +0100 Message-Id: <20230405165339.3468808-21-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: DDEF640023 X-Stat-Signature: kfohdr5omf7xif3zmukhp4m7ffbgzdna X-Rspam-User: X-HE-Tag: 1680713686-275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QKudDhEMElI09O77KVTaqNUoNid9Wt4kQ6ykQ6yvWBTYj8TUpwAUi9IrJv4w4Y2PGJpNql7BvS/ilCJ66CM9IbnZa+krjU5o4mPFZQj+/57+54/Njp344VtIc5283f+xD3AhMJbQ4xZeKtmtnXDx7Ig4FsCS3n2xOvo9xvS1U7qPsSJZ0OVeoG9FHtC/NaIWs8mfi2OqWwl6tkHD7yWJ6Orqoxn5rs+YfM1cn41dhBNmI3WHKt6Td0BD3yxP4oIS5qWuzYWX1Jt0rEQ2AzTlpKHOF+lJrgq6eloqLfFzw1xsAIXrFTpyuAcJZEG3puEU3th2ixOroZwOK5Mpw9zDtiEdaZpkvEHn7xY+zomPjqoNkKU5eGt9hstQpHOagjHLsPKUh1+didFyhivBaM5UAGdTiuUX39fqNDAwBNGEv403ycXAyh9V2wd3y4I9DgAOfMYf0XbYPeaQ1rqPQh5jYcHbACsIs00Ix9dBcJs4c= 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 fee431a089d3..6941be8dae7e 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2160,12 +2160,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; @@ -2177,31 +2177,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, @@ -2278,7 +2299,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;