diff mbox

net: sunrpc: svcauth_gss: use BUG_ON instead of if condition followed by BUG

Message ID 20171023181635.GA25334@embeddedor.com (mailing list archive)
State New, archived
Headers show

Commit Message

Gustavo A. R. Silva Oct. 23, 2017, 6:16 p.m. UTC
Use BUG_ON instead of if condition followed by BUG.

This issue was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
---
 net/sunrpc/auth_gss/svcauth_gss.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

Comments

J. Bruce Fields Oct. 23, 2017, 8:31 p.m. UTC | #1
In the past we've avoided BUG_ON(X) where X might have side effects, on
the theory that it should actually be OK just to compile out BUG_ON()s.
Has that changed?

In any case, I don't find that this improves readability; dropping.

--b.

On Mon, Oct 23, 2017 at 01:16:35PM -0500, Gustavo A. R. Silva wrote:
> Use BUG_ON instead of if condition followed by BUG.
> 
> This issue was detected with the help of Coccinelle.
> 
> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
> ---
>  net/sunrpc/auth_gss/svcauth_gss.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
> index 7b1ee5a..a10ce43 100644
> --- a/net/sunrpc/auth_gss/svcauth_gss.c
> +++ b/net/sunrpc/auth_gss/svcauth_gss.c
> @@ -855,11 +855,9 @@ unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct g
>  		return stat;
>  	if (integ_len > buf->len)
>  		return stat;
> -	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
> -		BUG();
> +	BUG_ON(xdr_buf_subsegment(buf, &integ_buf, 0, integ_len));
>  	/* copy out mic... */
> -	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
> -		BUG();
> +	BUG_ON(read_u32_from_xdr_buf(buf, integ_len, &mic.len));
>  	if (mic.len > RPC_MAX_AUTH_SIZE)
>  		return stat;
>  	mic.data = kmalloc(mic.len, GFP_KERNEL);
> @@ -1611,8 +1609,7 @@ svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
>  	BUG_ON(integ_len % 4);
>  	*p++ = htonl(integ_len);
>  	*p++ = htonl(gc->gc_seq);
> -	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len))
> -		BUG();
> +	BUG_ON(xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len));
>  	if (resbuf->tail[0].iov_base == NULL) {
>  		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
>  			goto out_err;
> -- 
> 2.7.4
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Weston Andros Adamson Oct. 24, 2017, 5:26 p.m. UTC | #2
Is there a reason to BUG() in these places? Couldn't we WARN_ON_ONCE and return an error?

-dros

> On Oct 23, 2017, at 4:31 PM, J. Bruce Fields <bfields@fieldses.org> wrote:
> 
> In the past we've avoided BUG_ON(X) where X might have side effects, on
> the theory that it should actually be OK just to compile out BUG_ON()s.
> Has that changed?
> 
> In any case, I don't find that this improves readability; dropping.
> 
> --b.
> 
> On Mon, Oct 23, 2017 at 01:16:35PM -0500, Gustavo A. R. Silva wrote:
>> Use BUG_ON instead of if condition followed by BUG.
>> 
>> This issue was detected with the help of Coccinelle.
>> 
>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>> ---
>> net/sunrpc/auth_gss/svcauth_gss.c | 9 +++------
>> 1 file changed, 3 insertions(+), 6 deletions(-)
>> 
>> diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
>> index 7b1ee5a..a10ce43 100644
>> --- a/net/sunrpc/auth_gss/svcauth_gss.c
>> +++ b/net/sunrpc/auth_gss/svcauth_gss.c
>> @@ -855,11 +855,9 @@ unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct g
>> 		return stat;
>> 	if (integ_len > buf->len)
>> 		return stat;
>> -	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
>> -		BUG();
>> +	BUG_ON(xdr_buf_subsegment(buf, &integ_buf, 0, integ_len));
>> 	/* copy out mic... */
>> -	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
>> -		BUG();
>> +	BUG_ON(read_u32_from_xdr_buf(buf, integ_len, &mic.len));
>> 	if (mic.len > RPC_MAX_AUTH_SIZE)
>> 		return stat;
>> 	mic.data = kmalloc(mic.len, GFP_KERNEL);
>> @@ -1611,8 +1609,7 @@ svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
>> 	BUG_ON(integ_len % 4);
>> 	*p++ = htonl(integ_len);
>> 	*p++ = htonl(gc->gc_seq);
>> -	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len))
>> -		BUG();
>> +	BUG_ON(xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len));
>> 	if (resbuf->tail[0].iov_base == NULL) {
>> 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
>> 			goto out_err;
>> -- 
>> 2.7.4
> --
> To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
J. Bruce Fields Oct. 24, 2017, 5:53 p.m. UTC | #3
On Tue, Oct 24, 2017 at 01:26:49PM -0400, Weston Andros Adamson wrote:
> Is there a reason to BUG() in these places? Couldn't we WARN_ON_ONCE and return an error?

I think the BUG() will just kill an nfsd thread that isn't holding any
interesting locks.

The failures look unlikely.  (Except for that read_u32... return, I
wonder if we're missing a check there.)

--b.

> 
> -dros
> 
> > On Oct 23, 2017, at 4:31 PM, J. Bruce Fields <bfields@fieldses.org> wrote:
> > 
> > In the past we've avoided BUG_ON(X) where X might have side effects, on
> > the theory that it should actually be OK just to compile out BUG_ON()s.
> > Has that changed?
> > 
> > In any case, I don't find that this improves readability; dropping.
> > 
> > --b.
> > 
> > On Mon, Oct 23, 2017 at 01:16:35PM -0500, Gustavo A. R. Silva wrote:
> >> Use BUG_ON instead of if condition followed by BUG.
> >> 
> >> This issue was detected with the help of Coccinelle.
> >> 
> >> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
> >> ---
> >> net/sunrpc/auth_gss/svcauth_gss.c | 9 +++------
> >> 1 file changed, 3 insertions(+), 6 deletions(-)
> >> 
> >> diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
> >> index 7b1ee5a..a10ce43 100644
> >> --- a/net/sunrpc/auth_gss/svcauth_gss.c
> >> +++ b/net/sunrpc/auth_gss/svcauth_gss.c
> >> @@ -855,11 +855,9 @@ unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct g
> >> 		return stat;
> >> 	if (integ_len > buf->len)
> >> 		return stat;
> >> -	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
> >> -		BUG();
> >> +	BUG_ON(xdr_buf_subsegment(buf, &integ_buf, 0, integ_len));
> >> 	/* copy out mic... */
> >> -	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
> >> -		BUG();
> >> +	BUG_ON(read_u32_from_xdr_buf(buf, integ_len, &mic.len));
> >> 	if (mic.len > RPC_MAX_AUTH_SIZE)
> >> 		return stat;
> >> 	mic.data = kmalloc(mic.len, GFP_KERNEL);
> >> @@ -1611,8 +1609,7 @@ svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
> >> 	BUG_ON(integ_len % 4);
> >> 	*p++ = htonl(integ_len);
> >> 	*p++ = htonl(gc->gc_seq);
> >> -	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len))
> >> -		BUG();
> >> +	BUG_ON(xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len));
> >> 	if (resbuf->tail[0].iov_base == NULL) {
> >> 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
> >> 			goto out_err;
> >> -- 
> >> 2.7.4
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jeff Layton Oct. 24, 2017, 6:18 p.m. UTC | #4
On Tue, 2017-10-24 at 13:53 -0400, J. Bruce Fields wrote:
> On Tue, Oct 24, 2017 at 01:26:49PM -0400, Weston Andros Adamson wrote:
> > Is there a reason to BUG() in these places? Couldn't we WARN_ON_ONCE and return an error?
> 
> I think the BUG() will just kill an nfsd thread that isn't holding any
> interesting locks.
> 

Not necessarily. If panic_on_oops is set (and it usually is in
"production" setups), it'll crash the box there.

> The failures look unlikely.  (Except for that read_u32... return, I
> wonder if we're missing a check there.)
> 

Agreed, looks like you only hit an error if the read attempts to go out
of bounds. In principle that shouldn't ever happen (and I haven't seen
any reports of it).

Still...I agree with Dros that it's better to handle this without
oopsing if we can. We can return an error from either of those
functions. A sane error and a WARN_ONCE would be better here.


> --b.
> 
> > 
> > -dros
> > 
> > > On Oct 23, 2017, at 4:31 PM, J. Bruce Fields <bfields@fieldses.org> wrote:
> > > 
> > > In the past we've avoided BUG_ON(X) where X might have side effects, on
> > > the theory that it should actually be OK just to compile out BUG_ON()s.
> > > Has that changed?
> > > 
> > > In any case, I don't find that this improves readability; dropping.
> > > 
> > > --b.
> > > 
> > > On Mon, Oct 23, 2017 at 01:16:35PM -0500, Gustavo A. R. Silva wrote:
> > > > Use BUG_ON instead of if condition followed by BUG.
> > > > 
> > > > This issue was detected with the help of Coccinelle.
> > > > 
> > > > Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
> > > > ---
> > > > net/sunrpc/auth_gss/svcauth_gss.c | 9 +++------
> > > > 1 file changed, 3 insertions(+), 6 deletions(-)
> > > > 
> > > > diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
> > > > index 7b1ee5a..a10ce43 100644
> > > > --- a/net/sunrpc/auth_gss/svcauth_gss.c
> > > > +++ b/net/sunrpc/auth_gss/svcauth_gss.c
> > > > @@ -855,11 +855,9 @@ unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct g
> > > > 		return stat;
> > > > 	if (integ_len > buf->len)
> > > > 		return stat;
> > > > -	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
> > > > -		BUG();
> > > > +	BUG_ON(xdr_buf_subsegment(buf, &integ_buf, 0, integ_len));
> > > > 	/* copy out mic... */
> > > > -	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
> > > > -		BUG();
> > > > +	BUG_ON(read_u32_from_xdr_buf(buf, integ_len, &mic.len));
> > > > 	if (mic.len > RPC_MAX_AUTH_SIZE)
> > > > 		return stat;
> > > > 	mic.data = kmalloc(mic.len, GFP_KERNEL);
> > > > @@ -1611,8 +1609,7 @@ svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
> > > > 	BUG_ON(integ_len % 4);
> > > > 	*p++ = htonl(integ_len);
> > > > 	*p++ = htonl(gc->gc_seq);
> > > > -	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len))
> > > > -		BUG();
> > > > +	BUG_ON(xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len));
> > > > 	if (resbuf->tail[0].iov_base == NULL) {
> > > > 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
> > > > 			goto out_err;
> > > > -- 
> > > > 2.7.4
> > > 
> > > --
> > > To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
> > > the body of a message to majordomo@vger.kernel.org
> > > More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
index 7b1ee5a..a10ce43 100644
--- a/net/sunrpc/auth_gss/svcauth_gss.c
+++ b/net/sunrpc/auth_gss/svcauth_gss.c
@@ -855,11 +855,9 @@  unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct g
 		return stat;
 	if (integ_len > buf->len)
 		return stat;
-	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
-		BUG();
+	BUG_ON(xdr_buf_subsegment(buf, &integ_buf, 0, integ_len));
 	/* copy out mic... */
-	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
-		BUG();
+	BUG_ON(read_u32_from_xdr_buf(buf, integ_len, &mic.len));
 	if (mic.len > RPC_MAX_AUTH_SIZE)
 		return stat;
 	mic.data = kmalloc(mic.len, GFP_KERNEL);
@@ -1611,8 +1609,7 @@  svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
 	BUG_ON(integ_len % 4);
 	*p++ = htonl(integ_len);
 	*p++ = htonl(gc->gc_seq);
-	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len))
-		BUG();
+	BUG_ON(xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len));
 	if (resbuf->tail[0].iov_base == NULL) {
 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
 			goto out_err;