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 5B07A43329; Tue, 14 Nov 2023 12:24:52 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A10ED406B8; Tue, 14 Nov 2023 12:24:19 +0100 (CET) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 78D77402D4 for ; Tue, 14 Nov 2023 12:24:10 +0100 (CET) Received: from dggpeml500011.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4SV3pK5MwLzvQWR; Tue, 14 Nov 2023 19:23:53 +0800 (CST) Received: from localhost.huawei.com (10.50.165.33) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Tue, 14 Nov 2023 19:24:09 +0800 From: Dengdui Huang To: CC: , , , , Subject: [PATCH v2 08/43] telemetry: replace strerror Date: Tue, 14 Nov 2023 19:23:32 +0800 Message-ID: <20231114112407.3496460-9-huangdengdui@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20231114112407.3496460-1-huangdengdui@huawei.com> References: <20231114082539.1858594-44-huangdengdui@huawei.com> <20231114112407.3496460-1-huangdengdui@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500011.china.huawei.com (7.185.36.84) X-CFilter-Loop: Reflected 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 The function strerror() is insecure in a multi-thread environment. This patch uses strerror_r() to replace it. Cc: stable@dpdk.org Signed-off-by: Dengdui Huang Acked-by: Chengwen Feng --- lib/telemetry/telemetry.c | 28 ++++++++++++++++++++++------ lib/telemetry/telemetry_legacy.c | 5 ++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c index 92982842a8..27c19979fb 100644 --- a/lib/telemetry/telemetry.c +++ b/lib/telemetry/telemetry.c @@ -411,6 +411,9 @@ client_handler(void *sock_id) static void * socket_listener(void *socket) { +#define ERR_BUFF_SZ 256 + char err_buf[ERR_BUFF_SZ] = {0}; + while (1) { pthread_t th; int rc; @@ -433,8 +436,9 @@ socket_listener(void *socket) rc = pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted); if (rc != 0) { + (void)strerror_r(rc, err_buf, sizeof(err_buf)); TMTY_LOG(ERR, "Error with create client thread: %s\n", - strerror(rc)); + err_buf); close(s_accepted); if (s->num_clients != NULL) rte_atomic_fetch_sub_explicit(s->num_clients, 1, @@ -467,9 +471,13 @@ unlink_sockets(void) static int create_socket(char *path) { +#define ERR_BUFF_SZ 256 + char err_buf[ERR_BUFF_SZ] = {0}; + int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); if (sock < 0) { - TMTY_LOG(ERR, "Error with socket creation, %s\n", strerror(errno)); + (void)strerror_r(errno, err_buf, sizeof(err_buf)); + TMTY_LOG(ERR, "Error with socket creation, %s\n", err_buf); return -1; } @@ -499,14 +507,16 @@ create_socket(char *path) TMTY_LOG(DEBUG, "Attempting unlink and retrying bind\n"); unlink(sun.sun_path); if (bind(sock, (void *) &sun, sizeof(sun)) < 0) { - TMTY_LOG(ERR, "Error binding socket: %s\n", strerror(errno)); + (void)strerror_r(errno, err_buf, sizeof(err_buf)); + TMTY_LOG(ERR, "Error binding socket: %s\n", err_buf); close(sock); return -errno; /* if unlink failed, this will be -EADDRINUSE as above */ } } if (listen(sock, 1) < 0) { - TMTY_LOG(ERR, "Error calling listen for socket: %s\n", strerror(errno)); + (void)strerror_r(errno, err_buf, sizeof(err_buf)); + TMTY_LOG(ERR, "Error calling listen for socket: %s\n", err_buf); unlink(sun.sun_path); close(sock); return -errno; @@ -531,6 +541,8 @@ set_thread_name(pthread_t id __rte_unused, const char *name __rte_unused) static int telemetry_legacy_init(void) { +#define ERR_BUFF_SZ 256 + char err_buf[ERR_BUFF_SZ] = {0}; pthread_t t_old; int rc; @@ -552,8 +564,9 @@ telemetry_legacy_init(void) } rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket); if (rc != 0) { + (void)strerror_r(rc, err_buf, sizeof(err_buf)); TMTY_LOG(ERR, "Error with create legacy socket thread: %s\n", - strerror(rc)); + err_buf); close(v1_socket.sock); v1_socket.sock = -1; unlink(v1_socket.path); @@ -570,7 +583,9 @@ telemetry_legacy_init(void) static int telemetry_v2_init(void) { +#define ERR_BUFF_SZ 256 char spath[sizeof(v2_socket.path)]; + char err_buf[ERR_BUFF_SZ] = {0}; pthread_t t_new; short suffix = 0; int rc; @@ -606,8 +621,9 @@ telemetry_v2_init(void) } rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket); if (rc != 0) { + (void)strerror_r(rc, err_buf, sizeof(err_buf)); TMTY_LOG(ERR, "Error with create socket thread: %s\n", - strerror(rc)); + err_buf); close(v2_socket.sock); v2_socket.sock = -1; unlink(v2_socket.path); diff --git a/lib/telemetry/telemetry_legacy.c b/lib/telemetry/telemetry_legacy.c index 4c1d1c353a..09e8d220ca 100644 --- a/lib/telemetry/telemetry_legacy.c +++ b/lib/telemetry/telemetry_legacy.c @@ -81,6 +81,8 @@ register_client(const char *cmd __rte_unused, const char *params, char *buffer __rte_unused, int buf_len __rte_unused) { #ifndef RTE_EXEC_ENV_WINDOWS +#define ERR_BUFF_SZ 256 + char err_buf[ERR_BUFF_SZ] = {0}; pthread_t th; char data[BUF_SIZE]; int fd; @@ -117,8 +119,9 @@ register_client(const char *cmd __rte_unused, const char *params, rc = pthread_create(&th, NULL, &legacy_client_handler, (void *)(uintptr_t)fd); if (rc != 0) { + (void)strerror_r(rc, err_buf, sizeof(err_buf)); fprintf(stderr, "Failed to create legacy client thread: %s\n", - strerror(rc)); + err_buf); close(fd); return -1; } -- 2.33.0