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

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