Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp19976pxb; Wed, 29 Sep 2021 23:33:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6OEA+g7xLcrMmAlPsW39T5MR047VZ3tUZkdy0PLbIdmTh8+o21fm1xzndqBicCTY4p6OU X-Received: by 2002:aa7:96c3:0:b0:44b:b418:c0da with SMTP id h3-20020aa796c3000000b0044bb418c0damr3935972pfq.32.1632983583631; Wed, 29 Sep 2021 23:33:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632983583; cv=none; d=google.com; s=arc-20160816; b=gcarhx08jIIMwD3zrbTl+WGlhPteZQnUy8qjPSTYidVVkPFQuxN5P5SGl0vrUt1P5C WR5SF2LY9qgOdTlxjQg3ta9vTUIdj+SqBhFiR2mkW/aVapceVUTW3aRrxNZuG7DfqpGc UqyXbWe9GsTFFAkWg2GjMlWRnhXOUCcF3FxrdS+Ch/h+uCUEanyO6klQnzFk8VlV2FoQ kc1ldkaP0UgcxVxWq9YIYtpm715WUGBbQ82DMVvjfTUEm4IKc24/+xNmL4ysZuYei3nW CoPWaexLZUqDT6vw7Yrl6hQWoUoTzcMJwXB3lWyOaVwVH/BJ4NGp9KbZy6MnYKpEtUWb Mimw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=EepkJrRlZwTddSfo/jzuw2ng8ax6xsdoU2dAI/qRSGQ=; b=pyGQ5xhJ/4JEnwBgEJlk+TRGynnrvyAe1jCF4LiWJMOqJAb/gylfMPVixjmYAdWh9y cF16K+Cux0dpyUENHmf9gJsIPKEXoeSVFAwUvzEVs+BWxQ+x7cQ1cW8yJtDdyePQANfB bnaBwb1nzI6a7CcsL2hJihwjrL923ZY6HkLOATb4TQeAszKWUazakjvZFyOAa1SiD2df ABwsv3CMdjUoxg4zgvPJHSYQIyylsjTVwoHyL1zkSWcvEBY35pQlRrY0hoPllSuQohUo CFCKgWeOf1NQADqJdXTOW6S5pb/5QAltFXP/eH53v8SFDHEPLXrLKixd1bS7q8bwP2qp H4Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="i5B5yvE/"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=63yWmBTr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mh15si7063464pjb.168.2021.09.29.23.32.48; Wed, 29 Sep 2021 23:33:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="i5B5yvE/"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=63yWmBTr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348078AbhI3Gdl (ORCPT + 99 others); Thu, 30 Sep 2021 02:33:41 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:48470 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348079AbhI3Gdk (ORCPT ); Thu, 30 Sep 2021 02:33:40 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 4857D1FE36; Thu, 30 Sep 2021 06:31:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1632983516; 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=EepkJrRlZwTddSfo/jzuw2ng8ax6xsdoU2dAI/qRSGQ=; b=i5B5yvE/Cuou9CJYmGdL8qf43x/Rg/kxHh6JzNh75UJS5hIX0Zk3QkaIKZZj+QWWo1vPQQ n94e5chAourdcQEOqcnQqRehjrEVMdZoKGtQwCro5sX3xCi99uHDbaRg8fjEMCCdqkSjds Y8Ahmh8CQSRHkqUfuB7TxhlKtB3PKpQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1632983516; 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=EepkJrRlZwTddSfo/jzuw2ng8ax6xsdoU2dAI/qRSGQ=; b=63yWmBTrBx4e15+WeuiMcCTWcAiXuSwPdrrwZVCqdDlVEWaqQoAebBplE+TxXcHUnglsPF Chadz94BcELp+lCg== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id 3FEE4A3B89; Thu, 30 Sep 2021 06:31:56 +0000 (UTC) Date: Thu, 30 Sep 2021 08:31:56 +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: <20210929175632.50b78be8.john@metanate.com> 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 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()... So I still wonder how the problem could be triggered at all. Even if the device object release itself is delayed, it shouldn't matter in the scenario above (as the snd_device-free-chains are already called beforehand). thanks, Takashi > > -- >8 -- > --- a/sound/core/rawmidi.c > +++ b/sound/core/rawmidi.c > @@ -1571,7 +1571,10 @@ static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi, > > static void release_rawmidi_device(struct device *dev) > { > - kfree(container_of(dev, struct snd_rawmidi, dev)); > + struct snd_rawmidi *rmidi = container_of(dev, struct snd_rawmidi, dev); > + > + WARN_ON(rmidi->seq_dev); > + kfree(rmidi); > } > > /** > -- 8< -- > > > --- a/sound/core/seq/seq_ports.c > > +++ b/sound/core/seq/seq_ports.c > > @@ -415,11 +415,16 @@ static int subscribe_port(struct snd_seq_client *client, > > grp->count--; > > } > > } > > - if (err >= 0 && send_ack && client->type == USER_CLIENT) > > + if (err < 0) > > + return err; > > + > > + if (send_ack && client->type == USER_CLIENT) > > snd_seq_client_notify_subscription(port->addr.client, port->addr.port, > > info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); > > + else if (client->type == KERNEL_CLIENT) > > + get_device(&client->data.kernel.card->card_dev); > > > > - return err; > > + return 0; > > } > > > > static int unsubscribe_port(struct snd_seq_client *client, > > @@ -439,6 +444,8 @@ static int unsubscribe_port(struct snd_seq_client *client, > > snd_seq_client_notify_subscription(port->addr.client, port->addr.port, > > info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); > > module_put(port->owner); > > + if (client->type == KERNEL_CLIENT) > > + snd_card_unref(client->data.kernel.card); > > return err; > > } > > >