Received: by 10.192.165.148 with SMTP id m20csp253914imm; Tue, 1 May 2018 22:48:13 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqg545MWs7xoRWrQ1LQwhldec0hPbOnWq8VQRW8T4Zlj1XrLz8pVAuxeVtyrTm2Cn9UF0Hf X-Received: by 2002:a17:902:4603:: with SMTP id o3-v6mr12302926pld.49.1525240093572; Tue, 01 May 2018 22:48:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525240093; cv=none; d=google.com; s=arc-20160816; b=xc+xizdilKe+GcsNe6dznGiTha6tVbqfYlSOv9BQ2dHsfVJV7rOL5h9CMJWLGLE0NY 01fVCU+c1D0f3hUz8iHxUo95DBU+sr9hmPVCiyA2lJElZ4Wci2s8fwPYg14u5CmSyK9b +/LYRQfXlWGV/NXrNpNh1X+banLB6SmU9wPGkk5YdT3x5fwshUG6uLWjA/D03cpMdBpv i8C1kNv4hDparNGd4W8/xx3TMwUgfrWf2XLoXtSEYLktZEVcG+Mu0RNAzc8FLZHkyABD GB0brE2jKDj+xtsGMjw8dOZmNoSQc3KnmMfuyRR+0t2saiIqfVLuQSKzMHOUcqtC9tQX vfiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:arc-authentication-results; bh=C3RJ9VePZQsWRmPL4t2YVXYP7gv7RTTaFBqcWkQqJX4=; b=v9kcAwgTPiCjkHK0ZVIntEHipjA5UuRNKLnZDiGgebUo3KWNVYuF3L4PgO3nZVNMy8 nwEnXYiKrb2PZ8iJeSzV/Y8NKYvwP3CWdJJZ8ljmNyDrN0ByAYbLPZ+CE/Wn5s88/eYy wco2lbC5xnEPgqmchLd/rpADx0qaE+mMWET00Oo0+qX3C8D0QQpXe75S6STc+YQh/1BJ JkQWn57Fo3FNEQVTyib3SPPrr20m/qfPmuxuMrtiuIerWVuttQkG07+grB01Q0f01jnX NLBVzYJ63z6kjyS2TVCW/U1IFRqs8Gz9rW5D1dokw9QiaGu7ZIyzhu4yzUME1l6YaMMi dupg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c23-v6si10998084pli.540.2018.05.01.22.47.59; Tue, 01 May 2018 22:48:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751074AbeEBFru (ORCPT + 99 others); Wed, 2 May 2018 01:47:50 -0400 Received: from esa5.microchip.iphmx.com ([216.71.150.166]:21887 "EHLO esa5.microchip.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750909AbeEBFrs (ORCPT ); Wed, 2 May 2018 01:47:48 -0400 X-IronPort-AV: E=Sophos;i="5.49,353,1520924400"; d="scan'208";a="11611640" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa5.microchip.iphmx.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 01 May 2018 22:47:48 -0700 Received: from ajaysk-VirtualBox (10.10.76.4) by chn-sv-exch05.mchp-main.com (10.10.76.106) with Microsoft SMTP Server id 14.3.352.0; Tue, 1 May 2018 22:47:46 -0700 Date: Wed, 2 May 2018 11:17:35 +0530 From: Ajay Singh To: Dan Carpenter CC: , "Gustavo A. R. Silva" , , , , "Ganesh Krishna" , Greg Kroah-Hartman Subject: Re: [PATCH] staging: wilc1000: fix infinite loop and out-of-bounds access Message-ID: <20180502111735.5a2c6faa@ajaysk-VirtualBox> In-Reply-To: <20180430152321.7pq4ol2ed7tzsrl4@mwanda> References: <20180430125040.GA19050@embeddedor.com> <20180430195916.596a93eb@ajaysk-VirtualBox> <20180430152321.7pq4ol2ed7tzsrl4@mwanda> Organization: Microchip Technology MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 30 Apr 2018 18:23:21 +0300 Dan Carpenter wrote: > On Mon, Apr 30, 2018 at 07:59:16PM +0530, Ajay Singh wrote: > > Reviewed-by: Ajay Singh > > > > On Mon, 30 Apr 2018 07:50:40 -0500 > > "Gustavo A. R. Silva" wrote: > > > > > If i < slot_id is initially true then it will remain true. Also, > > > as i is being decremented it will end up accessing memory out of > > > bounds. > > > > > > Fix this by incrementing *i* instead of decrementing it. > > > > Nice catch! > > Thanks for submitting the changes. > > > > > > > > Addresses-Coverity-ID: 1468454 ("Infinite loop") > > > Fixes: faa657641081 ("staging: wilc1000: refactor scan() to free > > > kmalloc memory on failure cases") > > > Signed-off-by: Gustavo A. R. Silva > > > --- > > > > > > BTW... at first sight it seems to me that variables slot_id > > > and i should be of type unsigned instead of signed. > > > > Yes, 'slot_id' & 'i' can be changed to unsigned int. > > > > A lot of people think making things unsigned improves the code but I > hate "unsigned int i"... I understand that in certain cases we do > loop more than INT_MAX but those are a tiny tiny minority of loops. > > Simple types like "int" are more readable than complicated types > like "unsigned int". Unsigned int just draws attention to itself > and distracts people from things that might potentially matter. We > have real life subtle loops like in xtea_encrypt() where we need to > use unsigned types. > > And look at the function we're talking about here: > > drivers/staging/wilc1000/wilc_wfi_cfgoperations.c > 577 static inline int > 578 wilc_wfi_cfg_alloc_fill_ssid(struct cfg80211_scan_request > *request, 579 struct hidden_network > *ntwk) 580 { > 581 int i; > 582 int slot_id = 0; > 583 > 584 ntwk->net_info = kcalloc(request->n_ssids, > 585 sizeof(struct > hidden_network), GFP_KERNEL); 586 if (!ntwk->net_info) > 587 goto out; > 588 > 589 ntwk->n_ssids = request->n_ssids; > 590 > 591 for (i = 0; i < request->n_ssids; i++) { > > request->n_ssids is an int. It can't possibly be INT_MAX because > the kcalloc() will fail. Ideally the static analysis tool should > be able to tell you that if you seed it with the knowledge of how > much memory it's possible to kmalloc(). So it's just laziness here > is why the static checker complains, it should see there is no > issue. Smatch fails here as well but I'll see if I can fix it. > > Anyway let's say it could be negative then 0 is greater than > negative values so the loop would be a no-op. I've seen code where > the user could set the loop bounds to s32min-4 but because it was > "int i" instead of "unsigned int i" then it meant the loop was a > no-op instead of being a security problem. In other words, > unsigned can be less secure. > > I honestly have never seen a bug in the kernel where we intended to > loop more than INT_MAX times, but there was a signedness bug > preventing it. That's the only reason I can see to change the > signedness. Am I missing something? > Hi Dan, Thanks for providing the detailed explanation. I understand your point, but what's your opinion about variable 'slot_id', as this variable is added to take only the positive value(i.e from 0 to maximum number of filled elements), so for more readability should we keep it same. BTW in my opinion 'request->n_ssids' should have been unsigned because it is suppose to hold only positive values along with smaller data type(may be u8 or u16, as the range might be enough to hold the value of n_ssids). So we have advantage of size reduction not just the increase in value range. Suppose we get negative value for "request->n_ssids" & kmalloc is pass then we have to add some more extra code to free data and return error code in wilc_wfi_cfg_alloc_fill_ssid(). In your opinion should this condition be handled as the received 'request->n_ssids' is of 'int' type. Regards, Ajay