linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] genetlink: fix unsigned int comparison with less than zero
@ 2016-11-10 15:57 Colin King
  2016-11-10 17:11 ` Cong Wang
  2016-11-13 17:15 ` David Miller
  0 siblings, 2 replies; 7+ messages in thread
From: Colin King @ 2016-11-10 15:57 UTC (permalink / raw)
  To: David S . Miller, Johannes Berg, pravin shelar, Wei Yongjun,
	Florian Westphal, Tycho Andersen, WANG Cong, Tom Herbert, netdev
  Cc: linux-kernel

From: Colin Ian King <colin.king@canonical.com>

family->id is unsigned, so the less than zero check for
failure return from idr_alloc is never true and so the error exit
is never handled.  Instead, assign err and check if this is less
than zero since this is a signed integer.

Issue found with static analysis with CoverityScan, CID 1375916

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 net/netlink/genetlink.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index f0b65fe..2ea61ba 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -360,12 +360,10 @@ int genl_register_family(struct genl_family *family)
 	} else
 		family->attrbuf = NULL;
 
-	family->id = idr_alloc(&genl_fam_idr, family,
+	family->id = err = idr_alloc(&genl_fam_idr, family,
 			       start, end + 1, GFP_KERNEL);
-	if (family->id < 0) {
-		err = family->id;
+	if (err < 0)
 		goto errout_free;
-	}
 
 	err = genl_validate_assign_mc_groups(family);
 	if (err)
-- 
2.10.2

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

* Re: [PATCH] genetlink: fix unsigned int comparison with less than zero
  2016-11-10 15:57 [PATCH] genetlink: fix unsigned int comparison with less than zero Colin King
@ 2016-11-10 17:11 ` Cong Wang
  2016-11-12 21:37   ` Johannes Berg
  2016-11-13 17:15 ` David Miller
  1 sibling, 1 reply; 7+ messages in thread
From: Cong Wang @ 2016-11-10 17:11 UTC (permalink / raw)
  To: Colin King
  Cc: David S . Miller, Johannes Berg, pravin shelar, Wei Yongjun,
	Florian Westphal, Tycho Andersen, Tom Herbert,
	Linux Kernel Network Developers, LKML

On Thu, Nov 10, 2016 at 7:57 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> family->id is unsigned, so the less than zero check for
> failure return from idr_alloc is never true and so the error exit
> is never handled.  Instead, assign err and check if this is less
> than zero since this is a signed integer.

Why family->id can't be just signed int? For me it should be.

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

* Re: [PATCH] genetlink: fix unsigned int comparison with less than zero
  2016-11-10 17:11 ` Cong Wang
@ 2016-11-12 21:37   ` Johannes Berg
  2016-11-13  5:25     ` Cong Wang
  0 siblings, 1 reply; 7+ messages in thread
From: Johannes Berg @ 2016-11-12 21:37 UTC (permalink / raw)
  To: Cong Wang, Colin King
  Cc: David S . Miller, pravin shelar, Wei Yongjun, Florian Westphal,
	Tycho Andersen, Tom Herbert, Linux Kernel Network Developers,
	LKML

On Thu, 2016-11-10 at 09:11 -0800, Cong Wang wrote:
> On Thu, Nov 10, 2016 at 7:57 AM, Colin King <colin.king@canonical.com
> > wrote:
> > 
> > From: Colin Ian King <colin.king@canonical.com>
> > 
> > family->id is unsigned, so the less than zero check for
> > failure return from idr_alloc is never true and so the error exit
> > is never handled.  Instead, assign err and check if this is less
> > than zero since this is a signed integer.
> 
> Why family->id can't be just signed int? For me it should be.

I suppose it could be, since family IDs are allocated in a 16-bit range
anyway. But family IDs can also never actually be negative, so having
an unsigned int in the struct makes sense too.

I tend to think this patch is fine.

johannes

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

* Re: [PATCH] genetlink: fix unsigned int comparison with less than zero
  2016-11-12 21:37   ` Johannes Berg
@ 2016-11-13  5:25     ` Cong Wang
  2016-11-13  7:31       ` Johannes Berg
  0 siblings, 1 reply; 7+ messages in thread
From: Cong Wang @ 2016-11-13  5:25 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Colin King, David S . Miller, pravin shelar, Wei Yongjun,
	Florian Westphal, Tycho Andersen, Tom Herbert,
	Linux Kernel Network Developers, LKML

On Sat, Nov 12, 2016 at 1:37 PM, Johannes Berg
<johannes@sipsolutions.net> wrote:
> On Thu, 2016-11-10 at 09:11 -0800, Cong Wang wrote:
>> On Thu, Nov 10, 2016 at 7:57 AM, Colin King <colin.king@canonical.com
>> > wrote:
>> >
>> > From: Colin Ian King <colin.king@canonical.com>
>> >
>> > family->id is unsigned, so the less than zero check for
>> > failure return from idr_alloc is never true and so the error exit
>> > is never handled.  Instead, assign err and check if this is less
>> > than zero since this is a signed integer.
>>
>> Why family->id can't be just signed int? For me it should be.
>
> I suppose it could be, since family IDs are allocated in a 16-bit range
> anyway. But family IDs can also never actually be negative, so having
> an unsigned int in the struct makes sense too.

All idr_* API's accept int, rather than unsigned int. This is my point.

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

* Re: [PATCH] genetlink: fix unsigned int comparison with less than zero
  2016-11-13  5:25     ` Cong Wang
@ 2016-11-13  7:31       ` Johannes Berg
  0 siblings, 0 replies; 7+ messages in thread
From: Johannes Berg @ 2016-11-13  7:31 UTC (permalink / raw)
  To: Cong Wang
  Cc: Colin King, David S . Miller, pravin shelar, Wei Yongjun,
	Florian Westphal, Tycho Andersen, Tom Herbert,
	Linux Kernel Network Developers, LKML


> > I suppose it could be, since family IDs are allocated in a 16-bit
> > range
> > anyway. But family IDs can also never actually be negative, so
> > having
> > an unsigned int in the struct makes sense too.
> 
> All idr_* API's accept int, rather than unsigned int. This is my
> point.

Sure, but that's an internal implementation detail. The struct
genl_family is also an external API towards its users, and there
negative numbers make no sense whatsoever.

johannes

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

* Re: [PATCH] genetlink: fix unsigned int comparison with less than zero
  2016-11-10 15:57 [PATCH] genetlink: fix unsigned int comparison with less than zero Colin King
  2016-11-10 17:11 ` Cong Wang
@ 2016-11-13 17:15 ` David Miller
  2016-11-14  6:29   ` Cong Wang
  1 sibling, 1 reply; 7+ messages in thread
From: David Miller @ 2016-11-13 17:15 UTC (permalink / raw)
  To: colin.king
  Cc: johannes.berg, pshelar, weiyongjun1, fw, tycho.andersen,
	xiyou.wangcong, tom, netdev, linux-kernel

From: Colin King <colin.king@canonical.com>
Date: Thu, 10 Nov 2016 15:57:58 +0000

> From: Colin Ian King <colin.king@canonical.com>
> 
> family->id is unsigned, so the less than zero check for
> failure return from idr_alloc is never true and so the error exit
> is never handled.  Instead, assign err and check if this is less
> than zero since this is a signed integer.
> 
> Issue found with static analysis with CoverityScan, CID 1375916
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  net/netlink/genetlink.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
> index f0b65fe..2ea61ba 100644
> --- a/net/netlink/genetlink.c
> +++ b/net/netlink/genetlink.c
> @@ -360,12 +360,10 @@ int genl_register_family(struct genl_family *family)
>  	} else
>  		family->attrbuf = NULL;
>  
> -	family->id = idr_alloc(&genl_fam_idr, family,
> +	family->id = err = idr_alloc(&genl_fam_idr, family,
>  			       start, end + 1, GFP_KERNEL);

First of all, if we make this change you must fixup the indentation of
the second l ine of this idr_alloc() call.  Arguments spanning
multiple lines of a call must be indented precisely to the column
following the openning parenthesis of the first line.

Next, the IDR helpers never give us values that fall within the
positive range of an integer that does not fall within the postive
range of an unsigned integer.

We are going to pass this value in later to release the ID, again
the interface will expect a signed rather than an unsigned int.

Therefore is makes sesne only to change the family->id type to
what it must be, which is a signed int.

I've commited the following to net-next:

====================
[PATCH] genetlink: Make family a signed integer.

The idr_alloc(), idr_remove(), et al. routines all expect IDs to be
signed integers.  Therefore make the genl_family member 'id' signed
too.

Signed-off-by: David S. Miller <davem@davemloft.net>
---
 include/net/genetlink.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/net/genetlink.h b/include/net/genetlink.h
index 3ec87ba..a34275b 100644
--- a/include/net/genetlink.h
+++ b/include/net/genetlink.h
@@ -48,7 +48,7 @@ struct genl_info;
  * @n_ops: number of operations supported by this family
  */
 struct genl_family {
-	unsigned int		id;		/* private */
+	int			id;		/* private */
 	unsigned int		hdrsize;
 	char			name[GENL_NAMSIZ];
 	unsigned int		version;
-- 
2.7.4

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

* Re: [PATCH] genetlink: fix unsigned int comparison with less than zero
  2016-11-13 17:15 ` David Miller
@ 2016-11-14  6:29   ` Cong Wang
  0 siblings, 0 replies; 7+ messages in thread
From: Cong Wang @ 2016-11-14  6:29 UTC (permalink / raw)
  To: David Miller
  Cc: Colin King, Johannes Berg, pravin shelar, Wei Yongjun,
	Florian Westphal, Tycho Andersen, Tom Herbert,
	Linux Kernel Network Developers, LKML

On Sun, Nov 13, 2016 at 9:15 AM, David Miller <davem@davemloft.net> wrote:
> I've commited the following to net-next:
>
> ====================
> [PATCH] genetlink: Make family a signed integer.
>
> The idr_alloc(), idr_remove(), et al. routines all expect IDs to be
> signed integers.  Therefore make the genl_family member 'id' signed
> too.

This is exactly what I replied to Johannes.

Thanks for the fix!

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

end of thread, other threads:[~2016-11-14  6:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-10 15:57 [PATCH] genetlink: fix unsigned int comparison with less than zero Colin King
2016-11-10 17:11 ` Cong Wang
2016-11-12 21:37   ` Johannes Berg
2016-11-13  5:25     ` Cong Wang
2016-11-13  7:31       ` Johannes Berg
2016-11-13 17:15 ` David Miller
2016-11-14  6:29   ` Cong Wang

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).