Skip to contentSkip to conversation

net: ipv6: xfrm6_state: remove VLA usage

PATCH Superseded Andreas Christoforou 4 participants 6 messages Mar 9, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ipv6: xfrm6_state: remove VLA usage

The kernel would like to have all stack VLA usage removed[1].

---
 net/ipv6/xfrm6_state.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/ipv6/xfrm6_state.c +7 -1
index b15075a..45c0d98 100644
@@ -62,7 +62,12 @@ __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;
+
+	count = kcalloc(maxclass + 1, sizeof(*count), GFP_KERNEL);
+
+	if (!count)
+		return -ENOMEM;
 
 	memset(count, 0, sizeof(count));
 
@@ -80,6 +85,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
 		src[i] = NULL;
 	}
 
+	kfree(count);
 	return 0;
 }
 
Signature · 1 line
2.7.4
#2 lore raw
In reply to Andreas Christoforou
Quoted 26 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed[1].

---
 net/ipv6/xfrm6_state.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/ipv6/xfrm6_state.c +6 -1
index b15075a..45c0d98 100644
@@ -62,7 +62,12 @@ __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;
+
+       count = kcalloc(maxclass + 1, sizeof(*count), GFP_KERNEL);
+
+       if (!count)
+               return -ENOMEM;
 
        memset(count, 0, sizeof(count));
 

net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 90

Quoted 6 lines
net/ipv6/xfrm6_state.c +1 -0
@@ -80,6 +85,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
                src[i] = NULL;
        }
 
+       kfree(count);
        return 0;
 }

Instead of dynamically allocating and freeing memory here, shouldn't we just get rid of the maxclass parameter and use XFRM_MAX_DEPTH as size for the count[] array, too?

Cheers,
Mathias

maintainer
#3 lore raw
Quoted 31 lines · Mathias Krause
Quoted 29 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed[1].

---
 net/ipv6/xfrm6_state.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/ipv6/xfrm6_state.c +6 -1
index b15075a..45c0d98 100644
@@ -62,7 +62,12 @@ __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;
+
+       count = kcalloc(maxclass + 1, sizeof(*count), GFP_KERNEL);
+
+       if (!count)
+               return -ENOMEM;
 
        memset(count, 0, sizeof(count));
 
@@ -80,6 +85,7 @@ __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
                src[i] = NULL;
        }
 
Quoted 7 lines

net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 90

Quoted 3 lines
net/ipv6/xfrm6_state.c +1 -0
+       kfree(count);
        return 0;
 }

Instead of dynamically allocating and freeing memory here, shouldn't we just get rid of the maxclass parameter and use XFRM_MAX_DEPTH as size for the count[] array, too?

Right, that's the way to go. Aside from that, allocating with GFP_KERNEL is definitely wrong here.

#4 lore raw

Right, thank you for your feedback I will create a new patch.

Kind Regards,
Andreas Christoforou

On Mar 9, 2018 15:02, "Steffen Klassert" <[email protected]> wrote:

Quoted 9 lines · Mathias Krause
Quoted 7 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed[1].

---
 net/ipv6/xfrm6_state.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 5 lines

net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int

Quoted 5 lines
net/ipv6/xfrm6_state.c +0 -0
index b15075a..45c0d98 100644
@@ -62,7 +62,12 @@ __xfrm6_sort(void **dst, void **src, int n, int

(*cmp)(void *p), int maxclass)

Quoted 11 lines
Quoted 11 lines
net/ipv6/xfrm6_state.c +6 -1
 {
        int i;
        int class[XFRM_MAX_DEPTH];
-       int count[maxclass];
+       int *count;
+
+       count = kcalloc(maxclass + 1, sizeof(*count), GFP_KERNEL);
+
+       if (!count)
+               return -ENOMEM;
 
Quoted 3 lines

net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int

Quoted 3 lines
net/ipv6/xfrm6_state.c +0 -0
        memset(count, 0, sizeof(count));
 
@@ -80,6 +85,7 @@ __xfrm6_sort(void **dst, void **src, int n, int

(*cmp)(void *p), int maxclass)

Quoted 10 lines

net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 90

Quoted 6 lines
net/ipv6/xfrm6_state.c +1 -0
                src[i] = NULL;
        }
 
+       kfree(count);
        return 0;
 }

Instead of dynamically allocating and freeing memory here, shouldn't we just get rid of the maxclass parameter and use XFRM_MAX_DEPTH as size for the count[] array, too?

Right, that's the way to go. Aside from that, allocating with GFP_KERNEL is definitely wrong here.

#5 lore raw
In reply to Andreas Christoforou

Hello!

Quoted 12 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed[1].

---
 net/ipv6/xfrm6_state.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/ipv6/xfrm6_state.c +0 -0
index b15075a..45c0d98 100644

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 +4 -1
@@ -62,7 +62,12 @@ __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;
+
+	count = kcalloc(maxclass + 1, sizeof(*count), GFP_KERNEL);
+

Empty line not needed here.

net/ipv6/xfrm6_state.c __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) · line 73

Quoted 5 lines
net/ipv6/xfrm6_state.c +2 -0
+	if (!count)
+		return -ENOMEM;
 
 	memset(count, 0, sizeof(count));
 

[...]

MBR, Sergei