Blame SOURCES/0015-tests-use-tail_alloc-instead-of-calloc-in-bpf-obj_ge.patch

4d44fe
From 14e5342e996ff122875a2306ba8d84dac096a48a Mon Sep 17 00:00:00 2001
4d44fe
From: "Dmitry V. Levin" <ldv@altlinux.org>
4d44fe
Date: Thu, 3 Jan 2019 23:36:22 +0000
4d44fe
Subject: [PATCH 15/27] tests: use tail_alloc instead of calloc in
4d44fe
 bpf-obj_get_info_by_fd-prog*
4d44fe
4d44fe
This guarantees that map_info and prog_info objects are not accessed
4d44fe
out of bounds.
4d44fe
4d44fe
* tests/bpf-obj_get_info_by_fd.c: Include <string.h>.
4d44fe
(main): Use tail_alloc instead of calloc for map_info and prog_info.
4d44fe
4d44fe
Conflicts:
4d44fe
	tests/bpf-obj_get_info_by_fd.c
4d44fe
4d44fe
Additional changes:
4d44fe
	tests-m32/bpf-obj_get_info_by_fd.c (copy of tests/bpf-obj_get_info_by_fd.c)
4d44fe
	tests-mx32/bpf-obj_get_info_by_fd.c (copy of tests/bpf-obj_get_info_by_fd.c)
4d44fe
4d44fe
---
4d44fe
 tests/bpf-obj_get_info_by_fd.c | 7 +++++--
4d44fe
 1 file changed, 5 insertions(+), 2 deletions(-)
4d44fe
4d44fe
Index: strace-4.24/tests/bpf-obj_get_info_by_fd.c
4d44fe
===================================================================
4d44fe
--- strace-4.24.orig/tests/bpf-obj_get_info_by_fd.c	2019-03-10 05:19:26.164412164 +0100
4d44fe
+++ strace-4.24/tests/bpf-obj_get_info_by_fd.c	2019-03-10 05:35:03.618024803 +0100
4d44fe
@@ -18,6 +18,7 @@
4d44fe
 #include <stdio.h>
4d44fe
 #include <stdint.h>
4d44fe
 #include <stdlib.h>
4d44fe
+#include <string.h>
4d44fe
 #include <unistd.h>
4d44fe
 #include <sys/sysmacros.h>
4d44fe
 #include <asm/unistd.h>
4d44fe
@@ -274,13 +275,14 @@
4d44fe
 	 * initializer element is not constant.
4d44fe
 	 */
4d44fe
 	#define  MAP_INFO_SZ (sizeof(*map_info) + 64)
4d44fe
-	struct bpf_map_info_struct *map_info = calloc(1, MAP_INFO_SZ);
4d44fe
+	struct bpf_map_info_struct *map_info = tail_alloc(MAP_INFO_SZ);
4d44fe
 	struct BPF_OBJ_GET_INFO_BY_FD_struct bpf_map_get_info_attr = {
4d44fe
 		.bpf_fd   = map_fd,
4d44fe
 		.info_len = MAP_INFO_SZ,
4d44fe
 		.info     = (uintptr_t) map_info,
4d44fe
 	};
4d44fe
 
4d44fe
+	memset(map_info, 0, MAP_INFO_SZ);
4d44fe
 	int ret = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &bpf_map_get_info_attr,
4d44fe
 			  sizeof(bpf_map_get_info_attr));
4d44fe
 	if (ret < 0)
4d44fe
@@ -330,7 +332,7 @@
4d44fe
 	 * initializer element is not constant.
4d44fe
 	 */
4d44fe
 	#define  PROG_INFO_SZ (sizeof(*prog_info) + 64)
4d44fe
-	struct bpf_prog_info_struct *prog_info = calloc(1, PROG_INFO_SZ);
4d44fe
+	struct bpf_prog_info_struct *prog_info = tail_alloc(PROG_INFO_SZ);
4d44fe
 	struct bpf_insn *xlated_prog = tail_alloc(sizeof(*xlated_prog) * 42);
4d44fe
 	uint32_t *map_ids = tail_alloc(sizeof(*map_ids) * 2);
4d44fe
 	struct BPF_OBJ_GET_INFO_BY_FD_struct bpf_prog_get_info_attr = {
4d44fe
@@ -340,6 +342,7 @@
4d44fe
 	};
4d44fe
 	size_t old_prog_info_len = PROG_INFO_SZ;
4d44fe
 
4d44fe
+	memset(prog_info, 0, PROG_INFO_SZ);
4d44fe
 	for (unsigned int i = 0; i < 4; i++) {
4d44fe
 		prog_info->jited_prog_len = 0;
4d44fe
 		switch (i) {
4d44fe
Index: strace-4.24/tests-m32/bpf-obj_get_info_by_fd.c
4d44fe
===================================================================
4d44fe
--- strace-4.24.orig/tests-m32/bpf-obj_get_info_by_fd.c	2018-06-04 03:11:05.000000000 +0200
4d44fe
+++ strace-4.24/tests-m32/bpf-obj_get_info_by_fd.c	2019-03-10 05:35:43.934621086 +0100
4d44fe
@@ -4,27 +4,7 @@
4d44fe
  * Copyright (c) 2018 The strace developers.
4d44fe
  * All rights reserved.
4d44fe
  *
4d44fe
- * Redistribution and use in source and binary forms, with or without
4d44fe
- * modification, are permitted provided that the following conditions
4d44fe
- * are met:
4d44fe
- * 1. Redistributions of source code must retain the above copyright
4d44fe
- *    notice, this list of conditions and the following disclaimer.
4d44fe
- * 2. Redistributions in binary form must reproduce the above copyright
4d44fe
- *    notice, this list of conditions and the following disclaimer in the
4d44fe
- *    documentation and/or other materials provided with the distribution.
4d44fe
- * 3. The name of the author may not be used to endorse or promote products
4d44fe
- *    derived from this software without specific prior written permission.
4d44fe
- *
4d44fe
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4d44fe
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4d44fe
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4d44fe
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4d44fe
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4d44fe
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4d44fe
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4d44fe
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4d44fe
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
4d44fe
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4d44fe
+ * SPDX-License-Identifier: GPL-2.0-or-later
4d44fe
  */
4d44fe
 
4d44fe
 #include "tests.h"
4d44fe
@@ -38,6 +18,7 @@
4d44fe
 #include <stdio.h>
4d44fe
 #include <stdint.h>
4d44fe
 #include <stdlib.h>
4d44fe
+#include <string.h>
4d44fe
 #include <unistd.h>
4d44fe
 #include <sys/sysmacros.h>
4d44fe
 #include <asm/unistd.h>
4d44fe
@@ -294,13 +275,14 @@
4d44fe
 	 * initializer element is not constant.
4d44fe
 	 */
4d44fe
 	#define  MAP_INFO_SZ (sizeof(*map_info) + 64)
4d44fe
-	struct bpf_map_info_struct *map_info = calloc(1, MAP_INFO_SZ);
4d44fe
+	struct bpf_map_info_struct *map_info = tail_alloc(MAP_INFO_SZ);
4d44fe
 	struct BPF_OBJ_GET_INFO_BY_FD_struct bpf_map_get_info_attr = {
4d44fe
 		.bpf_fd   = map_fd,
4d44fe
 		.info_len = MAP_INFO_SZ,
4d44fe
 		.info     = (uintptr_t) map_info,
4d44fe
 	};
4d44fe
 
4d44fe
+	memset(map_info, 0, MAP_INFO_SZ);
4d44fe
 	int ret = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &bpf_map_get_info_attr,
4d44fe
 			  sizeof(bpf_map_get_info_attr));
4d44fe
 	if (ret < 0)
4d44fe
@@ -350,7 +332,7 @@
4d44fe
 	 * initializer element is not constant.
4d44fe
 	 */
4d44fe
 	#define  PROG_INFO_SZ (sizeof(*prog_info) + 64)
4d44fe
-	struct bpf_prog_info_struct *prog_info = calloc(1, PROG_INFO_SZ);
4d44fe
+	struct bpf_prog_info_struct *prog_info = tail_alloc(PROG_INFO_SZ);
4d44fe
 	struct bpf_insn *xlated_prog = tail_alloc(sizeof(*xlated_prog) * 42);
4d44fe
 	uint32_t *map_ids = tail_alloc(sizeof(*map_ids) * 2);
4d44fe
 	struct BPF_OBJ_GET_INFO_BY_FD_struct bpf_prog_get_info_attr = {
4d44fe
@@ -360,6 +342,7 @@
4d44fe
 	};
4d44fe
 	size_t old_prog_info_len = PROG_INFO_SZ;
4d44fe
 
4d44fe
+	memset(prog_info, 0, PROG_INFO_SZ);
4d44fe
 	for (unsigned int i = 0; i < 4; i++) {
4d44fe
 		prog_info->jited_prog_len = 0;
4d44fe
 		switch (i) {
4d44fe
Index: strace-4.24/tests-mx32/bpf-obj_get_info_by_fd.c
4d44fe
===================================================================
4d44fe
--- strace-4.24.orig/tests-mx32/bpf-obj_get_info_by_fd.c	2018-06-04 03:11:05.000000000 +0200
4d44fe
+++ strace-4.24/tests-mx32/bpf-obj_get_info_by_fd.c	2019-03-10 05:35:48.837571989 +0100
4d44fe
@@ -4,27 +4,7 @@
4d44fe
  * Copyright (c) 2018 The strace developers.
4d44fe
  * All rights reserved.
4d44fe
  *
4d44fe
- * Redistribution and use in source and binary forms, with or without
4d44fe
- * modification, are permitted provided that the following conditions
4d44fe
- * are met:
4d44fe
- * 1. Redistributions of source code must retain the above copyright
4d44fe
- *    notice, this list of conditions and the following disclaimer.
4d44fe
- * 2. Redistributions in binary form must reproduce the above copyright
4d44fe
- *    notice, this list of conditions and the following disclaimer in the
4d44fe
- *    documentation and/or other materials provided with the distribution.
4d44fe
- * 3. The name of the author may not be used to endorse or promote products
4d44fe
- *    derived from this software without specific prior written permission.
4d44fe
- *
4d44fe
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4d44fe
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4d44fe
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4d44fe
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4d44fe
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4d44fe
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4d44fe
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4d44fe
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4d44fe
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
4d44fe
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4d44fe
+ * SPDX-License-Identifier: GPL-2.0-or-later
4d44fe
  */
4d44fe
 
4d44fe
 #include "tests.h"
4d44fe
@@ -38,6 +18,7 @@
4d44fe
 #include <stdio.h>
4d44fe
 #include <stdint.h>
4d44fe
 #include <stdlib.h>
4d44fe
+#include <string.h>
4d44fe
 #include <unistd.h>
4d44fe
 #include <sys/sysmacros.h>
4d44fe
 #include <asm/unistd.h>
4d44fe
@@ -294,13 +275,14 @@
4d44fe
 	 * initializer element is not constant.
4d44fe
 	 */
4d44fe
 	#define  MAP_INFO_SZ (sizeof(*map_info) + 64)
4d44fe
-	struct bpf_map_info_struct *map_info = calloc(1, MAP_INFO_SZ);
4d44fe
+	struct bpf_map_info_struct *map_info = tail_alloc(MAP_INFO_SZ);
4d44fe
 	struct BPF_OBJ_GET_INFO_BY_FD_struct bpf_map_get_info_attr = {
4d44fe
 		.bpf_fd   = map_fd,
4d44fe
 		.info_len = MAP_INFO_SZ,
4d44fe
 		.info     = (uintptr_t) map_info,
4d44fe
 	};
4d44fe
 
4d44fe
+	memset(map_info, 0, MAP_INFO_SZ);
4d44fe
 	int ret = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &bpf_map_get_info_attr,
4d44fe
 			  sizeof(bpf_map_get_info_attr));
4d44fe
 	if (ret < 0)
4d44fe
@@ -350,7 +332,7 @@
4d44fe
 	 * initializer element is not constant.
4d44fe
 	 */
4d44fe
 	#define  PROG_INFO_SZ (sizeof(*prog_info) + 64)
4d44fe
-	struct bpf_prog_info_struct *prog_info = calloc(1, PROG_INFO_SZ);
4d44fe
+	struct bpf_prog_info_struct *prog_info = tail_alloc(PROG_INFO_SZ);
4d44fe
 	struct bpf_insn *xlated_prog = tail_alloc(sizeof(*xlated_prog) * 42);
4d44fe
 	uint32_t *map_ids = tail_alloc(sizeof(*map_ids) * 2);
4d44fe
 	struct BPF_OBJ_GET_INFO_BY_FD_struct bpf_prog_get_info_attr = {
4d44fe
@@ -360,6 +342,7 @@
4d44fe
 	};
4d44fe
 	size_t old_prog_info_len = PROG_INFO_SZ;
4d44fe
 
4d44fe
+	memset(prog_info, 0, PROG_INFO_SZ);
4d44fe
 	for (unsigned int i = 0; i < 4; i++) {
4d44fe
 		prog_info->jited_prog_len = 0;
4d44fe
 		switch (i) {