public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [slabinfo PATCH] Fix off-by-one after readlink() call
@ 2011-10-14 16:56 Thomas Jarosch
  2011-10-14 17:16 ` Christoph Lameter
  0 siblings, 1 reply; 7+ messages in thread
From: Thomas Jarosch @ 2011-10-14 16:56 UTC (permalink / raw)
  To: Christoph Lameter; +Cc: linux-kernel


Signed-off-by: Thomas Jarosch <thomas.jarosch@intra2net.com>
---
 tools/slub/slabinfo.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/tools/slub/slabinfo.c b/tools/slub/slabinfo.c
index 868cc93..cc1a378 100644
--- a/tools/slub/slabinfo.c
+++ b/tools/slub/slabinfo.c
@@ -1145,7 +1145,7 @@ static void read_slab_dir(void)
 		switch (de->d_type) {
 		   case DT_LNK:
 			alias->name = strdup(de->d_name);
-			count = readlink(de->d_name, buffer, sizeof(buffer));
+			count = readlink(de->d_name, buffer, sizeof(buffer)-1);
 
 			if (count < 0)
 				fatal("Cannot read symlink %s\n", de->d_name);
-- 
1.7.6.4


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

* Re: [slabinfo PATCH] Fix off-by-one after readlink() call
  2011-10-14 16:56 [slabinfo PATCH] Fix off-by-one after readlink() call Thomas Jarosch
@ 2011-10-14 17:16 ` Christoph Lameter
  2011-10-14 17:26   ` Thomas Jarosch
  0 siblings, 1 reply; 7+ messages in thread
From: Christoph Lameter @ 2011-10-14 17:16 UTC (permalink / raw)
  To: Thomas Jarosch; +Cc: linux-kernel, Pekka Enberg

On Fri, 14 Oct 2011, Thomas Jarosch wrote:

> index 868cc93..cc1a378 100644
> --- a/tools/slub/slabinfo.c
> +++ b/tools/slub/slabinfo.c
> @@ -1145,7 +1145,7 @@ static void read_slab_dir(void)
>  		switch (de->d_type) {
>  		   case DT_LNK:
>  			alias->name = strdup(de->d_name);
> -			count = readlink(de->d_name, buffer, sizeof(buffer));
> +			count = readlink(de->d_name, buffer, sizeof(buffer)-1);
>

readlink required that the buffer size be specified.

READLINK(2)                                              Linux Programmer's Manual                                              READLINK(2)

NAME
       readlink - read value of a symbolic link

SYNOPSIS
       #include <unistd.h>

       ssize_t readlink(const char *path, char *buf, size_t bufsiz);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       readlink(): _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L

DESCRIPTION
       readlink()  places  the  contents  of the symbolic link path in the buffer buf, which has size bufsiz.  readlink() does not append a
       null byte to buf.  It will truncate the contents (to a length of bufsiz characters), in case the buffer is too small to hold all  of
       the contents.



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

* Re: [slabinfo PATCH] Fix off-by-one after readlink() call
  2011-10-14 17:16 ` Christoph Lameter
@ 2011-10-14 17:26   ` Thomas Jarosch
  2011-10-14 17:57     ` Christoph Lameter
  2011-10-14 19:53     ` David Rientjes
  0 siblings, 2 replies; 7+ messages in thread
From: Thomas Jarosch @ 2011-10-14 17:26 UTC (permalink / raw)
  To: Christoph Lameter; +Cc: linux-kernel, Pekka Enberg

On 10/14/2011 07:16 PM, Christoph Lameter wrote:
>> index 868cc93..cc1a378 100644
>> --- a/tools/slub/slabinfo.c
>> +++ b/tools/slub/slabinfo.c
>> @@ -1145,7 +1145,7 @@ static void read_slab_dir(void)
>>  		switch (de->d_type) {
>>  		   case DT_LNK:
>>  			alias->name = strdup(de->d_name);
>> -			count = readlink(de->d_name, buffer, sizeof(buffer));
>> +			count = readlink(de->d_name, buffer, sizeof(buffer)-1);
>>
> 
> DESCRIPTION
>        readlink()  places  the  contents  of the symbolic link path in the buffer buf, which has size bufsiz.  readlink() does not append a
>        null byte to buf.  It will truncate the contents (to a length of bufsiz characters), in case the buffer is too small to hold all  of
>        the contents.

The problem is the line after the readlink() call:

buffer[count] = '\0';

The common technique is to reduce the buffer size by one.
Another fix would be to check

"
if (count < 0 || count == sizeof(buffer))
    fatal();
"

Reducing the buffer size by one is easier IMHO.

Cheers,
Thomas

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

* Re: [slabinfo PATCH] Fix off-by-one after readlink() call
  2011-10-14 17:26   ` Thomas Jarosch
@ 2011-10-14 17:57     ` Christoph Lameter
  2011-10-14 19:53     ` David Rientjes
  1 sibling, 0 replies; 7+ messages in thread
From: Christoph Lameter @ 2011-10-14 17:57 UTC (permalink / raw)
  To: Thomas Jarosch; +Cc: linux-kernel, Pekka Enberg

On Fri, 14 Oct 2011, Thomas Jarosch wrote:

> Reducing the buffer size by one is easier IMHO.

Ok.

Acked-by: Christoph Lameter <cl@gentwo.org>


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

* Re: [slabinfo PATCH] Fix off-by-one after readlink() call
  2011-10-14 17:26   ` Thomas Jarosch
  2011-10-14 17:57     ` Christoph Lameter
@ 2011-10-14 19:53     ` David Rientjes
  2011-10-17  7:16       ` Pekka Enberg
  1 sibling, 1 reply; 7+ messages in thread
From: David Rientjes @ 2011-10-14 19:53 UTC (permalink / raw)
  To: Thomas Jarosch; +Cc: Christoph Lameter, linux-kernel, Pekka Enberg

On Fri, 14 Oct 2011, Thomas Jarosch wrote:

> The problem is the line after the readlink() call:
> 
> buffer[count] = '\0';
> 
> The common technique is to reduce the buffer size by one.
> Another fix would be to check
> 
> "
> if (count < 0 || count == sizeof(buffer))
>     fatal();
> "
> 
> Reducing the buffer size by one is easier IMHO.
> 

I think this should be used for changelog.

Acked-by: David Rientjes <rientjes@google.com>

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

* Re: [slabinfo PATCH] Fix off-by-one after readlink() call
  2011-10-14 19:53     ` David Rientjes
@ 2011-10-17  7:16       ` Pekka Enberg
  2011-10-17 14:48         ` [slabinfo PATCH v2] Fix off-by-one buffer corruption " Thomas Jarosch
  0 siblings, 1 reply; 7+ messages in thread
From: Pekka Enberg @ 2011-10-17  7:16 UTC (permalink / raw)
  To: David Rientjes; +Cc: Thomas Jarosch, Christoph Lameter, linux-kernel

On Fri, Oct 14, 2011 at 10:53 PM, David Rientjes <rientjes@google.com> wrote:
> On Fri, 14 Oct 2011, Thomas Jarosch wrote:
>
>> The problem is the line after the readlink() call:
>>
>> buffer[count] = '\0';
>>
>> The common technique is to reduce the buffer size by one.
>> Another fix would be to check
>>
>> "
>> if (count < 0 || count == sizeof(buffer))
>>     fatal();
>> "
>>
>> Reducing the buffer size by one is easier IMHO.
>>
>
> I think this should be used for changelog.
>
> Acked-by: David Rientjes <rientjes@google.com>

Thomas, please resend with proper changelog and with David's and
Christoph's ACKs included.

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

* [slabinfo PATCH v2] Fix off-by-one buffer corruption after readlink() call
  2011-10-17  7:16       ` Pekka Enberg
@ 2011-10-17 14:48         ` Thomas Jarosch
  0 siblings, 0 replies; 7+ messages in thread
From: Thomas Jarosch @ 2011-10-17 14:48 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: David Rientjes, Christoph Lameter, linux-kernel

readlink() never zero terminates the provided buffer.
Therefore we already do

    buffer[count] = 0;

This leads to an off-by-one buffer corruption as readlink()
might return the full size of the buffer.

The common technique is to reduce the buffer size by one.
Another fix would be to check

"
if (count < 0 || count == sizeof(buffer))
    fatal();
"

Reducing the buffer size by one is easier IMHO.

Signed-off-by: Thomas Jarosch <thomas.jarosch@intra2net.com>
Acked-by: David Rientjes <rientjes@google.com>
Acked-by: Christoph Lameter <cl@gentwo.org>
---
 tools/slub/slabinfo.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/tools/slub/slabinfo.c b/tools/slub/slabinfo.c
index 868cc93..cc1a378 100644
--- a/tools/slub/slabinfo.c
+++ b/tools/slub/slabinfo.c
@@ -1145,7 +1145,7 @@ static void read_slab_dir(void)
 		switch (de->d_type) {
 		   case DT_LNK:
 			alias->name = strdup(de->d_name);
-			count = readlink(de->d_name, buffer, sizeof(buffer));
+			count = readlink(de->d_name, buffer, sizeof(buffer)-1);
 
 			if (count < 0)
 				fatal("Cannot read symlink %s\n", de->d_name);
-- 
1.7.6.4

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

end of thread, other threads:[~2011-10-17 14:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-14 16:56 [slabinfo PATCH] Fix off-by-one after readlink() call Thomas Jarosch
2011-10-14 17:16 ` Christoph Lameter
2011-10-14 17:26   ` Thomas Jarosch
2011-10-14 17:57     ` Christoph Lameter
2011-10-14 19:53     ` David Rientjes
2011-10-17  7:16       ` Pekka Enberg
2011-10-17 14:48         ` [slabinfo PATCH v2] Fix off-by-one buffer corruption " Thomas Jarosch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox