lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220517112748.358295-1-shaozhengchao@huawei.com>
Date:   Tue, 17 May 2022 19:27:48 +0800
From:   Zhengchao Shao <shaozhengchao@...wei.com>
To:     <bpf@...r.kernel.org>, <netdev@...r.kernel.org>,
        <linux-kernel@...r.kernel.org>, <ast@...nel.org>,
        <daniel@...earbox.net>, <davem@...emloft.net>, <kuba@...nel.org>,
        <hawk@...nel.org>, <john.fastabend@...il.com>, <andrii@...nel.org>,
        <kafai@...com>, <songliubraving@...com>, <yhs@...com>,
        <kpsingh@...nel.org>
CC:     <weiyongjun1@...wei.com>, <shaozhengchao@...wei.com>,
        <yuehaibing@...wei.com>
Subject: [PATCH v2,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd

Before detach the prog, we should check detach prog exist or not.

Signed-off-by: Zhengchao Shao <shaozhengchao@...wei.com>
---
 samples/bpf/xdp_fwd_user.c | 52 +++++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 9 deletions(-)

diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 1828487bae9a..2294486ef10a 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -47,17 +47,51 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
 	return err;
 }
 
-static int do_detach(int idx, const char *name)
+static int do_detach(int idx, const char *name, const char *prog_name)
 {
-	int err;
+	int err = 1;
+	__u32 info_len, curr_prog_id;
+	struct bpf_prog_info prog_info = {};
+	int prog_fd;
+	char namepad[BPF_OBJ_NAME_LEN];
+
+	if (bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id)) {
+		printf("ERROR: bpf_xdp_query_id failed\n");
+		return err;
+	}
+
+	if (!curr_prog_id) {
+		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
+			xdp_flags, name);
+		return err;
+	}
 
-	err = bpf_xdp_detach(idx, xdp_flags, NULL);
-	if (err < 0)
-		printf("ERROR: failed to detach program from %s\n", name);
+	info_len = sizeof(prog_info);
+	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
+	if (prog_fd < 0 && errno == ENOENT) {
+		printf("ERROR: bpf_prog_get_fd_by_id failed\n");
+		return err;
+	}
+
+	err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
+	if (err) {
+		printf("ERROR: bpf_obj_get_info_by_fd failed\n");
+		return err;
+	}
+	snprintf(namepad, sizeof(namepad), "%s_prog", prog_name);
+
+	if (strcmp(prog_info.name, namepad)) {
+		printf("ERROR: %s isn't attached to %s\n", prog_name, name);
+	} else {
+		err = bpf_xdp_detach(idx, xdp_flags, NULL);
+		if (err < 0)
+			printf("ERROR: failed to detach program from %s\n",
+				name);
+		/* TODO: Remember to cleanup map, when adding use of shared map
+		 *  bpf_map_delete_elem((map_fd, &idx);
+		 */
+	}
 
-	/* TODO: Remember to cleanup map, when adding use of shared map
-	 *  bpf_map_delete_elem((map_fd, &idx);
-	 */
 	return err;
 }
 
@@ -169,7 +203,7 @@ int main(int argc, char **argv)
 			return 1;
 		}
 		if (!attach) {
-			err = do_detach(idx, argv[i]);
+			err = do_detach(idx, argv[i], prog_name);
 			if (err)
 				ret = err;
 		} else {
-- 
2.17.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ