Skip to contentSkip to conversation

main - asan: fix some reports from libasan

PATCH Zdenek Kabelac 1 participant 1 message Feb 7, 2022

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=f83b3962c10bf4e196fec0e3e735820ba908661f
Commit:        f83b3962c10bf4e196fec0e3e735820ba908661f
Parent:        8dccc2314e2482370bc6e5cf007eb210994abdef
Author:        Zdenek Kabelac <[email protected]>
AuthorDate:    Mon Feb 7 19:58:04 2022 +0100
Committer:     Zdenek Kabelac <[email protected]>
CommitterDate: Mon Feb 7 20:02:11 2022 +0100

asan: fix some reports from libasan

When compiled and used with:

CFLAGS="-fsanitize=address -g -O0" ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1

we have few reported issue - they where not normally spotted, since we were still accessing our own memory - but ouf of buffer-range.

TODO: there is still something to enhance with handling of #orphan vgids

---
 lib/config/config.c              | 4 +++-
 lib/format_text/format-text.c    | 2 +-
 lib/format_text/text_label.c     | 6 ++++--
 lib/metadata/metadata-exported.h | 2 +-
 lib/uuid/uuid.c                  | 5 +++++
 tools/Makefile.in                | 4 ++--
 tools/command.c                  | 2 +-
 7 files changed, 17 insertions(+), 8 deletions(-)
lib/config/config.c +3 -1
index c8dab5683..f9614779a 100644
@@ -522,7 +522,9 @@ int config_file_read_fd(struct dm_config_tree *cft, struct device *dev, dev_io_r
 	if (!(dev->flags & DEV_REGULAR) || size2)
 		use_plain_read = 0;
 
-	if (!(buf = zalloc(size + size2))) {
+	/* Ensure there is extra '\0' after end of buffer since we pass
+	 * buffer to funtions like strtoll() */
+	if (!(buf = zalloc(size + size2 + 1))) {
 		log_error("Failed to allocate circular buffer.");
 		return 0;
 	}
lib/format_text/format-text.c +1 -1
index 00443faa1..07aaa0b28 100644
@@ -2601,7 +2601,7 @@ struct format_type *create_text_format(struct cmd_context *cmd)
 	fmt->ops = &_text_handler;
 	fmt->name = FMT_TEXT_NAME;
 	fmt->alias = FMT_TEXT_ALIAS;
-	fmt->orphan_vg_name = ORPHAN_VG_NAME(FMT_TEXT_NAME);
+	strncpy(fmt->orphan_vg_name, ORPHAN_VG_NAME(FMT_TEXT_NAME), sizeof(fmt->orphan_vg_name));
 	fmt->features = FMT_SEGMENTS | FMT_TAGS | FMT_PRECOMMIT |
 			FMT_UNLIMITED_VOLS | FMT_RESIZE_PV |
 			FMT_UNLIMITED_STRIPESIZE | FMT_CONFIG_PROFILE |
lib/format_text/text_label.c +4 -2
index 29b470271..324c86e44 100644
@@ -410,6 +410,7 @@ static int _text_read(struct cmd_context *cmd, struct labeller *labeller, struct
 {
 	struct lvmcache_vgsummary vgsummary;
 	char pvid[ID_LEN + 1] __attribute__((aligned(8))) = { 0 };
+	char vgid[ID_LEN + 1] __attribute__((aligned(8))) = { 0 };
 	struct lvmcache_info *info;
 	const struct format_type *fmt = labeller->fmt;
 	struct label_header *lh = (struct label_header *) label_buf;
@@ -433,6 +434,7 @@ static int _text_read(struct cmd_context *cmd, struct labeller *labeller, struct
 	pvhdr = (struct pv_header *) ((char *) label_buf + xlate32(lh->offset_xl));
 
 	memcpy(pvid, &pvhdr->pv_uuid, ID_LEN);
+	strncpy(vgid, FMT_TEXT_ORPHAN_VG_NAME, ID_LEN);
 
 	/*
 	 * FIXME: stop adding the device to lvmcache initially as an orphan
@@ -449,8 +451,8 @@ static int _text_read(struct cmd_context *cmd, struct labeller *labeller, struct
 	 * Other reasons for lvmcache_add to return NULL are internal errors.
 	 */
 	if (!(info = lvmcache_add(cmd, labeller, pvid, dev, label_sector,
-				  FMT_TEXT_ORPHAN_VG_NAME,
-				  FMT_TEXT_ORPHAN_VG_NAME, 0, is_duplicate)))
+				  vgid,
+				  vgid, 0, is_duplicate)))
 		return_0;
 
 	lvmcache_set_device_size(info, xlate64(pvhdr->device_size_xl));
lib/metadata/metadata-exported.h +1 -1
index 7bac5b900..fd370d4b2 100644
@@ -371,7 +371,7 @@ struct format_type {
 	struct labeller *labeller;
 	const char *name;
 	const char *alias;
-	const char *orphan_vg_name;
+	char orphan_vg_name[ID_LEN];
 	struct volume_group *orphan_vg; /* Only one ever exists. */
 	uint32_t features;
 	void *library;
lib/uuid/uuid.c +5 -0
index be4cbc3ec..d8b72422b 100644
@@ -168,6 +168,11 @@ int id_write_format(const struct id *id, char *buffer, size_t size)
 
 	assert(ID_LEN == 32);
 
+	if (id->uuid[0] == '#') {
+		(void) dm_strncpy(buffer, (char*)id->uuid, size);
+		return 1;
+	}
+
 	/* split into groups separated by dashes */
 	if (size < (32 + 6 + 1)) {
 		if (size > 0)
tools/Makefile.in +2 -2
index e0e180474..25fe3b6b3 100644
@@ -190,9 +190,9 @@ command-lines-input.h: $(srcdir)/command-lines.in Makefile
 	$(Q) set -o pipefail && \
 	( cat $(srcdir)/license.inc && \
 	  echo "/* Do not edit. This file is generated by the Makefile. */" && \
-	  echo -en "const char _command_input[] =\n\n\"" && \
+	  echo -en "static const char _command_input[] =\n\n\"" && \
 	  $(EGREP) -v '^#|\-\-\-|^$$' $(srcdir)/command-lines.in | $(AWK) 'BEGIN {ORS = "\\n\"\n\""} //' && \
-	  echo "\\n\";" \
+	  echo "\\n\\n\";" \
 	) > $@
 
 $(SOURCES:%.c=%.d) $(SOURCES2:%.c=%.d): command-lines-input.h command-count.h cmds.h
tools/command.c +1 -1
index 18ffd64ed..8de8825e4 100644
@@ -2549,7 +2549,7 @@ static const char *_man_long_opt_name(const char *cmdname, int opt_enum)
 	}
 
 	if (strchr(long_opt, '[')) {
-		for (i = 0; i < sizeof(long_opt_name) - 1; ++long_opt, ++i) {
+		for (i = 0; *long_opt && i < sizeof(long_opt_name) - 1; ++long_opt, ++i) {
 			if (i < (sizeof(long_opt_name) - 8))
 				switch(*long_opt) {
 				case '[':