Skip to contentSkip to conversation

Suggestion on "int len" sanity

DISCUSSION XIAO Gang 5 participants 5 messages Jun 1, 2005 – Jun 3, 2005

lore
Share & tools
lore

XIAO Gang

author
#0 lore raw

I would like to make a security suggestion.

There are many length variables in the kernel, locally declared as "len" or "length", either as "int", "unsigned int" or "size_t". However, declaring a length as "int" leads easily to an erroneous situation, as the author (or even a code checker) might make the implicite hypothesis that the length is positive, so that it is enough to make a sanity check of the kind

if (length > limit) ERROR;

which is not enough.

On the other hand, when a variable is named "len" or "length", it is usually used for length and never should go negative. So could I suggest that the declarations of these variables to be uniformized to "size_t", via a gradual but sysmatic cleanup?

Signature · 2 lines
XIAO Gang (~{P$8U~})                          [email protected]
         home page: pcmath126.unice.fr/xiao.html 
#1 lore raw
Quoted 10 lines · XIAO Gang

I would like to make a security suggestion.

There are many length variables in the kernel, locally declared as "len" or "length", either as "int", "unsigned int" or "size_t". However, declaring a length as "int" leads easily to an erroneous situation, as the author (or even a code checker) might make the implicite hypothesis that the length is positive, so that it is enough to make a sanity check of the kind

Quoted 8 lines

if (length > limit) ERROR;

which is not enough.

On the other hand, when a variable is named "len" or "length", it is usually used for length and never should go negative. So could I suggest that the declarations of these variables to be uniformized to "size_t", via a gradual but sysmatic cleanup?

Could be. Can you pick an example and send a patch for it?

Jörn

Signature · 2 lines
Sometimes, asking the right question is already the answer.
-- Unknown
#2 lore raw
In reply to XIAO Gang
Quoted 10 lines · XIAO Gang

I would like to make a security suggestion.

There are many length variables in the kernel, locally declared as "len" or "length", either as "int", "unsigned int" or "size_t". However, declaring a length as "int" leads easily to an erroneous situation, as the author (or even a code checker) might make the implicite hypothesis that the length is positive, so that it is enough to make a sanity check of the kind

Quoted 8 lines

if (length > limit) ERROR;

which is not enough.

On the other hand, when a variable is named "len" or "length", it is usually used for length and never should go negative. So could I suggest that the declarations of these variables to be uniformized to "size_t", via a gradual but sysmatic cleanup?

Probably true for most cases, but be careful of code which would use -1 to report some errors if such thing exists.

Willy

#3 lore raw
Quoted 14 lines · Willy Tarreau
Quoted 13 lines · XIAO Gang

I would like to make a security suggestion.

There are many length variables in the kernel, locally declared as "len" or "length", either as "int", "unsigned int" or "size_t". However, declaring a length as "int" leads easily to an erroneous situation, as the author (or even a code checker) might make the implicite hypothesis that the length is positive, so that it is enough to make a sanity check of the kind

if (length > limit) ERROR;

which is not enough.

Quoted 7 lines
Quoted 4 lines

On the other hand, when a variable is named "len" or "length", it is usually used for length and never should go negative. So could I suggest that the declarations of these variables to be uniformized to "size_t", via a gradual but sysmatic cleanup?

Probably true for most cases, but be careful of code which would use -1 to report some errors if such thing exists.

In that case, use ssize_t.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#4 lore raw

At Fri, 3 Jun 2005 11:42:23 +0200 (CEST),

Quoted 21 lines · Geert Uytterhoeven
Quoted 19 lines · Willy Tarreau
Quoted 17 lines · XIAO Gang

I would like to make a security suggestion.

There are many length variables in the kernel, locally declared as "len" or "length", either as "int", "unsigned int" or "size_t". However, declaring a length as "int" leads easily to an erroneous situation, as the author (or even a code checker) might make the implicite hypothesis that the length is positive, so that it is enough to make a sanity check of the kind

if (length > limit) ERROR;

which is not enough.

On the other hand, when a variable is named "len" or "length", it is usually used for length and never should go negative. So could I suggest that the declarations of these variables to be uniformized to "size_t", via a gradual but sysmatic cleanup?

Quoted 4 lines
Quoted 2 lines

Probably true for most cases, but be careful of code which would use -1 to report some errors if such thing exists.

In that case, use ssize_t.

In some cases, we may want to avoid [s]size_t because it varies on 32 and 64bit archs (e.g. ioctl parameters)...

Takashi