LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File 0012-usock-accept-multiline-message-string-parameters.patch of Package libteam (Project home:ods:ppc64)

From 7262e2ce0ca6937879e58735bd4e2388417f87fa Mon Sep 17 00:00:00 2001
From: Jiri Pirko <jiri@resnulli.us>
Date: Thu, 6 Mar 2014 19:02:15 +0100
Subject: [patch libteam] usock: accept multiline message string parameters

Signed-off-by: Jiri Pirko <jiri@resnulli.us>
---
 libteamdctl/cli_usock.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 teamd/teamd_usock.c     | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/libteamdctl/cli_usock.c b/libteamdctl/cli_usock.c
index 74d5a7a..998c5ce 100644
--- a/libteamdctl/cli_usock.c
+++ b/libteamdctl/cli_usock.c
@@ -115,6 +115,40 @@ static int myasprintf(char **p_str, const char *fmt, ...)
 	return 0;
 }
 
+char *__strencode(char *str)
+{
+	char *newstr;
+	int i, j;
+	size_t len = strlen(str);
+
+	for (i = 0; i < strlen(str); i++) {
+		switch (str[i]) {
+		case '\n':
+		case '\\':
+			len++;
+		}
+	}
+	newstr = malloc(sizeof(char) * (len + 1));
+	if (!newstr)
+		return NULL;
+	j = 0;
+	for (i = 0; i <= strlen(str); i++) {
+		switch (str[i]) {
+		case '\n':
+			newstr[j++] = '\\';
+			newstr[j++] = 'n';
+			break;
+		case '\\':
+			newstr[j++] = '\\';
+			newstr[j++] = '\\';
+			break;
+		default:
+			newstr[j++] = str[i];
+		}
+	}
+	return newstr;
+}
+
 static int cli_usock_method_call(struct teamdctl *tdc, const char *method_name,
 				 char **p_reply, void *priv,
 				 const char *fmt, va_list ap)
@@ -134,8 +168,13 @@ static int cli_usock_method_call(struct teamdctl *tdc, const char *method_name,
 	while (*fmt) {
 		switch (*fmt++) {
 		case 's': /* string */
-			str = va_arg(ap, char *);
+			str = __strencode(va_arg(ap, char *));
+			if (!str) {
+				err = -ENOMEM;
+				goto free_msg;
+			}
 			err = myasprintf(&msg, "%s%s\n", msg, str);
+			free(str);
 			if (err)
 				goto free_msg;
 			break;
diff --git a/teamd/teamd_usock.c b/teamd/teamd_usock.c
index f2ce03b..f8a2861 100644
--- a/teamd/teamd_usock.c
+++ b/teamd/teamd_usock.c
@@ -45,6 +45,39 @@ struct usock_acc_conn {
 	int sock;
 };
 
+int __strdecode(char *str)
+{
+	char *cur;
+	char *cur2;
+	bool escaped = false;
+
+	cur = str;
+	while (*cur != '\0') {
+		if (!escaped && *cur == '\\') {
+			escaped = true;
+		} else if (escaped) {
+			escaped = false;
+			switch (*cur) {
+			case 'n':
+				*(cur - 1) = '\n';
+				break;
+			case '\\':
+				*(cur - 1) = '\\';
+				break;
+			default:
+				return -EINVAL;
+			}
+			cur2 = cur;
+			while (*cur2 != '\0') {
+				*cur2 = *(cur2 + 1);
+				cur2++;
+			}
+		}
+		cur++;
+	}
+	return 0;
+}
+
 static int usock_op_get_args(void *ops_priv, const char *fmt, ...)
 {
 	va_list ap;
@@ -65,6 +98,11 @@ static int usock_op_get_args(void *ops_priv, const char *fmt, ...)
 				err = -EINVAL;
 				goto out;
 			}
+			err = __strdecode(str);
+			if (err) {
+				teamd_log_err("Corrupted argument in message.");
+				goto out;
+			}
 			*pstr = str;
 			break;
 		default:
-- 
1.8.5.3