net: ipv6: xfrm6_state: remove VLA usage
Subsystems (3)
PATCH v2 1/1 net: ipv6: xfrm6_state: remove VLA usage
The kernel would like to have all stack VLA usage removed[1]. Instead of dynamic allocation, just use XFRM_MAX_DEPTH as already done for the "class" array, but as per feedback, I will not drop maxclass because that changes the behavior. In one case, it'll do this loop up to 5, the other caller up to 6.
[1] https://lkml.org/lkml/2018/3/7/621
---
v2:
- use XFRM_MAX_DEPTH for "count" array (Steffen and Mathias).
--- net/ipv6/xfrm6_state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/xfrm6_state.c +1 -1
@@ -62,7 +62,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) {int i;int class[XFRM_MAX_DEPTH ]; -int count[maxclass]; +int count[XFRM_MAX_DEPTH ];memset (count,0 ,sizeof (count));
Signature · 1 line
2.7.4
net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 67
Quoted 12 lines · On Sat, 10 Mar 2018 09:40:44 +0200 Andreas Christoforou…
net/ipv6/xfrm6_state.c +1
-1@@ -62,7 +62,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) {int i;int class[XFRM_MAX_DEPTH ]; -int count[maxclass]; +int count[XFRM_MAX_DEPTH ];memset (count,0 ,sizeof (count));
Can you perhaps initialize 'count' instead of calling memset(), now?
Signature · 1 line
Stefano
Quoted 14 lines · Stefano Brivio
Quoted 11 lines · On Sat, 10 Mar 2018 09:40:44 +0200 Andreas Christoforou…
net/ipv6/xfrm6_state.c +1
-1@@ -62,7 +62,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) {int i;int class[XFRM_MAX_DEPTH ]; -int count[maxclass]; +int count[XFRM_MAX_DEPTH ];
Quoted 3 lines
net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 67
Quoted 1 line
net/ipv6/xfrm6_state.c +0
-0memset (count,0 ,sizeof (count));Can you perhaps initialize 'count' instead of calling
memset(), now?
Do you mean:
int count[XFRM_MAX_DEPTH] = { };
instead of the memset()?
I thought the compiler would resolve these both to the same thing? The former looks better though! :)
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 19 lines · On Sat, 10 Mar 2018 09:18:46 -0800 Kees Cook <[email protected]> wrote:
Quoted 14 lines · Stefano Brivio
Quoted 11 lines · On Sat, 10 Mar 2018 09:40:44 +0200 Andreas Christoforou…
net/ipv6/xfrm6_state.c +1
-1@@ -62,7 +62,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) {int i;int class[XFRM_MAX_DEPTH ]; -int count[maxclass]; +int count[XFRM_MAX_DEPTH ];Quoted 3 lines
net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 67
Quoted 1 line
net/ipv6/xfrm6_state.c +0
-0memset (count,0 ,sizeof (count));Can you perhaps initialize 'count' instead of calling
memset(), now?
Quoted 5 lines
Do you mean:
int count[XFRM_MAX_DEPTH] = { };
instead of the
memset()?
Yep.
Quoted 1 line
I thought the compiler would resolve these both to the same thing?
Yes, for all practical purposes. With gcc 7.3.0 for x86_64, starting from -O1, it's exactly the same. With e.g. gcc 4.4.7, even with -O3, they can be a bit different depending on context.
Quoted 1 line
The former looks better though! :)
Yep! :)
Signature · 1 line
Stefano
Steffen Klassert
maintainerQuoted 32 lines · Stefano Brivio
Quoted 19 lines · On Sat, 10 Mar 2018 09:18:46 -0800 Kees Cook <[email protected]> wrote:
Quoted 14 lines · Stefano Brivio
Quoted 11 lines · On Sat, 10 Mar 2018 09:40:44 +0200 Andreas Christoforou…
net/ipv6/xfrm6_state.c +1
-1@@ -62,7 +62,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) {int i;int class[XFRM_MAX_DEPTH ]; -int count[maxclass]; +int count[XFRM_MAX_DEPTH ];Quoted 3 lines
net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 67
Quoted 1 line
net/ipv6/xfrm6_state.c +0
-0memset (count,0 ,sizeof (count));Can you perhaps initialize 'count' instead of calling
memset(), now?Quoted 5 lines
Do you mean:
int count[XFRM_MAX_DEPTH] = { };
instead of the
memset()?Yep.
Quoted 1 line
I thought the compiler would resolve these both to the same thing?
Quoted 7 lines
Yes, for all practical purposes. With gcc 7.3.0 for
x86_64, starting from -O1, it's exactly the same. With e.g. gcc 4.4.7, even with -O3, they can be a bit different depending on context.Quoted 1 line
The former looks better though! :)
Yep! :)
If Andreas does a v3 anyway, please also consider to trim the subject line to something like:
xfrm: remove VLA usage in __xfrm6_sort()
Andreas,
Quoted 22 lines · On Sat, 10 Mar 2018 09:40:44 +0200 Andreas Christoforou…
The kernel would like to have all stack VLA usage removed[1]. Instead of dynamic allocation, just use
XFRM_MAX_DEPTHas already done for the "class" array, but as per feedback, I will not drop maxclass because that changes the behavior. In one case, it'll do this loop up to 5, the other caller up to 6.[1] https://lkml.org/lkml/2018/3/7/621
Signed-off-by Andreas Christoforou <[email protected]>---
v2:
- useXFRM_MAX_DEPTHfor "count" array (Steffen and Mathias).--- net/ipv6/xfrm6_state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/ipv6/xfrm6_state.c +0
-0
net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 68
Quoted 8 lines
net/ipv6/xfrm6_state.c +1
-1@@ -62,7 +62,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) {int i;int class[XFRM_MAX_DEPTH ]; -int count[maxclass]; +int count[XFRM_MAX_DEPTH ];memset (count,0 ,sizeof (count));
I hope this didn't get too confusing. In the end, the change I proposed for this patch was simply to drop the memset and initialize 'count' like:
int count[XFRM_MAX_DEPTH] = { };and perhaps, while at it, move this before 'int i', for coding style reasons.
When you re-post, please also take care of Steffen's comment. He proposed to change the subject to:
xfrm: remove VLA usage in __xfrm6_sort()
Note that you should give an indication of which tree this patch should be applied to, by including this in the subject. The current subject doesn't specify it, it should have been:
[PATCH v2 ipsec-next] ...
Please see Documentation/networking/netdev-FAQ.txt for the difference between net and net-next, as the same distinction applies for ipsec and ipsec-next trees. Thanks.
Signature · 1 line
Stefano