Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10435660ybi; Wed, 24 Jul 2019 23:10:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqyBJ7Xn9ViAlDJqBZPJr+jjySsweQraN26i7X4rnm5D9Y/iYrp0RFGn6RE69bZagUkaghzP X-Received: by 2002:a63:58c:: with SMTP id 134mr88550702pgf.106.1564035010584; Wed, 24 Jul 2019 23:10:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564035010; cv=none; d=google.com; s=arc-20160816; b=01sFAtn3o2xs230dOr8df1GbGQfmrZav8yjU4y987ENFykdD5CiiFMchIiP3hUUqvo outq8IGEqy1AxhsaNV7UU/7W5MIEbB5IcEORrALawh7HALpIW6sJD7gAixxTVfqnUiUx NkLX3H1P2JjrSPhVyVwRgoIFPCjITCDwk/YZdggYhkePthmQkT2nfC2GB2wc7i5gvYjP tDlj6/MYWTK2BpIdIsPtcCe1xMe0gjWqV7b3Ijl6s65siVRl/yB/qT8cNUskS7cufAJX uG8cE5f3kchVAAan7gUk3aFqovDzGu5RnxjSg2Sr/pD7UlC82wXFlDzNq39dR3czzAdS M2jw== 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=8k4Fka7RZIYBtX0FeRFFMDl8q5vqyU1AzaKoC72yIdo=; b=SYKEjC6dndSZpBt5pzP4IrOImtqqAxyORkj0Vu5AMCmd06trz8HYdU1Vunir2uVBMa MT2BYtMvwGGkoBGZ5N5HRQa/JP9E5IrY+BuNBBMVGePUAulZFjfwPCDOSA/yaAf1mph7 9nEnDWpBhpkq8iLWePrKm88IA+fcvtTovcl2LaYt/cW1UZQjKSQL7jKS/eaXljqUNo4r rkhkTQdl4lgn0S7xXdpBJbsxZbCmJ4kCYrj5HaqSwPyYG60WVrSyT45+rn//rLZJ6gbk DREnIYwFUV6BgAO7FqMalifxAlnS0+nZ9YirTYUdgVPQNJNbJcvkPeeFkRFj9MTTc8HX VeRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EHj03AWu; 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 cf16si14580483plb.346.2019.07.24.23.09.55; Wed, 24 Jul 2019 23:10:10 -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=EHj03AWu; 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 S1728263AbfGYFto (ORCPT + 99 others); Thu, 25 Jul 2019 01:49:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:59042 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391124AbfGYFn7 (ORCPT ); Thu, 25 Jul 2019 01:43: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 E335522C7D; Thu, 25 Jul 2019 05:43:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564033438; bh=8t5QVbCM8xFqk4NuKu5dd9sVDoCfed5xoDeOenDTj5I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EHj03AWuMbCbK5KUtLuBTN2dJDhG6M+NJbUeZuMWvdO6BiOEb/+GhR5bpeb/+EeuA w4uQmQ+rawiii5CwfqyuDh437Q6UnNkxJtitxaEJTTip93T90XllvpYPpF6p6j9xgA fLu5gr2XG3/8kFIJk/u5dxCNgutUET4PkjzAvWwQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+97aae04ce27e39cbfca9@syzkaller.appspotmail.com, syzbot+4c595632b98bb8ffcc66@syzkaller.appspotmail.com, Takashi Iwai Subject: [PATCH 4.19 206/271] ALSA: seq: Break too long mutex context in the write loop Date: Wed, 24 Jul 2019 21:21:15 +0200 Message-Id: <20190724191712.734474221@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191655.268628197@linuxfoundation.org> References: <20190724191655.268628197@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Takashi Iwai commit ede34f397ddb063b145b9e7d79c6026f819ded13 upstream. The fix for the racy writes and ioctls to sequencer widened the application of client->ioctl_mutex to the whole write loop. Although it does unlock/relock for the lengthy operation like the event dup, the loop keeps the ioctl_mutex for the whole time in other situations. This may take quite long time if the user-space would give a huge buffer, and this is a likely cause of some weird behavior spotted by syzcaller fuzzer. This patch puts a simple workaround, just adding a mutex break in the loop when a large number of events have been processed. This shouldn't hit any performance drop because the threshold is set high enough for usual operations. Fixes: 7bd800915677 ("ALSA: seq: More protection for concurrent write and ioctl races") Reported-by: syzbot+97aae04ce27e39cbfca9@syzkaller.appspotmail.com Reported-by: syzbot+4c595632b98bb8ffcc66@syzkaller.appspotmail.com Cc: Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/seq/seq_clientmgr.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) --- a/sound/core/seq/seq_clientmgr.c +++ b/sound/core/seq/seq_clientmgr.c @@ -1004,7 +1004,7 @@ static ssize_t snd_seq_write(struct file { struct snd_seq_client *client = file->private_data; int written = 0, len; - int err; + int err, handled; struct snd_seq_event event; if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT)) @@ -1017,6 +1017,8 @@ static ssize_t snd_seq_write(struct file if (!client->accept_output || client->pool == NULL) return -ENXIO; + repeat: + handled = 0; /* allocate the pool now if the pool is not allocated yet */ mutex_lock(&client->ioctl_mutex); if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) { @@ -1076,12 +1078,19 @@ static ssize_t snd_seq_write(struct file 0, 0, &client->ioctl_mutex); if (err < 0) break; + handled++; __skip_event: /* Update pointers and counts */ count -= len; buf += len; written += len; + + /* let's have a coffee break if too many events are queued */ + if (++handled >= 200) { + mutex_unlock(&client->ioctl_mutex); + goto repeat; + } } out: