Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sysfs: correctly handle short reads on PREALLOC attrs.

attributes declared with __ATTR_PREALLOC use sysfs_kf_read()
which ignores the 'count' arg.
So a 1-byte read request can return more bytes than that.

This is seen with the 'dash' shell when 'read' is used on
some 'md' sysfs attributes.

So only return the 'min' of count and the attribute length.

Signed-off-by: NeilBrown <neilb@suse.com>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

NeilBrown and committed by
Greg Kroah-Hartman
65da3484 cfcf6a91

+3 -1
+3 -1
fs/sysfs/file.c
··· 108 108 { 109 109 const struct sysfs_ops *ops = sysfs_file_ops(of->kn); 110 110 struct kobject *kobj = of->kn->parent->priv; 111 + size_t len; 111 112 112 113 /* 113 114 * If buf != of->prealloc_buf, we don't know how ··· 116 115 */ 117 116 if (pos || WARN_ON_ONCE(buf != of->prealloc_buf)) 118 117 return 0; 119 - return ops->show(kobj, of->kn->priv, buf); 118 + len = ops->show(kobj, of->kn->priv, buf); 119 + return min(count, len); 120 120 } 121 121 122 122 /* kernfs write callback for regular sysfs files */