Skip to contentSkip to conversation

security: remove BUG_ON in security_skb_classify_flow

PATCH In review Jiayuan Chen 3 participants 5 messages Apr 8 – Apr 13 Security (LSM)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 security: remove BUG_ON in security_skb_classify_flow

A BPF program attached to the xfrm_decode_session hook can return a non-zero value, which causes BUG_ON(rc) in security_skb_classify_flow() to trigger a kernel panic.

Remove the BUG_ON and change the return type from void to int, so that callers can optionally handle the error.

---
 include/linux/security.h |  7 ++++---
 security/security.c      | 16 +++++++++++-----
 2 files changed, 15 insertions(+), 8 deletions(-)
include/linux/security.h +4 -3
index ee88dd2d2d1f..6d210dc4c649 100644
@@ -1975,7 +1975,7 @@ int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
 				       struct xfrm_policy *xp,
 				       const struct flowi_common *flic);
 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid);
-void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic);
+int security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic);
 
 #else	/* CONFIG_SECURITY_NETWORK_XFRM */
 
@@ -2038,9 +2038,10 @@ static inline int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
 	return 0;
 }
 
-static inline void security_skb_classify_flow(struct sk_buff *skb,
-					      struct flowi_common *flic)
+static inline int security_skb_classify_flow(struct sk_buff *skb,
+					     struct flowi_common *flic)
 {
+	return 0;
 }
 
 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
security/security.c +11 -5
index a26c1474e2e4..26a34eb363c2 100644
@@ -4990,12 +4990,18 @@ int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
 	return call_int_hook(xfrm_decode_session, skb, secid, 1);
 }
 
-void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
+/**
+ * security_skb_classify_flow() - Set the flow's secid from the security label
+ * @skb: packet
+ * @flic: flow common structure to set
+ *
+ * Decode the packet in @skb and set the flow's secid in @flic.
+ *
+ * Return: Return 0 if successful.
+ */
+int security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
 {
-	int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid,
-			       0);
-
-	BUG_ON(rc);
+	return call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid, 0);
 }
 EXPORT_SYMBOL(security_skb_classify_flow);
 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 3 lines · Jiayuan Chen

A BPF program attached to the xfrm_decode_session hook can return a non-zero value, which causes BUG_ON(rc) in security_skb_classify_flow() to trigger a kernel panic.

It would seem worth it to have pointed at the previous discussion at

https://lore.kernel.org/all/CAEjxPJ5aA01in+Z1yLF1cwe-3uqL_E8SKGK4J294D5eRG5__5Q@mail.gmail.com/

Based on that, I guess this is probably ok, but still,

Quoted 2 lines

Remove the BUG_ON and change the return type from void to int, so that callers can optionally handle the error.

but you don't have the existing callers handling the error. It's conceivable they won't care, but it's also possible that they were counting on a BUG_ON in that case.

What *should* callers (icmp_reply, etc) do if an error code is returned? Should they ignore it? In that case, would it be better to change security_skb_classify_flow() to return void?

Quoted 66 lines
---
 include/linux/security.h |  7 ++++---
 security/security.c      | 16 +++++++++++-----
 2 files changed, 15 insertions(+), 8 deletions(-)
include/linux/security.h +4 -3
index ee88dd2d2d1f..6d210dc4c649 100644
@@ -1975,7 +1975,7 @@ int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
 				       struct xfrm_policy *xp,
 				       const struct flowi_common *flic);
 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid);
-void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic);
+int security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic);
 
 #else	/* CONFIG_SECURITY_NETWORK_XFRM */
 
@@ -2038,9 +2038,10 @@ static inline int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
 	return 0;
 }
 
-static inline void security_skb_classify_flow(struct sk_buff *skb,
-					      struct flowi_common *flic)
+static inline int security_skb_classify_flow(struct sk_buff *skb,
+					     struct flowi_common *flic)
 {
+	return 0;
 }
 
 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
security/security.c +11 -5
index a26c1474e2e4..26a34eb363c2 100644
@@ -4990,12 +4990,18 @@ int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
 	return call_int_hook(xfrm_decode_session, skb, secid, 1);
 }
 
-void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
+/**
+ * security_skb_classify_flow() - Set the flow's secid from the security label
+ * @skb: packet
+ * @flic: flow common structure to set
+ *
+ * Decode the packet in @skb and set the flow's secid in @flic.
+ *
+ * Return: Return 0 if successful.
+ */
+int security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
 {
-	int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid,
-			       0);
-
-	BUG_ON(rc);
+	return call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid, 0);
 }
 EXPORT_SYMBOL(security_skb_classify_flow);
 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
Signature · 1 line
2.43.0
#2 lore raw
Quoted 12 lines · Serge E. Hallyn
Quoted 3 lines · Jiayuan Chen

A BPF program attached to the xfrm_decode_session hook can return a non-zero value, which causes BUG_ON(rc) in security_skb_classify_flow() to trigger a kernel panic.

It would seem worth it to have pointed at the previous discussion at

https://lore.kernel.org/all/CAEjxPJ5aA01in+Z1yLF1cwe-3uqL_E8SKGK4J294D5eRG5__5Q@mail.gmail.com/

Based on that, I guess this is probably ok, but still,

Quoted 2 lines

Remove the BUG_ON and change the return type from void to int, so that callers can optionally handle the error.

Quoted 8 lines

but you don't have the existing callers handling the error. It's conceivable they won't care, but it's also possible that they were counting on a BUG_ON in that case.

What *should* callers (icmp_reply, etc) do if an error code is returned? Should they ignore it? In that case, would it be better to change security_skb_classify_flow() to return void?

Thanks for your pointer.

So I think Feng's patch is sufficient and can by applied ?

maintainer
#3 lore raw
Quoted 18 lines · Jiayuan Chen
Quoted 16 lines · Serge E. Hallyn
Quoted 3 lines · Jiayuan Chen

A BPF program attached to the xfrm_decode_session hook can return a non-zero value, which causes BUG_ON(rc) in security_skb_classify_flow() to trigger a kernel panic.

It would seem worth it to have pointed at the previous discussion at

https://lore.kernel.org/all/CAEjxPJ5aA01in+Z1yLF1cwe-3uqL_E8SKGK4J294D5eRG5__5Q@mail.gmail.com/

Based on that, I guess this is probably ok, but still,

Quoted 2 lines

Remove the BUG_ON and change the return type from void to int, so that callers can optionally handle the error.

but you don't have the existing callers handling the error. It's conceivable they won't care, but it's also possible that they were counting on a BUG_ON in that case.

Quoted 7 lines
Quoted 4 lines

What *should* callers (icmp_reply, etc) do if an error code is returned? Should they ignore it? In that case, would it be better to change security_skb_classify_flow() to return void?

Thanks for your pointer.

So I think Feng's patch is sufficient and can by applied ?

Well, selinux_xfrm_decode_session() calls selinux_xfrm_skb_sid_ingress() which *can* return -EINVAL.

So I'd like to know, what is supposed to happen in that case?

Stephen, do you know? Is it safe for callers to ignore this?

#4 lore raw
Quoted 26 lines · Serge E. Hallyn
Quoted 24 lines · Jiayuan Chen
Quoted 12 lines · Serge E. Hallyn
Quoted 3 lines · Jiayuan Chen

A BPF program attached to the xfrm_decode_session hook can return a non-zero value, which causes BUG_ON(rc) in security_skb_classify_flow() to trigger a kernel panic.

It would seem worth it to have pointed at the previous discussion at

https://lore.kernel.org/all/CAEjxPJ5aA01in+Z1yLF1cwe-3uqL_E8SKGK4J294D5eRG5__5Q@mail.gmail.com/

Based on that, I guess this is probably ok, but still,

Quoted 2 lines

Remove the BUG_ON and change the return type from void to int, so that callers can optionally handle the error.

Quoted 8 lines

but you don't have the existing callers handling the error. It's conceivable they won't care, but it's also possible that they were counting on a BUG_ON in that case.

What *should* callers (icmp_reply, etc) do if an error code is returned? Should they ignore it? In that case, would it be better to change security_skb_classify_flow() to return void?

Thanks for your pointer.

Quoted 8 lines
Quoted 1 line

So I think Feng's patch is sufficient and can by applied ?

Well, selinux_xfrm_decode_session() calls selinux_xfrm_skb_sid_ingress() which *can* return -EINVAL.

So I'd like to know, what is supposed to happen in that case?

Stephen, do you know? Is it safe for callers to ignore this?

I'm in favor of dropping the BUG_ON() from security_skb_classify_flow() and just make it return void, ignoring any non-zero return from xfrm_decode_session.
A slightly cleaner approach would be to introduce a separate LSM hook for skb_classify_flow() that returns void rather than calling xfrm_decode_session() from security_skb_classify_flow(); then any bug handling can be done in the individual security module.