src/openvpn/dco_freebsd.c: handle malloc failure
PATCH 1/1 src/openvpn/dco_freebsd.c: handle malloc failure
malloc was not checked against NULL, I was able
to get core dump in case of failure
--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)
src/openvpn/dco_freebsd.c +5 -0
@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + { +close (fd); +return false; + } ifcr.ifcr_count = ifcr.ifcr_total; ifcr.ifcr_buffer = buf;
Signature · 1 line
2.35.3.windows.1
Quoted 13 lines · Ilya Shipitsin
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 598
Quoted 5 lines
src/openvpn/dco_freebsd.c +2
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + {
I’d ‘goto out;’ instead, because that’s how we handle other errors in this function. (free(NULL) is guaranteed to be safe, so we can just do that.)
Fwiw: I usually don’t bother handling malloc failure in userspace, because modern systems all overallocate anyway, so the first thing you know about lack of memory is the out-of-memory killer terminating you. It’s a policy choice for the project, so I don’t object to handling it either.
Best regards,
Kristof
Hi,
Quoted 13 lines · Ilya Shipitsin
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 597
Quoted 4 lines
src/openvpn/dco_freebsd.c +1
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL )
as style guideline, we wanted to go for if (!A) rather than if (A == NULL). Although I am not sure if the whole codebase was cleaned up yet or not.
Cheers,
Quoted 7 lines
src/openvpn/dco_freebsd.c +4
-0+ { +close (fd); +return false; + } ifcr.ifcr_count = ifcr.ifcr_total; ifcr.ifcr_buffer = buf;
Signature · 1 line
Antonio Quartulli
ср, 17 мая 2023 г. в 22:43, Kristof Provost <[email protected]...>:
Quoted 16 lines · Ilya Shipitsin
Quoted 15 lines
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) }
Quoted 8 lines
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 598
Quoted 3 lines
src/openvpn/dco_freebsd.c +2
-0buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + {I’d ‘goto out;’ instead, because that’s how we handle other errors in this function.
(free(NULL) is guaranteed to be safe, so we can just do that.)
on "goto out" we'll end with "return available;"
Quoted 6 lines
Fwiw: I usually don’t bother handling malloc failure in userspace, because modern systems all overallocate anyway, so the first thing you know about lack of memory is the out-of-memory killer terminating you. It’s a policy choice for the project, so I don’t object to handling it either.
I agree it's a highly unlikely condition.
Quoted 4 lines
Best regards,
Kristof
ср, 17 мая 2023 г. в 22:47, Antonio Quartulli <[email protected]...>:
Quoted 18 lines
Hi,
Quoted 15 lines · Ilya Shipitsin
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) }
Quoted 7 lines
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 597
Quoted 2 lines
src/openvpn/dco_freebsd.c +1
-0buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL )as style guideline, we wanted to go for if (!A) rather than if (A == NULL). Although I am not sure if the whole codebase was cleaned up yet or not.
I'm fine with either :)
Quoted 14 lines
Cheers,
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 604
Quoted 7 lines
src/openvpn/dco_freebsd.c +4
-0+ { +close (fd); +return false; + } ifcr.ifcr_count = ifcr.ifcr_total; ifcr.ifcr_buffer = buf;--
Antonio Quartulli
Quoted 27 lines · Илья Шипицин
ср, 17 мая 2023 г. в 22:43, Kristof Provost <[email protected]...>:
Quoted 16 lines · Ilya Shipitsin
Quoted 15 lines
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) }Quoted 8 lines
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 598
Quoted 3 lines
src/openvpn/dco_freebsd.c +2
-0buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + {I’d ‘goto out;’ instead, because that’s how we handle other errors in this function.
(free(NULL) is guaranteed to be safe, so we can just do that.)
Quoted 2 lines
on "goto out" we'll end with "return available;"
‘available’ defaults to ‘false’, so that seems fine to me.
Best regards,
Kristof
ср, 17 мая 2023 г. в 23:04, Kristof Provost <[email protected]...>:
Quoted 24 lines · Илья Шипицин
Quoted 23 lines
ср, 17 мая 2023 г. в 22:43, Kristof Provost <[email protected]...>:
Quoted 21 lines · Ilya Shipitsin
Quoted 13 lines
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 598
Quoted 5 lines
src/openvpn/dco_freebsd.c +2
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + {I’d ‘goto out;’ instead, because that’s how we handle other errors in
Quoted 9 lines
this
Quoted 6 lines
Quoted 3 lines
function.
(free(NULL) is guaranteed to be safe, so we can just do that.)on "goto out" we'll end with "return available;"
‘available’ defaults to ‘false’, so that seems fine to me.
looks fragile :)
I do not see benefits of such an approach. But it will work indeed
Quoted 4 lines
Best regards,
Kristof
Quoted 31 lines · Илья Шипицин
ср, 17 мая 2023 г. в 23:04, Kristof Provost <[email protected]...>:
Quoted 29 lines · Илья Шипицин
Quoted 23 lines
ср, 17 мая 2023 г. в 22:43, Kristof Provost <[email protected]...>:
Quoted 21 lines · Ilya Shipitsin
Quoted 13 lines
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 598
Quoted 5 lines
src/openvpn/dco_freebsd.c +2
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + {I’d ‘goto out;’ instead, because that’s how we handle other errors in
this
Quoted 4 lines
Quoted 3 lines
function.
(free(NULL) is guaranteed to be safe, so we can just do that.)
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
on "goto out" we'll end with "return available;"
‘available’ defaults to ‘false’, so that seems fine to me.
looks fragile :)
I do not see benefits of such an approach. But it will work indeed
The reasoning is that it keeps the error handing consistent. If we ever extend the function to e.g. open another file descriptor we’d only have to change the error handling in one location.
We’re not adding new ways for the function to fail either. The next potential error is the ioctl() call, where we do exactly the same thing (i.e. goto out) on error, so it already relies on available being set to false.
Best regards,
Kristof
Hi,
Quoted 37 lines · Kristof Provost
Quoted 36 lines · Илья Шипицин
ср, 17 мая 2023 г. в 23:04, Kristof Provost <[email protected]...>:
Quoted 33 lines · Илья Шипицин
Quoted 23 lines
ср, 17 мая 2023 г. в 22:43, Kristof Provost <[email protected]...>:
Quoted 21 lines · Ilya Shipitsin
Quoted 13 lines
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>--- src/openvpn/dco_freebsd.c | 5 +++++ 1 file changed, 5 insertions(+)src/openvpn/dco_freebsd.c +0
-0src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 598
Quoted 5 lines
src/openvpn/dco_freebsd.c +2
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf =malloc (ifcr.ifcr_total *IFNAMSIZ ); +if (buf ==NULL ) + {I’d ‘goto out;’ instead, because that’s how we handle other errors in
this
Quoted 6 lines
Quoted 3 lines
function.
(free(NULL) is guaranteed to be safe, so we can just do that.)on "goto out" we'll end with "return available;"
‘available’ defaults to ‘false’, so that seems fine to me.
Quoted 8 lines
Quoted 4 lines
looks fragile :)
I do not see benefits of such an approach. But it will work indeed
The reasoning is that it keeps the error handing consistent. If we ever extend the function to e.g. open another file descriptor we’d only have to change the error handling in one location.
We’re not adding new ways for the function to fail either. The next potential error is the
ioctl()call, where we do exactly the same thing (i.e. goto out) on error, so it already relies on available being set to false.
I agree on this style too.
Should we require more clean up work in the future, we can just add extra lines right after the out label (with the appropriate checks, of course), thus keeping error handling in one place.
Cheers,
Quoted 8 lines
Best regards,
Kristof_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Signature · 4 lines
Antonio Quartulli -- Antonio Quartulli
Hi,
Quoted 5 lines · Kristof Provost
Fwiw: I usually don???t bother handling malloc failure in userspace, because modern systems all overallocate anyway, so the first thing you know about lack of memory is the out-of-memory killer terminating you. It???s a policy choice for the project, so I don???t object to handling it either.
In OpenVPN, we do check malloc() returns :-) - and there's cases where it actually hit, like running with too low ulimits and --mlock.
If a malloc fail is not recoverable, we have check_malloc_return(p), which will do
voidout_of_memory(void)
{
fprintf(stderr, PACKAGE_NAME ": Out of Memory\n");
exit(1);
}
static inline void
check_malloc_return(const void *p)
{
if (!p)
{
out_of_memory();
}
}so it depends a bit on the context if "return an error upstream, and wait for the next allocation to fail" or "abort right away" is the best choice. In low level functions, "return error" might lead to a better error message from upstream (or not).
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Am 17.05.23 um 22:47 schrieb Antonio Quartulli:
Quoted 11 lines
Hi,
Quoted 8 lines · Ilya Shipitsin
malloc was not checked against NULL, I was able
to get core dump in case of failureSigned-off-by Ilya Shipitsin <[email protected]...>---
src/openvpn/dco_freebsd.c| 5 +++++1 file changed, 5 insertions(+)
Quoted 12 lines
src/openvpn/dco_freebsd.c dco_available(int msglevel) · line 596
Quoted 8 lines
src/openvpn/dco_freebsd.c +1
-0@@ -594,6 +594,11 @@ dco_available(int msglevel) } buf = malloc(ifcr.ifcr_total * IFNAMSIZ); + if (buf == NULL)as style guideline, we wanted to go for if (!A) rather than if (A == NULL). Although I am not sure if the whole codebase was cleaned up yet or not.
That, and constants usually go on the left-hand side of comparison so the compiler flags the accidental if (foo = NULL) even if it does not produce "add a pair of parentheses if you really mean to use an assignment as if() expression" like warnings. You can't if (NULL = foo)...
And in practice, if anyone hits C++ some day, it might make a difference if you're in boolean or pointer contexts, so use if (!A).
Hi,
Quoted 4 lines · Matthias Andree
That, and constants usually go on the left-hand side of comparison so the compiler flags the accidental if (foo = NULL) even if it does not produce "add a pair of parentheses if you really mean to use an assignment as
if()expression" like warnings. You can't if (NULL = foo)...
This is actually not OpenVPN style today (it's "syzzer style" so we find it in a few crypto modules).
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...чт, 18 мая 2023 г. в 08:47, Gert Doering <[email protected]...>:
Quoted 30 lines
Hi,
Quoted 5 lines · Kristof Provost
Fwiw: I usually don???t bother handling malloc failure in userspace, because modern systems all overallocate anyway, so the first thing you know about lack of memory is the out-of-memory killer terminating you. It???s a policy choice for the project, so I don???t object to handling it either.
In OpenVPN, we do check
malloc()returns :-) - and there's cases where it actually hit, like running with too low ulimits and --mlock.If a malloc fail is not recoverable, we have
check_malloc_return(p), which will dovoid
out_of_memory(void){ fprintf(stderr, PACKAGE_NAME ": Out of Memory\n"); exit(1); } static inline void check_malloc_return(const void *p) { if (!p) { out_of_memory(); } }
Quoted 5 lines
so it depends a bit on the context if "return an error upstream, and wait for the next allocation to fail" or "abort right away" is the best choice. In low level functions, "return error" might lead to a better error message from upstream (or not).
this one is recoverable.
if malloc fails, dco fails, but openvpn still able to function
Quoted 13 lines
gert
--
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never
doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh
MistressGert Doering - Munich, Germany
[email protected]...
чт, 18 мая 2023 г. в 09:10, Илья Шипицин <[email protected]...>:
Quoted 34 lines
чт, 18 мая 2023 г. в 08:47, Gert Doering <[email protected]...>:
Quoted 30 lines
Hi,
Quoted 5 lines · Kristof Provost
Fwiw: I usually don???t bother handling malloc failure in userspace, because modern systems all overallocate anyway, so the first thing you know about lack of memory is the out-of-memory killer terminating you. It???s a policy choice for the project, so I don???t object to handling it either.
In OpenVPN, we do check
malloc()returns :-) - and there's cases where it actually hit, like running with too low ulimits and --mlock.If a malloc fail is not recoverable, we have
check_malloc_return(p), which will dovoid
out_of_memory(void){ fprintf(stderr, PACKAGE_NAME ": Out of Memory\n"); exit(1); } static inline void check_malloc_return(const void *p) { if (!p) { out_of_memory(); } }
Quoted 9 lines
Quoted 5 lines
so it depends a bit on the context if "return an error upstream, and wait for the next allocation to fail" or "abort right away" is the best choice. In low level functions, "return error" might lead to a better error message from upstream (or not).
this one is recoverable.
if malloc fails, dco fails, but openvpn still able to function
maybe we should add something like "data channel offload failed due to ..."
Quoted 16 lines
Quoted 15 lines
gert
--
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never
doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh
MistressGert Doering - Munich, Germany
[email protected]...
Am 18.05.23 um 09:09 schrieb Gert Doering:
Quoted 9 lines
Hi,
Quoted 4 lines · Matthias Andree
That, and constants usually go on the left-hand side of comparison so the compiler flags the accidental if (foo = NULL) even if it does not produce "add a pair of parentheses if you really mean to use an assignment as
if()expression" like warnings. You can't if (NULL = foo)...This is actually not OpenVPN style today (it's "syzzer style" so we find it in a few crypto modules).
I personally find if (B) or if (!B) more concise and clearer, too.