Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp597749ybz; Fri, 17 Apr 2020 06:49:49 -0700 (PDT) X-Google-Smtp-Source: APiQypJKMebuVmFti1ey4LsjKGTYKa7+vWHFOXpFXgtVk0KWwyfw0+T+tiEHbqnXWJuEoxpZd5xM X-Received: by 2002:a17:906:8549:: with SMTP id h9mr2983252ejy.145.1587131389761; Fri, 17 Apr 2020 06:49:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587131389; cv=none; d=google.com; s=arc-20160816; b=tSlzN6CsHPh+hlEfzZgDnlOPnaXalendRqDWAKvrFFV44gG06rkaJM94GTK1shn+rC cZPEfhP7eUnBsNfDm7CEZSjLHBJbYGSjXz6g0xRkwjnoiwYBt6XN55jIGeAe7X12LMLt bBzUXaLbY2/HkBeiSro+Y3Hi8BIrzE4zg/kCxM618ucGbAm9KvjJrsfwnV33b0SoG/pj dhvpa4udXzU0RcDH63KJxiG4nkTZcPhYZoegz1wWUhu6TuUIo6u6C5upnsBEpSMCmX7B Vqjv8qJULM/z71URr31zTMWSvCyTFJXLZkcZA9SoaxmxBX5+5X71CP494vQl7FNKUBGv DwAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=op4dPjwrD9QDYZwBa3JIN9ahH57aXRQ+t3CflVy3cr8=; b=v/mJglFqwAyjnxb4fp+if0SWEBeIu21y/7ukSIL1ltrOsJ1B45/n8j/HCoM12b+4Jn LgIC5cduLBhj2dZJERcRPT68CPSOWbqJt+0FK1ini6/4czE4JoU7mE0mspcZpRqZNuBk l14joHq72yNTSZ2dwwf8incSAEpvrhSem6bsB13a30iHFupod12hcvc0wEIvbF+AT0If K4PbzYSzfKerCjXqJOn6+DHu2SMq5SvSI0RCLx299TIY8Jy/k39U4AzA2m2WU5Cn/N5S Zp+EPKhXe55dkKvyQjvxAla1eLpPCfe8LH6+uoqYteo7G/cl3I5GW92D+qeU5PPsIx0w Xfyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=kyP8zVi6; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p18si14505816ejl.98.2020.04.17.06.49.25; Fri, 17 Apr 2020 06:49:49 -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=@ziepe.ca header.s=google header.b=kyP8zVi6; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730699AbgDQNsV (ORCPT + 99 others); Fri, 17 Apr 2020 09:48:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1730625AbgDQNsU (ORCPT ); Fri, 17 Apr 2020 09:48:20 -0400 Received: from mail-qv1-xf42.google.com (mail-qv1-xf42.google.com [IPv6:2607:f8b0:4864:20::f42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52409C061A0C for ; Fri, 17 Apr 2020 06:48:18 -0700 (PDT) Received: by mail-qv1-xf42.google.com with SMTP id p13so863707qvt.12 for ; Fri, 17 Apr 2020 06:48:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=op4dPjwrD9QDYZwBa3JIN9ahH57aXRQ+t3CflVy3cr8=; b=kyP8zVi6dK+UN06S/0vCyZbk6Wtw/t/ZTqGvDTUz3If10pQdSdOZd9AowuRmbjJknz I4JKNNtQkvmO9a/mTLcoipilUCUNDBOz3EFrwUh8mcd7d9nuXcWNWHjFCN/7mEQ0cIt5 pdOxvmMJdeyBjbMlKDS8iK2PYVhBFFW7+DbhLghYbb49krZFE3TyZzvh/rDDSC5PofxS j6CQR1ePXr8j1TJ6skaDb117FUQWVlIwJrCj38t/j2V+1P75J5VnmJEx046X8rky2hk2 jzCRSd+EQ0uOFgVWxqSiky3Gxwkdtk9AhjQPsHfWnbIDhFeAF21kWGstZDUTwlqAZL9g eAQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=op4dPjwrD9QDYZwBa3JIN9ahH57aXRQ+t3CflVy3cr8=; b=qyBnwhc2d0F7v2VkCK+8eBrhWUGtRs7ASO2kz45IfcO/nDdomyPM0NzY3KpdMdh9iV /z4vdtkrq6yuJAnoPvbdUkiayliz/ppplOkbtJWGRyIjsCPvJT+Kzk53lGtJ+vnZeDaH 2RjtZlAWIzF8Rgm3QzQfOU2ivPu4mfv+YOuNv3xbxTAsu+zAosfSL7jY32ZyP95TBPJ+ XXvWrhm8euI8t5Vfd+RBJ/IqG+9BnrIxoidTfm11et0rekc2lyjpPvZoEiMYoPb7dtY+ zzn3Gah8OxqkQ8OYD/mHRkLns5a8+pUx0MQYOSbIQXvwndEunq/6zV3Ds+uB6eXE2uvP e0qg== X-Gm-Message-State: AGi0Pua4F0qGslYaXl6BJKQ6aFR6Zb0sf8Xs+oomdPOAAoWgS4jvauyE 9ZiGY4hrhJdLadsGkOfkvMXB3A== X-Received: by 2002:ad4:4f01:: with SMTP id fb1mr2915039qvb.162.1587131297274; Fri, 17 Apr 2020 06:48:17 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-68-57-212.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.57.212]) by smtp.gmail.com with ESMTPSA id f68sm16922485qtb.19.2020.04.17.06.48.16 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 17 Apr 2020 06:48:16 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1jPRLs-0008VR-5o; Fri, 17 Apr 2020 10:48:16 -0300 Date: Fri, 17 Apr 2020 10:48:16 -0300 From: Jason Gunthorpe To: Dan Carpenter Cc: g@ziepe.ca, Christophe JAILLET , selvin.xavier@broadcom.com, devesh.sharma@broadcom.com, dledford@redhat.com, leon@kernel.org, colin.king@canonical.com, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [PATCH] RDMA/ocrdma: Fix an off-by-one issue in 'ocrdma_add_stat' Message-ID: <20200417134816.GD26002@ziepe.ca> References: <20200328073040.24429-1-christophe.jaillet@wanadoo.fr> <20200414183441.GA28870@ziepe.ca> <20200416130847.GP1163@kadam> <20200416184754.GZ5100@ziepe.ca> <20200417112624.GS1163@kadam> <20200417122542.GC5100@ziepe.ca> <20200417130955.GU1163@kadam> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200417130955.GU1163@kadam> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 17, 2020 at 04:09:55PM +0300, Dan Carpenter wrote: > On Fri, Apr 17, 2020 at 09:25:42AM -0300, Jason Gunthorpe wrote: > > On Fri, Apr 17, 2020 at 02:26:24PM +0300, Dan Carpenter wrote: > > > On Thu, Apr 16, 2020 at 03:47:54PM -0300, Jason Gunthorpe wrote: > > > > On Thu, Apr 16, 2020 at 04:08:47PM +0300, Dan Carpenter wrote: > > > > > On Tue, Apr 14, 2020 at 03:34:41PM -0300, Jason Gunthorpe wrote: > > > > > > The memcpy is still kind of silly right? What about this: > > > > > > > > > > > > static int ocrdma_add_stat(char *start, char *pcur, char *name, u64 count) > > > > > > { > > > > > > size_t len = (start + OCRDMA_MAX_DBGFS_MEM) - pcur; > > > > > > int cpy_len; > > > > > > > > > > > > cpy_len = snprintf(pcur, len, "%s: %llu\n", name, count); > > > > > > if (cpy_len >= len || cpy_len < 0) { > > > > > > > > > > The kernel version of snprintf() doesn't and will never return > > > > > negatives. It would cause a huge security headache if it started > > > > > returning negatives. > > > > > > > > Begs the question why it returns an int then :) > > > > > > People should use "int" as their default type. "int i;". It means > > > "This is a normal number. Nothing special about it. It's not too high. > > > It's not defined by hardware requirements." Other types call attention > > > to themselves, but int is the humble datatype. > > > > No, I strongly disagree with this, it is one of my pet peeves to see > > 'int' being used for data which is known to be only ever be positive > > just to save typing 'unsigned'. > > > > Not only is it confusing, but allowing signed values has caused tricky > > security bugs, unfortuntely. > > I have the opposite pet peeve. > > I complain about it a lot. It pains me every time I see a "u32 i;". I > think there is a static analysis warning for using signed which > encourages people to write code like that. That warning really upsets > me for two reasons 1) The static checker should know the range of values > but it doesn't so it makes me sad to see inferior technology being used > when it should deleted instead. 2) I have never seen this warning > prevent a real life bug. I have.. But I'm having trouble finding it in the git torrent.. Maybe this one: commit c2b37f76485f073f020e60b5954b6dc4e55f693c Author: Boris Pismenny Date: Thu Mar 8 15:51:41 2018 +0200 IB/mlx5: Fix integer overflows in mlx5_ib_create_srq > You would need to hit a series of fairly rare events for this > warning to be useful and I have never seen that happen yet. IIRC the case was the uapi rightly used u32, which was then wrongly implicitly cast to some internal function, accepting int, which then did something sort of like int len if (len >= sizeof(a)) return -EINVAL copy_from_user(a, b, len) Which explodes when a negative len is implicitly cast to unsigned long to call copy_from_user. > The most common bug caused by unsigned variables is that it breaks the > kernel error handling You mean returning -ERRNO? Sure, those should be int, but that is a case where a value actually can take on -ve numbers, so it really should be signed. > but there are other problems as well. There was an example a little > while back where someone "fixed" a security problem by making things > unsigned. > > for (i = 0; i < user_value; i++) { This is clearly missing input validation on user_value, the only reason int helps at all here is pure dumb luck for this one case. If it had used something like copy_to_user it would be broken. > Originally if user_value was an int then the loop would have been a > harmless no-op but now it was a large positive value so it lead to > memory corruption. Another example is: > > for (i = 0; i < user_value - 1; i++) { Again, code like this is simply missing required input validation. The for loop works with int by dumb luck, and this would be broken if it called copy_from_user. > From my experience with static analysis and security audits, making > things unsigned en mass causes more security bugs. There are definitely > times where making variables unsigned is correct for security reasons > like when you are taking a size from userspace. Any code that casts a unsigned value from userspace to a signed value in the kernel is deeply suspect, IMHO. If you get the in habit of using types properly then it is less likely this bug-class will happen. If your habit is to just always use 'int' for everything then you *will* accidently cause a user value to be implicitly casted. > Complicated types call attention to themselves and they hurt > readability. You sometimes *need* other datatypes and you want those to > stand out but if everything is special then nothing is special. If the programmer knows the value is never negative it should be recorded in the code, otherwise it is hard to tell if there are problems or not. Is this code wrong? int array_idx; ... if (array_idx < ARRAY_SIZE(foo)) return foo[array_idx]; Since 'int' was used the entire code flow has to be studied to determine if 'array_idx' is ever accidently set to negative. If it is unsigned I can tell you there is no problem right away. I do agree with you that people blindly changing things due to security scanners is not good.. Jason