Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2848808pxa; Tue, 25 Aug 2020 05:12:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGpza3vVZMRj88G3SbLa/W3uE5h2tQWK7vv95p7kztuTzZSavKOd6zF2yExR3kOezfvq9m X-Received: by 2002:a50:8f44:: with SMTP id 62mr9885856edy.351.1598357559045; Tue, 25 Aug 2020 05:12:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598357559; cv=none; d=google.com; s=arc-20160816; b=ILZ20oivPQ1dt1qp5BmWGxLslvQ2Luf/eAR2RVGUenTC0mWwQEG87TCWEBJtyWrc0R lc20C7P0w1pPL1QzCwPRFtmwYq+1KoP/Z+jPoFG4F6hL7roUow9yo9BFrmuZrAInYQKY hd0aoOpjGXVF4jHpzdOj3a4tjhXjMBq81YL2NqpQs0Aagg+rD21TksgCOaz820RQR+5I wJ5089EpeKgI0D4/RQg8wzWQa+R2Wupl5OUsWRd8TQS9JX33SGgr7wYr3L1kD+x7wYjI GQJglRzbII/ON/WmNvSHTxppP7MTP05M8U0E8Yc8NpsBBz2Jy0hkEXH4C4m7SttfvwpQ F4nw== 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 :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=zY3MtqAAYKwWtAo5WxjZfID16LDI8rpksHmaNhm0t2A=; b=k97H15cdPoK6JKrqctLbf3Ri5kxylTZ5Wg05kEZyAZmH+DOHBnej7nzj++ezmvcFbA wDpoF9kdAktBpnAQvAoOjPGjWwmFUCPtOjTdDxlfrFjicNZQUIO8qyuKbktHVuccszyf YHz0lywpo6kAfkRBQJ/SmNkHatef8tpaCy0y5N4EkwG5dZPMOhFZm925tm8RDspxZ4sv EBuMxkl7sfGITpUkPBSUWaHEJwgZtnWfZLDTS2tQ7o/3vdb+BWRUDby5nl1Ljdnjsa4O aksgO2oPaz1hEa/jAT+uU8AcuOZgK0jUBq9OHVD+7gIQZCMyLLlu7M5++yS0TLHe0aHy gspw== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gs14si8699068ejb.615.2020.08.25.05.12.15; Tue, 25 Aug 2020 05:12:39 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729256AbgHYJH5 convert rfc822-to-8bit (ORCPT + 99 others); Tue, 25 Aug 2020 05:07:57 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([207.82.80.151]:41047 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729192AbgHYJHw (ORCPT ); Tue, 25 Aug 2020 05:07:52 -0400 Received: from AcuMS.aculab.com (156.67.243.126 [156.67.243.126]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-72-wbod_bd6NRy5MRd_jCobBg-1; Tue, 25 Aug 2020 10:07:48 +0100 X-MC-Unique: wbod_bd6NRy5MRd_jCobBg-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) by AcuMS.aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Tue, 25 Aug 2020 10:07:47 +0100 Received: from AcuMS.Aculab.com ([fe80::43c:695e:880f:8750]) by AcuMS.aculab.com ([fe80::43c:695e:880f:8750%12]) with mapi id 15.00.1347.000; Tue, 25 Aug 2020 10:07:47 +0100 From: David Laight To: 'Alex Dewar' CC: Greg Kroah-Hartman , "Gustavo A. R. Silva" , "accessrunner-general@lists.sourceforge.net" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: RE: [PATCH] usb: atm: don't use snprintf() for sysfs attrs Thread-Topic: [PATCH] usb: atm: don't use snprintf() for sysfs attrs Thread-Index: AQHWemUzoPleWxCDKky1KLXQzyLa5alIeCYw///ywACAAB0QkA== Date: Tue, 25 Aug 2020 09:07:47 +0000 Message-ID: <0d32d8092289498a9702b76d44074017@AcuMS.aculab.com> References: <20200824222322.22962-1-alex.dewar90@gmail.com> <3e882693bb344424af37d4d35f3db605@AcuMS.aculab.com> <20200825081752.hj3zyhg2qumzd64n@lenovo-laptop> In-Reply-To: <20200825081752.hj3zyhg2qumzd64n@lenovo-laptop> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: aculab.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alex Dewar > Sent: 25 August 2020 09:18 > On Tue, Aug 25, 2020 at 08:12:05AM +0000, David Laight wrote: > > From: Alex Dewar > > > Sent: 24 August 2020 23:23 > > > 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. > > > > > > In all of these cases, sprintf() suffices as we know that the formatted > > > strings will be less than PAGE_SIZE in length. > > > > Hmmmm.... > > I much prefer to see bounded string ops. > > sysfs really ought to be passing through the buffer length. > > The buffer size should probably be SYSFS_BUF_LEN not PAGE_SIZE > > (even it happens to typically be the same). > > If PAGE_SIZE is big (or small) passing a 4k buffer may be > > more appropriate than a PAGE_SIZE one. > > > > David > > We could use scnprintf() instead I guess. But an expression like: > return sprintf(buf, "%d\n", value); > will never overflow if buf is PAGE_SIZE, right...? Certainly the return value from snprintf() isn't what you want here (it almost never is) - so scnprintf() is much better. A simple "%d" or "%u" wont overflow, but a "%s" might (even if it is really expected that it shouldn't). Even a "%*d" can go horribly wrong. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)