naccyde / rpms / iproute

Forked from rpms/iproute 8 months ago
Clone

Blame SOURCES/0057-bpf-remove-strict-dependency-on-af_alg.patch

8def76
From 19729e1302017ef33e139903b28f9a778b2a8748 Mon Sep 17 00:00:00 2001
8def76
From: Andrea Claudi <aclaudi@redhat.com>
8def76
Date: Thu, 13 Jun 2019 14:37:56 +0200
8def76
Subject: [PATCH] bpf: remove strict dependency on af_alg
8def76
8def76
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1716361
8def76
Upstream Status: iproute2.git commit 6e5094dbb7c06
8def76
8def76
commit 6e5094dbb7c0682a9ca6eb2a64ec51f0a8a33a22
8def76
Author: Daniel Borkmann <daniel@iogearbox.net>
8def76
Date:   Wed Jul 18 01:31:20 2018 +0200
8def76
8def76
    bpf: remove strict dependency on af_alg
8def76
8def76
    Do not bail out when AF_ALG is not supported by the kernel and
8def76
    only do so when a map is requested in object ns where we're
8def76
    calculating the hash. Otherwise, the loader can operate just
8def76
    fine, therefore lets not fail early when it's not needed.
8def76
8def76
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
8def76
    Signed-off-by: David Ahern <dsahern@gmail.com>
8def76
---
8def76
 lib/bpf.c | 74 +++++++++++++++++++++----------------------------------
8def76
 1 file changed, 28 insertions(+), 46 deletions(-)
8def76
8def76
diff --git a/lib/bpf.c b/lib/bpf.c
8def76
index 9dc37c787d907..ead8b5a7219f0 100644
8def76
--- a/lib/bpf.c
8def76
+++ b/lib/bpf.c
8def76
@@ -1130,6 +1130,7 @@ struct bpf_elf_ctx {
8def76
 	GElf_Ehdr		elf_hdr;
8def76
 	Elf_Data		*sym_tab;
8def76
 	Elf_Data		*str_tab;
8def76
+	char			obj_uid[64];
8def76
 	int			obj_fd;
8def76
 	int			map_fds[ELF_MAX_MAPS];
8def76
 	struct bpf_elf_map	maps[ELF_MAX_MAPS];
8def76
@@ -1143,6 +1144,7 @@ struct bpf_elf_ctx {
8def76
 	enum bpf_prog_type	type;
8def76
 	__u32			ifindex;
8def76
 	bool			verbose;
8def76
+	bool			noafalg;
8def76
 	struct bpf_elf_st	stat;
8def76
 	struct bpf_hash_entry	*ht[256];
8def76
 	char			*log;
8def76
@@ -1258,22 +1260,15 @@ static int bpf_obj_hash(const char *object, uint8_t *out, size_t len)
8def76
 		return -EINVAL;
8def76
 
8def76
 	cfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
8def76
-	if (cfd < 0) {
8def76
-		fprintf(stderr, "Cannot get AF_ALG socket: %s\n",
8def76
-			strerror(errno));
8def76
+	if (cfd < 0)
8def76
 		return cfd;
8def76
-	}
8def76
 
8def76
 	ret = bind(cfd, (struct sockaddr *)&alg, sizeof(alg));
8def76
-	if (ret < 0) {
8def76
-		fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
8def76
+	if (ret < 0)
8def76
 		goto out_cfd;
8def76
-	}
8def76
 
8def76
 	ofd = accept(cfd, NULL, 0);
8def76
 	if (ofd < 0) {
8def76
-		fprintf(stderr, "Error accepting socket: %s\n",
8def76
-			strerror(errno));
8def76
 		ret = ofd;
8def76
 		goto out_cfd;
8def76
 	}
8def76
@@ -1318,29 +1313,7 @@ out_cfd:
8def76
 	return ret;
8def76
 }
8def76
 
8def76
-static const char *bpf_get_obj_uid(const char *pathname)
8def76
-{
8def76
-	static bool bpf_uid_cached;
8def76
-	static char bpf_uid[64];
8def76
-	uint8_t tmp[20];
8def76
-	int ret;
8def76
-
8def76
-	if (bpf_uid_cached)
8def76
-		goto done;
8def76
-
8def76
-	ret = bpf_obj_hash(pathname, tmp, sizeof(tmp));
8def76
-	if (ret) {
8def76
-		fprintf(stderr, "Object hashing failed!\n");
8def76
-		return NULL;
8def76
-	}
8def76
-
8def76
-	hexstring_n2a(tmp, sizeof(tmp), bpf_uid, sizeof(bpf_uid));
8def76
-	bpf_uid_cached = true;
8def76
-done:
8def76
-	return bpf_uid;
8def76
-}
8def76
-
8def76
-static int bpf_init_env(const char *pathname)
8def76
+static void bpf_init_env(void)
8def76
 {
8def76
 	struct rlimit limit = {
8def76
 		.rlim_cur = RLIM_INFINITY,
8def76
@@ -1350,15 +1323,8 @@ static int bpf_init_env(const char *pathname)
8def76
 	/* Don't bother in case we fail! */
8def76
 	setrlimit(RLIMIT_MEMLOCK, &limit);
8def76
 
8def76
-	if (!bpf_get_work_dir(BPF_PROG_TYPE_UNSPEC)) {
8def76
+	if (!bpf_get_work_dir(BPF_PROG_TYPE_UNSPEC))
8def76
 		fprintf(stderr, "Continuing without mounted eBPF fs. Too old kernel?\n");
8def76
-		return 0;
8def76
-	}
8def76
-
8def76
-	if (!bpf_get_obj_uid(pathname))
8def76
-		return -1;
8def76
-
8def76
-	return 0;
8def76
 }
8def76
 
8def76
 static const char *bpf_custom_pinning(const struct bpf_elf_ctx *ctx,
8def76
@@ -1394,7 +1360,7 @@ static void bpf_make_pathname(char *pathname, size_t len, const char *name,
8def76
 	case PIN_OBJECT_NS:
8def76
 		snprintf(pathname, len, "%s/%s/%s",
8def76
 			 bpf_get_work_dir(ctx->type),
8def76
-			 bpf_get_obj_uid(NULL), name);
8def76
+			 ctx->obj_uid, name);
8def76
 		break;
8def76
 	case PIN_GLOBAL_NS:
8def76
 		snprintf(pathname, len, "%s/%s/%s",
8def76
@@ -1427,7 +1393,7 @@ static int bpf_make_obj_path(const struct bpf_elf_ctx *ctx)
8def76
 	int ret;
8def76
 
8def76
 	snprintf(tmp, sizeof(tmp), "%s/%s", bpf_get_work_dir(ctx->type),
8def76
-		 bpf_get_obj_uid(NULL));
8def76
+		 ctx->obj_uid);
8def76
 
8def76
 	ret = mkdir(tmp, S_IRWXU);
8def76
 	if (ret && errno != EEXIST) {
8def76
@@ -1696,6 +1662,12 @@ static int bpf_maps_attach_all(struct bpf_elf_ctx *ctx)
8def76
 	const char *map_name;
8def76
 
8def76
 	for (i = 0; i < ctx->map_num; i++) {
8def76
+		if (ctx->maps[i].pinning == PIN_OBJECT_NS &&
8def76
+		    ctx->noafalg) {
8def76
+			fprintf(stderr, "Missing kernel AF_ALG support for PIN_OBJECT_NS!\n");
8def76
+			return -ENOTSUP;
8def76
+		}
8def76
+
8def76
 		map_name = bpf_map_fetch_name(ctx, i);
8def76
 		if (!map_name)
8def76
 			return -EIO;
8def76
@@ -2451,14 +2423,24 @@ static int bpf_elf_ctx_init(struct bpf_elf_ctx *ctx, const char *pathname,
8def76
 			    enum bpf_prog_type type, __u32 ifindex,
8def76
 			    bool verbose)
8def76
 {
8def76
-	int ret = -EINVAL;
8def76
+	uint8_t tmp[20];
8def76
+	int ret;
8def76
 
8def76
-	if (elf_version(EV_CURRENT) == EV_NONE ||
8def76
-	    bpf_init_env(pathname))
8def76
-		return ret;
8def76
+	if (elf_version(EV_CURRENT) == EV_NONE)
8def76
+		return -EINVAL;
8def76
+
8def76
+	bpf_init_env();
8def76
 
8def76
 	memset(ctx, 0, sizeof(*ctx));
8def76
 	bpf_get_cfg(ctx);
8def76
+
8def76
+	ret = bpf_obj_hash(pathname, tmp, sizeof(tmp));
8def76
+	if (ret)
8def76
+		ctx->noafalg = true;
8def76
+	else
8def76
+		hexstring_n2a(tmp, sizeof(tmp), ctx->obj_uid,
8def76
+			      sizeof(ctx->obj_uid));
8def76
+
8def76
 	ctx->verbose = verbose;
8def76
 	ctx->type    = type;
8def76
 	ctx->ifindex = ifindex;
8def76
-- 
8def76
2.20.1
8def76