diff mbox series

[1/2] Bluetooth: check message types in l2cap_get_conf_opt

Message ID 20190110062833.GA15047@kroah.com
State Awaiting Upstream
Delegated to: David Miller
Headers show
Series [1/2] Bluetooth: check message types in l2cap_get_conf_opt | expand

Commit Message

Greg KH Jan. 10, 2019, 6:28 a.m. UTC
l2cap_get_conf_opt can handle a "default" message type, but it needs to
be verified that it really is the correct type (CONF_EFS or CONF_RFC)
before passing it back to the caller.  To do this we need to check the
return value of this call now and handle the error correctly up the
stack.

Based on a patch from Ran Menscher.

Reported-by: Ran Menscher <ran.menscher@karambasecurity.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 net/bluetooth/l2cap_core.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

Comments

Joe Perches Jan. 10, 2019, 9:02 p.m. UTC | #1
On Thu, 2019-01-10 at 07:28 +0100, Greg Kroah-Hartman wrote:
> l2cap_get_conf_opt can handle a "default" message type, but it needs to
> be verified that it really is the correct type (CONF_EFS or CONF_RFC)
> before passing it back to the caller.  To do this we need to check the
> return value of this call now and handle the error correctly up the
> stack.
[]
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
[]
> @@ -3324,7 +3328,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
>  	void *endptr = data + data_size;
>  	void *req = chan->conf_req;
>  	int len = chan->conf_len;
> -	int type, hint, olen;
> +	int type, hint, olen, err;

err doesn't seem the right name for any of these as the
return is now negative only when there is an error.

Maybe opt_len instead.

>  	unsigned long val;
>  	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
>  	struct l2cap_conf_efs efs;
> @@ -3336,7 +3340,10 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
>  	BT_DBG("chan %p", chan);
>  
>  	while (len >= L2CAP_CONF_OPT_SIZE) {
> -		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
> +		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
> +		if (err < 0)
> +			return err;
> +		len -= err;

especially as you subtract the positive return not
an error value.

>  
>  		hint  = type & L2CAP_CONF_HINT;
>  		type &= L2CAP_CONF_MASK;
> @@ -3539,7 +3546,7 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
>  	struct l2cap_conf_req *req = data;
>  	void *ptr = req->data;
>  	void *endptr = data + size;
> -	int type, olen;
> +	int type, olen, err;
>  	unsigned long val;
>  	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
>  	struct l2cap_conf_efs efs;
> @@ -3547,7 +3554,10 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
>  	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
>  
>  	while (len >= L2CAP_CONF_OPT_SIZE) {
> -		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		err = l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		if (err < 0)
> +			return err;
> +		len -= err;
>  
>  		switch (type) {
>  		case L2CAP_CONF_MTU:
> @@ -3707,7 +3717,7 @@ void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
>  
>  static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
>  {
> -	int type, olen;
> +	int type, olen, err;
>  	unsigned long val;
>  	/* Use sane default values in case a misbehaving remote device
>  	 * did not send an RFC or extended window size option.
> @@ -3727,7 +3737,10 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
>  		return;
>  
>  	while (len >= L2CAP_CONF_OPT_SIZE) {
> -		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		err = l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		if (err < 0)
> +			return;
> +		len -= err;
>  
>  		switch (type) {
>  		case L2CAP_CONF_RFC:
Greg KH Jan. 11, 2019, 5:32 a.m. UTC | #2
On Thu, Jan 10, 2019 at 01:02:09PM -0800, Joe Perches wrote:
> On Thu, 2019-01-10 at 07:28 +0100, Greg Kroah-Hartman wrote:
> > l2cap_get_conf_opt can handle a "default" message type, but it needs to
> > be verified that it really is the correct type (CONF_EFS or CONF_RFC)
> > before passing it back to the caller.  To do this we need to check the
> > return value of this call now and handle the error correctly up the
> > stack.
> []
> > diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> []
> > @@ -3324,7 +3328,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
> >  	void *endptr = data + data_size;
> >  	void *req = chan->conf_req;
> >  	int len = chan->conf_len;
> > -	int type, hint, olen;
> > +	int type, hint, olen, err;
> 
> err doesn't seem the right name for any of these as the
> return is now negative only when there is an error.
> 
> Maybe opt_len instead.

I was copying the style that was used in the rest of the file.  If the
maintainers want me to use a different name, I'll be glad to do so.  My
personal preference is just 'ret'.

> >  	unsigned long val;
> >  	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
> >  	struct l2cap_conf_efs efs;
> > @@ -3336,7 +3340,10 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
> >  	BT_DBG("chan %p", chan);
> >  
> >  	while (len >= L2CAP_CONF_OPT_SIZE) {
> > -		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
> > +		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
> > +		if (err < 0)
> > +			return err;
> > +		len -= err;
> 
> especially as you subtract the positive return not
> an error value.

True, 'ret' would be nicer, but again, I was trying to follow the file's
style.

thanks,

greg k-h
Marcel Holtmann Jan. 18, 2019, 9:35 a.m. UTC | #3
Hi Greg,

> l2cap_get_conf_opt can handle a "default" message type, but it needs to
> be verified that it really is the correct type (CONF_EFS or CONF_RFC)
> before passing it back to the caller.  To do this we need to check the
> return value of this call now and handle the error correctly up the
> stack.
> 
> Based on a patch from Ran Menscher.
> 
> Reported-by: Ran Menscher <ran.menscher@karambasecurity.com>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
> net/bluetooth/l2cap_core.c | 25 +++++++++++++++++++------
> 1 file changed, 19 insertions(+), 6 deletions(-)
> 
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 2a7fb517d460..93daf94565cf 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -2980,6 +2980,10 @@ static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
> 		break;
> 
> 	default:
> +		/* Only CONF_EFS and CONF_RFC are allowed here */
> +		if ((opt->type != L2CAP_CONF_EFS) &&
> +		    (opt->type != L2CAP_CONF_RFC))
> +			return -EPROTO;

after re-reading that specification, this also includes CONF_QOS since that is a multi-field variable as well. Even if we currently don’t act on that field, we need to accept it being send.

> 		*val = (unsigned long) opt->val;
> 		break;
> 	}
> @@ -3324,7 +3328,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
> 	void *endptr = data + data_size;
> 	void *req = chan->conf_req;
> 	int len = chan->conf_len;
> -	int type, hint, olen;
> +	int type, hint, olen, err;
> 	unsigned long val;
> 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
> 	struct l2cap_conf_efs efs;
> @@ -3336,7 +3340,10 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
> 	BT_DBG("chan %p", chan);
> 
> 	while (len >= L2CAP_CONF_OPT_SIZE) {
> -		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
> +		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
> +		if (err < 0)
> +			return err;
> +		len -= err;

We need to handle not yet known options correctly since otherwise we are breaking forwards compatibility if newer specifications introduce new parameters. So just returning with an error here is not acceptable. It will fail qualification test cases.

Don’t we rather have proper length checks in l2cap_parse_conf_{req,rsp} instead of doing this. I think your second patch is enough.

> 
> 		hint  = type & L2CAP_CONF_HINT;
> 		type &= L2CAP_CONF_MASK;
> @@ -3539,7 +3546,7 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
> 	struct l2cap_conf_req *req = data;
> 	void *ptr = req->data;
> 	void *endptr = data + size;
> -	int type, olen;
> +	int type, olen, err;
> 	unsigned long val;
> 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
> 	struct l2cap_conf_efs efs;
> @@ -3547,7 +3554,10 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
> 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
> 
> 	while (len >= L2CAP_CONF_OPT_SIZE) {
> -		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		err = l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		if (err < 0)
> +			return err;
> +		len -= err;
> 
> 		switch (type) {
> 		case L2CAP_CONF_MTU:
> @@ -3707,7 +3717,7 @@ void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
> 
> static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
> {
> -	int type, olen;
> +	int type, olen, err;
> 	unsigned long val;
> 	/* Use sane default values in case a misbehaving remote device
> 	 * did not send an RFC or extended window size option.
> @@ -3727,7 +3737,10 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
> 		return;
> 
> 	while (len >= L2CAP_CONF_OPT_SIZE) {
> -		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		err = l2cap_get_conf_opt(&rsp, &type, &olen, &val);
> +		if (err < 0)
> +			return;
> +		len -= err;
> 
> 		switch (type) {
> 		case L2CAP_CONF_RFC:

Regards

Marcel
Greg KH Jan. 18, 2019, 10:19 a.m. UTC | #4
On Fri, Jan 18, 2019 at 10:35:30AM +0100, Marcel Holtmann wrote:
> Hi Greg,
> 
> > l2cap_get_conf_opt can handle a "default" message type, but it needs to
> > be verified that it really is the correct type (CONF_EFS or CONF_RFC)
> > before passing it back to the caller.  To do this we need to check the
> > return value of this call now and handle the error correctly up the
> > stack.
> > 
> > Based on a patch from Ran Menscher.
> > 
> > Reported-by: Ran Menscher <ran.menscher@karambasecurity.com>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > ---
> > net/bluetooth/l2cap_core.c | 25 +++++++++++++++++++------
> > 1 file changed, 19 insertions(+), 6 deletions(-)
> > 
> > diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> > index 2a7fb517d460..93daf94565cf 100644
> > --- a/net/bluetooth/l2cap_core.c
> > +++ b/net/bluetooth/l2cap_core.c
> > @@ -2980,6 +2980,10 @@ static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
> > 		break;
> > 
> > 	default:
> > +		/* Only CONF_EFS and CONF_RFC are allowed here */
> > +		if ((opt->type != L2CAP_CONF_EFS) &&
> > +		    (opt->type != L2CAP_CONF_RFC))
> > +			return -EPROTO;
> 
> after re-reading that specification, this also includes CONF_QOS since that is a multi-field variable as well. Even if we currently don’t act on that field, we need to accept it being send.

/me hands you some \n characters...

Ok, will fix up.

> > 		*val = (unsigned long) opt->val;
> > 		break;
> > 	}
> > @@ -3324,7 +3328,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
> > 	void *endptr = data + data_size;
> > 	void *req = chan->conf_req;
> > 	int len = chan->conf_len;
> > -	int type, hint, olen;
> > +	int type, hint, olen, err;
> > 	unsigned long val;
> > 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
> > 	struct l2cap_conf_efs efs;
> > @@ -3336,7 +3340,10 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
> > 	BT_DBG("chan %p", chan);
> > 
> > 	while (len >= L2CAP_CONF_OPT_SIZE) {
> > -		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
> > +		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
> > +		if (err < 0)
> > +			return err;
> > +		len -= err;
> 
> We need to handle not yet known options correctly since otherwise we are breaking forwards compatibility if newer specifications introduce new parameters. So just returning with an error here is not acceptable. It will fail qualification test cases.

So what should we do here?  We can't keep going as the size is
incorrect.

> Don’t we rather have proper length checks in l2cap_parse_conf_{req,rsp} instead of doing this. I think your second patch is enough.

It is?  Ok, if that's all that is needed, that's fine with me.  I was
just taking the patch from the original submitter, I don't understand
the bluetooth protocol at all :)

thanks,

greg k-h
Marcel Holtmann Jan. 18, 2019, 11:09 a.m. UTC | #5
Hi Greg,

>>> l2cap_get_conf_opt can handle a "default" message type, but it needs to
>>> be verified that it really is the correct type (CONF_EFS or CONF_RFC)
>>> before passing it back to the caller.  To do this we need to check the
>>> return value of this call now and handle the error correctly up the
>>> stack.
>>> 
>>> Based on a patch from Ran Menscher.
>>> 
>>> Reported-by: Ran Menscher <ran.menscher@karambasecurity.com>
>>> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>> ---
>>> net/bluetooth/l2cap_core.c | 25 +++++++++++++++++++------
>>> 1 file changed, 19 insertions(+), 6 deletions(-)
>>> 
>>> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
>>> index 2a7fb517d460..93daf94565cf 100644
>>> --- a/net/bluetooth/l2cap_core.c
>>> +++ b/net/bluetooth/l2cap_core.c
>>> @@ -2980,6 +2980,10 @@ static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
>>> 		break;
>>> 
>>> 	default:
>>> +		/* Only CONF_EFS and CONF_RFC are allowed here */
>>> +		if ((opt->type != L2CAP_CONF_EFS) &&
>>> +		    (opt->type != L2CAP_CONF_RFC))
>>> +			return -EPROTO;
>> 
>> after re-reading that specification, this also includes CONF_QOS since that is a multi-field variable as well. Even if we currently don’t act on that field, we need to accept it being send.
> 
> /me hands you some \n characters...
> 
> Ok, will fix up.
> 
>>> 		*val = (unsigned long) opt->val;
>>> 		break;
>>> 	}
>>> @@ -3324,7 +3328,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
>>> 	void *endptr = data + data_size;
>>> 	void *req = chan->conf_req;
>>> 	int len = chan->conf_len;
>>> -	int type, hint, olen;
>>> +	int type, hint, olen, err;
>>> 	unsigned long val;
>>> 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
>>> 	struct l2cap_conf_efs efs;
>>> @@ -3336,7 +3340,10 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
>>> 	BT_DBG("chan %p", chan);
>>> 
>>> 	while (len >= L2CAP_CONF_OPT_SIZE) {
>>> -		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
>>> +		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
>>> +		if (err < 0)
>>> +			return err;
>>> +		len -= err;
>> 
>> We need to handle not yet known options correctly since otherwise we are breaking forwards compatibility if newer specifications introduce new parameters. So just returning with an error here is not acceptable. It will fail qualification test cases.
> 
> So what should we do here?  We can't keep going as the size is
> incorrect.
> 
>> Don’t we rather have proper length checks in l2cap_parse_conf_{req,rsp} instead of doing this. I think your second patch is enough.
> 
> It is?  Ok, if that's all that is needed, that's fine with me.  I was
> just taking the patch from the original submitter, I don't understand
> the bluetooth protocol at all :)

I need to get my brain back into the nasty details of that protocol. I know for sure that just aborting is violating the handling of unknown options and that will fail qualification.

Let me look at how they managed to trick us.

Regards

Marcel
Marcel Holtmann Jan. 18, 2019, 12:08 p.m. UTC | #6
Hi Greg,

>>>> l2cap_get_conf_opt can handle a "default" message type, but it needs to
>>>> be verified that it really is the correct type (CONF_EFS or CONF_RFC)
>>>> before passing it back to the caller.  To do this we need to check the
>>>> return value of this call now and handle the error correctly up the
>>>> stack.
>>>> 
>>>> Based on a patch from Ran Menscher.
>>>> 
>>>> Reported-by: Ran Menscher <ran.menscher@karambasecurity.com>
>>>> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>> ---
>>>> net/bluetooth/l2cap_core.c | 25 +++++++++++++++++++------
>>>> 1 file changed, 19 insertions(+), 6 deletions(-)
>>>> 
>>>> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
>>>> index 2a7fb517d460..93daf94565cf 100644
>>>> --- a/net/bluetooth/l2cap_core.c
>>>> +++ b/net/bluetooth/l2cap_core.c
>>>> @@ -2980,6 +2980,10 @@ static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
>>>> 		break;
>>>> 
>>>> 	default:
>>>> +		/* Only CONF_EFS and CONF_RFC are allowed here */
>>>> +		if ((opt->type != L2CAP_CONF_EFS) &&
>>>> +		    (opt->type != L2CAP_CONF_RFC))
>>>> +			return -EPROTO;
>>> 
>>> after re-reading that specification, this also includes CONF_QOS since that is a multi-field variable as well. Even if we currently don’t act on that field, we need to accept it being send.
>> 
>> /me hands you some \n characters...
>> 
>> Ok, will fix up.
>> 
>>>> 		*val = (unsigned long) opt->val;
>>>> 		break;
>>>> 	}
>>>> @@ -3324,7 +3328,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
>>>> 	void *endptr = data + data_size;
>>>> 	void *req = chan->conf_req;
>>>> 	int len = chan->conf_len;
>>>> -	int type, hint, olen;
>>>> +	int type, hint, olen, err;
>>>> 	unsigned long val;
>>>> 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
>>>> 	struct l2cap_conf_efs efs;
>>>> @@ -3336,7 +3340,10 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
>>>> 	BT_DBG("chan %p", chan);
>>>> 
>>>> 	while (len >= L2CAP_CONF_OPT_SIZE) {
>>>> -		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
>>>> +		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
>>>> +		if (err < 0)
>>>> +			return err;
>>>> +		len -= err;
>>> 
>>> We need to handle not yet known options correctly since otherwise we are breaking forwards compatibility if newer specifications introduce new parameters. So just returning with an error here is not acceptable. It will fail qualification test cases.
>> 
>> So what should we do here?  We can't keep going as the size is
>> incorrect.
>> 
>>> Don’t we rather have proper length checks in l2cap_parse_conf_{req,rsp} instead of doing this. I think your second patch is enough.
>> 
>> It is?  Ok, if that's all that is needed, that's fine with me.  I was
>> just taking the patch from the original submitter, I don't understand
>> the bluetooth protocol at all :)
> 
> I need to get my brain back into the nasty details of that protocol. I know for sure that just aborting is violating the handling of unknown options and that will fail qualification.
> 
> Let me look at how they managed to trick us.

actually this one was easy to understand. Just fixing was overly complicated by focusing on the default case and then punching in exceptions.

Since all option types are fixed size, I now added length checks to these. Patch has been posted.

Regards

Marcel
diff mbox series

Patch

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 2a7fb517d460..93daf94565cf 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -2980,6 +2980,10 @@  static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
 		break;
 
 	default:
+		/* Only CONF_EFS and CONF_RFC are allowed here */
+		if ((opt->type != L2CAP_CONF_EFS) &&
+		    (opt->type != L2CAP_CONF_RFC))
+			return -EPROTO;
 		*val = (unsigned long) opt->val;
 		break;
 	}
@@ -3324,7 +3328,7 @@  static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 	void *endptr = data + data_size;
 	void *req = chan->conf_req;
 	int len = chan->conf_len;
-	int type, hint, olen;
+	int type, hint, olen, err;
 	unsigned long val;
 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
 	struct l2cap_conf_efs efs;
@@ -3336,7 +3340,10 @@  static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 	BT_DBG("chan %p", chan);
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
-		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		err = l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (err < 0)
+			return err;
+		len -= err;
 
 		hint  = type & L2CAP_CONF_HINT;
 		type &= L2CAP_CONF_MASK;
@@ -3539,7 +3546,7 @@  static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
 	struct l2cap_conf_req *req = data;
 	void *ptr = req->data;
 	void *endptr = data + size;
-	int type, olen;
+	int type, olen, err;
 	unsigned long val;
 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
 	struct l2cap_conf_efs efs;
@@ -3547,7 +3554,10 @@  static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
-		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
+		err = l2cap_get_conf_opt(&rsp, &type, &olen, &val);
+		if (err < 0)
+			return err;
+		len -= err;
 
 		switch (type) {
 		case L2CAP_CONF_MTU:
@@ -3707,7 +3717,7 @@  void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
 
 static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
 {
-	int type, olen;
+	int type, olen, err;
 	unsigned long val;
 	/* Use sane default values in case a misbehaving remote device
 	 * did not send an RFC or extended window size option.
@@ -3727,7 +3737,10 @@  static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
 		return;
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
-		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
+		err = l2cap_get_conf_opt(&rsp, &type, &olen, &val);
+		if (err < 0)
+			return;
+		len -= err;
 
 		switch (type) {
 		case L2CAP_CONF_RFC: