From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 632ABA00C2 for ; Thu, 3 Nov 2022 10:29:03 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5EEB940694; Thu, 3 Nov 2022 10:29:03 +0100 (CET) Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by mails.dpdk.org (Postfix) with ESMTP id 3414740693 for ; Thu, 3 Nov 2022 10:29:02 +0100 (CET) Received: by mail-wm1-f47.google.com with SMTP id p16so688286wmc.3 for ; Thu, 03 Nov 2022 02:29:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7AsilvqlWFLaU1azy5Ce9my3uw4QnEZ/cy5QIMlOG1I=; b=Ph/iovnzZtBuTypaPDIlZERgYM4KwoBIqXvr8bH8wJ6AJrq7MhGrUn9b+Lp7wVr6+J EZ0XBMPRtSNDsKy0S5yR2v9OYenQnTWmNT3beMZDH5QT0+riEUUhBN5lFPq0CFDWW081 rdkQwpoycg1yjHcKZa3lIXNbCWxUtGKLvs73YW61W/F711SJehF4qx1ofCqJ1vB63H+2 Q7o4CqgOOv9XihtU+3TD0I2gaXvhm9fV8+82OVu8e6nRKLn4P5pH9JbgAR+9MCXXYvux IUeZg7gZu1PqYOvbwP5Px+CpG9AJkGC1ivhh7g8TyaWNkLa0JNuEPAAOhUwupRwRqj6k q0/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7AsilvqlWFLaU1azy5Ce9my3uw4QnEZ/cy5QIMlOG1I=; b=hOfIJvL3nEEOaNi9RxvWS+hvW24HykRJZgiiaAhWoEHHzRKEHSj7jVl2sCmKQmXYc1 pLzLbDV7Rut156At7QBZBmuTVDewKYLGSTjpwlVcDeSZYGvLFBc9H7e9qy4x+v9J2n2k rQkl+zTIyFqiSUWjh1nVQMw+8qNsRuVy5v06W10vBL8bVeOgmu+vDcaRwgDdY7Eig144 2RlwlU4Fs6Ya2XiLDTACRLSl/tOaI2Oa9lDMiVLW6/fKFXpXXh2JWxJMdETbKsYKqO8f 81keUf0yDgCmVHYRbmBKkXNKhUq79Q4oiKWJYxfiKC+092qjT+2vmVugO7bXj9Xl7oIt rBnw== X-Gm-Message-State: ACrzQf3LHfa8SzgcpKXPBuUGd1/auvF8+Jq2caLEWHcXcd6ML31w1y91 3Zv9LFnltS0uL5nB66GSglL8f1oiBlYfKA== X-Google-Smtp-Source: AMsMyM6OnIeG2sAYd343kVq6/deFQK/wsk/0JLayyWh3Mgiw1A/gdwFqVLN+jC1tUhASfl9TxJfC/Q== X-Received: by 2002:a1c:2987:0:b0:3c6:c0cc:b4c2 with SMTP id p129-20020a1c2987000000b003c6c0ccb4c2mr18059201wmp.56.1667467741873; Thu, 03 Nov 2022 02:29:01 -0700 (PDT) Received: from localhost ([137.220.119.58]) by smtp.gmail.com with ESMTPSA id e7-20020a05600c448700b003c64c186206sm633435wmo.16.2022.11.03.02.29.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 02:29:00 -0700 (PDT) From: luca.boccassi@gmail.com To: Bruce Richardson Cc: Ciara Power , =?UTF-8?q?Morten=20Br=C3=B8rup?= , Chengwen Feng , dpdk stable Subject: patch 'telemetry: fix escaping of invalid json characters' has been queued to stable release 20.11.7 Date: Thu, 3 Nov 2022 09:26:35 +0000 Message-Id: <20221103092758.1099402-17-luca.boccassi@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221103092758.1099402-1-luca.boccassi@gmail.com> References: <20221103092758.1099402-1-luca.boccassi@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Hi, FYI, your patch has been queued to stable release 20.11.7 Note it hasn't been pushed to http://dpdk.org/browse/dpdk-stable yet. It will be pushed if I get no objections before 11/05/22. So please shout if anyone has objections. Also note that after the patch there's a diff of the upstream commit vs the patch applied to the branch. This will indicate if there was any rebasing needed to apply to the stable branch. If there were code changes for rebasing (ie: not only metadata diffs), please double check that the rebase was correctly done. Queued patches are on a temporary branch at: https://github.com/kevintraynor/dpdk-stable This queued commit can be viewed at: https://github.com/kevintraynor/dpdk-stable/commit/8d11cb48eb40641e17af79149b3297488dd9f7ec Thanks. Luca Boccassi --- >From 8d11cb48eb40641e17af79149b3297488dd9f7ec Mon Sep 17 00:00:00 2001 From: Bruce Richardson Date: Fri, 9 Sep 2022 10:35:13 +0100 Subject: [PATCH] telemetry: fix escaping of invalid json characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ upstream commit babc5214449d3566134294f30e6b4907bd813ac9 ] For string values returned from telemetry, escape any values that cannot normally appear in a json string. According to the json spec[1], the characters than need to be handled are control chars (char value < 0x20) and '"' and '\' characters. To handle this, we replace the snprintf call with a separate string copying and encapsulation routine which checks each character as it copies it to the final array. [1] https://www.rfc-editor.org/rfc/rfc8259.txt Bugzilla ID: 1037 Fixes: 6dd571fd07c3 ("telemetry: introduce new functionality") Signed-off-by: Bruce Richardson Acked-by: Ciara Power Acked-by: Morten Brørup Acked-by: Chengwen Feng --- lib/librte_telemetry/telemetry.c | 11 ++++-- lib/librte_telemetry/telemetry_json.h | 48 ++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/librte_telemetry/telemetry.c b/lib/librte_telemetry/telemetry.c index 9d970d167e..c3522b7173 100644 --- a/lib/librte_telemetry/telemetry.c +++ b/lib/librte_telemetry/telemetry.c @@ -181,9 +181,14 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) MAX_CMD_LEN, cmd ? cmd : "none"); break; case RTE_TEL_STRING: - used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}", - MAX_CMD_LEN, cmd, - RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str); + prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", + MAX_CMD_LEN, cmd); + cb_data_buf = &out_buf[prefix_used]; + buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ + + used = rte_tel_json_str(cb_data_buf, buf_len, 0, d->data.str); + used += prefix_used; + used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); break; case RTE_TEL_DICT: prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", diff --git a/lib/librte_telemetry/telemetry_json.h b/lib/librte_telemetry/telemetry_json.h index db70690274..13df5d07e3 100644 --- a/lib/librte_telemetry/telemetry_json.h +++ b/lib/librte_telemetry/telemetry_json.h @@ -44,6 +44,52 @@ __json_snprintf(char *buf, const int len, const char *format, ...) return 0; /* nothing written or modified */ } +static const char control_chars[0x20] = { + ['\n'] = 'n', + ['\r'] = 'r', + ['\t'] = 't', +}; + +/** + * @internal + * Does the same as __json_snprintf(buf, len, "\"%s\"", str) + * except that it does proper escaping as necessary. + * Drops any invalid characters we don't support + */ +static inline int +__json_format_str(char *buf, const int len, const char *str) +{ + char tmp[len]; + int tmpidx = 0; + + tmp[tmpidx++] = '"'; + while (*str != '\0') { + if (*str < (int)RTE_DIM(control_chars)) { + int idx = *str; /* compilers don't like char type as index */ + if (control_chars[idx] != 0) { + tmp[tmpidx++] = '\\'; + tmp[tmpidx++] = control_chars[idx]; + } + } else if (*str == '"' || *str == '\\') { + tmp[tmpidx++] = '\\'; + tmp[tmpidx++] = *str; + } else + tmp[tmpidx++] = *str; + /* we always need space for closing quote and null character. + * Ensuring at least two free characters also means we can always take an + * escaped character like "\n" without overflowing + */ + if (tmpidx > len - 2) + return 0; + str++; + } + tmp[tmpidx++] = '"'; + tmp[tmpidx] = '\0'; + + strcpy(buf, tmp); + return tmpidx; +} + /* Copies an empty array into the provided buffer. */ static inline int rte_tel_json_empty_array(char *buf, const int len, const int used) @@ -62,7 +108,7 @@ rte_tel_json_empty_obj(char *buf, const int len, const int used) static inline int rte_tel_json_str(char *buf, const int len, const int used, const char *str) { - return used + __json_snprintf(buf + used, len - used, "\"%s\"", str); + return used + __json_format_str(buf + used, len - used, str); } /* Appends a string into the JSON array in the provided buffer. */ -- 2.34.1 --- Diff of the applied patch vs upstream commit (please double-check if non-empty: --- --- - 2022-11-03 09:27:26.507441742 +0000 +++ 0017-telemetry-fix-escaping-of-invalid-json-characters.patch 2022-11-03 09:27:25.321421435 +0000 @@ -1 +1 @@ -From babc5214449d3566134294f30e6b4907bd813ac9 Mon Sep 17 00:00:00 2001 +From 8d11cb48eb40641e17af79149b3297488dd9f7ec Mon Sep 17 00:00:00 2001 @@ -8,0 +9,2 @@ +[ upstream commit babc5214449d3566134294f30e6b4907bd813ac9 ] + @@ -28,2 +30,2 @@ - lib/telemetry/telemetry.c | 11 +++++--- - lib/telemetry/telemetry_json.h | 48 +++++++++++++++++++++++++++++++++- + lib/librte_telemetry/telemetry.c | 11 ++++-- + lib/librte_telemetry/telemetry_json.h | 48 ++++++++++++++++++++++++++- @@ -32,5 +34,5 @@ -diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c -index 164ea0601a..72334d2429 100644 ---- a/lib/telemetry/telemetry.c -+++ b/lib/telemetry/telemetry.c -@@ -234,9 +234,14 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) +diff --git a/lib/librte_telemetry/telemetry.c b/lib/librte_telemetry/telemetry.c +index 9d970d167e..c3522b7173 100644 +--- a/lib/librte_telemetry/telemetry.c ++++ b/lib/librte_telemetry/telemetry.c +@@ -181,9 +181,14 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) @@ -54 +56 @@ -diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h +diff --git a/lib/librte_telemetry/telemetry_json.h b/lib/librte_telemetry/telemetry_json.h @@ -56,2 +58,2 @@ ---- a/lib/telemetry/telemetry_json.h -+++ b/lib/telemetry/telemetry_json.h +--- a/lib/librte_telemetry/telemetry_json.h ++++ b/lib/librte_telemetry/telemetry_json.h