Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp597954img; Fri, 22 Mar 2019 04:52:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqyVOOIdaUMZoeBIneu/LpGXk5TYSU/Z/f1KDx/ilCOIZShubcnZkj1KekqZlKm7fKcXw5Zh X-Received: by 2002:a62:4746:: with SMTP id u67mr8692347pfa.243.1553255570901; Fri, 22 Mar 2019 04:52:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553255570; cv=none; d=google.com; s=arc-20160816; b=GqGG5/LZmrHk+RFL1w5YR97kFkXy+tRFvlLpFgmrJFaguSAjihmrV9ZXrzJ9vyZDE+ 67KxvrgFbGPdJNFRY+Y6+JndRMp3jsX0xOJ1zk4UveWTLBKr+tP13NY2/fTNDezMldzD o4JYHkaZW2Hbi0Ks0FPcDcrdphQ0I/xtKF1TPHbKbykhknswinYyPG7itGOhWFYsmD/X wel0nOTD20A7k7WUSoe/hu8rGQcediwjDcFIGX5WWI61OHI5TAoUCrJU2HSiuyWa7DRC T4a7/RzoxBqfL+Ptk1eXvI0PTQLNyr41VQ11NoWCmkqalh72tkIihqwrSUjVlDk/KMg0 ZT/g== 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=mT/YGoHpYaQOSapf9zvd8zpN7hsF9aDDJ/Ls5PukWSE=; b=hcr+3dmPKWE7YDvXTQ7Cc9eXQ/KGqEHjtb8GNzXt13up63pmvYLQ5lu1MOfferZuwK mRlit2B0BG4jEFUUW2A5lTWxLLeD8HXyZBZmSN63q820V2xjPIh8SApGQ+3vUTAlvX/q 9caFyWlHiPkodD57N50NQBgwP8XtBCQpEX/QKBPtdtl18lp9xhAf3y4OLOgNHRIjR5G6 OUofrEoXsA0qwlDMEf/i7fopj5DC4mYBDrL92D4/toZcYlOKHqg7m1xkfgmxcdB/Ghov info+nGC8oZgj/qNLa8/lJKd74FpHpSUf4tmqRjRnBGr5UTjhw22g9v9NkJA3ZYVwB3b y0DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vGs9tyyL; 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 e21si6235503pfn.179.2019.03.22.04.52.35; Fri, 22 Mar 2019 04:52: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=vGs9tyyL; 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 S1732851AbfCVLwC (ORCPT + 99 others); Fri, 22 Mar 2019 07:52:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:55876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732154AbfCVLv7 (ORCPT ); Fri, 22 Mar 2019 07:51:59 -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 B53412192B; Fri, 22 Mar 2019 11:51:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553255518; bh=x04yC2S3tL6CMTkpcmbqbWA7QxnK8NNDoQ2CwqoQQUw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vGs9tyyLaS4r6YPuFTdX035m59fViJYTSslcSF6HQfS4kQ4Opro++o7O49EoxI0yU WlnU8l9qBwo6zle355w+/IyIM5JM4T7pgaCaicETE3B6C6LiZ+CpVY3KZmVAZx3pjq be7v6xyDE1tj9WfcFw2juyXG8zkzipQh1DDri/o8= 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.14 100/183] splice: dont merge into linked buffers Date: Fri, 22 Mar 2019 12:15:28 +0100 Message-Id: <20190322111248.929624659@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111241.819468003@linuxfoundation.org> References: <20190322111241.819468003@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.14-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 @@ -239,6 +239,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, @@ -247,6 +255,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 @@ -1580,6 +1580,8 @@ retry: */ obuf->flags &= ~PIPE_BUF_FLAG_GIFT; + pipe_buf_mark_unmergeable(obuf); + obuf->len = len; opipe->nrbufs++; ibuf->offset += obuf->len; @@ -1654,6 +1656,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 @@ -183,6 +183,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;