Skip to content

Commit 3c446f1

Browse files
gaurav1086Sasha Levin
authored andcommitted
bpf, xdp, samples: Fix null pointer dereference in *_user code
[ Upstream commit 6903cda ] Memset on the pointer right after malloc can cause a NULL pointer deference if it failed to allocate memory. A simple fix is to replace malloc()/memset() pair with a simple call to calloc(). Fixes: 0fca931 ("samples/bpf: program demonstrating access to xdp_rxq_info") Signed-off-by: Gaurav Singh <gaurav1086@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 4af5b6b commit 3c446f1

3 files changed

Lines changed: 7 additions & 21 deletions

File tree

‎samples/bpf/xdp_monitor_user.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,8 @@ static void *alloc_rec_per_cpu(int record_size)
509509
{
510510
unsigned int nr_cpus = bpf_num_possible_cpus();
511511
void *array;
512-
size_t size;
513512

514-
size = record_size * nr_cpus;
515-
array = malloc(size);
516-
memset(array, 0, size);
513+
array = calloc(nr_cpus, record_size);
517514
if (!array) {
518515
fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
519516
exit(EXIT_FAIL_MEM);
@@ -528,8 +525,7 @@ static struct stats_record *alloc_stats_record(void)
528525
int i;
529526

530527
/* Alloc main stats_record structure */
531-
rec = malloc(sizeof(*rec));
532-
memset(rec, 0, sizeof(*rec));
528+
rec = calloc(1, sizeof(*rec));
533529
if (!rec) {
534530
fprintf(stderr, "Mem alloc error\n");
535531
exit(EXIT_FAIL_MEM);

‎samples/bpf/xdp_redirect_cpu_user.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,8 @@ static struct datarec *alloc_record_per_cpu(void)
210210
{
211211
unsigned int nr_cpus = bpf_num_possible_cpus();
212212
struct datarec *array;
213-
size_t size;
214213

215-
size = sizeof(struct datarec) * nr_cpus;
216-
array = malloc(size);
217-
memset(array, 0, size);
214+
array = calloc(nr_cpus, sizeof(struct datarec));
218215
if (!array) {
219216
fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
220217
exit(EXIT_FAIL_MEM);
@@ -229,11 +226,11 @@ static struct stats_record *alloc_stats_record(void)
229226

230227
size = sizeof(*rec) + n_cpus * sizeof(struct record);
231228
rec = malloc(size);
232-
memset(rec, 0, size);
233229
if (!rec) {
234230
fprintf(stderr, "Mem alloc error\n");
235231
exit(EXIT_FAIL_MEM);
236232
}
233+
memset(rec, 0, size);
237234
rec->rx_cnt.cpu = alloc_record_per_cpu();
238235
rec->redir_err.cpu = alloc_record_per_cpu();
239236
rec->kthread.cpu = alloc_record_per_cpu();

‎samples/bpf/xdp_rxq_info_user.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ static struct datarec *alloc_record_per_cpu(void)
198198
{
199199
unsigned int nr_cpus = bpf_num_possible_cpus();
200200
struct datarec *array;
201-
size_t size;
202201

203-
size = sizeof(struct datarec) * nr_cpus;
204-
array = malloc(size);
205-
memset(array, 0, size);
202+
array = calloc(nr_cpus, sizeof(struct datarec));
206203
if (!array) {
207204
fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
208205
exit(EXIT_FAIL_MEM);
@@ -214,11 +211,8 @@ static struct record *alloc_record_per_rxq(void)
214211
{
215212
unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
216213
struct record *array;
217-
size_t size;
218214

219-
size = sizeof(struct record) * nr_rxqs;
220-
array = malloc(size);
221-
memset(array, 0, size);
215+
array = calloc(nr_rxqs, sizeof(struct record));
222216
if (!array) {
223217
fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
224218
exit(EXIT_FAIL_MEM);
@@ -232,8 +226,7 @@ static struct stats_record *alloc_stats_record(void)
232226
struct stats_record *rec;
233227
int i;
234228

235-
rec = malloc(sizeof(*rec));
236-
memset(rec, 0, sizeof(*rec));
229+
rec = calloc(1, sizeof(struct stats_record));
237230
if (!rec) {
238231
fprintf(stderr, "Mem alloc error\n");
239232
exit(EXIT_FAIL_MEM);

0 commit comments

Comments
 (0)