fs: sysfs: don't pass count == 0 to bin file readers
PATCH 1/1 fs: sysfs: don't pass count == 0 to bin file readers
If count == 0 bytes are requested by a reader, sysfs_kf_bin_read() deliberately returns 0 without passing a potentially harmful value to some externally defined underlying battr->read() function.
However in case of (pos == size && count) the next clause always sets count to 0 and this value is handed over to battr->read().
The change intends to make obsolete (and remove later) a redundant sanity check in battr->read(), if it is present, or add more protection to struct bin_attribute users, who does not care about input arguments.
--- fs/sysfs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/sysfs/file.c +1 -1
@@ -90,7 +90,7 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,return 0 ;if (size) { -if (pos > size) +if (pos >= size)return 0 ;if (pos + count > size) count = size - pos;
Signature · 1 line
1.7.10.4
Hello,
Quoted 22 lines · Vladimir Zapolskiy
If count == 0 bytes are requested by a reader,
sysfs_kf_bin_read()deliberately returns 0 without passing a potentially harmful value to some externally defined underlyingbattr->read()function.However in case of (pos == size && count) the next clause always sets count to 0 and this value is handed over to
battr->read().The change intends to make obsolete (and remove later) a redundant sanity check in
battr->read(), if it is present, or add more protection to structbin_attributeusers, who does not care about input arguments.Signed-off-by Vladimir Zapolskiy <[email protected]>--- fs/sysfs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/sysfs/file.c +0
-0
fs/sysfs/file.c static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf, · line 96
Quoted 8 lines
fs/sysfs/file.c +1
-1@@ -90,7 +90,7 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,return 0 ;if (size) { -if (pos > size) +if (pos >= size)return 0 ;if (pos + count > size) count = size - pos;
Hmmm... maybe just move that test upwards?
if (!count || pos >= size) return 0; count = min(count, size - pos);
Signature · 1 line
tejun
Hello Tejun,
Quoted 36 lines · Tejun Heo
Hello,
Quoted 22 lines · Vladimir Zapolskiy
If count == 0 bytes are requested by a reader,
sysfs_kf_bin_read()deliberately returns 0 without passing a potentially harmful value to some externally defined underlyingbattr->read()function.However in case of (pos == size && count) the next clause always sets count to 0 and this value is handed over to
battr->read().The change intends to make obsolete (and remove later) a redundant sanity check in
battr->read(), if it is present, or add more protection to structbin_attributeusers, who does not care about input arguments.Signed-off-by Vladimir Zapolskiy <[email protected]>--- fs/sysfs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/sysfs/file.c +0
-0fs/sysfs/file.c static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf, · line 96
Quoted 8 lines
fs/sysfs/file.c +1
-1@@ -90,7 +90,7 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,return 0 ;if (size) { -if (pos > size) +if (pos >= size)return 0 ;if (pos + count > size) count = size - pos;Hmmm... maybe just move that test upwards?
Quoted 5 lines
if (!count || pos >= size) return 0; count = min(count, size - pos);
If the code block stays within if (size && count) { ... }, then !count check is redundant (you may notice that !count check is already present above but not shown in diff's 3 lines context), and I agree that
if (pos >= size) return 0; if (pos + count > size) count = size - pos;
and
if (pos >= size) return 0; count = min(count, size - pos);
are equal.
But "!size" is a special case,
if (!count || pos >= size) return 0;
seems to be incorrect in case of !size ===> (pos >= size) == true.
To the sent change I may add a replacement of "if (pos + count > size) ..." with min_t (ssize_t, count, size - pos), if you wish.
--
With best wishes,
Vladimir
Hello, Vladimir.
Quoted 6 lines · Vladimir Zapolskiy
But "!size" is a special case,
if (!count || pos >= size) return 0;seems to be incorrect in case of !size ===> (pos >= size) == true.
Hmmm... Why is that wrong tho? If size is zero and pos is zero, there's nothing to do, no?
Thanks.
Signature · 1 line
tejun
Hello Tejun,
Quoted 5 lines · Tejun Heo
Hello, Vladimir.
Quoted 2 lines · Vladimir Zapolskiy
But "!size" is a special case,
Quoted 7 lines
Quoted 4 lines
if (!count || pos >= size) return 0;seems to be incorrect in case of !size ===> (pos >= size) == true.
Hmmm... Why is that wrong tho? If size is zero and pos is zero, there's nothing to do, no?
positive size value in the context means the fixed exact length of the file and if size == 0, then it represents some undefined size, often dynamic in runtime. So, if size is zero and pos is zero it stands for reading from the beginning of the file as many bytes as allowed by battr->read() realization. This special case is utilized by quite many bin_attribute users, probably more than half of them set .size to 0.
Quoted 2 lines
Thanks.
--
With best wishes,
Vladimir
Tejun Heo
Acked-byQuoted 16 lines · Vladimir Zapolskiy
Hello Tejun,
Quoted 5 lines · Tejun Heo
Hello, Vladimir.
Quoted 2 lines · Vladimir Zapolskiy
But "!size" is a special case,
Quoted 7 lines
Quoted 4 lines
if (!count || pos >= size) return 0;seems to be incorrect in case of !size ===> (pos >= size) == true.
Hmmm... Why is that wrong tho? If size is zero and pos is zero, there's nothing to do, no?
Quoted 6 lines
positive size value in the context means the fixed exact length of the file and if size == 0, then it represents some undefined size, often dynamic in runtime. So, if size is zero and pos is zero it stands for reading from the beginning of the file as many bytes as allowed by
battr->read()realization. This special case is utilized by quite manybin_attributeusers, probably more than half of them set .size to 0.
Ah, right, I completely forgot about that. Please feel free to add
to the original patch.
Thanks for the patience.
Signature · 1 line
tejun