Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp1712021pxa; Sat, 29 Aug 2020 00:01:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUuh6OoQSF6oOE5q7UZyjnzNUeuPSJvH1oxpXOBySYe+7sTW597H0U7hxEf9Fhtdx53I4h X-Received: by 2002:a17:906:eb11:: with SMTP id mb17mr1071961ejb.255.1598684470531; Sat, 29 Aug 2020 00:01:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598684470; cv=none; d=google.com; s=arc-20160816; b=tLiCtRTp49xC5cYs23JCdVDegbAuUnHQFdcIIyDdfJYKWgq6jN76Qc5uXXFsNepu/f OuQWernyg8cx25xK8HzVnHBVi7GeNeyipMTltMdrmk5KioS/XsD71oD4kpoOHrJghzmH kAAUO9/t/wQHdJtU4jFLZLUROclJQmQbrJGihfDeiDkNbvnK4Km5Kk8OoMMl5CHM9Fva QH1xD1YqNWAjJWh4xJud4VxrEhcK8bsmIOgOLig/20I3sl9htUHTBZj/owIHbSYKybbz qUHKja7cLAX/CH/AeLToHw6GBJi5fkFkgbg6V2U5Ob6Zs+nZplqCRgBfkUk/Dl/Ey/bA +9AQ== 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:autocrypt:from:references:cc:to:subject; bh=DPbKEFaWNowFySa1ektYp+9sXLz/CsLAXB1avS4exuk=; b=XOIw2W8bfg+ohbVpvSqst9OMTNvGCqVFbhI+7ZaAO5g7NgbvGBc88is8wt0GON//dS a/SQjTad+Ils/mCr46vZcY9wo1+neVtwv+domogHUzzUyQiDMr+Gua8xVWExu6nRWUT+ wThCGM2QOTTMukQtNbOTpVfS5dcIEU6NiZ7NmowB1cQ0vMfjAxBHj6Ws5Yzr27VegQlO 04jbMvJDmrrIebhV8aGWAHD3R+SceSXmFh2ih4VXcRSzt4lP1cxZQaiO0PcGQZxq/XzM kLh3ru0yFh1wPRwL0udRjJOaOcWToM9gMJcgyVcoK+q5cPDMvIioHM2Prbydpt9AoRiZ Eh+A== 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 d14si1047362edp.413.2020.08.29.00.00.47; Sat, 29 Aug 2020 00:01:10 -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 S1726115AbgH2G7S (ORCPT + 99 others); Sat, 29 Aug 2020 02:59:18 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:34865 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725929AbgH2G7P (ORCPT ); Sat, 29 Aug 2020 02:59:15 -0400 Received: by mail-lj1-f193.google.com with SMTP id i10so1283665ljn.2 for ; Fri, 28 Aug 2020 23:59:12 -0700 (PDT) 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:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=DPbKEFaWNowFySa1ektYp+9sXLz/CsLAXB1avS4exuk=; b=l0+6esUHhvXdeD7lQE4SLnV5ivaCa6bjZQr7d+XZMwDum8KSWDjZ+UxZcnLN2uzUB+ lVW+UTqOofbv/LZP++1uxDE+MneMML+D0z0Pp7rUe1ptZVqsrtWmsMVrOXn2XkEU0sGL FdGB2v0Q/JIKBLSIZgRBBTxoYPnzGrWS6xJG3Y+Umsx2eFoU/cfr2W2K6KkJnINpMwyK ZJ/FrwcXPpmeKpIbo+yEICVCiW/Jlj+Uko1ZFMNwXQyB6bpomKGP/rushnV6IiCrPT2X /qeaeB7mdGW13M1BkW3G0VWTGY6S4+OB4mm09pqshXkGxI7UCUekTpqwKAxoUO5hjB8d 8xIg== X-Gm-Message-State: AOAM532bwiZPerPGEuRqHSMMhOzEpqGR6VFl1+xaiBZUS3K5noCRxCTz GImYcDn5U9J6qpy4JRZwksaiQil7j0g= X-Received: by 2002:a2e:4c02:: with SMTP id z2mr996265lja.177.1598684351527; Fri, 28 Aug 2020 23:59:11 -0700 (PDT) Received: from [192.168.1.8] ([213.87.147.111]) by smtp.gmail.com with ESMTPSA id g19sm306193ljk.125.2020.08.28.23.59.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Aug 2020 23:59:11 -0700 (PDT) Subject: Re: [PATCH] sysfs: Add sysfs_emit to replace sprintf to PAGE_SIZE buffers. To: Joe Perches , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Kees Cook , "Gustavo A . R . Silva" , Julia Lawall , Alex Dewar , linux-kernel@vger.kernel.org References: From: Denis Efremov Autocrypt: addr=efremov@linux.com; keydata= mQINBFsJUXwBEADDnzbOGE/X5ZdHqpK/kNmR7AY39b/rR+2Wm/VbQHV+jpGk8ZL07iOWnVe1 ZInSp3Ze+scB4ZK+y48z0YDvKUU3L85Nb31UASB2bgWIV+8tmW4kV8a2PosqIc4wp4/Qa2A/ Ip6q+bWurxOOjyJkfzt51p6Th4FTUsuoxINKRMjHrs/0y5oEc7Wt/1qk2ljmnSocg3fMxo8+ y6IxmXt5tYvt+FfBqx/1XwXuOSd0WOku+/jscYmBPwyrLdk/pMSnnld6a2Fp1zxWIKz+4VJm QEIlCTe5SO3h5sozpXeWS916VwwCuf8oov6706yC4MlmAqsQpBdoihQEA7zgh+pk10sCvviX FYM4gIc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fAqUDO T30gV5bQ8BHqvfWyEhJT+CS3JJyP8BmmSgPa0Adlp6Do+pRsOO1YNNO78SYABhMi3fEa7X37 WxL31TrNCPnIauTgZtf/KCFQJpKaakC3ffEkPhyTjEl7oOE9xccNjccZraadi+2uHV0ULA1m ycHhb817A03n1I00QwLf2wOkckdqTqRbFFI/ik69hF9hemK/BmAHpShI+z1JsYT9cSs8D7wb aF/jQVy4URensgAPkgXsRiboqOj/rTz9F5mpd/gPU/IOUPFEMoo4TInt/+dEVECHioU3RRrW EahrGMfRngbdp/mKs9aBR56ECMfFFUPyI3VJsNbgpcIJjV/0N+JdJKQpJ/4uQ2zNm0wH/RU8 CRJvEwtKemX6fp/zLI36Gvz8zJIjSBIEqCb7vdgvWarksrhmi6/Jay5zRZ03+k6YwiqgX8t7 ANwvYa1h1dQ36OiTqm1cIxRCGl4wrypOVGx3OjCar7sBLD+NkwO4RaqFvdv0xuuy4x01VnOF Message-ID: Date: Sat, 29 Aug 2020 09:59:08 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 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 Hi, On 8/29/20 1:52 AM, Joe Perches wrote: > sprintf does not know the PAGE_SIZE maximum of the temporary buffer > used for outputting sysfs content requests and it's possible to > overrun the buffer length. > > Add a generic sysfs_emit mechanism that knows that the size of the > temporary buffer and ensures that no overrun is done. > > Signed-off-by: Joe Perches > --- It could be a good idea to update the docs to, i.e.: https://www.kernel.org/doc/html/latest/filesystems/sysfs.html > fs/sysfs/file.c | 30 ++++++++++++++++++++++++++++++ > include/linux/sysfs.h | 8 ++++++++ > 2 files changed, 38 insertions(+) > > diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c > index eb6897ab78e7..06a13bbd7080 100644 > --- a/fs/sysfs/file.c > +++ b/fs/sysfs/file.c > @@ -707,3 +707,33 @@ 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. > + * @pos: current position in buffer > + * (pos - buf) must always be < PAGE_SIZE > + * @fmt: format > + * @...: arguments to format > + * > + * > + * Returns number of characters written at pos. > + */ > +int sysfs_emit(char *buf, char *pos, const char *fmt, ...) > +{ > + va_list args; > + bool bad_pos = pos < buf; > + bool bad_len = (pos - buf) >= PAGE_SIZE; > + int len; > + > + if (WARN(bad_pos || bad_len, "(pos < buf):%d (pos >= PAGE_SIZE):%d\n", > + bad_pos, bad_len)) > + return 0; > + > + va_start(args, fmt); > + len = vscnprintf(pos, PAGE_SIZE - (pos - buf), fmt, args); > + va_end(args); > + > + return len; > +} > +EXPORT_SYMBOL_GPL(sysfs_emit); > diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h > index 34e84122f635..5a21d3d30016 100644 > --- a/include/linux/sysfs.h > +++ b/include/linux/sysfs.h > @@ -329,6 +329,8 @@ int sysfs_groups_change_owner(struct kobject *kobj, > int sysfs_group_change_owner(struct kobject *kobj, > const struct attribute_group *groups, kuid_t kuid, > kgid_t kgid); > +__printf(3, 4) > +int sysfs_emit(char *buf, char *pos, const char *fmt, ...); > > #else /* CONFIG_SYSFS */ > > @@ -576,6 +578,12 @@ static inline int sysfs_group_change_owner(struct kobject *kobj, > return 0; > } > > +__printf(3, 4) > +static inline int sysfs_emit(char *buf, char *pos, const char *fmt, ...) > +{ > + return 0; > +} > + > #endif /* CONFIG_SYSFS */ > > static inline int __must_check sysfs_create_file(struct kobject *kobj, > Thanks, Denis