Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp28048ybl; Thu, 5 Dec 2019 14:32:47 -0800 (PST) X-Google-Smtp-Source: APXvYqxGnp5oyERxAZE4kOI9hnPvLNvtFHBhjZKmyafbB/8FWpPRIvjAO/VVLifyI3YymrS2XT9X X-Received: by 2002:aca:b805:: with SMTP id i5mr9885202oif.110.1575585167465; Thu, 05 Dec 2019 14:32:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575585167; cv=none; d=google.com; s=arc-20160816; b=MkL7Ro5eHWqKwiHlb1nkEZfChZfW+w1kkxc3wpI34eRVDaFnlcWwcB29Fn/LEs2ytE Pu5ictlI88GdelJ3tWyqxN3RwQZfobOfsihYIXDIuwPkec5wvXX4UgYsKeELNWopn7w/ D3pX40WvyhTKqPdqDdKav/lgzFurmUhnOqg93Q36Qjl7NQHZnE44H19wDdKQYJWzADvX fuaB69PPcRtMPApP+SajS9W97QeFGI2QaZSm9xVBMg8+oEVCMwCiASIE+HnCFo+vO6jk 52XcSeWxTePgQ4Ceu7sS+XoHkMmmbf1e8ZEZcVOvuG6yc7NAC/YMEtrxW5pJgPOFXtje GhiA== 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:cc:to:from :subject:organization:dkim-signature; bh=38xi2TGNPFhGvwzO4gnWgKizEkSGuV4mkJf443b5kzE=; b=yeJWBBknEk0KmC6Y+tGhfD+SfkYWUqyO7kyXxGWfYe3ZL0ieEfozts+haqhbggeuLz SO0E5o0oN/xj7EYjEO5PyIO7dDj8LFX/iUmNg44f4bzYd5qdMYdWG6nIJEc7Zc9GZUov 8nTbl2gk+Td7WYDIP+CeX8WYj7jCmiXdZFaCegAvZPKIyIfNHZIqBTxdBUMVR4ai8142 pm4bsJ33u3FP59dfGR3DEvq+7TfZ072nt58vKBi2+BRX5YZGabjLBIEAfQ1qQk7BdLgE 8jaTX8Co7bbLcjNInImSryx+0M0EHS78kTUtFtOizYNIlcYM7h9RvRccU7MPSW0CFgQi jIBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ABJq57Uq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 34si5716713otu.34.2019.12.05.14.32.35; Thu, 05 Dec 2019 14:32:47 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=ABJq57Uq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727861AbfLEWap (ORCPT + 99 others); Thu, 5 Dec 2019 17:30:45 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:35128 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727595AbfLEWao (ORCPT ); Thu, 5 Dec 2019 17:30:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575585043; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=38xi2TGNPFhGvwzO4gnWgKizEkSGuV4mkJf443b5kzE=; b=ABJq57UqQedJmbwVSOV+pGYWttwsM9PL6vD7+cquwbxiLCkxzMRUGGJxvwFp68CaHNNr5H 6CxztlJ3hIbFhz+AK+Jz7rdloqaY5uxhL7Uj7JrGHa10SxFV6Y1RSpk5mp2YlI+z+tf7UN YdWHFq/5JDN/kbn+k+uaXDuD1qlD8fk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-131-10AAV6W9MlCauT2HZuk5hQ-1; Thu, 05 Dec 2019 17:30:40 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0BB94593A2; Thu, 5 Dec 2019 22:30:39 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-120-250.rdu2.redhat.com [10.10.120.250]) by smtp.corp.redhat.com (Postfix) with ESMTP id D388160C80; Thu, 5 Dec 2019 22:30:37 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 2/2] pipe: Fix missing mask update after pipe_wait() [ver #2] From: David Howells To: torvalds@linux-foundation.org, ebiggers@kernel.org Cc: dhowells@redhat.com, viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 05 Dec 2019 22:30:37 +0000 Message-ID: <157558503716.10278.17734879104574600890.stgit@warthog.procyon.org.uk> In-Reply-To: <157558502272.10278.8718685637610645781.stgit@warthog.procyon.org.uk> References: <157558502272.10278.8718685637610645781.stgit@warthog.procyon.org.uk> User-Agent: StGit/unknown-version MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: 10AAV6W9MlCauT2HZuk5hQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix pipe_write() to not cache the ring index mask and max_usage as their values are invalidated by calling pipe_wait() because the latter function drops the pipe lock, thereby allowing F_SETPIPE_SZ change them. Without this, pipe_write() may subsequently miscalculate the array indices and pipe fullness, leading to an oops like the following: BUG: KASAN: slab-out-of-bounds in pipe_write+0xc25/0xe10 fs/pipe.c:481 Write of size 8 at addr ffff8880771167a8 by task syz-executor.3/7987 ... CPU: 1 PID: 7987 Comm: syz-executor.3 Not tainted 5.4.0-rc2-syzkaller #0 ... Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x113/0x167 lib/dump_stack.c:113 print_address_description.constprop.8.cold.10+0x9/0x31d mm/kasan/report.c:374 __kasan_report.cold.11+0x1b/0x3a mm/kasan/report.c:506 kasan_report+0x12/0x20 mm/kasan/common.c:634 __asan_report_store8_noabort+0x17/0x20 mm/kasan/generic_report.c:137 pipe_write+0xc25/0xe10 fs/pipe.c:481 call_write_iter include/linux/fs.h:1895 [inline] new_sync_write+0x3fd/0x7e0 fs/read_write.c:483 __vfs_write+0x94/0x110 fs/read_write.c:496 vfs_write+0x18a/0x520 fs/read_write.c:558 ksys_write+0x105/0x220 fs/read_write.c:611 __do_sys_write fs/read_write.c:623 [inline] __se_sys_write fs/read_write.c:620 [inline] __x64_sys_write+0x6e/0xb0 fs/read_write.c:620 do_syscall_64+0xca/0x5d0 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x49/0xbe This is not a problem for pipe_read() as the mask is recalculated on each pass of the loop, after pipe_wait() has been called. Fixes: 8cefc107ca54 ("pipe: Use head and tail pointers for the ring, not cursor and length") Reported-by: syzbot+838eb0878ffd51f27c41@syzkaller.appspotmail.com Signed-off-by: David Howells cc: Eric Biggers --- fs/pipe.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index da782ee251d2..8061b093140d 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -389,7 +389,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) { struct file *filp = iocb->ki_filp; struct pipe_inode_info *pipe = filp->private_data; - unsigned int head, max_usage, mask; + unsigned int head; ssize_t ret = 0; int do_wakeup = 0; size_t total_len = iov_iter_count(from); @@ -408,13 +408,12 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) } head = pipe->head; - max_usage = pipe->max_usage; - mask = pipe->ring_size - 1; /* We try to merge small writes */ chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ if (!pipe_empty(head, pipe->tail) && chars != 0) { - struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask]; + struct pipe_buffer *buf = + &pipe->bufs[(head - 1) & (pipe->ring_size - 1)]; int offset = buf->offset + buf->len; if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) { @@ -443,8 +442,9 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) } head = pipe->head; - if (!pipe_full(head, pipe->tail, max_usage)) { - struct pipe_buffer *buf = &pipe->bufs[head & mask]; + if (!pipe_full(head, pipe->tail, pipe->max_usage)) { + struct pipe_buffer *buf = + &pipe->bufs[head & (pipe->ring_size - 1)]; struct page *page = pipe->tmp_page; int copied; @@ -465,7 +465,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) spin_lock_irq(&pipe->wait.lock); head = pipe->head; - if (pipe_full(head, pipe->tail, max_usage)) { + if (pipe_full(head, pipe->tail, pipe->max_usage)) { spin_unlock_irq(&pipe->wait.lock); continue; } @@ -484,7 +484,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); /* Insert it into the buffer array */ - buf = &pipe->bufs[head & mask]; + buf = &pipe->bufs[head & (pipe->ring_size - 1)]; buf->page = page; buf->ops = &anon_pipe_buf_ops; buf->offset = 0; @@ -510,7 +510,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) break; } - if (!pipe_full(head, pipe->tail, max_usage)) + if (!pipe_full(head, pipe->tail, pipe->max_usage)) continue; /* Wait for buffer space to become available. */