Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2711430pxk; Sun, 6 Sep 2020 10:28:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysC7jdeXD5EyqW1Q9dYCdrniPqReIvzzhwwjkUwkLCX0vggQceDePlSktFjXXZhILFmNB/ X-Received: by 2002:a17:906:d97b:: with SMTP id rp27mr18131207ejb.18.1599413286302; Sun, 06 Sep 2020 10:28:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599413286; cv=none; d=google.com; s=arc-20160816; b=Fxiqz9evl/sO2PdWuq1yGqMMOv9XmkRyeVSf1M4mglPTszeqcTmubDgtCH175OdZ29 Nt+f8GS1FGuPLnfdsdoXiqWXHicBZVNGV8/t0yLxlZac0Bf80rKBcDVfrKc70jwhZO60 FdInBFFTQbkhTrE/MpL8OK6EMamPO0s/U/zCUixjUetf6/FoQio+KXop2fjr/hVq1VcU TA0/DYukuWDSvoY5s3K1RR7rXKSDyUYPYvd/aoA6dNZ52bFeuDZrC7sxNHc3snRYfiul IDNbcaYNl/gERe3CWch3jfykOCukaqDl/8Io0sN92qtPy0mLDgJ6H+anNOGoqN5eu2kv ruSg== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=vjFx76A7y9ALSXa7SjZgSs9426I4WBWBmYOyhkXjks0=; b=AE338eYDM+p+idiaVaIvFEuZc2AfoTd3/9KQ3+SD6gjFvusinKqMwiPE07DDrAG0uZ RDaBh+PWuiunuAjNNVAT0QDfnuDeB5tLMm22HAzVNsDkAc7hnFJWT/QXqj0+DT1sAgji D7EosQvU4zoC4HCO2NuR49926Ng3qIKCKD6p494D++rpcRBZ/69/l3+4eVIsbDw5oyvL HLyR3+GZrwOXt2DoWe/AFVQEUSaMsAI0En6tkAB+7q2FZ4YDAI/5QkEZgFrgijAywxFD zdk02QGnsjWOQkrEA4MqEZUj0rOORVJPLXAqXariZ+QlRcH+fN0E+b4NQd8mBOb+JRC+ eVbA== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id kt7si6580235ejb.282.2020.09.06.10.27.43; Sun, 06 Sep 2020 10:28:06 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729149AbgIFRY3 (ORCPT + 99 others); Sun, 6 Sep 2020 13:24:29 -0400 Received: from smtprelay0084.hostedemail.com ([216.40.44.84]:46130 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725816AbgIFRY1 (ORCPT ); Sun, 6 Sep 2020 13:24:27 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay06.hostedemail.com (Postfix) with ESMTP id 5816F18224D93; Sun, 6 Sep 2020 17:24:25 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,,RULES_HIT:41:355:379:599:800:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1543:1593:1594:1711:1730:1747:1777:1792:1981:2194:2199:2393:2559:2562:2828:2914:3138:3139:3140:3141:3142:3355:3622:3865:3866:3867:3868:3870:3871:3872:3874:4321:5007:6119:7875:7903:8603:8660:9010:10004:10400:10848:11026:11232:11473:11658:11914:12043:12291:12296:12297:12555:12683:12740:12760:12895:12986:13148:13230:13439:14181:14659:14721:21080:21451:21627:21939:21990:30003:30034:30054:30069:30070:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: wall63_3303465270c5 X-Filterd-Recvd-Size: 4608 Received: from XPS-9350.home (unknown [47.151.133.149]) (Authenticated sender: joe@perches.com) by omf06.hostedemail.com (Postfix) with ESMTPA; Sun, 6 Sep 2020 17:24:23 +0000 (UTC) Message-ID: <743a648dc817cddd2e7046283c868f1c08742f29.camel@perches.com> Subject: Re: [PATCH V2] sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs output From: Joe Perches To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Kees Cook , "Gustavo A . R . Silva" , Denis Efremov , Julia Lawall , Alex Dewar , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Date: Sun, 06 Sep 2020 10:24:20 -0700 In-Reply-To: References: Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.36.4-0ubuntu1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 2020-08-29 at 16:48 -0700, Joe Perches wrote: > Output defects can exist in sysfs content using sprintf and snprintf. > > sprintf does not know the PAGE_SIZE maximum of the temporary buffer > used for outputting sysfs content and it's possible to overrun the > PAGE_SIZE buffer length. > > Add a generic sysfs_emit function that knows that the size of the > temporary buffer and ensures that no overrun is done. > > Add a generic sysfs_emit_at function that can be used in multiple > call situations that also ensures that no overrun is done. > > Signed-off-by: Joe Perches > --- > > V2: Simplify sysfs_emit and add sysfs_emit_at > Include Documentation change Greg? Rafael? Thoughts on this? One additional possibility is to validate the buf address to be page aligned by adding a test of buf and offset_in_page(buf) ie: WARN(!buf || offset_in_page(buf), etc... Output defects can exist in sysfs content using sprintf and snprintf. sprintf does not know the PAGE_SIZE maximum of the temporary buffer used for outputting sysfs content and it's possible to overrun the PAGE_SIZE buffer length. Add a generic sysfs_emit function that knows that the size of the temporary buffer and ensures that no overrun is done. Add a generic sysfs_emit_at function that can be used in multiple call situations that also ensures that no overrun is done. Validate the output buffer argument to be page aligned. Validate the offset len argument to be within the PAGE_SIZE buf. Signed-off-by: Joe Perches --- fs/sysfs/file.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index eb6897ab78e7..96d0da65e088 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "sysfs.h" @@ -707,3 +708,57 @@ int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid) return 0; } EXPORT_SYMBOL_GPL(sysfs_change_owner); + +/** + * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer. + * @buf: start of PAGE_SIZE buffer. + * @fmt: format + * @...: optional arguments to @format + * + * + * Returns number of characters written to @buf. + */ +int sysfs_emit(char *buf, const char *fmt, ...) +{ + va_list args; + int len; + + if (WARN(!buf || offset_in_page(buf), + "invalid sysfs_emit: buf:%p\n", buf)) + return 0; + + va_start(args, fmt); + len = vscnprintf(buf, PAGE_SIZE, fmt, args); + va_end(args); + + return len; +} +EXPORT_SYMBOL_GPL(sysfs_emit); + +/** + * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer. + * @buf: start of PAGE_SIZE buffer. + * @at: offset in @buf to start write in bytes + * @at must be >= 0 && < PAGE_SIZE + * @fmt: format + * @...: optional arguments to @fmt + * + * + * Returns number of characters written starting at &@buf[@at]. + */ +int sysfs_emit_at(char *buf, int at, const char *fmt, ...) +{ + va_list args; + int len; + + if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE, + "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at)) + return 0; + + va_start(args, fmt); + len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args); + va_end(args); + + return len; +} +EXPORT_SYMBOL_GPL(sysfs_emit_at);