Skip to content

Commit 65da348

Browse files
NeilBrowngregkh
authored andcommitted
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 <[email protected]> Acked-by: Tejun Heo <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent cfcf6a9 commit 65da348

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

fs/sysfs/file.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
108108
{
109109
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
110110
struct kobject *kobj = of->kn->parent->priv;
111+
size_t len;
111112

112113
/*
113114
* If buf != of->prealloc_buf, we don't know how
114115
* large it is, so cannot safely pass it to ->show
115116
*/
116117
if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
117118
return 0;
118-
return ops->show(kobj, of->kn->priv, buf);
119+
len = ops->show(kobj, of->kn->priv, buf);
120+
return min(count, len);
119121
}
120122

121123
/* kernfs write callback for regular sysfs files */

0 commit comments

Comments
 (0)