Skip to contentSkip to conversation

net/ieee802154: fix uninit value bug in dgram_sendmsg

PATCH Superseded Haimin Zhang 3 participants 6 messages Aug 22, 2022 – Aug 30, 2022 1 message not in the archive

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/ieee802154: fix uninit value bug in dgram_sendmsg

There is uninit value bug in dgram_sendmsg function in net/ieee802154/socket.c when the length of valid data pointed by the msg->msg_name isn't verified.

This length is specified by msg->msg_namelen. Function ieee802154_addr_from_sa is called by dgram_sendmsg, which use msg->msg_name as struct sockaddr_ieee802154* and read it, that will eventually lead to uninit value read. So we should check the length of msg->msg_name is not less than sizeof(struct sockaddr_ieee802154) before entering the ieee802154_addr_from_sa.

---
 net/ieee802154/socket.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ieee802154/socket.c +4 -0
index 718fb77bb..efbe08590 100644
@@ -655,6 +655,10 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	if (msg->msg_name) {
 		DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
 				 daddr, msg->msg_name);
+		if (msg->msg_namelen < sizeof(*daddr)) {
+			err = -EINVAL;
+			goto out_skb;
+		}
 
 		ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
 	} else {
Signature · 1 line
2.27.0
maintainer
#1 lore raw

Hello.

Quoted 4 lines · Haimin Zhang

There is uninit value bug in dgram_sendmsg function in net/ieee802154/socket.c when the length of valid data pointed by the msg->msg_name isn't verified.

Quoted 8 lines

This length is specified by msg->msg_namelen. Function ieee802154_addr_from_sa is called by dgram_sendmsg, which use msg->msg_name as struct sockaddr_ieee802154* and read it, that will eventually lead to uninit value read. So we should check the length of msg->msg_name is not less than sizeof(struct sockaddr_ieee802154) before entering the ieee802154_addr_from_sa.

This patch has been applied to the wpan tree and will be part of the next pull request to net. Thanks!

Btw, I got a warning from the checkpatch script that your author and SOB email addresses do not match. Might be a good idea to fix this.
If you are having trouble to send patches through the company mail server there is always the option to use the gmail address for sending the mail and an internal From: header in the patch to fix up the author.

regards
Stefan Schmidt

#2 lore raw

Hi,

Quoted 15 lines · Stefan Schmidt

Hello.

Quoted 11 lines · Haimin Zhang

There is uninit value bug in dgram_sendmsg function in net/ieee802154/socket.c when the length of valid data pointed by the msg->msg_name isn't verified.

This length is specified by msg->msg_namelen. Function ieee802154_addr_from_sa is called by dgram_sendmsg, which use msg->msg_name as struct sockaddr_ieee802154* and read it, that will eventually lead to uninit value read. So we should check the length of msg->msg_name is not less than sizeof(struct sockaddr_ieee802154) before entering the ieee802154_addr_from_sa.

Quoted 5 lines
Quoted 1 line

This patch has been applied to the wpan tree and will be part of the next pull request to net. Thanks!

For me this patch is buggy or at least it is questionable how to deal with the size of ieee802154_addr_sa here.

There should be a helper to calculate the size which depends on the addr_type field. It is not required to send the last 6 bytes if addr_type is IEEE802154_ADDR_SHORT.
Nitpick is that we should check in the beginning of that function.

- Alex

A message that is not in the archive (sent off-list or not yet received): CAB2z9exhnzte0rpT9t6=VpFCm9x+zZdmr01UHFxqvYy8y9ifag@mail.gmail.com

#4 lore raw

Hi,

cc mailing lists again.

Quoted 3 lines · zhang haiming

If msg_namelen is too small like 1, the addr_type field will be unexpected. Meanwhile, check msg_namelen < sizeof(*daddr) is

Then check if space for addr_type is available, if not -EINVAL. If addr_type available, evaluate it, if it's unknown -EINVAL, the minimum length differs here if it's known.

Quoted 2 lines

necessary and enough as dgram_bind and dgram_connect did.

you probably found similar issues.

It is a nitpick and I see that the current behaviour is not correct here.

- Alex

maintainer
#5 lore raw
In reply to Alexander Aring

Hello Alex.

Quoted 19 lines · Alexander Aring

Hi,

Quoted 15 lines · Stefan Schmidt

Hello.

Quoted 11 lines · Haimin Zhang

There is uninit value bug in dgram_sendmsg function in net/ieee802154/socket.c when the length of valid data pointed by the msg->msg_name isn't verified.

This length is specified by msg->msg_namelen. Function ieee802154_addr_from_sa is called by dgram_sendmsg, which use msg->msg_name as struct sockaddr_ieee802154* and read it, that will eventually lead to uninit value read. So we should check the length of msg->msg_name is not less than sizeof(struct sockaddr_ieee802154) before entering the ieee802154_addr_from_sa.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This patch has been applied to the wpan tree and will be part of the next pull request to net. Thanks!

For me this patch is buggy or at least it is questionable how to deal with the size of ieee802154_addr_sa here.

You are right. I completely missed this. Thanks for spotting!

Quoted 4 lines

There should be a helper to calculate the size which depends on the addr_type field. It is not required to send the last 6 bytes if addr_type is IEEE802154_ADDR_SHORT.
Nitpick is that we should check in the beginning of that function.

Haimin, in ieee802154 we could have two different sizes for ieee802154_addr_sa depending on the addr_type. We have short and extended addresses.

Could you please rework this patch to take this into account as Alex suggested?

I reverted your original patch from my tree.

regards
Stefan Schmidt

#6 lore raw

Thanks to all.
I have sent patch v2 to fix this.

Quoted 51 lines · Stefan Schmidt

Hello Alex.

Quoted 19 lines · Alexander Aring

Hi,

Quoted 15 lines · Stefan Schmidt

Hello.

Quoted 11 lines · Haimin Zhang

There is uninit value bug in dgram_sendmsg function in net/ieee802154/socket.c when the length of valid data pointed by the msg->msg_name isn't verified.

This length is specified by msg->msg_namelen. Function ieee802154_addr_from_sa is called by dgram_sendmsg, which use msg->msg_name as struct sockaddr_ieee802154* and read it, that will eventually lead to uninit value read. So we should check the length of msg->msg_name is not less than sizeof(struct sockaddr_ieee802154) before entering the ieee802154_addr_from_sa.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This patch has been applied to the wpan tree and will be part of the next pull request to net. Thanks!

For me this patch is buggy or at least it is questionable how to deal with the size of ieee802154_addr_sa here.

You are right. I completely missed this. Thanks for spotting!

Quoted 4 lines

There should be a helper to calculate the size which depends on the addr_type field. It is not required to send the last 6 bytes if addr_type is IEEE802154_ADDR_SHORT.
Nitpick is that we should check in the beginning of that function.

Haimin, in ieee802154 we could have two different sizes for ieee802154_addr_sa depending on the addr_type. We have short and extended addresses.

Could you please rework this patch to take this into account as Alex suggested?

I reverted your original patch from my tree.

regards
Stefan Schmidt