Add some 'malloc' failure checks
Subsystems (4)
v2 cover Add some 'malloc' failure checks
From: Kunwu Chan <[email protected]>
The "malloc" call may not be successful.Add the malloc failure checking to avoid possible null dereference.
Changes since v1 [1]:
- As Daniel Borkmann suggested, change patch 4/4 only
- Other 3 patches no changes.
[1] https://lore.kernel.org/all/[email protected]/
Kunwu Chan (4):
selftests/bpf: Add some null pointer checks selftests/bpf/sockopt: Add a null pointer check for the run_test selftests/bpf: Add a null pointer check for the load_btf_spec selftests/bpf: Add a null pointer check for the
serial_test_tp_attach_query
tools/testing/selftests/bpf/prog_tests/sockopt.c | 6 ++++++ tools/testing/selftests/bpf/prog_tests/tp_attach_query.c | 3 +++ tools/testing/selftests/bpf/test_progs.c | 7 +++++++ tools/testing/selftests/bpf/test_verifier.c | 2 ++ 4 files changed, 18 insertions(+)
Signature · 1 line
2.40.1
PATCH v2 1/4 selftests/bpf: Add some null pointer checks
From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. This patch will add the malloc failure checking to avoid possible null dereference.
--- tools/testing/selftests/bpf/test_progs.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/testing/selftests/bpf/test_progs.c +7 -0
@@ -582,6 +582,11 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) val_buf1 =malloc (stack_trace_len); val_buf2 =malloc (stack_trace_len); +if (!val_buf1 || !val_buf2) { + err = -ENOMEM ; + goto out; + } + cur_key_p =NULL ; next_key_p = &key;while (bpf_map_get_next_key (smap_fd, cur_key_p, next_key_p) ==0 ) {
@@ -1197,6 +1202,8 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)int subtest_num = state->subtest_num; state->subtest_states =malloc (subtest_num *sizeof (*subtest_state)); +if (!state->subtest_states) +return -ENOMEM ;for (int i =0 ; i < subtest_num; i++) { subtest_state = &state->subtest_states[i];
Signature · 1 line
2.40.1
Re: [PATCH bpf-next v2 1/4] selftests/bpf: Add some null pointer checks
Quoted 16 lines · On 5/10/24 2:58 PM, [email protected] wrote:
From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. This patch will add the malloc failure checking to avoid possible null dereference.
Signed-off-by Kunwu Chan <[email protected]>--- tools/testing/selftests/bpf/test_progs.c | 7 +++++++ 1 file changed, 7 insertions(+)tools/testing/selftests/bpf/test_progs.c +0
-0
tools/testing/selftests/bpf/test_progs.c int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) · line 586
Quoted 5 lines
tools/testing/selftests/bpf/test_progs.c +2
-0@@ -582,6 +582,11 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) val_buf1 =malloc (stack_trace_len); val_buf2 =malloc (stack_trace_len); +if (!val_buf1 || !val_buf2) { + err = -ENOMEM ;
Return from here instead of going to out where free(val_buf*) is being called.
Quoted 15 lines
tools/testing/selftests/bpf/test_progs.c +5
-0+ goto out; + } + cur_key_p =NULL ; next_key_p = &key;while (bpf_map_get_next_key (smap_fd, cur_key_p, next_key_p) ==0 ) {@@ -1197,6 +1202,8 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)int subtest_num = state->subtest_num; state->subtest_states =malloc (subtest_num *sizeof (*subtest_state)); +if (!state->subtest_states) +return -ENOMEM ;for (int i =0 ; i < subtest_num; i++) { subtest_state = &state->subtest_states[i];
Signature · 2 lines
BR, Muhammad Usama Anjum
Re: [PATCH bpf-next v2 1/4] selftests/bpf: Add some null pointer checks
Quoted 18 lines · Muhammad Usama Anjum
Quoted 17 lines · On 5/10/24 2:58 PM, [email protected] wrote:
From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. This patch will add the malloc failure checking to avoid possible null dereference.
Signed-off-by Kunwu Chan <[email protected]>--- tools/testing/selftests/bpf/test_progs.c | 7 +++++++ 1 file changed, 7 insertions(+)tools/testing/selftests/bpf/test_progs.c +0
-0@@ -582,6 +582,11 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
Quoted 5 lines
tools/testing/selftests/bpf/test_progs.c int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) · line 586
Quoted 4 lines
tools/testing/selftests/bpf/test_progs.c +2
-0val_buf1 =malloc (stack_trace_len); val_buf2 =malloc (stack_trace_len); +if (!val_buf1 || !val_buf2) { + err = -ENOMEM ;Return from here instead of going to out where
free(val_buf*)is being called.
I think it's no harm. And Unify the processing at the end to achieve uniform format.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/bpf/test_progs.c +5
-0+ goto out; + } + cur_key_p =NULL ; next_key_p = &key;while (bpf_map_get_next_key (smap_fd, cur_key_p, next_key_p) ==0 ) {@@ -1197,6 +1202,8 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)int subtest_num = state->subtest_num; state->subtest_states =malloc (subtest_num *sizeof (*subtest_state)); +if (!state->subtest_states) +return -ENOMEM ;for (int i =0 ; i < subtest_num; i++) { subtest_state = &state->subtest_states[i];
Signature · 2 lines
Thanks, Kunwu.Chan
Re: [PATCH bpf-next v2 1/4] selftests/bpf: Add some null pointer checks
In reply to [email protected]Quoted 1 line
There is a 'malloc' call, which can be unsuccessful.
two calls?
Quoted 1 line
This patch will add the malloc failure checking
…
Please use imperative wordings for improved change descriptions also in your patches.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.9#n94
Regards,
Markus
PATCH v2 2/4 selftests/bpf/sockopt: Add a null pointer check for the run_test
In reply to [email protected]From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. Add the malloc failure checking to avoid possible null dereference and give more information
about test fail reasons.
--- tools/testing/selftests/bpf/prog_tests/sockopt.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/prog_tests/sockopt.c +6 -0
@@ -1100,6 +1100,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring) } optval =malloc (test->get_optlen); +if (!optval) { +log_err ("Failed to allocate memory" ); + ret =-1 ; + goto close_sock_fd; + } +memset (optval,0 , test->get_optlen);socklen_t optlen = test->get_optlen; socklen_t expected_get_optlen = test->get_optlen_ret ?:
Signature · 1 line
2.40.1
Re: [PATCH bpf-next v2 2/4] selftests/bpf/sockopt: Add a null pointer check for the run_test
Quoted 8 lines · On 5/10/24 2:58 PM, [email protected] wrote:
From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. Add the malloc failure checking to avoid possible null dereference and give more information
about test fail reasons.Signed-off-by Kunwu Chan <[email protected]>
LGTM
Quoted 21 lines
--- tools/testing/selftests/bpf/prog_tests/sockopt.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/testing/selftests/bpf/prog_tests/sockopt.c +6
-0@@ -1100,6 +1100,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring) } optval =malloc (test->get_optlen); +if (!optval) { +log_err ("Failed to allocate memory" ); + ret =-1 ; + goto close_sock_fd; + } +memset (optval,0 , test->get_optlen);socklen_t optlen = test->get_optlen; socklen_t expected_get_optlen = test->get_optlen_ret ?:
Signature · 2 lines
BR, Muhammad Usama Anjum
PATCH v2 3/4 selftests/bpf: Add a null pointer check for the load_btf_spec
In reply to [email protected]From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. Add the malloc failure checking to avoid possible null dereference.
--- tools/testing/selftests/bpf/test_verifier.c | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/bpf/test_verifier.c +2 -0
@@ -762,6 +762,8 @@ static int load_btf_spec(__u32 *types, int types_len, ); raw_btf =malloc (sizeof (hdr) + types_len + strings_len); +if (!raw_btf) +return -ENOMEM ; ptr = raw_btf;memcpy (ptr, &hdr,sizeof (hdr));
Signature · 1 line
2.40.1
Re: [PATCH bpf-next v2 3/4] selftests/bpf: Add a null pointer check for the load_btf_spec
Quoted 7 lines · On 5/10/24 2:58 PM, [email protected] wrote:
From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. Add the malloc failure checking to avoid possible null dereference.
Signed-off-by Kunwu Chan <[email protected]>
LGTM
Quoted 17 lines
--- tools/testing/selftests/bpf/test_verifier.c | 2 ++ 1 file changed, 2 insertions(+)tools/testing/selftests/bpf/test_verifier.c +2
-0@@ -762,6 +762,8 @@ static int load_btf_spec(__u32 *types, int types_len, ); raw_btf =malloc (sizeof (hdr) + types_len + strings_len); +if (!raw_btf) +return -ENOMEM ; ptr = raw_btf;memcpy (ptr, &hdr,sizeof (hdr));
Signature · 2 lines
BR, Muhammad Usama Anjum
PATCH v2 4/4 selftests/bpf: Add a null pointer check for the serial_test_tp_attach_query
In reply to [email protected]From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. Add the malloc failure checking to avoid possible null dereference.
---
Changes in v2:
- Use ASSERT* instead of CHECK
- Add suggested-by tag
--- tools/testing/selftests/bpf/prog_tests/tp_attach_query.c | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/bpf/prog_tests/tp_attach_query.c +3 -0
@@ -39,6 +39,9 @@ void serial_test_tp_attach_query(void) attr.wakeup_events =1 ; query =malloc (sizeof (*query) +sizeof (__u32) * num_progs); +if (!ASSERT_OK_PTR (query,"malloc" )) +return ; +for (i =0 ; i < num_progs; i++) { err =bpf_prog_test_load (file,BPF_PROG_TYPE_TRACEPOINT , &obj[i], &prog_fd[i]);
Signature · 1 line
2.40.1
Re: [PATCH bpf-next v2 4/4] selftests/bpf: Add a null pointer check for the serial_test_tp_attach_query
Quoted 21 lines · On 5/10/24 2:58 PM, [email protected] wrote:
From: Kunwu Chan <[email protected]>
There is a 'malloc' call, which can be unsuccessful. Add the malloc failure checking to avoid possible null dereference.
Signed-off-by Kunwu Chan <[email protected]>Suggested-by Daniel Borkmann <[email protected]>---
Changes in v2:
- Use ASSERT* instead of CHECK
- Add suggested-by tag--- tools/testing/selftests/bpf/prog_tests/tp_attach_query.c | 3 +++ 1 file changed, 3 insertions(+)tools/testing/selftests/bpf/prog_tests/tp_attach_query.c +0
-0
tools/testing/selftests/bpf/prog_tests/tp_attach_query.c void serial_test_tp_attach_query(void) · line 44
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tp_attach_query.c +3
-0@@ -39,6 +39,9 @@ void serial_test_tp_attach_query(void) attr.wakeup_events =1 ; query =malloc (sizeof (*query) +sizeof (__u32) * num_progs); +if (!ASSERT_OK_PTR (query,"malloc" )) +return ; +
LGTM
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tp_attach_query.c +0
-0for (i =0 ; i < num_progs; i++) { err =bpf_prog_test_load (file,BPF_PROG_TYPE_TRACEPOINT , &obj[i], &prog_fd[i]);
Signature · 2 lines
BR, Muhammad Usama Anjum