diff mbox series

dm: Allow the use of escaped characters in str_field_delimit()

Message ID 20241112175758.114080-1-pvmohammedanees2003@gmail.com (mailing list archive)
State New
Headers show
Series dm: Allow the use of escaped characters in str_field_delimit() | expand

Commit Message

Mohammed Anees Nov. 12, 2024, 5:57 p.m. UTC
Escape characters were not handled before, which could lead to
unwanted issues. Some device-mapper names may contain backslashes (`\`)
as valid characters and should not be treated as escape characters. Only
escape characters followed directly by the separator are considered
valid and need to be processed. After handling, the escape characters
are removed to ensure the final string is correctly parsed without
unwanted escape sequences which were used only for escaping.

Signed-off-by: Mohammed Anees <pvmohammedanees2003@gmail.com>
---
 drivers/md/dm-init.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

Comments

Zdenek Kabelac Nov. 13, 2024, 10:39 a.m. UTC | #1
Dne 12. 11. 24 v 18:57 Mohammed Anees napsal(a):
> Escape characters were not handled before, which could lead to
> unwanted issues. Some device-mapper names may contain backslashes (`\`)
> as valid characters and should not be treated as escape characters. Only
> escape characters followed directly by the separator are considered
> valid and need to be processed. After handling, the escape characters
> are removed to ensure the final string is correctly parsed without
> unwanted escape sequences which were used only for escaping.
>
> Signed-off-by: Mohammed Anees <pvmohammedanees2003@gmail.com>
> ---
>   drivers/md/dm-init.c | 28 ++++++++++++++++++++++++----
>   1 file changed, 24 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
> index b37bbe762500..dad9d523f7fb 100644
> --- a/drivers/md/dm-init.c
> +++ b/drivers/md/dm-init.c
> @@ -88,13 +88,33 @@ static void __init dm_setup_cleanup(struct list_head *devices)
>   static char __init *str_field_delimit(char **str, char separator)
>   {
>

There is libdevmapper  project  (ATM distirbuted through lvm2 project) which 
is handling the management of name & uuid with mangling - so they are properly 
visible on systems with udev.

IMHO this escape handling does not belong to kernel and is rather related to 
the actual user space running on top.


Regards


Zdenek
Mikulas Patocka Nov. 18, 2024, 10:38 a.m. UTC | #2
On Tue, 12 Nov 2024, Mohammed Anees wrote:

> Escape characters were not handled before, which could lead to
> unwanted issues. Some device-mapper names may contain backslashes (`\`)
> as valid characters and should not be treated as escape characters. Only
> escape characters followed directly by the separator are considered
> valid and need to be processed. After handling, the escape characters
> are removed to ensure the final string is correctly parsed without
> unwanted escape sequences which were used only for escaping.
> 
> Signed-off-by: Mohammed Anees <pvmohammedanees2003@gmail.com>

Hi

Does anyone really need this? Is there some use case for using escape 
characters in device mapper names?

Mikulas

> ---
>  drivers/md/dm-init.c | 28 ++++++++++++++++++++++++----
>  1 file changed, 24 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
> index b37bbe762500..dad9d523f7fb 100644
> --- a/drivers/md/dm-init.c
> +++ b/drivers/md/dm-init.c
> @@ -88,13 +88,33 @@ static void __init dm_setup_cleanup(struct list_head *devices)
>  static char __init *str_field_delimit(char **str, char separator)
>  {
>  	char *s;
> +	/* This variable handles removing escape characters, which are
> +	 * only used to avoid the separator and aren't needed in the
> +	 * final string.
> +	 */
> +	char *write;
>  
> -	/* TODO: add support for escaped characters */
>  	*str = skip_spaces(*str);
> -	s = strchr(*str, separator);
> +	s = *str;
> +	write = *str;
> +
> +	/* Find the separator and handle escape character */
> +	while (*s) {
> +		/* If '\' is followed by the separator, skip '\' by
> +		 * incrementing s, write will then overwrite the
> +		 * escape character with the separator.
> +		 */
> +		if (*s == '\\' && *(s + 1) != '\0' && *(s + 1) == separator)
> +			s++;
> +		else if (*s == separator)
> +			break;
> +
> +		*write++ = *s++;
> +	}
> +
>  	/* Delimit the field and remove trailing spaces */
> -	if (s)
> -		*s = '\0';
> +	if (write)
> +		*write = '\0';
>  	*str = strim(*str);
>  	return s ? ++s : NULL;
>  }
> -- 
> 2.47.0
>
diff mbox series

Patch

diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
index b37bbe762500..dad9d523f7fb 100644
--- a/drivers/md/dm-init.c
+++ b/drivers/md/dm-init.c
@@ -88,13 +88,33 @@  static void __init dm_setup_cleanup(struct list_head *devices)
 static char __init *str_field_delimit(char **str, char separator)
 {
 	char *s;
+	/* This variable handles removing escape characters, which are
+	 * only used to avoid the separator and aren't needed in the
+	 * final string.
+	 */
+	char *write;
 
-	/* TODO: add support for escaped characters */
 	*str = skip_spaces(*str);
-	s = strchr(*str, separator);
+	s = *str;
+	write = *str;
+
+	/* Find the separator and handle escape character */
+	while (*s) {
+		/* If '\' is followed by the separator, skip '\' by
+		 * incrementing s, write will then overwrite the
+		 * escape character with the separator.
+		 */
+		if (*s == '\\' && *(s + 1) != '\0' && *(s + 1) == separator)
+			s++;
+		else if (*s == separator)
+			break;
+
+		*write++ = *s++;
+	}
+
 	/* Delimit the field and remove trailing spaces */
-	if (s)
-		*s = '\0';
+	if (write)
+		*write = '\0';
 	*str = strim(*str);
 	return s ? ++s : NULL;
 }