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 1B5A8A0093; Thu, 23 Jun 2022 18:43:12 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 45E784280E; Thu, 23 Jun 2022 18:43:03 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 902FA40A82 for ; Thu, 23 Jun 2022 18:42:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002580; x=1687538580; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ndzsh55Qhi4bo4uHF+vIIOHfFT4xTmROSJfyCvq6obA=; b=cyCQm5/PS2b6SIuJAWX5fy3+vMS5S0UFyhfioW1wcxvq9kkQjzxWRG/i qXJEuY5z81XszL+njOI7bnpza1eF8F3XwRGVaeQccvUMmlHrDyDWsxJz7 uMaC4RDxjVwvfHGoGSHLZXi4N7OvOLUtasfQLYqv8YU70CdqsHNAkFkdV dsIcWj5mJ7Dl9CGMSUvHLrDVeuImZkIKBwcmfHHDd7iV+yqfYHc3rZEpb m9tHCPWT84dH/3vQh67ZGHv6y6XednPN65Dthi2mFs63D3wxm3i1ass9/ bvm/tf0vp+LquVdCzEtzYaw0XvBCGmWp/cyaRoNM/HflGBWnSpgXosJ46 g==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589080" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589080" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:42:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267929" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:42:57 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 2/6] telemetry: fix escaping of invalid json characters Date: Thu, 23 Jun 2022 17:42:41 +0100 Message-Id: <20220623164245.561371-3-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 Signed-off-by: Bruce Richardson --- lib/telemetry/telemetry_json.h | 48 +++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h index db70690274..13df5d07e3 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/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