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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D11AEC43334 for ; Fri, 10 Jun 2022 14:30:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237912AbiFJOaZ (ORCPT ); Fri, 10 Jun 2022 10:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238667AbiFJOaV (ORCPT ); Fri, 10 Jun 2022 10:30:21 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E789B5EBCD for ; Fri, 10 Jun 2022 07:30:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B4A78B835E2 for ; Fri, 10 Jun 2022 14:30:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C4AAC34114; Fri, 10 Jun 2022 14:30:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654871413; bh=4R7G5Bva8GD1Rzpj+x9E8Jwv4C/y2r3xH49G6sWMIsg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Clqtt5HQ5vkazJfFx9KHW5lsJMbawWQEsUBtVBWgSOga3lXGaNNVF6Rvjjb0JNKMx Fj3i3ecQBVw58T4n7dAUmoR0LXm3iJptYouf7arq/dI1yIntxQxLV1i7Y0M5JK/ULE YcF/PyPovfIap8aRz4/mz1qwgOOzYVHzAE93dZ2Q= Date: Fri, 10 Jun 2022 16:30:10 +0200 From: Greg KH To: Shreenidhi Shedi Cc: arnd@arndb.de, linux-kernel@vger.kernel.org, Shreenidhi Shedi Subject: Re: [PATCH 1/2] char: lp: ensure that index has not exceeded LP_NO Message-ID: References: <20220603130040.601673-1-sshedi@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 10, 2022 at 07:12:02PM +0530, Shreenidhi Shedi wrote: > On 10/06/22 6:58 pm, Greg KH wrote: > > On Fri, Jun 03, 2022 at 06:30:39PM +0530, Shreenidhi Shedi wrote: > >> From: Shreenidhi Shedi > >> > >> After finishing the loop, index value can be equal to LP_NO and lp_table > >> array is of size LP_NO, so this can end up in accessing an out of bound > >> address in lp_register function. > >> > >> Signed-off-by: Shreenidhi Shedi > >> --- > >> drivers/char/lp.c | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/drivers/char/lp.c b/drivers/char/lp.c > >> index 0e22e3b0a..d474d02b6 100644 > >> --- a/drivers/char/lp.c > >> +++ b/drivers/char/lp.c > >> @@ -972,7 +972,7 @@ static void lp_attach(struct parport *port) > >> if (port_num[i] == -1) > >> break; > >> > >> - if (!lp_register(i, port)) > >> + if (i < LP_NO && !lp_register(i, port)) > >> lp_count++; > > > > How can this ever be needed? Look at the check further up for the check > > of lp_count which prevents this from every going too large. > > > > So how can an address be accessed out of bound here? > > > > thanks, > > > > greg k-h > > Thanks for the review. Assume lp_count is less than LP_NO now and we enter the for loop > and for some reason for loop exits after i reaching the value LP_NO Wait, how can that happen? That's what I am saying, the loop will never reach that value from what I can tell. Yes, this whole thing should be moved to something more sane like an idr structure, but as-is, it seems correct to me. Have you tested the code with that many devices to see if it really can overflow? thanks, greg k-h