linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
@ 2011-12-18 11:58 Ryota Ozaki
  2011-12-18 21:28 ` KOSAKI Motohiro
  2011-12-18 22:44 ` David Rientjes
  0 siblings, 2 replies; 8+ messages in thread
From: Ryota Ozaki @ 2011-12-18 11:58 UTC (permalink / raw)
  To: linux-kernel, Greg Kroah-Hartman; +Cc: linux-mm, stable

/sys/devices/system/node/{online,possible} involve a garbage byte
because print_nodes_state returns content size + 1. To fix the bug,
the patch changes the use of cpuset_sprintf_cpulist to follow the
use at other places, which is clearer and safer.

This bug was introduced since v2.6.24.

Signed-off-by: Ryota Ozaki <ozaki.ryota@gmail.com>
---
 drivers/base/node.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/base/node.c b/drivers/base/node.c
index 5693ece..ef7c1f9 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -587,11 +587,9 @@ static ssize_t print_nodes_state(enum node_states state, char *buf)
 {
 	int n;
 
-	n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
-	if (n > 0 && PAGE_SIZE > n + 1) {
-		*(buf + n++) = '\n';
-		*(buf + n++) = '\0';
-	}
+	n = nodelist_scnprintf(buf, PAGE_SIZE-2, node_states[state]);
+	buf[n++] = '\n';
+	buf[n] = '\0';
 	return n;
 }
 
-- 
1.7.5.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-18 11:58 [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state Ryota Ozaki
@ 2011-12-18 21:28 ` KOSAKI Motohiro
  2011-12-20  4:22   ` Ryota Ozaki
  2011-12-18 22:44 ` David Rientjes
  1 sibling, 1 reply; 8+ messages in thread
From: KOSAKI Motohiro @ 2011-12-18 21:28 UTC (permalink / raw)
  To: Ryota Ozaki; +Cc: linux-kernel, Greg Kroah-Hartman, linux-mm, stable

(12/18/11 6:58 AM), Ryota Ozaki wrote:
> /sys/devices/system/node/{online,possible} involve a garbage byte
> because print_nodes_state returns content size + 1. To fix the bug,
> the patch changes the use of cpuset_sprintf_cpulist to follow the
> use at other places, which is clearer and safer.
> 
> This bug was introduced since v2.6.24.
> 
> Signed-off-by: Ryota Ozaki<ozaki.ryota@gmail.com>
> ---
>   drivers/base/node.c |    8 +++-----
>   1 files changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/base/node.c b/drivers/base/node.c
> index 5693ece..ef7c1f9 100644
> --- a/drivers/base/node.c
> +++ b/drivers/base/node.c
> @@ -587,11 +587,9 @@ static ssize_t print_nodes_state(enum node_states state, char *buf)
>   {
>   	int n;
> 
> -	n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
> -	if (n>  0&&  PAGE_SIZE>  n + 1) {
> -		*(buf + n++) = '\n';
> -		*(buf + n++) = '\0';
> -	}
> +	n = nodelist_scnprintf(buf, PAGE_SIZE-2, node_states[state]);

PAGE_SIZE-1. This seems another off by one. buf[n++] = '\n' mean
override old trailing '\0' and buf[n] = '\0' mean to append one byte.
Then totally, we append one byte.

> +	buf[n++] = '\n';
> +	buf[n] = '\0';
>   	return n;
>   }
> 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-18 11:58 [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state Ryota Ozaki
  2011-12-18 21:28 ` KOSAKI Motohiro
@ 2011-12-18 22:44 ` David Rientjes
  2011-12-18 22:48   ` KOSAKI Motohiro
  2011-12-20  4:43   ` Ryota Ozaki
  1 sibling, 2 replies; 8+ messages in thread
From: David Rientjes @ 2011-12-18 22:44 UTC (permalink / raw)
  To: Ryota Ozaki; +Cc: linux-kernel, Greg Kroah-Hartman, linux-mm, stable

On Sun, 18 Dec 2011, Ryota Ozaki wrote:

> /sys/devices/system/node/{online,possible} involve a garbage byte
> because print_nodes_state returns content size + 1. To fix the bug,
> the patch changes the use of cpuset_sprintf_cpulist to follow the
> use at other places, which is clearer and safer.
> 

It's not a garbage byte, sysdev files use a buffer created with 
get_zeroed_page(), so extra byte is guaranteed to be zero since 
nodelist_scnprintf() won't write to it.  So the issue here is that 
print_nodes_state() returns a size that is off by one according to 
ISO C99 although it won't cause a problem in practice.

> This bug was introduced since v2.6.24.
> 

It's not a bug, the result of a 4-node system would be "0-3\n\0" and 
returns 5 correctly.  You can verify this very simply with strace.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-18 22:44 ` David Rientjes
@ 2011-12-18 22:48   ` KOSAKI Motohiro
  2011-12-19 20:53     ` David Rientjes
  2011-12-20  4:43   ` Ryota Ozaki
  1 sibling, 1 reply; 8+ messages in thread
From: KOSAKI Motohiro @ 2011-12-18 22:48 UTC (permalink / raw)
  To: David Rientjes
  Cc: Ryota Ozaki, linux-kernel, Greg Kroah-Hartman, linux-mm, stable

(12/18/11 5:44 PM), David Rientjes wrote:
> On Sun, 18 Dec 2011, Ryota Ozaki wrote:
>
>> /sys/devices/system/node/{online,possible} involve a garbage byte
>> because print_nodes_state returns content size + 1. To fix the bug,
>> the patch changes the use of cpuset_sprintf_cpulist to follow the
>> use at other places, which is clearer and safer.
>>
>
> It's not a garbage byte, sysdev files use a buffer created with
> get_zeroed_page(), so extra byte is guaranteed to be zero since
> nodelist_scnprintf() won't write to it.  So the issue here is that
> print_nodes_state() returns a size that is off by one according to
> ISO C99 although it won't cause a problem in practice.
>
>> This bug was introduced since v2.6.24.
>>
>
> It's not a bug, the result of a 4-node system would be "0-3\n\0" and
> returns 5 correctly.  You can verify this very simply with strace.

Usually, /sys files don't output trailing 'JPY0'. And, 'JPY0' is not regular
io friendly. So I can imagine some careless programmer think it is 
garbage. Is there any benefit to show trailing 'JPY0'?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-18 22:48   ` KOSAKI Motohiro
@ 2011-12-19 20:53     ` David Rientjes
  2011-12-19 21:15       ` KOSAKI Motohiro
  0 siblings, 1 reply; 8+ messages in thread
From: David Rientjes @ 2011-12-19 20:53 UTC (permalink / raw)
  To: KOSAKI Motohiro
  Cc: Ryota Ozaki, linux-kernel, Greg Kroah-Hartman, linux-mm, stable

[-- Attachment #1: Type: TEXT/PLAIN, Size: 337 bytes --]

On Sun, 18 Dec 2011, KOSAKI Motohiro wrote:

> Usually, /sys files don't output trailing 'AJPY0'. And, 'AJPY0' is not regular
> io friendly. So I can imagine some careless programmer think it is garbage. Is
> there any benefit to show trailing 'AJPY0'?
> 

Nope, it could be removed since the buffer is allocated with 
get_zeroed_page().

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-19 20:53     ` David Rientjes
@ 2011-12-19 21:15       ` KOSAKI Motohiro
  0 siblings, 0 replies; 8+ messages in thread
From: KOSAKI Motohiro @ 2011-12-19 21:15 UTC (permalink / raw)
  To: David Rientjes
  Cc: Ryota Ozaki, linux-kernel, Greg Kroah-Hartman, linux-mm, stable

(12/19/11 3:53 PM), David Rientjes wrote:
> On Sun, 18 Dec 2011, KOSAKI Motohiro wrote:
>
>> Usually, /sys files don't output trailing 'AJPY0'. And, 'AJPY0' is not regular
>> io friendly. So I can imagine some careless programmer think it is garbage. Is
>> there any benefit to show trailing 'AJPY0'?
>>
>
> Nope, it could be removed since the buffer is allocated with
> get_zeroed_page().

ok, thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-18 21:28 ` KOSAKI Motohiro
@ 2011-12-20  4:22   ` Ryota Ozaki
  0 siblings, 0 replies; 8+ messages in thread
From: Ryota Ozaki @ 2011-12-20  4:22 UTC (permalink / raw)
  To: KOSAKI Motohiro; +Cc: linux-kernel, Greg Kroah-Hartman, linux-mm, stable

Hi Kosaki-san,

I'm sorry for my late reply.
2011/12/19 KOSAKI Motohiro <kosaki.motohiro@gmail.com>:
> (12/18/11 6:58 AM), Ryota Ozaki wrote:
>> /sys/devices/system/node/{online,possible} involve a garbage byte
>> because print_nodes_state returns content size + 1. To fix the bug,
>> the patch changes the use of cpuset_sprintf_cpulist to follow the
>> use at other places, which is clearer and safer.
>>
>> This bug was introduced since v2.6.24.
>>
>> Signed-off-by: Ryota Ozaki<ozaki.ryota@gmail.com>
>> ---
>>   drivers/base/node.c |    8 +++-----
>>   1 files changed, 3 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/base/node.c b/drivers/base/node.c
>> index 5693ece..ef7c1f9 100644
>> --- a/drivers/base/node.c
>> +++ b/drivers/base/node.c
>> @@ -587,11 +587,9 @@ static ssize_t print_nodes_state(enum node_states state, char *buf)
>>   {
>>       int n;
>>
>> -     n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
>> -     if (n>  0&&  PAGE_SIZE>  n + 1) {
>> -             *(buf + n++) = '\n';
>> -             *(buf + n++) = '\0';
>> -     }
>> +     n = nodelist_scnprintf(buf, PAGE_SIZE-2, node_states[state]);
>
> PAGE_SIZE-1. This seems another off by one. buf[n++] = '¥n' mean
> override old trailing '¥0' and buf[n] = '¥0' mean to append one byte.
> Then totally, we append one byte.

Thanks for pointing this out, you're right. (nodelist_)scnprintf returns
size-1 at most, thus we need to remain just one byte. I'll fix it in
the next patch.

Actually I bring the code from another and such the flaw can be found in other
functions. So I'll them as well.

  ozaki-r

>
>> +     buf[n++] = '\n';
>> +     buf[n] = '\0';
>>       return n;
>>   }
>>
>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state
  2011-12-18 22:44 ` David Rientjes
  2011-12-18 22:48   ` KOSAKI Motohiro
@ 2011-12-20  4:43   ` Ryota Ozaki
  1 sibling, 0 replies; 8+ messages in thread
From: Ryota Ozaki @ 2011-12-20  4:43 UTC (permalink / raw)
  To: David Rientjes; +Cc: linux-kernel, Greg Kroah-Hartman, linux-mm, stable

Hi David,

I'm so sorry for my late reply.

On Mon, Dec 19, 2011 at 7:44 AM, David Rientjes <rientjes@google.com> wrote:
> On Sun, 18 Dec 2011, Ryota Ozaki wrote:
>
>> /sys/devices/system/node/{online,possible} involve a garbage byte
>> because print_nodes_state returns content size + 1. To fix the bug,
>> the patch changes the use of cpuset_sprintf_cpulist to follow the
>> use at other places, which is clearer and safer.
>>
>
> It's not a garbage byte, sysdev files use a buffer created with
> get_zeroed_page(), so extra byte is guaranteed to be zero since
> nodelist_scnprintf() won't write to it.  So the issue here is that
> print_nodes_state() returns a size that is off by one according to

I see. It's certainly not a garbage but just a zero-cleared byte.

> ISO C99 although it won't cause a problem in practice.
>
>> This bug was introduced since v2.6.24.
>>
>
> It's not a bug, the result of a 4-node system would be "0-3\n\0" and
> returns 5 correctly.  You can verify this very simply with strace.

Of course I confirmed the trailing '\0'. I'm sure it's not critical issue
but it actually influences my script; I have to change from rstrip("\n")
to rstrip("\n\0") in python. Yes, it's pretty trivial but enough to get
rid of the extra for me :-)

Anyway the subject and the comment of my patch need to be fixed. I'll
send a revised one later.

Thanks.
  ozaki-r

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-12-20  4:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-18 11:58 [PATCH][RESEND] mm: Fix off-by-one bug in print_nodes_state Ryota Ozaki
2011-12-18 21:28 ` KOSAKI Motohiro
2011-12-20  4:22   ` Ryota Ozaki
2011-12-18 22:44 ` David Rientjes
2011-12-18 22:48   ` KOSAKI Motohiro
2011-12-19 20:53     ` David Rientjes
2011-12-19 21:15       ` KOSAKI Motohiro
2011-12-20  4:43   ` Ryota Ozaki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).