2007-11-08 11:07:00

by Miao Xie

[permalink] [raw]
Subject: [PATCH] sysfs: fix off-by-one error in fill_read_buffer

Hi,everyone.
I found that there is a off-by-one problem in the following code.

Version: 2.6.24-rc2
File: fs/sysfs/file.c:118-122
Function: fill_read_buffer
--------------------------------------------------------------------
count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);

sysfs_put_active_two(attr_sd);

BUG_ON(count > (ssize_t)PAGE_SIZE);
--------------------------------------------------------------------
Because according to the specification of the sysfs and the implement of the
show methods, the show methods return the number of bytes which would be
generated for the given input, excluding the trailing null.So if the return
value of the show methods equals PAGE_SIZE - 1, the buffer is full in fact.
And if the return value equals PAGE_SIZE, the resulting string was already
truncated,or buffer overflow occurred.

This patch fixes an off-by-one error in fill_read_buffer.

Signed-off-by: Miao Xie <[email protected]>

---
fs/sysfs/file.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 27d1785..f98d66d 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -119,7 +119,7 @@ static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer

sysfs_put_active_two(attr_sd);

- BUG_ON(count > (ssize_t)PAGE_SIZE);
+ BUG_ON(count >= (ssize_t)PAGE_SIZE);
if (count >= 0) {
buffer->needs_read_fill = 0;
buffer->count = count;
--
1.5.3




2007-11-12 14:08:26

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH] sysfs: fix off-by-one error in fill_read_buffer

Miao Xie wrote:
> Hi,everyone.
> I found that there is a off-by-one problem in the following code.
>
> Version: 2.6.24-rc2
> File: fs/sysfs/file.c:118-122
> Function: fill_read_buffer
> --------------------------------------------------------------------
> count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
>
> sysfs_put_active_two(attr_sd);
>
> BUG_ON(count > (ssize_t)PAGE_SIZE);
> --------------------------------------------------------------------
> Because according to the specification of the sysfs and the implement of
> the
> show methods, the show methods return the number of bytes which would be
> generated for the given input, excluding the trailing null.So if the return
> value of the show methods equals PAGE_SIZE - 1, the buffer is full in fact.
> And if the return value equals PAGE_SIZE, the resulting string was already
> truncated,or buffer overflow occurred.
>
> This patch fixes an off-by-one error in fill_read_buffer.
>
> Signed-off-by: Miao Xie <[email protected]>

It isn't strictly a bug. If the ->show() op fills full PAGE_SIZE and
returns PAGE_SIZE, the user will get full PAGE_SIZE bytes correctly, so
it will work. However, considering normal use cases, return value of
PAGE_SIZE very likely indicates an error condition, so considering it a
BUG condition is a good idea.

Miao, can you please note that the code works fine with PAGE_SIZE return
but it's likely to indicate truncated result or overflow in normal use
cases as a comment on top of the BUG_ON()?

Thanks.

--
tejun

2007-11-13 03:50:18

by Miao Xie

[permalink] [raw]
Subject: Re: [PATCH] sysfs: fix off-by-one error in fill_read_buffer

on 2007-11-12 22:08 Tejun Heo wrote:
> It isn't strictly a bug. If the ->show() op fills full PAGE_SIZE and
> returns PAGE_SIZE, the user will get full PAGE_SIZE bytes correctly, so
> it will work. However, considering normal use cases, return value of
> PAGE_SIZE very likely indicates an error condition, so considering it a
> BUG condition is a good idea.
>
> Miao, can you please note that the code works fine with PAGE_SIZE return
> but it's likely to indicate truncated result or overflow in normal use
> cases as a comment on top of the BUG_ON()?
>
> Thanks.

OK, I did it.

Signed-off-by: Miao Xie <[email protected]>

---
fs/sysfs/file.c | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 27d1785..7f6a8d2 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -119,7 +119,9 @@ static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer

sysfs_put_active_two(attr_sd);

- BUG_ON(count > (ssize_t)PAGE_SIZE);
+ /* the code works fine with PAGE_SIZE return but it's likely to
+ indicate truncated result or overflow in normal use cases. */
+ BUG_ON(count >= (ssize_t)PAGE_SIZE);
if (count >= 0) {
buffer->needs_read_fill = 0;
buffer->count = count;
--
1.5.3


2007-11-13 04:43:43

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH] sysfs: fix off-by-one error in fill_read_buffer

Miao Xie wrote:
> on 2007-11-12 22:08 Tejun Heo wrote:
>> It isn't strictly a bug. If the ->show() op fills full PAGE_SIZE and
>> returns PAGE_SIZE, the user will get full PAGE_SIZE bytes correctly, so
>> it will work. However, considering normal use cases, return value of
>> PAGE_SIZE very likely indicates an error condition, so considering it a
>> BUG condition is a good idea.
>>
>> Miao, can you please note that the code works fine with PAGE_SIZE return
>> but it's likely to indicate truncated result or overflow in normal use
>> cases as a comment on top of the BUG_ON()?
>>
>> Thanks.
>
> OK, I did it.
>
> Signed-off-by: Miao Xie <[email protected]>

Acked-by: Tejun Heo <[email protected]>

Greg, can you please push this one? Thanks.

--
tejun