net: esp: cleanup esp_output_tail_tcp() in case of unsupported ESPINTCP
Subsystems (3)
PATCH 1/1 net: esp: cleanup esp_output_tail_tcp() in case of unsupported ESPINTCP
xmit() functions should consume skb or return error codes in error paths.
When the configuration "CONFIG_INET_ESPINTCP" is not used, the implementation of the function "esp_output_tail_tcp" violates this rule. The function frees the skb and returns the error code.
This change removes the kfree_skb from both functions, for both esp4 and esp6.
This should not be reachable in the current code, so this change is just a cleanup.
This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.
--- net/ipv4/esp4.c | 3 +-- net/ipv6/esp6.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
net/ipv4/esp4.c +1 -2
@@ -238,8 +238,7 @@ static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)#else static int esp_output_tail_tcp (struct xfrm_state *x,struct sk_buff *skb) { -kfree_skb (skb); - +WARN_ON (1 );return -EOPNOTSUPP ; }#endif
net/ipv6/esp6.c +1 -2
@@ -255,8 +255,7 @@ static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)#else static int esp_output_tail_tcp (struct xfrm_state *x,struct sk_buff *skb) { -kfree_skb (skb); - +WARN_ON (1 );return -EOPNOTSUPP ; }#endif
Signature · 1 line
2.40.1
Simon Horman
reviewerQuoted 8 lines · Hagar Hemdan
xmit()functions should consume skb or return error codes in error paths.
When the configuration "CONFIG_INET_ESPINTCP" is not used, the implementation of the function "esp_output_tail_tcp" violates this rule. The function frees the skb and returns the error code.
This change removes thekfree_skbfrom both functions, for both esp4 and esp6.
Quoted 8 lines
This should not be reachable in the current code, so this change is just a cleanup.
This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.
Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")Signed-off-by Hagar Hemdan <[email protected]>
Hi Hagar,
If esp_output() may be the x->type->output callback called from esp_output() then I agree that this seems to be a problem as it looks like a double free may occur.
However, I believe that your proposed fix introduces will result in skb being leaked leak in the case of esp_output_done() calling esp_output_tail_tcp(). Perhaps a solution is for esp_output_done() to free the skb if esp_output_tail_tcp() fails.
I did not analyse other call-chains, but I think such analysis is needed.
...
Quoted 24 lines · Simon Horman
Quoted 8 lines · Hagar Hemdan
xmit()functions should consume skb or return error codes in error paths.
When the configuration "CONFIG_INET_ESPINTCP" is not used, the implementation of the function "esp_output_tail_tcp" violates this rule. The function frees the skb and returns the error code.
This change removes thekfree_skbfrom both functions, for both esp4 and esp6.Quoted 8 lines
This should not be reachable in the current code, so this change is just a cleanup.
This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.
Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")Signed-off-by Hagar Hemdan <[email protected]>Hi Hagar,
If
esp_output()may be thex->type->outputcallback called fromesp_output()then I agree that this seems to be a problem as it looks like a double free may occur.
Quoted 8 lines
However, I believe that your proposed fix introduces will result in skb being leaked leak in the case of
esp_output_done()callingesp_output_tail_tcp(). Perhaps a solution is foresp_output_done()to free the skb ifesp_output_tail_tcp()fails.I did not analyse other call-chains, but I think such analysis is needed.
...
Hi Simon,
I see all calls to esp_output_tail_tcp() is surrounded by the condition "x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP" which I see it is related to enabling of CONFIG_INET_ESPINTCP configuration (introduced in this commit e27cca96cd68 ("xfrm: add espintcp (RFC 8229)").
For calling of x->type->output (resolved to esp_output()) in xfrm_output_one(), I see there is no double free here as esp_output() calls esp_output_tail() which calls esp_output_tail_tcp() only if x->encap->encap_type == TCP_ENCAP_ESPINTCP which points to the first implementation of esp_output_tail_tcp(). This first definition doesn't free skb.
So my understanding is the 2nd esp_output_tail_tcp() should not be called and this is why I called WARN_ON() as this func is unreachable. Removing free(skb) here is just for silencing double free Coverity false positive.
Is there something else I miss?
Simon Horman
reviewerQuoted 16 lines · Hagar Hemdan
Quoted 15 lines · Simon Horman
Quoted 14 lines · Hagar Hemdan
xmit()functions should consume skb or return error codes in error paths.
When the configuration "CONFIG_INET_ESPINTCP" is not used, the implementation of the function "esp_output_tail_tcp" violates this rule. The function frees the skb and returns the error code.
This change removes thekfree_skbfrom both functions, for both esp4 and esp6.This should not be reachable in the current code, so this change is just a cleanup.
This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")Signed-off-by Hagar Hemdan <[email protected]>Hi Hagar,
If
esp_output()may be thex->type->outputcallback called fromesp_output()
Hi Hagar,
FTR, I meant to say "If ... called from xfrm_output_one()", but I don't think that effects the direction of the conversation at this point.
Quoted 25 lines
Quoted 11 lines
then I agree that this seems to be a problem as it looks like a double free may occur.
However, I believe that your proposed fix introduces will result in skb being leaked leak in the case of
esp_output_done()callingesp_output_tail_tcp(). Perhaps a solution is foresp_output_done()to free the skb ifesp_output_tail_tcp()fails.I did not analyse other call-chains, but I think such analysis is needed.
...
Hi Simon,
I see all calls to
esp_output_tail_tcp()is surrounded by the condition "x->encap&&x->encap->encap_type==TCP_ENCAP_ESPINTCP" which I see it is related to enabling ofCONFIG_INET_ESPINTCPconfiguration (introduced in this commite27cca96cd68("xfrm: add espintcp (RFC 8229)").For calling of
x->type->output(resolved toesp_output()) inxfrm_output_one(), I see there is no double free here asesp_output()callsesp_output_tail()which callsesp_output_tail_tcp()only ifx->encap->encap_type==TCP_ENCAP_ESPINTCPwhich points to the first implementation ofesp_output_tail_tcp(). This first definition doesn't free skb.
Quoted 5 lines
So my understanding is the 2nd
esp_output_tail_tcp()should not be called and this is why I calledWARN_ON()as this func is unreachable. Removing free(skb) here is just for silencing double free Coverity false positive.
Is there something else I miss?
Thanks, I missed the important detail that calls to esp_output_tail_tcp() are guarded by "x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP".
Assuming that condition is always false if CONFIG_INET_ESPINTCP is not set, then I agree with your analysis and I don't see any problems with your patch.
It might be worth calling out in the commit message that the WARN_ON is added because esp_output_tail_tcp() should never be called if CONFIG_INET_ESPINTCP is not set.
Quoted 64 lines · Simon Horman
Quoted 16 lines · Hagar Hemdan
Quoted 15 lines · Simon Horman
Quoted 14 lines · Hagar Hemdan
xmit()functions should consume skb or return error codes in error paths.
When the configuration "CONFIG_INET_ESPINTCP" is not used, the implementation of the function "esp_output_tail_tcp" violates this rule. The function frees the skb and returns the error code.
This change removes thekfree_skbfrom both functions, for both esp4 and esp6.This should not be reachable in the current code, so this change is just a cleanup.
This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")Signed-off-by Hagar Hemdan <[email protected]>Hi Hagar,
If
esp_output()may be thex->type->outputcallback called fromesp_output()Hi Hagar,
FTR, I meant to say "If ... called from
xfrm_output_one()", but I don't think that effects the direction of the conversation at this point.Quoted 25 lines
Quoted 11 lines
then I agree that this seems to be a problem as it looks like a double free may occur.
However, I believe that your proposed fix introduces will result in skb being leaked leak in the case of
esp_output_done()callingesp_output_tail_tcp(). Perhaps a solution is foresp_output_done()to free the skb ifesp_output_tail_tcp()fails.I did not analyse other call-chains, but I think such analysis is needed.
...
Hi Simon,
I see all calls to
esp_output_tail_tcp()is surrounded by the condition "x->encap&&x->encap->encap_type==TCP_ENCAP_ESPINTCP" which I see it is related to enabling ofCONFIG_INET_ESPINTCPconfiguration (introduced in this commite27cca96cd68("xfrm: add espintcp (RFC 8229)").For calling of
x->type->output(resolved toesp_output()) inxfrm_output_one(), I see there is no double free here asesp_output()callsesp_output_tail()which callsesp_output_tail_tcp()only ifx->encap->encap_type==TCP_ENCAP_ESPINTCPwhich points to the first implementation ofesp_output_tail_tcp(). This first definition doesn't free skb.Quoted 5 lines
So my understanding is the 2nd
esp_output_tail_tcp()should not be called and this is why I calledWARN_ON()as this func is unreachable. Removing free(skb) here is just for silencing double free Coverity false positive.
Is there something else I miss?Thanks, I missed the important detail that calls to
esp_output_tail_tcp()are guarded by "x->encap&&x->encap->encap_type==TCP_ENCAP_ESPINTCP".
Quoted 7 lines
Assuming that condition is always false if
CONFIG_INET_ESPINTCPis not set, then I agree with your analysis and I don't see any problems with your patch.It might be worth calling out in the commit message that the
WARN_ONis added becauseesp_output_tail_tcp()should never be called ifCONFIG_INET_ESPINTCPis not set.
Hi Simon,
Thanks. yes, I will update the commit msg in rev2.