Re: [PATCH] sysfs: Add sysfs_emit to replace sprintf to PAGE_SIZE buffers.

From: Greg Kroah-Hartman
Date: Sat Aug 29 2020 - 02:23:19 EST


On Fri, Aug 28, 2020 at 03:52:13PM -0700, 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 <joe@xxxxxxxxxxx>
> ---
> 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

sysfs files are always supposed to be "one value per file", so why would
you ever need a 'pos' variable to show the location in the buffer?

This would encourage people to do things they shouldn't be doing, and it
makes the people who are doing things properly, have to constantly see
'pos' as something that maybe they should be using?

I think if you tried to convert a subsystem to use this (try USB as an
example), you would see why that parameter isn't needed.

thanks,

greg k-h