From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70A62C433F5 for ; Thu, 30 Sep 2021 06:55:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 50D0C615E5 for ; Thu, 30 Sep 2021 06:55:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348411AbhI3G5h (ORCPT ); Thu, 30 Sep 2021 02:57:37 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:51628 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347826AbhI3G5f (ORCPT ); Thu, 30 Sep 2021 02:57:35 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 150B81FE38; Thu, 30 Sep 2021 06:55:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1632984953; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vBYVE+FuPoCe63k5/yqqS9Hxvmo7X180WS6qdmXKozo=; b=YYmV059ysXKqgj5pwukBY2iSxfrh8admY2H+PJ+rjC0Lt5WoU0cX+rs4Q07/EOD4MZPGXF 6BfUwC/OH3ZKn0JHK/MYMnkPlFBjYWM0Jc2K/Wm7cBF+4Ysh7nIgjVuFZZYekZcdLRf2l7 2yVPnunZvfhp/p50xuUamyTP3eXubko= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1632984953; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vBYVE+FuPoCe63k5/yqqS9Hxvmo7X180WS6qdmXKozo=; b=nIeRErmvcdaet6Dj+9RjGNHn6JbEF31wPkNbvFt2v8ZgH/CGgELW+s80hS5C3gmtHZffv7 uPlPt6vB4xmPF3Aw== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id EECAAA3B81; Thu, 30 Sep 2021 06:55:52 +0000 (UTC) Date: Thu, 30 Sep 2021 08:55:52 +0200 Message-ID: From: Takashi Iwai To: John Keeping Cc: linux-kernel@vger.kernel.org, alsa-devel@alsa-project.org, Takashi Iwai Subject: Re: [PATCH] ALSA: rawmidi: Fix potential UAF from sequencer destruction In-Reply-To: References: <20210929113620.2194847-1-john@metanate.com> <20210929161758.49ce947f.john@metanate.com> <20210929175632.50b78be8.john@metanate.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 30 Sep 2021 08:31:56 +0200, Takashi Iwai wrote: > > On Wed, 29 Sep 2021 18:56:32 +0200, > John Keeping wrote: > > > > On Wed, 29 Sep 2021 17:28:57 +0200 > > Takashi Iwai wrote: > > > > > On Wed, 29 Sep 2021 17:17:58 +0200, > > > John Keeping wrote: > > > > > > > > On Wed, 29 Sep 2021 16:51:47 +0200 > > > > Takashi Iwai wrote: > > > > > > > > > On Wed, 29 Sep 2021 13:36:20 +0200, > > > > > John Keeping wrote: > > > > > > > > > > > > If the sequencer device outlives the rawmidi device, then > > > > > > snd_rawmidi_dev_seq_free() will run after release_rawmidi_device() has > > > > > > freed the snd_rawmidi structure. > > > > > > > > > > > > This can easily be reproduced with CONFIG_DEBUG_KOBJECT_RELEASE. > > > > > > > > > > > > Keep a reference to the rawmidi device until the sequencer has been > > > > > > destroyed in order to avoid this. > > > > > > > > > > > > Signed-off-by: John Keeping > > > > > > > > > > Thanks for the patch. I wonder, though, how this could be triggered. > > > > > Is this the case where the connected sequencer device is being used > > > > > while the sound card gets released? Or is it something else? > > > > > > > > I'm not sure if it's possible to trigger via the ALSA API; I haven't > > > > found a route that can trigger it, but that doesn't mean there isn't > > > > one :-) > > > > > > > > Mostly this is useful to make CONFIG_DEBUG_KOBJECT_RELEASE cleaner. > > > > > > Hm, then could you check whether the patch below papers over it > > > instead? > > > > No, this patch doesn't solve it. The issue is that the effect of the > > final device_put() is delayed from the time it is called and there is no > > way to guarantee the ordering without ensuring the sequencer has been > > destroyed before the final reference to the rawmidi device is put. > > > > Both of the functions involved are called from the core > > device::release() hook. > > > > I'm using the patch below to easily check that the sequencer has been > > freed before the rawmidi data. This can easily be triggered by > > unplugging a USB MIDI device (it's not 100% since the kobject release > > delays are random). > > Hm, it's strange. I suppose you're *not* using the MIDI device, > right? > > The release path for the USB-audio driver is: > usb_audio_disconnect() -> > snd_card_free_when_closed() -> > release_card_device() (via put_device(&card->card_dev)) -> > snd_card_do_free() > > And here in snd_card_do_free(), the snd_device free-callback chains > are called at the beginning (snd_device_free_all()). > As it's executed in a reverse loop, snd_rawmidi_dev_seq_free() shall > be called before snd_rawmidi_dev_free(). Since the final put_device() > for the rawmidi device is called in the latter function, the device > release must not happen before snd_rawmidi_dev_seq_free()... Correction: now I finally understood what I misunderstood. Although the snd_device call chain mentioned above itself is correct, the snd_rawmidi_dev_seq_free() function isn't called directly from the snd_device chain, but it's rater the own private_free of snd_seq_device object. That is, the call of snd_seq_device private_free is done in a wrong place; it should be called in the snd_device call chain instead of the device release. A fix patch is something like below. Could you check whether this fixes the problem? thanks, Takashi --- a/sound/core/seq_device.c +++ b/sound/core/seq_device.c @@ -156,6 +156,8 @@ static int snd_seq_device_dev_free(struct snd_device *device) struct snd_seq_device *dev = device->device_data; cancel_autoload_drivers(); + if (dev->private_free) + dev->private_free(dev); put_device(&dev->dev); return 0; } @@ -183,11 +185,7 @@ static int snd_seq_device_dev_disconnect(struct snd_device *device) static void snd_seq_dev_release(struct device *dev) { - struct snd_seq_device *sdev = to_seq_dev(dev); - - if (sdev->private_free) - sdev->private_free(sdev); - kfree(sdev); + kfree(to_seq_dev(dev)); } /*