Skip to content

Commit

Permalink
sysfs: correctly handle short reads on PREALLOC attrs.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
NeilBrown authored and gregkh committed Oct 4, 2015
1 parent cfcf6a9 commit 65da348
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fs/sysfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
{
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
struct kobject *kobj = of->kn->parent->priv;
size_t len;

/*
* If buf != of->prealloc_buf, we don't know how
* large it is, so cannot safely pass it to ->show
*/
if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
return 0;
return ops->show(kobj, of->kn->priv, buf);
len = ops->show(kobj, of->kn->priv, buf);
return min(count, len);
}

/* kernfs write callback for regular sysfs files */
Expand Down

0 comments on commit 65da348

Please sign in to comment.