File 0063-mdadm-refactor-ident-name-handling.patch of Package mdadm.35776

From 67417d9222c505103357191bb0e0ae300892e8a9 Mon Sep 17 00:00:00 2001
From: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Date: Thu, 1 Jun 2023 09:27:48 +0200
Subject: [PATCH] mdadm: refactor ident->name handling
Git-commit: 67417d9222c505103357191bb0e0ae300892e8a9
Patch-mainline: mdadm-4.3
References: bsc#1226413


Create dedicated setter for name in mddev_ident and propagate it.
Following changes are made:
- move duplicated code from  config.c and mdadm.c into new function.
- Add error enum in mdadm.h.
- Use MD_NAME_MAX instead of hardcoded value in mddev_ident.
- Use secure functions.
- Add more detailed verification of the name.
- make error messages reusable for cmdline and config:
    - for cmdline, these are errors so use pr_err().
    - for config, these are just warnings, so use pr_info().

(Coly Li: only keep changes of lib.c and mdadm.h, for SLE15-SP5)


Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
Signed-off-by: Coly Li <colyli@suse.de>
---
 lib.c    | 18 +++++++++++++
 mdadm.h  | 20 ++++++++++-----
 4 files changed, 104 insertions(+), 23 deletions(-)

Index: mdadm-4.2/lib.c
===================================================================
--- mdadm-4.2.orig/lib.c
+++ mdadm-4.2/lib.c
@@ -27,6 +27,24 @@
 #include	<ctype.h>
 #include	<limits.h>
 
+/**
+ * is_string_lq() - Check if string length with NULL byte is lower or equal to requested.
+ * @str: string to check.
+ * @max_len: max length.
+ *
+ * @str length must be bigger than 0 and be lower or equal @max_len, including termination byte.
+ */
+bool is_string_lq(const char * const str, size_t max_len)
+{
+	assert(str);
+
+	size_t _len = strnlen(str, max_len);
+
+	if (_len > 0 && _len < max_len)
+		return true;
+	return false;
+}
+
 bool is_dev_alive(char *path)
 {
 	if (!path)
Index: mdadm-4.2/mdadm.h
===================================================================
--- mdadm-4.2.orig/mdadm.h
+++ mdadm-4.2/mdadm.h
@@ -281,6 +281,11 @@ static inline void __put_unaligned32(__u
 
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
 
+/**
+ * This is true for native and DDF, IMSM allows 16.
+ */
+#define MD_NAME_MAX 32
+
 extern const char Name[];
 
 struct md_bb_entry {
@@ -412,6 +417,12 @@ struct spare_criteria {
 	unsigned int sector_size;
 };
 
+typedef enum mdadm_status {
+	MDADM_STATUS_SUCCESS = 0,
+	MDADM_STATUS_ERROR,
+	MDADM_STATUS_UNDEF,
+} mdadm_status_t;
+
 enum mode {
 	ASSEMBLE=1,
 	BUILD,
@@ -534,7 +545,7 @@ struct mddev_ident {
 
 	int	uuid_set;
 	int	uuid[4];
-	char	name[33];
+	char	name[MD_NAME_MAX + 1];
 
 	int super_minor;
 
@@ -1546,6 +1557,7 @@ extern int check_partitions(int fd, char
 extern int fstat_is_blkdev(int fd, char *devname, dev_t *rdev);
 extern int stat_is_blkdev(char *devname, dev_t *rdev);
 
+extern bool is_string_lq(const char * const str, size_t max_len);
 extern bool is_dev_alive(char *path);
 extern int get_mdp_major(void);
 extern int get_maj_min(char *dev, int *major, int *minor);
@@ -1931,11 +1943,6 @@ enum r0layout {
 #define VARIABLE_OFFSET 3
 
 /**
- * This is true for native and DDF, IMSM allows 16.
- */
-#define MD_NAME_MAX 32
-
-/**
  * is_container() - check if @level is &LEVEL_CONTAINER
  * @level: level value
  *
openSUSE Build Service is sponsored by