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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2F42C433B4 for ; Tue, 6 Apr 2021 14:06:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ABB89613C7 for ; Tue, 6 Apr 2021 14:06:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232983AbhDFOGJ (ORCPT ); Tue, 6 Apr 2021 10:06:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:38894 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233554AbhDFOGI (ORCPT ); Tue, 6 Apr 2021 10:06:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 746B06139C; Tue, 6 Apr 2021 14:05:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617717960; bh=qEX/6JcIH0tvPPBJ0/FFoBpj98iXPxdaCuo5ByOWTbU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gFYaUKc25ivU3lsCHPxdml6UB6rqURBPXl6bgdwev7/XrzmsZy8h9j5ShkstUVbJa D65PWwl9DRiPj+PKOuzh8MVRdM9tdp45wcd58nFmMM3N3rykhFqQH/SA5Q8Wwyyicp fF5PANEvHR7hLtadRncftDmPghckev8aXyFIDbjs= Date: Tue, 6 Apr 2021 16:05:57 +0200 From: Greg KH To: Anirudh Rayabharam Cc: "David S. Miller" , Jakub Kicinski , Oliver Neukum , Emil Renner Berthing , Geert Uytterhoeven , Zheng Yongjun , Rustam Kovhaev , syzbot+c49fe6089f295a05e6f8@syzkaller.appspotmail.com, linux-usb@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] net: hso: fix null-ptr-deref during tty device unregistration Message-ID: References: <20210406124402.20930-1-mail@anirudhrb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210406124402.20930-1-mail@anirudhrb.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org On Tue, Apr 06, 2021 at 06:13:59PM +0530, Anirudh Rayabharam wrote: > Multiple ttys try to claim the same the minor number causing a double > unregistration of the same device. The first unregistration succeeds > but the next one results in a null-ptr-deref. > > The get_free_serial_index() function returns an available minor number > but doesn't assign it immediately. The assignment is done by the caller > later. But before this assignment, calls to get_free_serial_index() > would return the same minor number. > > Fix this by modifying get_free_serial_index to assign the minor number > immediately after one is found to be and rename it to obtain_minor() > to better reflect what it does. Similary, rename set_serial_by_index() > to release_minor() and modify it to free up the minor number of the > given hso_serial. Every obtain_minor() should have corresponding > release_minor() call. > > Reported-by: syzbot+c49fe6089f295a05e6f8@syzkaller.appspotmail.com > Tested-by: syzbot+c49fe6089f295a05e6f8@syzkaller.appspotmail.com > > Signed-off-by: Anirudh Rayabharam > --- > drivers/net/usb/hso.c | 32 ++++++++++++-------------------- > 1 file changed, 12 insertions(+), 20 deletions(-) > > diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c > index 31d51346786a..295ca330e70c 100644 > --- a/drivers/net/usb/hso.c > +++ b/drivers/net/usb/hso.c > @@ -611,7 +611,7 @@ static struct hso_serial *get_serial_by_index(unsigned index) > return serial; > } > > -static int get_free_serial_index(void) > +static int obtain_minor(struct hso_serial *serial) > { > int index; > unsigned long flags; > @@ -619,8 +619,10 @@ static int get_free_serial_index(void) > spin_lock_irqsave(&serial_table_lock, flags); > for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) { > if (serial_table[index] == NULL) { > + serial_table[index] = serial->parent; > + serial->minor = index; > spin_unlock_irqrestore(&serial_table_lock, flags); > - return index; > + return 0; Minor note, you might want to convert this to use an idr structure in the future, this "loop and find a free minor" isn't really needed now that we have a data structure that does this all for us :) But that's not going to fix this issue, that's for future changes. thanks, greg k-h