The function strerror() is insecure in a multi-thread environment.
This patch uses strerror_r() to replace it.

Signed-off-by: Dengdui Huang <huangdeng...@huawei.com>
Acked-by: Chengwen Feng <fengcheng...@huawei.com>
Acked-by: Morten Brørup <m...@smartsharesystems.com>
Acked-by: Huisong Li <lihuis...@huawei.com>
---
 lib/power/guest_channel.c        | 15 ++++++++++++---
 lib/power/power_kvm_vm.c         |  6 +++++-
 lib/power/power_pstate_cpufreq.c | 14 +++++++++++---
 lib/power/rte_power_pmd_mgmt.c   |  6 ++++--
 4 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/lib/power/guest_channel.c b/lib/power/guest_channel.c
index bc3f55b6bf..33e6ea612e 100644
--- a/lib/power/guest_channel.c
+++ b/lib/power/guest_channel.c
@@ -14,6 +14,7 @@
 
 #include <rte_log.h>
 #include <rte_power.h>
+#include <rte_errno.h>
 
 #include "guest_channel.h"
 
@@ -57,6 +58,7 @@ guest_channel_host_connect(const char *path, unsigned int 
lcore_id)
 {
        int flags, ret;
        struct rte_power_channel_packet pkt;
+       char errmsg[RTE_STRERR_BUFSIZE];
        char fd_path[PATH_MAX];
        int fd = -1;
 
@@ -77,8 +79,10 @@ guest_channel_host_connect(const char *path, unsigned int 
lcore_id)
                        fd_path, lcore_id);
        fd = open(fd_path, O_RDWR);
        if (fd < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                GUEST_CHANNEL_LOG(ERR, "Unable to connect to '%s' with error "
-                               "%s", fd_path, strerror(errno));
+                               "%s", fd_path, errmsg);
                return -1;
        }
 
@@ -105,9 +109,11 @@ guest_channel_host_connect(const char *path, unsigned int 
lcore_id)
        global_fds[lcore_id] = fd;
        ret = guest_channel_send_msg(&pkt, lcore_id);
        if (ret != 0) {
+               if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
ret);
                GUEST_CHANNEL_LOG(ERR,
                                "Error on channel '%s' communications test: %s",
-                               fd_path, ret > 0 ? strerror(ret) :
+                               fd_path, ret > 0 ? errmsg :
                                "channel not connected");
                goto error;
        }
@@ -161,6 +167,7 @@ int power_guest_channel_read_msg(void *pkt,
                size_t pkt_len,
                unsigned int lcore_id)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        int ret;
        struct pollfd fds;
 
@@ -186,8 +193,10 @@ int power_guest_channel_read_msg(void *pkt,
                GUEST_CHANNEL_LOG(DEBUG, "Timeout occurred during poll 
function.");
                return -1;
        } else if (ret < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                GUEST_CHANNEL_LOG(ERR, "Error occurred during poll function: 
%s",
-                               strerror(errno));
+                               errmsg);
                return -1;
        }
 
diff --git a/lib/power/power_kvm_vm.c b/lib/power/power_kvm_vm.c
index f15be8fac5..c1e6b3cec4 100644
--- a/lib/power/power_kvm_vm.c
+++ b/lib/power/power_kvm_vm.c
@@ -5,6 +5,7 @@
 #include <string.h>
 
 #include <rte_log.h>
+#include <rte_errno.h>
 
 #include "rte_power_guest_channel.h"
 #include "guest_channel.h"
@@ -71,6 +72,7 @@ power_kvm_vm_set_freq(__rte_unused unsigned int lcore_id,
 static inline int
 send_msg(unsigned int lcore_id, uint32_t scale_direction)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        int ret;
 
        if (lcore_id >= RTE_MAX_LCORE) {
@@ -82,8 +84,10 @@ send_msg(unsigned int lcore_id, uint32_t scale_direction)
        ret = guest_channel_send_msg(&pkt[lcore_id], lcore_id);
        if (ret == 0)
                return 1;
+       if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0)
+               snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret);
        POWER_LOG(DEBUG, "Error sending message: %s",
-                       ret > 0 ? strerror(ret) : "channel not connected");
+                       ret > 0 ? errmsg : "channel not connected");
        return -1;
 }
 
diff --git a/lib/power/power_pstate_cpufreq.c b/lib/power/power_pstate_cpufreq.c
index 4755909466..5538f3209b 100644
--- a/lib/power/power_pstate_cpufreq.c
+++ b/lib/power/power_pstate_cpufreq.c
@@ -13,6 +13,7 @@
 
 #include <rte_memcpy.h>
 #include <rte_stdatomic.h>
+#include <rte_errno.h>
 
 #include "rte_power_pmd_mgmt.h"
 #include "power_pstate_cpufreq.h"
@@ -75,6 +76,7 @@ static struct pstate_power_info 
lcore_power_info[RTE_MAX_LCORE];
 static int32_t
 power_read_turbo_pct(uint64_t *outVal)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        int fd, ret;
        char val[4] = {0};
        char *endptr;
@@ -82,24 +84,30 @@ power_read_turbo_pct(uint64_t *outVal)
        fd = open(POWER_SYSFILE_TURBO_PCT, O_RDONLY);
 
        if (fd < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                POWER_LOG(ERR, "Error opening '%s': %s", 
POWER_SYSFILE_TURBO_PCT,
-                                strerror(errno));
+                                errmsg);
                return fd;
        }
 
        ret = read(fd, val, sizeof(val));
 
        if (ret < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                POWER_LOG(ERR, "Error reading '%s': %s", 
POWER_SYSFILE_TURBO_PCT,
-                                strerror(errno));
+                                errmsg);
                goto out;
        }
 
        errno = 0;
        *outVal = (uint64_t) strtol(val, &endptr, 10);
        if (errno != 0 || (*endptr != 0 && *endptr != '\n')) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                POWER_LOG(ERR, "Error converting str to digits, read from %s: 
%s",
-                                POWER_SYSFILE_TURBO_PCT, strerror(errno));
+                                POWER_SYSFILE_TURBO_PCT, errmsg);
                ret = -1;
                goto out;
        }
diff --git a/lib/power/rte_power_pmd_mgmt.c b/lib/power/rte_power_pmd_mgmt.c
index 5e50613f5b..308129cad0 100644
--- a/lib/power/rte_power_pmd_mgmt.c
+++ b/lib/power/rte_power_pmd_mgmt.c
@@ -487,6 +487,7 @@ rte_power_ethdev_pmgmt_queue_enable(unsigned int lcore_id, 
uint16_t port_id,
                uint16_t queue_id, enum rte_power_pmd_mgmt_type mode)
 {
        const union queue qdata = {.portid = port_id, .qid = queue_id};
+       char errmsg[RTE_STRERR_BUFSIZE];
        struct pmd_core_cfg *lcore_cfg;
        struct queue_list_entry *queue_cfg;
        struct rte_eth_dev_info info;
@@ -574,8 +575,9 @@ rte_power_ethdev_pmgmt_queue_enable(unsigned int lcore_id, 
uint16_t port_id,
        /* add this queue to the list */
        ret = queue_list_add(lcore_cfg, &qdata);
        if (ret < 0) {
-               POWER_LOG(DEBUG, "Failed to add queue to list: %s",
-                               strerror(-ret));
+               if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
-ret);
+               POWER_LOG(DEBUG, "Failed to add queue to list: %s", errmsg);
                goto end;
        }
        /* new queue is always added last */
-- 
2.33.0

Reply via email to