Skip to contentSkip to conversation

Bluetooth: Verify that l2cap_get_conf_opt provides large enough buffer

PATCH Applied Marcel Holtmann 3 participants 7 messages Jan 18, 2019 – Jan 23, 2019

Subsystems (2)
lore
Share & tools
lore

Marcel Holtmann

author maintainer
#0 lore raw

PATCH 1/1 Bluetooth: Verify that l2cap_get_conf_opt provides large enough buffer

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
 net/bluetooth/l2cap_core.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/bluetooth/l2cap_core.c +6 -0
index 77799e7d5a34..ccdc5c67d22a 100644
@@ -3337,6 +3337,8 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (len < 0)
+			break;
 
 		hint  = type & L2CAP_CONF_HINT;
 		type &= L2CAP_CONF_MASK;
@@ -3555,6 +3557,8 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
+		if (len < 0)
+			break;
 
 		switch (type) {
 		case L2CAP_CONF_MTU:
@@ -3740,6 +3744,8 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
+		if (len < 0)
+			break;
 
 		switch (type) {
 		case L2CAP_CONF_RFC:
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Marcel Holtmann

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
 net/bluetooth/l2cap_core.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/bluetooth/l2cap_core.c +0 -0
index 77799e7d5a34..ccdc5c67d22a 100644

net/bluetooth/l2cap_core.c static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data · line 3341

Quoted 5 lines
net/bluetooth/l2cap_core.c +2 -0
@@ -3337,6 +3337,8 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (len < 0)
+			break;

<snip>

Patch looks good to me, thanks for fixing this all up:

author maintainer
#2 lore raw

Hi Greg,

Quoted 28 lines
Quoted 28 lines

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
net/bluetooth/l2cap_core.c | 6 ++++++

1 file changed, 6 insertions(+)
net/bluetooth/l2cap_core.c +0 -0
index 77799e7d5a34..ccdc5c67d22a 100644
@@ -3337,6 +3337,8 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 
Quoted 8 lines

net/bluetooth/l2cap_core.c static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data · line 3341

Quoted 4 lines
net/bluetooth/l2cap_core.c +2 -0
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (len < 0)
+			break;

<snip>

Patch looks good to me, thanks for fixing this all up:

it would be still good if we can get this verified by the reporter.

Regards

Marcel

#3 lore raw
Quoted 37 lines · Marcel Holtmann

Hi Greg,

Quoted 35 lines
Quoted 32 lines

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
net/bluetooth/l2cap_core.c | 6 ++++++

1 file changed, 6 insertions(+)
net/bluetooth/l2cap_core.c +2 -0
index 77799e7d5a34..ccdc5c67d22a 100644
@@ -3337,6 +3337,8 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (len < 0)
+			break;

<snip>

Quoted 3 lines
Quoted 1 line

Patch looks good to me, thanks for fixing this all up:

it would be still good if we can get this verified by the reporter.

The "reporter" seems to have disappeared once they reported this stuff, so I would not count on them doing anything here, we asked numerous times :(

If the patches look correct, I recommend just merging it and I can backport it to the stable releases and the distros can pick it up from there.

thanks,

greg k-h

author maintainer
#4 lore raw

Hi Greg,

Quoted 39 lines
Quoted 35 lines
Quoted 35 lines
Quoted 32 lines

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
net/bluetooth/l2cap_core.c | 6 ++++++

1 file changed, 6 insertions(+)
net/bluetooth/l2cap_core.c +2 -0
index 77799e7d5a34..ccdc5c67d22a 100644
@@ -3337,6 +3337,8 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (len < 0)
+			break;

<snip>

Quoted 3 lines
Quoted 1 line

Patch looks good to me, thanks for fixing this all up:

it would be still good if we can get this verified by the reporter.

Quoted 7 lines

The "reporter" seems to have disappeared once they reported this stuff, so I would not count on them doing anything here, we asked numerous times :(

If the patches look correct, I recommend just merging it and I can backport it to the stable releases and the distros can pick it up from there.

I think that I have Johan merge them into bluetooth-next first and let them sit a little so we can verify they do not break anything else. You can pick them up later into -stable if nobody complained and we didn’t break qualification.

Regards

Marcel

#5 lore raw
Quoted 45 lines · Marcel Holtmann

Hi Greg,

Quoted 43 lines
Quoted 35 lines
Quoted 35 lines
Quoted 32 lines

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
net/bluetooth/l2cap_core.c | 6 ++++++

1 file changed, 6 insertions(+)
net/bluetooth/l2cap_core.c +2 -0
index 77799e7d5a34..ccdc5c67d22a 100644
@@ -3337,6 +3337,8 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data
 
 	while (len >= L2CAP_CONF_OPT_SIZE) {
 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
+		if (len < 0)
+			break;

<snip>

Quoted 3 lines
Quoted 1 line

Patch looks good to me, thanks for fixing this all up:

it would be still good if we can get this verified by the reporter.

The "reporter" seems to have disappeared once they reported this stuff, so I would not count on them doing anything here, we asked numerous times :(

Quoted 8 lines
Quoted 3 lines

If the patches look correct, I recommend just merging it and I can backport it to the stable releases and the distros can pick it up from there.

I think that I have Johan merge them into bluetooth-next first and let them sit a little so we can verify they do not break anything else. You can pick them up later into -stable if nobody complained and we didn’t break qualification.

Sounds good to me, thanks.

greg k-h

#6 lore raw
In reply to Marcel Holtmann

Hi Marcel,

Quoted 13 lines · Marcel Holtmann

The function l2cap_get_conf_opt will return L2CAP_CONF_OPT_SIZE + opt->len as length value. The opt->len however is in control over the remote user and can be used by an attacker to gain access beyond the bounds of the actual packet.

To prevent any potential leak of heap memory, it is enough to check that the resulting len calculation after calling l2cap_get_conf_opt is not below zero. A well formed packet will always return >= 0 here and will end with the length value being zero after the last option has been parsed. In case of malformed packets messing with the opt->len field the length value will become negative. If that is the case, then just abort and ignore the option.

Quoted 8 lines

In case an attacker uses a too short opt->len value, then garbage will be parsed, but that is protected by the unknown option handling and also the option parameter size checks.

---
 net/bluetooth/l2cap_core.c | 6 ++++++
 1 file changed, 6 insertions(+)

Applied to bluetooth-next. Thanks.

Johan