Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp493448pxa; Thu, 27 Aug 2020 07:53:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxiYy2wEClrh2ibtQq2lpfHSVD/kGD4XWk8/7oxTLobu67BvbQuA2jqF+2l2YFeDZeNgd24 X-Received: by 2002:a50:de04:: with SMTP id z4mr20597936edk.10.1598539981054; Thu, 27 Aug 2020 07:53:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598539981; cv=none; d=google.com; s=arc-20160816; b=WsDAMDIQa/U+9Lg4UP/uV6dQQALVsvYWpkQjrYiCIGkN9h55t9h90RA1HeGgDend7L jwFRBM3/IorBPKmaha9YZvtvfUjUR9Lh7o5FoyQ14KtyMQCJ2zLU3r5wgrxmTGa9FeSe IIRwr3gYHyd1SKmUYXmJHTMjR/MHUrMxCacTJRV4Xd8zZiV80m+Ghe4yNGjsD3JYYyhp aEPW7NeKLc6BZjY9MnaMME7q8TGugi6EvGc7q1QXwMvlwut8gak8xIHoyKYcFLnXouDB GwEVslK8KiVo3sl/87tzu79o6eB2JcZSzqOU/HN3AaHwSVtPp9N9kb7UzwApeMHIsczB AxIQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=SQRD+t3B3BhDgCZyy/X8yI1I6ZdI7vaahMfVCu8hVtc=; b=MukCWrrQRaAiyiMAF6KMX4KD9E1m6tlGc6iD6rrfSfDwdv6AIQBY2lylyl61+uzvAO PvZdIipYcUrXPHYXtbzA0PHGK3Q+URWNe2M3L7o3SXcOoBntYiD2yrJ6T/hdWa+pBTfx w4JieS6/BEMyFuT2N2rFJqLwWM3TISQ+EMx3Ri6qWd9MHSz+6gXtr0PhMdKbMM98s24v nygWv+8rZMtsTY3nXAme+MhHz5HSp9kguIGP7WBRpBqaVAr+9OLitY85v5rpigbkh5EB 39GQ02NVUWi1BQcm2OfOfl/VjfCjguFJBTxN5waGhk1ytpduew3vv73bHJuuICgpeS9M SHOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=CpXkU+hX; 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 j7si1580470ejm.538.2020.08.27.07.52.38; Thu, 27 Aug 2020 07:53:01 -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=@rasmusvillemoes.dk header.s=google header.b=CpXkU+hX; 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 S1728246AbgH0Oue (ORCPT + 99 others); Thu, 27 Aug 2020 10:50:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728138AbgH0NlT (ORCPT ); Thu, 27 Aug 2020 09:41:19 -0400 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DDBDC06121B for ; Thu, 27 Aug 2020 06:41:10 -0700 (PDT) Received: by mail-lj1-x243.google.com with SMTP id i10so6499285ljn.2 for ; Thu, 27 Aug 2020 06:41:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=SQRD+t3B3BhDgCZyy/X8yI1I6ZdI7vaahMfVCu8hVtc=; b=CpXkU+hXp1QnM7ojLivh4d5Bmqi4h8btqinup18fFNC7GXCdtN0smcpO17RkifyAIx Y0amYg/fPzSsfG8gt9M90i3OSb/BtPcX2JDgFtmSAXjkVNEkrvyylvDZYP76e+5BDB8U lNx/uq7bb+38w5YPBEm02y20Fnv2ie3aoJoxs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=SQRD+t3B3BhDgCZyy/X8yI1I6ZdI7vaahMfVCu8hVtc=; b=N2h2Fsc1yTdsRGm8ZEzmCpuhgWtCeOeNRgdhJ8UXnbXM+2/h1ZBvuaJTSYN1FjTDIQ TEdMcbH/tGRpdV3WWuhDUry5FClpPKbfidKIl+SYS6yZSLYpLPHeJuFFF8KppJwPOKs+ xeKI/hOb73NF2k9hVK7cz1MbPOy9r+a4FjcXRkNqUFadEKsi5rSeH8raoctDH8LlxTuU i1XAX3qQ3vevtDP/zI7EJ4Y9j+xihx8PHbgVRQVntXDUnphYC8HmAfkHHRQHnGLio2ep wkTAQsTn8siF9vfRPpOyyo2mfkUtEG+A/PsrOlA/T9Wd28fAl02X/fVLzahBXViVxr03 1xbw== X-Gm-Message-State: AOAM533f+g/O1tnvfdNhveykTtcD2sswBBVVbdj07SQnJzA0QlkUdpxL 2CCfTOzd+pauxR4XKVYyUIthmBpfg1E0+mMkdoU= X-Received: by 2002:a2e:a314:: with SMTP id l20mr10255281lje.213.1598535668264; Thu, 27 Aug 2020 06:41:08 -0700 (PDT) Received: from [172.16.11.132] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id n24sm483425lji.83.2020.08.27.06.41.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Aug 2020 06:41:07 -0700 (PDT) Subject: Re: [PATCH] usb: atm: don't use snprintf() for sysfs attrs To: Alex Dewar , Greg Kroah-Hartman Cc: Kees Cook , "Gustavo A. R. Silva" , accessrunner-general@lists.sourceforge.net, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org References: <20200824222322.22962-1-alex.dewar90@gmail.com> <48f2dc90-7852-eaf1-55d7-2c85cf954688@rasmusvillemoes.dk> <20200827071537.GA168593@kroah.com> <20200827131819.7rcl2f5js3hkoqj2@lenovo-laptop> From: Rasmus Villemoes Message-ID: Date: Thu, 27 Aug 2020 15:41:06 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200827131819.7rcl2f5js3hkoqj2@lenovo-laptop> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27/08/2020 15.18, Alex Dewar wrote: > On Thu, Aug 27, 2020 at 09:15:37AM +0200, Greg Kroah-Hartman wrote: >> On Thu, Aug 27, 2020 at 08:42:06AM +0200, Rasmus Villemoes wrote: >>> On 25/08/2020 00.23, Alex Dewar wrote: >>>> kernel/cpu.c: don't use snprintf() for sysfs attrs >>>> >>>> As per the documentation (Documentation/filesystems/sysfs.rst), >>>> snprintf() should not be used for formatting values returned by sysfs. >>>> >>> >>> Can we have a sysfs_sprintf() (could just be a macro that does sprintf) >>> to make it clear to the next reader that we know we're in a sysfs show >>> method? It would make auditing uses of sprintf() much easier. >> >> Code churn to keep code checkers quiet for pointless reasons? What >> could go wrong with that... I did not (mean to) suggest replacing existing sprintf() calls in sysfs show methods. But when changes _are_ being made, such as when replacing snprintf() calls for whatever reasons, can we please not make it harder for people doing manual audits (those are "code checkers" as well, I suppose, but they do tend to only make noise when finding something). >> It should be pretty obvious to any reader that you are in a sysfs show >> method, as almost all of them are trivially tiny and obvious. git grep doesn't immediately show that, not even with a suitable -C argument, as you can't really know the potential callers unless you open the file and see that the function is only assigned as a .show method. And even that can be a pain because it's all hidden behind five levels of magic macros that build identifiers with ##. > Perhaps I should have mentioned this in the commit message, but the problem > is that snprintf() doesn't return the number of bytes written to the > destination buffer, I'm perfectly well aware of that, TYVM (you may want to 'git log --author Villemoes lib/vsprintf.c'). but the number of bytes that *would have been written if > they fitted*, which may be more than the bounds specified [1]. So "return > snprintf(...)" for sysfs attributes is an antipattern. If you need bounded > string ops, scnprintf() is the way to go. Using snprintf() can give a > false sense of security, because it isn't necessarily safe. Huh? This all seems utterly irrelevant WRT a change that replaces PAGE_SIZE by INT_MAX (because that's what sprintf() is going to pretend you passed). You get the same return value. But I'm not at all concerned about whether one passes the proper buffer size or not in sysfs show methods; with my embedded hat on, I'm all for saving a few bytes of .text here and there. The problem, as far as I'm concerned, is merely that adding sprintf() callers makes it harder to find the problematic sprintf() instances. >> Anyway, we've had this for 20 years, My bad, for a moment I forgot that code and patterns of that vintage cannot have bugs. Rasmus