Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp2716811rwb; Sun, 4 Sep 2022 23:33:01 -0700 (PDT) X-Google-Smtp-Source: AA6agR4omxi1n4sAEj1T6ajxZo1dx5rxR/bqlCgV00H56b145S4hvKCIBTXpV5lGN/KLNDYpHQ9v X-Received: by 2002:a05:6402:254a:b0:448:b73d:a96f with SMTP id l10-20020a056402254a00b00448b73da96fmr25210564edb.199.1662359581248; Sun, 04 Sep 2022 23:33:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662359581; cv=none; d=google.com; s=arc-20160816; b=TIDmWszHaczLF3jfkS+F1fJVD+FkmMW8ek9TQzYT2njAS1vgN+gqW6pnHi6QZvm1VX OUY6Z651KCYhbQpJGnxRsLhCE31bLNk9PjeMNRSuGoTgVZptm6UUKuHujRdD7Nhp+GQF DzfTyXe7CDyqEBKTaFynVKMoeEi7tnlGQDKxSvEfk7KvMefNQQUKK8/USe6DOv1fMMUJ 64swbvBbbInlYn/NqK3pkL22Vw19ZJtcOwnxwsuNDZ2ZT5x/BNzZ3o2JTUXTLi2FWS6K Q02Ym9zdI35xXRwQL+DPwltcAr8kgY69amuqxqennbkwQHKBpmUbnL7xER+HeGrYE1H2 i00A== 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=QKk8S8qzU6Fl+27XFb6gRsgKrKn8Tc1/vki/7ncZFyw=; b=MU52EgB7/zRtrkwMP3MI3ZSl+fbWgagr5C6vKTrLYJ0v1hi3UMojhTnCcrP9IOJDbM AZmCu2l1LqOFdFXCuz+NycO/vo9YeJJW+Ct7fYNkFYuWKQ/vAcTI6nRmpwv6dkrr87uf Vo9K869x8qliFTL2TLeXAkGc8Inm7YGuPrVumdaxBja399mv2dLXYMNtOhv66XeLFv4B eb/jKdSt9N4mQfkxKX4ySpDKK72WJaqVKjlKDNIMs/w0jyabW5SRePrLGtDg7jNdbx06 wwhqHJk35jqd5WyWZOeKIsr3rpvl+yHLQQvyI+uF1UrmTF1JDqOk7wOuW3ORtuEcPWzy ApPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ZpKRwJEF; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h4-20020a056402280400b00448b88378f8si8906290ede.359.2022.09.04.23.32.36; Sun, 04 Sep 2022 23:33:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ZpKRwJEF; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S236189AbiIEGGp (ORCPT + 99 others); Mon, 5 Sep 2022 02:06:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236160AbiIEGGn (ORCPT ); Mon, 5 Sep 2022 02:06:43 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83DDB2AE29 for ; Sun, 4 Sep 2022 23:06:42 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3882B38769; Mon, 5 Sep 2022 06:06:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1662358001; 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=QKk8S8qzU6Fl+27XFb6gRsgKrKn8Tc1/vki/7ncZFyw=; b=ZpKRwJEFT3NGCGnDwg8Tlr1zpbNWkVmJkYso6akLLRdM48nJx1PnsW92KT0gcsLxwrJX1v FqwjgCD/6gRgBFSIsTsozg7iSer8vFnMx6MgpQ5DtAUKOo4LQbSfXB2/ZgWT3Zdo4Cb/NF BxhW91TCfM7YV/FaxNMfTKMzShzz334= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1662358001; 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=QKk8S8qzU6Fl+27XFb6gRsgKrKn8Tc1/vki/7ncZFyw=; b=dYZACzk6L1wmlrWzTT2SrLCrQAOduUW9zu9wJnHMAbC5tVgkZk4OxJ0TIFkwXyCIgrCaOv BSd7e/vIQv5o9IBA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 155FB13A66; Mon, 5 Sep 2022 06:06:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 6Px0BPGRFWPgUAAAMHmgww (envelope-from ); Mon, 05 Sep 2022 06:06:41 +0000 Date: Mon, 05 Sep 2022 08:06:40 +0200 Message-ID: <87zgfejqxb.wl-tiwai@suse.de> From: Takashi Iwai To: butt3rflyh4ck Cc: cuibixuan@linux.alibaba.com, LKML , alsa-devel@alsa-project.org, tiwai@suse.com Subject: Re: A new null-ptr-deref Write bug in snd_pcm_format_set_silence In-Reply-To: References: <87edwrl9i8.wl-tiwai@suse.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 04 Sep 2022 19:06:05 +0200, butt3rflyh4ck wrote: > > I have tested this patch, it works well. Great, I'm going to submit a proper patch. thanks, Takashi > Regards, > butt3rflyh4ck. > > On Sun, Sep 4, 2022 at 6:27 PM Takashi Iwai wrote: > > > > On Sun, 04 Sep 2022 11:48:37 +0200, > > butt3rflyh4ck wrote: > > > > > > Hi, there is a new null-ptr-deref Write bug in > > > snd_pcm_format_set_slience in sound/core/pcm_misc.c in the latest > > > upstream kernel and can reproduce it. > > > We call SNDCTL_DSP_SYNC and SNDCTL_DSP_SPEED in multiple threads to > > > trigger the vulnerability. > > > > > > See the Call Trace: > > > ================================================================== > > > Call Trace: > > > > > > __dump_stack lib/dump_stack.c:88 [inline] > > > dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 > > > kasan_report+0xb1/0x1e0 mm/kasan/report.c:495 > > > check_region_inline mm/kasan/generic.c:183 [inline] > > > kasan_check_range+0x13d/0x180 mm/kasan/generic.c:189 > > > memset+0x20/0x40 mm/kasan/shadow.c:44 > > > snd_pcm_format_set_silence sound/core/pcm_misc.c:441 [inline] > > > snd_pcm_format_set_silence+0x215/0x350 sound/core/pcm_misc.c:424 > > > snd_pcm_oss_sync+0x60e/0x800 sound/core/oss/pcm_oss.c:1690 > > > snd_pcm_oss_ioctl+0x2087/0x3420 sound/core/oss/pcm_oss.c:2634 > > > vfs_ioctl fs/ioctl.c:51 [inline] > > > __do_sys_ioctl fs/ioctl.c:870 [inline] > > > __se_sys_ioctl fs/ioctl.c:856 [inline] > > > __x64_sys_ioctl+0x193/0x200 fs/ioctl.c:856 > > > do_syscall_x64 arch/x86/entry/common.c:50 [inline] > > > do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 > > > entry_SYSCALL_64_after_hwframe+0x63/0xcd > > > ================================================================== > > > We can see the function snd_pcm_format_set_silence code below: > > > ``` > > > int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, > > > unsigned int samples) > > > { > > > int width; > > > unsigned char *dst; > > > const unsigned char *pat; > > > > > > if (!valid_format(format)) > > > return -EINVAL; > > > if (samples == 0) > > > return 0; > > > width = pcm_formats[(INT)format].phys; /* physical width */ > > > pat = pcm_formats[(INT)format].silence; > > > if (!width || !pat) > > > return -EINVAL; > > > /* signed or 1 byte data */ > > > if (pcm_formats[(INT)format].signd == 1 || width <= 8) { > > > unsigned int bytes = samples * width / 8; > > > memset(data, *pat, bytes); ///// [1] ---------> data is NULL > > > return 0; > > > } > > > ...... > > > } > > > ``` > > > [1], the data pointer is NULL, we can know snd_pcm_format_set_silence > > > called in line 1690 in sound/core/oss/pcm_oss.c from call stack trace. > > > let we see code below: > > > ``` > > > static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file) > > > { > > > int err = 0; > > > unsigned int saved_f_flags; > > > struct snd_pcm_substream *substream; > > > struct snd_pcm_runtime *runtime; > > > snd_pcm_format_t format; > > > unsigned long width; > > > size_t size; > > > > > > substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; > > > if (substream != NULL) { > > > runtime = substream->runtime; > > > if (atomic_read(&substream->mmap_count)) > > > goto __direct; > > > err = snd_pcm_oss_make_ready(substream); > > > if (err < 0) > > > return err; > > > atomic_inc(&runtime->oss.rw_ref); > > > if (mutex_lock_interruptible(&runtime->oss.params_lock)) { > > > atomic_dec(&runtime->oss.rw_ref); > > > return -ERESTARTSYS; > > > } > > > format = snd_pcm_oss_format_from(runtime->oss.format); > > > width = snd_pcm_format_physical_width(format); > > > if (runtime->oss.buffer_used > 0) { > > > #ifdef OSS_DEBUG > > > pcm_dbg(substream->pcm, "sync: buffer_used\n"); > > > #endif > > > size = (8 * (runtime->oss.period_bytes - > > > runtime->oss.buffer_used) + 7) / width; > > > snd_pcm_format_set_silence(format, > > > runtime->oss.buffer > > > + runtime->oss.buffer_used, ///// [2] > > > size); > > > err = snd_pcm_oss_sync1(substream, > > > runtime->oss.period_bytes); > > > if (err < 0) > > > goto unlock; > > > } else if (runtime->oss.period_ptr > 0) { > > > > > > ``` > > > [2] runtime->oss.buffer + runtime->oss.buffer_used is the data > > > pointer, but runtime->oss.buffer is NULL here but it doesn't make > > > sense. > > > runtime->oss.buffter is allocated by kvzalloc, if runtime->oss_buffer > > > is NULL, it would return an ENOMEM error. > > > Maybe I think there is a race condition, the runtime->oss.buffer is > > > freed and set to NULL but we can use runtime->oss.buffter via ioctl. > > > > Yeah, likely it's in a small race window between two calls. > > > > Could you try the patch below? > > > > > > thanks, > > > > Takashi > > > > --- > > --- a/sound/core/oss/pcm_oss.c > > +++ b/sound/core/oss/pcm_oss.c > > @@ -1672,14 +1672,14 @@ static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file) > > runtime = substream->runtime; > > if (atomic_read(&substream->mmap_count)) > > goto __direct; > > - err = snd_pcm_oss_make_ready(substream); > > - if (err < 0) > > - return err; > > atomic_inc(&runtime->oss.rw_ref); > > if (mutex_lock_interruptible(&runtime->oss.params_lock)) { > > atomic_dec(&runtime->oss.rw_ref); > > return -ERESTARTSYS; > > } > > + err = snd_pcm_oss_make_ready_locked(substream); > > + if (err < 0) > > + goto unlock; > > format = snd_pcm_oss_format_from(runtime->oss.format); > > width = snd_pcm_format_physical_width(format); > > if (runtime->oss.buffer_used > 0) { > > > > -- > Active Defense Lab of Venustech >