Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3786437ybi; Mon, 29 Jul 2019 12:36:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqz2OfDRbX7A3jb9Do/zCLuynh/NuvEtnSiUVwtcKnrn+fyPKQAonNZ+AwwGr2ZbJYFTrjT+ X-Received: by 2002:a63:784c:: with SMTP id t73mr108741685pgc.268.1564428960157; Mon, 29 Jul 2019 12:36:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564428960; cv=none; d=google.com; s=arc-20160816; b=JYJ1S6PGdmS3SCufx0JxnEal34hP3ifppme3y/Kz9dlzhdF87w/8of5lSIHjuQ+/Ax yxb9sr1l3JB9rFMsuKmsZFgIV7Lcbr9pSJcnEz1wcMs+cRlSyCRIcWGUdhSL//suQomW O+mqpM5m5TxV78pnXz1zY5a3Xr9p5RWIfL5TFf8A+iikkL3bYJdT92/fRpXyQtBbnQj2 MZklkkbXxzQ3yAE6bQY8li6++bnHK2GqfxZIgu1n81E+cLdLf5z95Q1OlzNnDL9xVQfT +Y3PuN4hxf74kIDKPdmBs6SHPDvAigg1KZLu21iLAKG4q6fQ+xBCM2qFO2aO1rU3egMx ey3w== 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=3vvY70+xotLl9AMnK5r2KG+dG5k4+BKRBj4lw96n8Lg=; b=sznaTb4r5IGYL0c7QhaGGwlU1YZOJtuYm7EPjQbfdKOBd9NiCB7LDVvt9emPTz+6uJ EGWMn8YgmHibLItzBuKQgIKS1x5bV1mCePsVasObyApKm/o2lFevqt9voGnZzeFCUpRe +ymgsLFmu8A5NIMud4kcpSspg35EZgAqNlT2OhhqxVdhdryV2rFqwVxgkSB9BJ09MRDL 90R54vW1c0bOCafg6due92OVt2VKffnW86r84qYon5D6FmCXlFcxHOGXNDKOsll4Ds32 OOEEtkYnaQ8W6evypOt0aqV9tVv0LkQOsCqIKs5/FFaCeSMAq0Bndw70TtXmqzdBMrym 3Uvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zy7tXwNH; 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 q13si26770735pls.369.2019.07.29.12.35.45; Mon, 29 Jul 2019 12:36:00 -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=zy7tXwNH; 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 S1729051AbfG2Tat (ORCPT + 99 others); Mon, 29 Jul 2019 15:30:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:43490 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726674AbfG2Tap (ORCPT ); Mon, 29 Jul 2019 15:30:45 -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 EA32D21850; Mon, 29 Jul 2019 19:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564428644; bh=02lJq1r0mBN4D9aZrTeZswREr3BnLBzB9N1Q5gXdA7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zy7tXwNHfM+3bHJycpkA1aMdcpkVdQsuihlDzr6tEpmhNZpxQmyptFbKVcQL53fzJ DAlc3LRsY+kfY684ShFu00dF7mZa0c/kTObS3btX202mE7kOV/mGb/FPTmVEcahPW5 /Q3wiYoTIL0HNjxZPh8j1kLm2Z7QbZDmtD+IxqN8= 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.14 139/293] ALSA: seq: Break too long mutex context in the write loop Date: Mon, 29 Jul 2019 21:20:30 +0200 Message-Id: <20190729190835.203109671@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190820.321094988@linuxfoundation.org> References: <20190729190820.321094988@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 @@ -1001,7 +1001,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)) @@ -1014,6 +1014,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)) { @@ -1073,12 +1075,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: