Skip to contentSkip to conversation

flow_dissector: avoid uninitialized variable access

PATCH In review Arnd Bergmann 5 participants 11 messages Oct 21, 2016 – Oct 24, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 flow_dissector: avoid uninitialized variable access

gcc warns about an uninitialized pointer dereference in the vlan priority handling:

net/core/flow_dissector.c: In function '__skb_flow_dissect':
net/core/flow_dissector.c:281:61: error: 'vlan' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 1 line

From all I can tell, this warning is about a real bug, and we

should not attempt look up the vlan header if there was no vlan tag.

Fixes f6a66927692e ("flow_dissector: Get vlan priority in addition to vlan id")

---
I'm not sure about this one, please have a closer look at what the original code does before applying.

---
 net/core/flow_dissector.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/flow_dissector.c +2 -2
index 44e6ba9d3a6b..dd6003bf27e1 100644
@@ -245,7 +245,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 	}
 	case htons(ETH_P_8021AD):
 	case htons(ETH_P_8021Q): {
-		const struct vlan_hdr *vlan;
+		const struct vlan_hdr *vlan = NULL;
 
 		if (skb && skb_vlan_tag_present(skb))
 			proto = skb->protocol;
@@ -264,7 +264,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 		}
 
 		skip_vlan = true;
-		if (dissector_uses_key(flow_dissector,
+		if (vlan && dissector_uses_key(flow_dissector,
 				       FLOW_DISSECTOR_KEY_VLAN)) {
 			key_vlan = skb_flow_dissector_target(flow_dissector,
 							     FLOW_DISSECTOR_KEY_VLAN,
Signature · 1 line
2.9.0
#1 lore raw
Quoted 9 lines · Fri, Oct 21, 2016 at 05:55:53PM CEST, [email protected] wrote:

gcc warns about an uninitialized pointer dereference in the vlan priority handling:

net/core/flow_dissector.c: In function '__skb_flow_dissect':
net/core/flow_dissector.c:281:61: error: 'vlan' may be used uninitialized in this function [-Werror=maybe-uninitialized]

From all I can tell, this warning is about a real bug, and we should not attempt look up the vlan header if there was no vlan tag.

I don't see how vlan could be used uninitialized. But I understand that this is impossible for gcc to track it. Please just use uninitialized_var()

Quoted 35 lines
Fixes f6a66927692e ("flow_dissector: Get vlan priority in addition to vlan id")

---
I'm not sure about this one, please have a closer look at what the original code does before applying.
---
net/core/flow_dissector.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
net/core/flow_dissector.c +2 -2
index 44e6ba9d3a6b..dd6003bf27e1 100644
@@ -245,7 +245,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 	}
 	case htons(ETH_P_8021AD):
 	case htons(ETH_P_8021Q): {
-		const struct vlan_hdr *vlan;
+		const struct vlan_hdr *vlan = NULL;
 
 		if (skb && skb_vlan_tag_present(skb))
 			proto = skb->protocol;
@@ -264,7 +264,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 		}
 
 		skip_vlan = true;
-		if (dissector_uses_key(flow_dissector,
+		if (vlan && dissector_uses_key(flow_dissector,
 				       FLOW_DISSECTOR_KEY_VLAN)) {
 			key_vlan = skb_flow_dissector_target(flow_dissector,
 							     FLOW_DISSECTOR_KEY_VLAN,
Signature · 1 line
2.9.0
#2 lore raw
Quoted 7 lines · CEST Jiri Pirko
Quoted 6 lines · Fri, Oct 21, 2016 at 05:55:53PM CEST, [email protected] wrote:

gcc warns about an uninitialized pointer dereference in the vlan priority handling:

net/core/flow_dissector.c: In function '__skb_flow_dissect':
net/core/flow_dissector.c:281:61: error: 'vlan' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 7 lines
Quoted 3 lines

From all I can tell, this warning is about a real bug, and we should not attempt look up the vlan header if there was no vlan tag.

I don't see how vlan could be used uninitialized. But I understand that this is impossible for gcc to track it. Please just use uninitialized_var()

I usually try to avoid uninitialized_var(), as making it obvious to the compiler why something is known tends to result in more readable source code and better object code.

Can you explain why "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN) && skb_vlan_tag_present(skb)" implies "eth_type_vlan(proto))"?

If I add uninitialized_var() here, I would at least put that in a comment here.

On a related note, I also don't see how "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN)" implies that skb is non-NULL. I guess this is related to the first one.

Arnd

#3 lore raw
Quoted 7 lines · CEST Arnd Bergmann

Can you explain why "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN) && skb_vlan_tag_present(skb)" implies "eth_type_vlan(proto))"?

If I add uninitialized_var() here, I would at least put that in a comment here.

Found it now myself: if skb_vlan_tag_present(skb), then we don't access 'vlan', otherwise we know it is initialized because eth_type_vlan(proto) has to be true.

Quoted 4 lines

On a related note, I also don't see how "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN)" implies that skb is non-NULL. I guess this is related to the first one.

I'm still unsure about this one.

I also found something else that is suspicious: 'vlan' points to the local _vlan variable, but that has gone out of scope by the time we access the pointer, which doesn't seem safe.

Arnd

#4 lore raw
Quoted 13 lines · Arnd Bergmann
Quoted 7 lines · CEST Arnd Bergmann

Can you explain why "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN) && skb_vlan_tag_present(skb)" implies "eth_type_vlan(proto))"?

If I add uninitialized_var() here, I would at least put that in a comment here.

Found it now myself: if skb_vlan_tag_present(skb), then we don't access 'vlan', otherwise we know it is initialized because eth_type_vlan(proto) has to be true.

Quoted 6 lines
Quoted 4 lines

On a related note, I also don't see how "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN)" implies that skb is non-NULL. I guess this is related to the first one.

I'm still unsure about this one.

Only skb_flow_dissect_flow_keys_buf() calls this function with skb == NULL. It uses flow_keys_buf_dissector_keys which does not specify FLOW_DISSECTOR_KEY_VLAN, so the if statement is false.

A similar assumption is made for FLOW_DISSECTOR_KEY_ETH_ADDRS higher up.

Quoted 3 lines

I also found something else that is suspicious: 'vlan' points to the local _vlan variable, but that has gone out of scope by the time we access the pointer, which doesn't seem safe.

I see no harm in moving _vlan to the same scope as vlan.

#5 lore raw
Quoted 19 lines · Eric Garver
Quoted 18 lines · Arnd Bergmann
Quoted 7 lines · CEST Arnd Bergmann

Can you explain why "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN) && skb_vlan_tag_present(skb)" implies "eth_type_vlan(proto))"?

If I add uninitialized_var() here, I would at least put that in a comment here.

Found it now myself: if skb_vlan_tag_present(skb), then we don't access 'vlan', otherwise we know it is initialized because eth_type_vlan(proto) has to be true.

Quoted 4 lines

On a related note, I also don't see how "dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_VLAN)" implies that skb is non-NULL. I guess this is related to the first one.

Quoted 8 lines
Quoted 1 line

I'm still unsure about this one.

Only skb_flow_dissect_flow_keys_buf() calls this function with skb == NULL. It uses flow_keys_buf_dissector_keys which does not specify FLOW_DISSECTOR_KEY_VLAN, so the if statement is false.

A similar assumption is made for FLOW_DISSECTOR_KEY_ETH_ADDRS higher up.

This is a serious problem. We can't rely on the callers to know which keys they are allowed to use to avoid crashing the kernel. We should fix those to check if skb is NULL, add a comment to the head of the function warning people to never assume skb is non-NULL, and also maybe add a degenerative check that both data argument and skb are not NULL.

Tom

Quoted 5 lines
Quoted 3 lines

I also found something else that is suspicious: 'vlan' points to the local _vlan variable, but that has gone out of scope by the time we access the pointer, which doesn't seem safe.

I see no harm in moving _vlan to the same scope as vlan.

#6 lore raw

PATCH 1/1 flow_dissector: fix vlan tag handling

gcc warns about an uninitialized pointer dereference in the vlan priority handling:

net/core/flow_dissector.c: In function '__skb_flow_dissect':
net/core/flow_dissector.c:281:61: error: 'vlan' may be used uninitialized in this function [-Werror=maybe-uninitialized]

As pointed out by Jiri Pirko, the variable is never actually used without being initialized first as the only way it end up uninitialized is with skb_vlan_tag_present(skb)==true, and that means it does not get accessed.

However, the warning hints at some related issues that I'm addressing here:

- the second check for the vlan tag is different from the first one that tests the skb for being NULL first, causing both the warning and a possible NULL pointer dereference that was not entirely fixed.
- The same patch that introduced the NULL pointer check dropped an earlier optimization that skipped the repeated check of the protocol type
- The local '_vlan' variable is referenced through the 'vlan' pointer but the variable has gone out of scope by the time that it is accessed, causing undefined behavior as the stack may have been overwritten.

Caching the result of the 'skb && skb_vlan_tag_present(skb)' check in a local variable allows the compiler to further optimize the later check. With those changes, the warning also disappears.

Fixes 3805a938a6c2 ("flow_dissector: Check skb for VLAN only if skb specified.")

---

net/core/flow_dissector.c +4 -7
index 44e6ba9d3a6b..17be1b66cc41 100644
@@ -246,13 +246,10 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 	case htons(ETH_P_8021AD):
 	case htons(ETH_P_8021Q): {
 		const struct vlan_hdr *vlan;
+		struct vlan_hdr _vlan;
+		bool vlan_tag_present = (skb && skb_vlan_tag_present(skb));
 
-		if (skb && skb_vlan_tag_present(skb))
-			proto = skb->protocol;
-
-		if (eth_type_vlan(proto)) {
-			struct vlan_hdr _vlan;
-
+		if (!vlan_tag_present || eth_type_vlan(skb->protocol)) {
 			vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
 						    data, hlen, &_vlan);
 			if (!vlan)
@@ -270,7 +267,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 							     FLOW_DISSECTOR_KEY_VLAN,
 							     target_container);
 
-			if (skb_vlan_tag_present(skb)) {
+			if (vlan_tag_present) {
 				key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
 				key_vlan->vlan_priority =
 					(skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
#7 lore raw

Re: [PATCH net-next] flow_dissector: fix vlan tag handling

Quoted 38 lines · Sat, Oct 22, 2016 at 10:30:08PM CEST, [email protected] wrote:

gcc warns about an uninitialized pointer dereference in the vlan priority handling:

net/core/flow_dissector.c: In function '__skb_flow_dissect':
net/core/flow_dissector.c:281:61: error: 'vlan' may be used uninitialized in this function [-Werror=maybe-uninitialized]

As pointed out by Jiri Pirko, the variable is never actually used without being initialized first as the only way it end up uninitialized is with skb_vlan_tag_present(skb)==true, and that means it does not get accessed.

However, the warning hints at some related issues that I'm addressing here:

- the second check for the vlan tag is different from the first one that tests the skb for being NULL first, causing both the warning and a possible NULL pointer dereference that was not entirely fixed.
- The same patch that introduced the NULL pointer check dropped an earlier optimization that skipped the repeated check of the protocol type
- The local '_vlan' variable is referenced through the 'vlan' pointer but the variable has gone out of scope by the time that it is accessed, causing undefined behavior as the stack may have been overwritten.

Caching the result of the 'skb && skb_vlan_tag_present(skb)' check in a local variable allows the compiler to further optimize the later check. With those changes, the warning also disappears.

Fixes 3805a938a6c2 ("flow_dissector: Check skb for VLAN only if skb specified.")

---

net/core/flow_dissector.c +0 -0
index 44e6ba9d3a6b..17be1b66cc41 100644

net/core/flow_dissector.c bool __skb_flow_dissect(const struct sk_buff *skb, · line 250

Quoted 5 lines
net/core/flow_dissector.c +2 -0
@@ -246,13 +246,10 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 	case htons(ETH_P_8021AD):
 	case htons(ETH_P_8021Q): {
 		const struct vlan_hdr *vlan;
+		struct vlan_hdr _vlan;
+		bool vlan_tag_present = (skb && skb_vlan_tag_present(skb));

Drop the unnecessary "()"

net/core/flow_dissector.c bool __skb_flow_dissect(const struct sk_buff *skb, · line 251

Quoted 3 lines
net/core/flow_dissector.c +0 -2
-		if (skb && skb_vlan_tag_present(skb))
-			proto = skb->protocol;

This does not look correct. I believe that you need to set proto for further processing.

Quoted 18 lines
net/core/flow_dissector.c +2 -5
-
-		if (eth_type_vlan(proto)) {
-			struct vlan_hdr _vlan;
-
+		if (!vlan_tag_present || eth_type_vlan(skb->protocol)) {
 			vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
 						    data, hlen, &_vlan);
 			if (!vlan)
@@ -270,7 +267,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 							     FLOW_DISSECTOR_KEY_VLAN,
 							     target_container);
 
-			if (skb_vlan_tag_present(skb)) {
+			if (vlan_tag_present) {
 				key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
 				key_vlan->vlan_priority =
 					(skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
#8 lore raw

Re: [PATCH net-next] flow_dissector: fix vlan tag handling

Quoted 13 lines · CEST Jiri Pirko

net/core/flow_dissector.c bool __skb_flow_dissect(const struct sk_buff *skb, · line 250

Quoted 10 lines · Sat, Oct 22, 2016 at 10:30:08PM CEST, [email protected] wrote:
net/core/flow_dissector.c +2 -0
index 44e6ba9d3a6b..17be1b66cc41 100644
@@ -246,13 +246,10 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 	case htons(ETH_P_8021AD):
 	case htons(ETH_P_8021Q): {
 		const struct vlan_hdr *vlan;
+		struct vlan_hdr _vlan;
+		bool vlan_tag_present = (skb && skb_vlan_tag_present(skb));

Drop the unnecessary "()"

ok

Quoted 7 lines

net/core/flow_dissector.c bool __skb_flow_dissect(const struct sk_buff *skb, · line 251

Quoted 3 lines
net/core/flow_dissector.c +0 -2
-		if (skb && skb_vlan_tag_present(skb))
-			proto = skb->protocol;

This does not look correct. I believe that you need to set proto for further processing.

Ah, of course. I only looked at the usage in this 'case' statement, but the variable is also used after the 'goto again' and at the end of the function.

Arnd

#9 lore raw
In reply to Jiri Pirko
Quoted 3 lines · Jiri Pirko

I don't see how vlan could be used uninitialized. But I understand that this is impossible for gcc to track it. Please just use uninitialized_var()

Actually, I think we should never use "uninitialized_var()" except possibly for arrays or structures that gcc can complain about.

It's a horrible thing to use, in that it adds extra cruft to the source code, and then shuts up a compiler warning (even the _reliable_ warnings from gcc).

It's much better to just initialize the variable, and if gcc some day gets smarter and sees that it is unnecessary and always overwritten, so much the better. The cost of initializing a single word is basically zero.

Linus

#10 lore raw
Quoted 8 lines · Sat, Oct 22, 2016 at 03:48:48AM CEST, [email protected] wrote:
Quoted 3 lines · Jiri Pirko

I don't see how vlan could be used uninitialized. But I understand that this is impossible for gcc to track it. Please just use uninitialized_var()

Actually, I think we should never use "uninitialized_var()" except possibly for arrays or structures that gcc can complain about.

Quoted 8 lines

It's a horrible thing to use, in that it adds extra cruft to the source code, and then shuts up a compiler warning (even the _reliable_ warnings from gcc).

It's much better to just initialize the variable, and if gcc some day gets smarter and sees that it is unnecessary and always overwritten, so much the better. The cost of initializing a single word is basically zero.

On the other hand, I would agrue that initializing a var to "some" value that is never used might confuse the reader. He would naturally try to understand the reason for that exact value in initialization.