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: <20240413213904.146261-1-qde@naccy.de>
Date: Sat, 13 Apr 2024 23:39:04 +0200
From: Quentin Deslandes <qde@...cy.de>
To: bpf@...r.kernel.org,
	Quentin Deslandes <qde@...cy.de>
Cc: Andrii Nakryiko <andrii@...nel.org>,
	Eduard Zingerman <eddyz87@...il.com>,
	Alexei Starovoitov <ast@...nel.org>,
	Daniel Borkmann <daniel@...earbox.net>,
	Martin KaFai Lau <martin.lau@...ux.dev>,
	Song Liu <song@...nel.org>,
	Yonghong Song <yonghong.song@...ux.dev>,
	John Fastabend <john.fastabend@...il.com>,
	KP Singh <kpsingh@...nel.org>,
	Stanislav Fomichev <sdf@...gle.com>,
	Hao Luo <haoluo@...gle.com>,
	Jiri Olsa <jolsa@...nel.org>,
	linux-kernel@...r.kernel.org,
	kernel-team@...a.com
Subject: [RFC PATCH bpf-next] libbpf: print character arrays as strings if possible

Introduce the new print_strings flag in btf_dump_type_data_opts. If
enabled, libbpf will print character arrays as strings if they meet the
following conditions:
- Contains a nul-termination character ('\0')
- Contains only printable characters before the nul-termination character

If print_strings is set to false (default value), the existing
behavior remains unchanged.

With print_strings=false:
str_array = (__u8[14])[
    'H',
    'e',
    'l',
    'l',
    'o',
],

With print_strings=true:
str_array = (__u8[14])"Hello",

Signed-off-by: Quentin Deslandes <qde@...cy.de>
---
 tools/lib/bpf/btf.h      |  3 ++-
 tools/lib/bpf/btf_dump.c | 25 ++++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 8e6880d91c84..cf190900d483 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -284,9 +284,10 @@ struct btf_dump_type_data_opts {
 	bool compact;		/* no newlines/indentation */
 	bool skip_names;	/* skip member/type names */
 	bool emit_zeroes;	/* show 0-valued fields */
+	bool print_strings; /* print char arrays as string */
 	size_t :0;
 };
-#define btf_dump_type_data_opts__last_field emit_zeroes
+#define btf_dump_type_data_opts__last_field print_strings

 LIBBPF_API int
 btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 4d9f30bf7f01..d396a10f37b2 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -67,6 +67,7 @@ struct btf_dump_data {
 	bool compact;
 	bool skip_names;
 	bool emit_zeroes;
+	bool print_strings;
 	__u8 indent_lvl;	/* base indent level */
 	char indent_str[BTF_DATA_INDENT_STR_LEN];
 	/* below are used during iteration */
@@ -2021,6 +2022,21 @@ static int btf_dump_var_data(struct btf_dump *d,
 	return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0);
 }

+static bool btf_dump_isprint_str(const char *data, unsigned int len)
+{
+	unsigned int i;
+
+	for (i = 0; i < len; ++i) {
+		if (data[i] == '\0')
+			return true;
+
+		if (!isprint(data[i]))
+			return false;
+	}
+
+	return false;
+}
+
 static int btf_dump_array_data(struct btf_dump *d,
 			       const struct btf_type *t,
 			       __u32 id,
@@ -2047,8 +2063,14 @@ static int btf_dump_array_data(struct btf_dump *d,
 		 * char arrays, so if size is 1 and element is
 		 * printable as a char, we'll do that.
 		 */
-		if (elem_size == 1)
+		if (elem_size == 1) {
 			d->typed_dump->is_array_char = true;
+			if (d->typed_dump->print_strings &&
+					btf_dump_isprint_str(data, array->nelems)) {
+				btf_dump_type_values(d, "\"%s\"", data);
+				return 0;
+			}
+		}
 	}

 	/* note that we increment depth before calling btf_dump_print() below;
@@ -2533,6 +2555,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
 	d->typed_dump->compact = OPTS_GET(opts, compact, false);
 	d->typed_dump->skip_names = OPTS_GET(opts, skip_names, false);
 	d->typed_dump->emit_zeroes = OPTS_GET(opts, emit_zeroes, false);
+	d->typed_dump->print_strings = OPTS_GET(opts, print_strings, false);

 	ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);

--
2.44.0

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ