Re: [PATCH] netfilter: xt_hashlimit: Fix integer divide round to zero.

From: Alban Browaeys
Date: Mon Feb 06 2017 - 09:28:26 EST


Le lundi 06 fÃvrier 2017 Ã 14:04 +0100, Pablo Neira Ayuso a ÃcritÂ:
> On Sat, Feb 04, 2017 at 11:47:31PM +0100, Alban Browaeys wrote:
> > diff --git a/net/netfilter/xt_hashlimit.c
> > b/net/netfilter/xt_hashlimit.c
> > index 10063408141d..df75ad643eef 100644
> > --- a/net/netfilter/xt_hashlimit.c
> > +++ b/net/netfilter/xt_hashlimit.c
> > @@ -463,21 +463,19 @@ static u32 xt_hashlimit_len_to_chunks(u32 len)
> > Â/* Precision saver. */
> > Âstatic u64 user2credits(u64 user, int revision)
> > Â{
> > > > + /* Avoid overflow: divide the constant operands first */
> > > > Â if (revision == 1) {
> > > > - /* If multiplying would overflow... */
> > > > - if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY_v1))
> > > > - /* Divide first. */
> > > > - return div64_u64(user, XT_HASHLIMIT_SCALE)
> > > > - * HZ * CREDITS_PER_JIFFY_v1;
> > > > + return div64_u64(user, div64_u64(XT_HASHLIMIT_SCALE,
> > > > + HZ * CREDITS_PER_JIFFY_v1));
> > Â
> > > > - return div64_u64(user * HZ * CREDITS_PER_JIFFY_v1,
> > > > + return user * div64_u64(HZ * CREDITS_PER_JIFFY_v1,
> > Â ÂXT_HASHLIMIT_SCALE);
>
> I see two return statements here, the one coming later is
> shadowed, this can't be right.

I fixed revision 2 case then copy the code to revision 1
and lost the condition in the process.
The code duplication is useless. I will rework it in v2.

Thank you for the review.