Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp609923img; Fri, 22 Mar 2019 05:06:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqzXvv7bpdtz5xPGJaGiojkh5Y5mZ4rWTnJkq5FSuk0iMbS6+yfuod8VxqEOqMxRXy5m+dIh X-Received: by 2002:a62:58c7:: with SMTP id m190mr8825126pfb.4.1553256410925; Fri, 22 Mar 2019 05:06:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553256410; cv=none; d=google.com; s=arc-20160816; b=E82Eo4aVoBmW6Jl0MF/F5HS77rHlnlF0SAMgbCz0m2vFkBUii7Ab8TCWqJRRRji137 lNGLINbhyoqQaopfiYrhHqjIUgg7jjaayDbODJ02pRpnYWtVjIjEtA5rm/XBxyrvrpeG F1MBxEAWi3FZN6j8mfcf/RNe+XBcsZ4wFYCDo6SDZMzyib01TWESlY/qQGOTkYk8i++2 GlptSt0ygqZa6UDzneXq2gEJxkoVw/wM2zHudcBDTwKgTOIcg/N4zf/Oa/oOwbyFYY2Y Qt97xSkN9bPkRdPKehXZob7YU/Md//fqu+pEYgCFjN1xJ5pM4ThO11AqVJ0u77VV5uVn fJaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LcHeXikCTssSBkkakdzHy+J7QNGsgieprEmUZZyWfuI=; b=SJEhQ7uAX+rkYaDwp2b57oQzs81AaIfe+0gIX7MAjDZBEbTD3zSKatDl2JoksWWlVv aYXMqZOp1g+v0X0I+vl8RSIMrJOR/jGkN8aFMpJAqfEDzUq34mz4nutkkvEg+kcBwt2Q VYalmFGJBAWesa3LeLwFRhW/T5jMiomZSjXTVRILYw18adEuJj4iAv8IjPCSxWNfqBOj +e+pM5pQSZL4p1YZwTdne5ckUi8QA6h/8rPvUkXe+bsztI+p+uNYgwagVHzJCCZkppP8 LTnDLGOy4unZ+u9A2tr+SEWCKKvHLtQQKg2kPr8MB137I54IGdn8QiwiGSdbKUav0Zrs EmXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=D4q9OWHx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y2si6720940pll.133.2019.03.22.05.06.33; Fri, 22 Mar 2019 05:06:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=D4q9OWHx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388785AbfCVMFP (ORCPT + 99 others); Fri, 22 Mar 2019 08:05:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:43656 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388460AbfCVMFM (ORCPT ); Fri, 22 Mar 2019 08:05:12 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 09C4621929; Fri, 22 Mar 2019 12:05:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553256311; bh=cEx+BKpucAahQioEBw5JRIhb6sqvaynHd41tZ5y+dYo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D4q9OWHxxM6wH+XouczmUsCtSozy+/P4DxtfSATs2IfEIPH2ei8fZH4q2h/3gT8jW 19MOeJx56gcp5+UejQan9eE/byXJd/h80wjFJOTs42ethiiKjFib3pBDNQ5YJAYor+ x0qQPCKSL483G7lxvD4gbq7VR8AQduDq0PhwK3aU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro , Jann Horn Subject: [PATCH 4.19 164/280] splice: dont merge into linked buffers Date: Fri, 22 Mar 2019 12:15:17 +0100 Message-Id: <20190322111323.143782969@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111306.356185024@linuxfoundation.org> References: <20190322111306.356185024@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn commit a0ce2f0aa6ad97c3d4927bf2ca54bcebdf062d55 upstream. Before this patch, it was possible for two pipes to affect each other after data had been transferred between them with tee(): ============ $ cat tee_test.c int main(void) { int pipe_a[2]; if (pipe(pipe_a)) err(1, "pipe"); int pipe_b[2]; if (pipe(pipe_b)) err(1, "pipe"); if (write(pipe_a[1], "abcd", 4) != 4) err(1, "write"); if (tee(pipe_a[0], pipe_b[1], 2, 0) != 2) err(1, "tee"); if (write(pipe_b[1], "xx", 2) != 2) err(1, "write"); char buf[5]; if (read(pipe_a[0], buf, 4) != 4) err(1, "read"); buf[4] = 0; printf("got back: '%s'\n", buf); } $ gcc -o tee_test tee_test.c $ ./tee_test got back: 'abxx' $ ============ As suggested by Al Viro, fix it by creating a separate type for non-mergeable pipe buffers, then changing the types of buffers in splice_pipe_to_pipe() and link_pipe(). Cc: Fixes: 7c77f0b3f920 ("splice: implement pipe to pipe splicing") Fixes: 70524490ee2e ("[PATCH] splice: add support for sys_tee()") Suggested-by: Al Viro Signed-off-by: Jann Horn Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/pipe.c | 14 ++++++++++++++ fs/splice.c | 4 ++++ include/linux/pipe_fs_i.h | 1 + 3 files changed, 19 insertions(+) --- a/fs/pipe.c +++ b/fs/pipe.c @@ -234,6 +234,14 @@ static const struct pipe_buf_operations .get = generic_pipe_buf_get, }; +static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = { + .can_merge = 0, + .confirm = generic_pipe_buf_confirm, + .release = anon_pipe_buf_release, + .steal = anon_pipe_buf_steal, + .get = generic_pipe_buf_get, +}; + static const struct pipe_buf_operations packet_pipe_buf_ops = { .can_merge = 0, .confirm = generic_pipe_buf_confirm, @@ -242,6 +250,12 @@ static const struct pipe_buf_operations .get = generic_pipe_buf_get, }; +void pipe_buf_mark_unmergeable(struct pipe_buffer *buf) +{ + if (buf->ops == &anon_pipe_buf_ops) + buf->ops = &anon_pipe_buf_nomerge_ops; +} + static ssize_t pipe_read(struct kiocb *iocb, struct iov_iter *to) { --- a/fs/splice.c +++ b/fs/splice.c @@ -1593,6 +1593,8 @@ retry: */ obuf->flags &= ~PIPE_BUF_FLAG_GIFT; + pipe_buf_mark_unmergeable(obuf); + obuf->len = len; opipe->nrbufs++; ibuf->offset += obuf->len; @@ -1667,6 +1669,8 @@ static int link_pipe(struct pipe_inode_i */ obuf->flags &= ~PIPE_BUF_FLAG_GIFT; + pipe_buf_mark_unmergeable(obuf); + if (obuf->len > len) obuf->len = len; --- a/include/linux/pipe_fs_i.h +++ b/include/linux/pipe_fs_i.h @@ -182,6 +182,7 @@ void generic_pipe_buf_get(struct pipe_in int generic_pipe_buf_confirm(struct pipe_inode_info *, struct pipe_buffer *); int generic_pipe_buf_steal(struct pipe_inode_info *, struct pipe_buffer *); void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *); +void pipe_buf_mark_unmergeable(struct pipe_buffer *buf); extern const struct pipe_buf_operations nosteal_pipe_buf_ops;