]> www.infradead.org Git - users/mchehab/rasdaemon.git/commitdiff
rasdaemon: don't use unsafe strcpy, strcat and sprintf
authorMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 18 Jul 2024 11:02:30 +0000 (13:02 +0200)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 18 Jul 2024 15:40:13 +0000 (17:40 +0200)
Remove all occurrences of those calls.

While here, also fix a couple missing whitespace warnings.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
16 files changed:
mce-amd-k8.c
mce-amd-smca.c
mce-amd.c
mce-intel.c
ras-aer-handler.c
ras-arm-handler.c
ras-cpu-isolation.c
ras-cxl-handler.c
ras-events.c
ras-extlog-handler.c
ras-memory-failure-handler.c
ras-non-standard-handler.c
ras-page-isolation.c
ras-record.c
ras-report.c
types.h

index a937516960bbeb6026d34edf164d3098eb969d67..d76d49b5f832d50c3ba944fa6be105fff04ffbe9 100644 (file)
@@ -297,7 +297,8 @@ int parse_amd_k8_event(struct ras_events *ras, struct mce_event *e)
                decode_k8_threashold(e);
                break;
        default:
-               strcpy(e->error_msg, "Don't know how to decode this bank");
+               strscpy(e->error_msg, "Don't know how to decode this bank",
+                       sizeof(e->error_msg));
        }
 
        /* IP doesn't matter on memory errors */
index f3d4171516745976ed2a3b3f0964a2ba38152fe6..3e39e94c82fbf67615b10387bdbe82b170cbba5b 100644 (file)
@@ -952,17 +952,20 @@ void decode_smca_error(struct mce_event *e, struct mce_priv *m)
        }
 
        if (i >= MAX_NR_BANKS) {
-               strcpy(e->mcastatus_msg, "Couldn't find bank type with IPID");
+               strscpy(e->mcastatus_msg, "Couldn't find bank type with IPID",
+                       sizeof(e->mcastatus_msg));
                return;
        }
 
        if (bank_type >= N_SMCA_BANK_TYPES) {
-               strcpy(e->mcastatus_msg, "Don't know how to decode this bank");
+               strscpy(e->mcastatus_msg, "Don't know how to decode this bank",
+                       sizeof(e->mcastatus_msg));
                return;
        }
 
        if (bank_type == SMCA_RESERVED) {
-               strcpy(e->mcastatus_msg, "Bank 4 is reserved.\n");
+               strscpy(e->mcastatus_msg, "Bank 4 is reserved.\n",
+                       sizeof(e->mcastatus_msg));
                return;
        }
 
index 028a329b3ea845e8fca1a91bed458cae6edea83b..4ab6329795c4f79e34ebff2f2411fa0f401543f8 100644 (file)
--- a/mce-amd.c
+++ b/mce-amd.c
@@ -80,16 +80,21 @@ void decode_amd_errcode(struct mce_event *e)
 
        if (e->status & MCI_STATUS_UC) {
                if (e->status & MCI_STATUS_PCC)
-                       strcpy(e->error_msg, "System Fatal error.");
+                       strscpy(e->error_msg, "System Fatal error.",
+                               sizeof(e->error_msg));
                if (e->mcgstatus & MCG_STATUS_RIPV)
-                       strcpy(e->error_msg,
-                              "Uncorrected, software restartable error.");
-               strcpy(e->error_msg,
-                      "Uncorrected, software containable error.");
+                       strscpy(e->error_msg,
+                               "Uncorrected, software restartable error.",
+                               sizeof(e->error_msg));
+               strscpy(e->error_msg,
+                       "Uncorrected, software containable error.",
+                       sizeof(e->error_msg));
        } else if (e->status & MCI_STATUS_DEFERRED) {
-               strcpy(e->error_msg, "Deferred error, no action required.");
+               strscpy(e->error_msg, "Deferred error, no action required.",
+                       sizeof(e->error_msg));
        } else {
-               strcpy(e->error_msg, "Corrected error, no action required.");
+               strscpy(e->error_msg, "Corrected error, no action required.",
+                       sizeof(e->error_msg));
        }
 
        if (!(e->status & MCI_STATUS_VAL))
index bec84ec9405c3f33929fc2fe1068b421c36d6a88..b399467eb783c6a105d53d07913556e925c1876e 100644 (file)
@@ -151,9 +151,9 @@ static void decode_memory_controller(struct mce_event *e, uint32_t status)
        char channel[30];
 
        if ((status & 0xf) == 0xf)
-               sprintf(channel, "unspecified");
+               snprintf(channel, sizeof(channel), "unspecified");
        else
-               sprintf(channel, "%u", status & 0xf);
+               snprintf(channel, sizeof(channel), "%u", status & 0xf);
 
        mce_snprintf(e->error_msg, "MEMORY CONTROLLER %s_CHANNEL%s_ERR",
                     mmm_mnemonic[(status >> 4) & 7], channel);
@@ -195,14 +195,14 @@ static void decode_mcg(struct mce_event *e)
 
 static void bank_name(struct mce_event *e)
 {
-       char *buf = e->bank_name;
-
        switch (e->bank) {
        case MCE_THERMAL_BANK:
-               strcpy(buf, "THERMAL EVENT");
+               strscpy(e->bank_name, "THERMAL EVENT", sizeof(e->bank_name));
                break;
        case MCE_TIMEOUT_BANK:
-               strcpy(buf, "Timeout waiting for exception on other CPUs");
+               strscpy(e->bank_name,
+                       "Timeout waiting for exception on other CPUs",
+                       sizeof(e->bank_name));
                break;
        default:
                break;
@@ -435,7 +435,7 @@ static int domsr(int cpu, int msr, int bit)
        unsigned long long data;
        int fd;
 
-       sprintf(fpath, "/dev/cpu/%d/msr", cpu);
+       snprintf(fpath, sizeof(fpath), "/dev/cpu/%d/msr", cpu);
        fd = open(fpath, O_RDWR);
        if (fd == -1) {
                switch (errno) {
index 138abe2e487418c2b92cabd29a9ce2479ddff883..e5fe23a2138082c509a6220c5b1dea5122a02757 100644 (file)
@@ -188,9 +188,9 @@ int ras_aer_event_handler(struct trace_seq *s,
        sel_data[3] = bus;
        sel_data[4] = (((dev & 0x1f) << 3) | (fn & 0x7));
 
-       sprintf(ipmi_add_sel,
-               "ipmitool raw 0x0a 0x44 0x00 0x00 0xc0 0x00 0x00 0x00 0x00 0x3a 0xcd 0x00 0xc0 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
-         sel_data[0], sel_data[1], sel_data[2], sel_data[3], sel_data[4]);
+       snprintf(ipmi_add_sel, sizeof(ipmi_add_sel),
+                "ipmitool raw 0x0a 0x44 0x00 0x00 0xc0 0x00 0x00 0x00 0x00 0x3a 0xcd 0x00 0xc0 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
+                sel_data[0], sel_data[1], sel_data[2], sel_data[3], sel_data[4]);
 
        rc = system(ipmi_add_sel);
        if (rc)
index c09094373af77e9e616ea1a53194e65cae3667fa..77f635817fe87f4563b38d12e2acf8b4097f34cc 100644 (file)
@@ -40,7 +40,7 @@
 #define ARM_INFO_FLAGS_PROPAGATED      BIT(2)
 #define ARM_INFO_FLAGS_OVERFLOW                BIT(3)
 
-#define ARM_ERR_TYPE_MASK              GENMASK(4,1)
+#define ARM_ERR_TYPE_MASK              GENMASK(4, 1)
 #define ARM_CACHE_ERROR                        BIT(1)
 #define ARM_TLB_ERROR                  BIT(2)
 #define ARM_BUS_ERROR                  BIT(3)
index 0daa63a6ac65ccca7d7de258fc45efba71640d87..90200c5d26f022e05f3b4d606222adb0a990c359 100644 (file)
@@ -255,14 +255,13 @@ void cpu_infos_free(void)
 static int do_cpu_offline(unsigned int cpu)
 {
        int fd, rc;
-       char buf[2] = "";
+       char buf[2] = "0";
 
        cpu_infos[cpu].state = CPU_OFFLINE_FAILED;
        fd = open_sys_file(cpu, O_RDWR, cpu_path_format);
        if (fd == -1)
                return HANDLE_FAILED;
 
-       strcpy(buf, "0");
        rc = write(fd, buf, strlen(buf));
        if (rc < 0) {
                log(TERM, LOG_ERR, "cpu%u offline failed, errno:%d\n", cpu, errno);
index dd3cfeb0439d813ddcaab613f0bb3e486e5d9da4..e49c1282498547adbe1187a3b934085ef9fab471 100644 (file)
@@ -40,7 +40,7 @@ static void convert_timestamp(unsigned long long ts, char *ts_ptr, uint16_t size
                strftime(ts_ptr, size, "%Y-%m-%d %H:%M:%S %z", tm);
 
        if (!ts || !tm)
-               strncpy(ts_ptr, "1970-01-01 00:00:00 +0000",
+               strscpy(ts_ptr, "1970-01-01 00:00:00 +0000",
                        size);
 }
 
@@ -55,7 +55,7 @@ static void get_timestamp(struct trace_seq *s, struct tep_record *record,
        if (tm)
                strftime(ts_ptr, size, "%Y-%m-%d %H:%M:%S %z", tm);
        else
-               strncpy(ts_ptr, "1970-01-01 00:00:00 +0000", size);
+               strscpy(ts_ptr, "1970-01-01 00:00:00 +0000", size);
 }
 
 struct cxl_event_flags {
@@ -85,7 +85,7 @@ static char *uuid_be(const char *uu)
        static const unsigned char be[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
 
        for (i = 0; i < 16; i++) {
-               p += sprintf(p, "%.2x", (unsigned char)uu[be[i]]);
+               p += snprintf(p, sizeof(uuid), "%.2x", (unsigned char)uu[be[i]]);
                switch (i) {
                case 3:
                case 5:
@@ -249,7 +249,7 @@ int ras_cxl_poison_event_handler(struct trace_seq *s,
                        return -1;
                convert_timestamp(val, ev.overflow_ts, sizeof(ev.overflow_ts));
        } else {
-               strncpy(ev.overflow_ts, "1970-01-01 00:00:00 +0000",
+               strscpy(ev.overflow_ts, "1970-01-01 00:00:00 +0000",
                        sizeof(ev.overflow_ts));
        }
 
index 53e15dedf36deeb26d4b852122cf78ee019dc0e7..81535e8fd0f43320c9f31a9f75b00e60443065b5 100644 (file)
@@ -100,8 +100,7 @@ static int get_debugfs_dir(char *tracing_dir, size_t len)
 
                if (!strcmp(type, "debugfs")) {
                        fclose(fp);
-                       strncpy(tracing_dir, dir, len - 1);
-                       tracing_dir[len - 1] = '\0';
+                       strscpy(tracing_dir, dir, len - 1);
                        return 0;
                }
        } while (1);
@@ -114,10 +113,17 @@ static int get_debugfs_dir(char *tracing_dir, size_t len)
 static int open_trace(struct ras_events *ras, char *name, int flags)
 {
        char fname[MAX_PATH + 1];
+       int rc;
 
-       strcpy(fname, ras->tracing);
-       strcat(fname, "/");
-       strcat(fname, name);
+       rc = strscpy(fname, ras->tracing, sizeof(fname));
+       if (rc < 0)
+               return rc;
+       rc = strscat(fname, "/", sizeof(fname));
+       if (rc < 0)
+               return rc;
+       rc = strscat(fname, name, sizeof(fname));
+       if (rc < 0)
+               return rc;
 
        return open(fname, flags);
 }
@@ -131,8 +137,13 @@ static int get_tracing_dir(struct ras_events *ras)
 
        get_debugfs_dir(ras->debugfs, sizeof(ras->debugfs));
 
-       strcpy(fname, ras->debugfs);
-       strcat(fname, "/tracing");
+       rc = strscpy(fname, ras->debugfs, sizeof(fname));
+       if (rc < 0)
+               return rc;
+       rc = strscat(fname, "/tracing", sizeof(fname));
+       if (rc < 0)
+               return rc;
+
        dir = opendir(fname);
        if (!dir)
                return -1;
@@ -145,10 +156,14 @@ static int get_tracing_dir(struct ras_events *ras)
        }
        closedir(dir);
 
-       strcpy(ras->tracing, ras->debugfs);
-       strcat(ras->tracing, "/tracing");
+       strscpy(ras->tracing, ras->debugfs, sizeof(ras->tracing));
+       strscat(ras->tracing, "/tracing", sizeof(ras->tracing));
        if (has_instances) {
-               strcat(ras->tracing, "/instances/" TOOL_NAME);
+               rc = strscat(ras->tracing, "/instances/" TOOL_NAME,
+                            sizeof(ras->tracing));
+               if (rc < 0)
+                       return rc;
+
                rc = mkdir(ras->tracing, 0700);
                if (rc < 0 && errno != EEXIST) {
                        log(ALL, LOG_INFO,
index 8b5680fc8d8026a57286523dbfeee9617c6818b7..8d4739ebc74a4c8fc601f646394290cc9cef396d 100644 (file)
@@ -108,40 +108,85 @@ static char *err_cper_data(const char *c)
 {
        const struct cper_mem_err_compact *cpd = (struct cper_mem_err_compact *)c;
        static char buf[256];
+       unsigned int rc, size = sizeof(buf);
        char *p = buf;
 
        if (cpd->validation_bits == 0)
                return "";
-       p += sprintf(p, " (");
-       if (cpd->validation_bits & CPER_MEM_VALID_NODE)
-               p += sprintf(p, "node: %d ", cpd->node);
-       if (cpd->validation_bits & CPER_MEM_VALID_CARD)
-               p += sprintf(p, "card: %d ", cpd->card);
-       if (cpd->validation_bits & CPER_MEM_VALID_MODULE)
-               p += sprintf(p, "module: %d ", cpd->module);
-       if (cpd->validation_bits & CPER_MEM_VALID_BANK)
-               p += sprintf(p, "bank: %d ", cpd->bank);
-       if (cpd->validation_bits & CPER_MEM_VALID_DEVICE)
-               p += sprintf(p, "device: %d ", cpd->device);
-       if (cpd->validation_bits & CPER_MEM_VALID_ROW)
-               p += sprintf(p, "row: %d ", cpd->row);
-       if (cpd->validation_bits & CPER_MEM_VALID_COLUMN)
-               p += sprintf(p, "column: %d ", cpd->column);
-       if (cpd->validation_bits & CPER_MEM_VALID_BIT_POSITION)
-               p += sprintf(p, "bit_pos: %d ", cpd->bit_pos);
-       if (cpd->validation_bits & CPER_MEM_VALID_REQUESTOR_ID)
-               p += sprintf(p, "req_id: 0x%llx ", cpd->requestor_id);
-       if (cpd->validation_bits & CPER_MEM_VALID_RESPONDER_ID)
-               p += sprintf(p, "resp_id: 0x%llx ", cpd->responder_id);
-       if (cpd->validation_bits & CPER_MEM_VALID_TARGET_ID)
-               p += sprintf(p, "tgt_id: 0x%llx ", cpd->target_id);
-       if (cpd->validation_bits & CPER_MEM_VALID_RANK_NUMBER)
-               p += sprintf(p, "rank: %d ", cpd->rank);
-       if (cpd->validation_bits & CPER_MEM_VALID_CARD_HANDLE)
-               p += sprintf(p, "card_handle: %d ", cpd->mem_array_handle);
-       if (cpd->validation_bits & CPER_MEM_VALID_MODULE_HANDLE)
-               p += sprintf(p, "module_handle: %d ", cpd->mem_dev_handle);
-       p += sprintf(p - 1, ")");
+       rc = snprintf(p, size, " (");
+       p += rc;
+       size -= rc;
+       if (cpd->validation_bits & CPER_MEM_VALID_NODE) {
+               rc = snprintf(p, size, "node: %d ", cpd->node);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_CARD) {
+               rc = snprintf(p, size, "card: %d ", cpd->card);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_MODULE) {
+               rc = snprintf(p, size, "module: %d ", cpd->module);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_BANK) {
+               rc = snprintf(p, size, "bank: %d ", cpd->bank);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_DEVICE) {
+               rc = snprintf(p, size, "device: %d ", cpd->device);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_ROW) {
+               rc = snprintf(p, size, "row: %d ", cpd->row);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_COLUMN) {
+               rc = snprintf(p, size, "column: %d ", cpd->column);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_BIT_POSITION) {
+               rc = snprintf(p, size, "bit_pos: %d ", cpd->bit_pos);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_REQUESTOR_ID) {
+               rc = snprintf(p, size, "req_id: 0x%llx ", cpd->requestor_id);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_RESPONDER_ID) {
+               rc = snprintf(p, size, "resp_id: 0x%llx ", cpd->responder_id);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_TARGET_ID) {
+               rc = snprintf(p, size, "tgt_id: 0x%llx ", cpd->target_id);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_RANK_NUMBER) {
+               rc = snprintf(p, size, "rank: %d ", cpd->rank);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_CARD_HANDLE) {
+               rc = snprintf(p, size, "card_handle: %d ", cpd->mem_array_handle);
+               p += rc;
+               size -= rc;
+       }
+       if (cpd->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
+               rc = snprintf(p, size, "module_handle: %d ", cpd->mem_dev_handle);
+               p += rc;
+               size -= rc;
+       }
+       rc = snprintf(p - 1, size, ")");
 
        return buf;
 }
@@ -154,7 +199,7 @@ static char *uuid_le(const char *uu)
        static const unsigned char le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
 
        for (i = 0; i < 16; i++) {
-               p += sprintf(p, "%.2x", (unsigned char)uu[le[i]]);
+               p += snprintf(p, sizeof(uuid), "%.2x", (unsigned char)uu[le[i]]);
                switch (i) {
                case 3:
                case 5:
index 1a4d17ae2ae7def0f056a2785c443293d88a08c3..358bc9a247257fd814d52a733beb86f3d3e22fd3 100644 (file)
@@ -197,12 +197,12 @@ int ras_memory_failure_event_handler(struct trace_seq *s,
                strftime(ev.timestamp, sizeof(ev.timestamp),
                         "%Y-%m-%d %H:%M:%S %z", tm);
        else
-               strncpy(ev.timestamp, "1970-01-01 00:00:00 +0000", sizeof(ev.timestamp));
+               strscpy(ev.timestamp, "1970-01-01 00:00:00 +0000", sizeof(ev.timestamp));
        trace_seq_printf(s, "%s ", ev.timestamp);
 
        if (tep_get_field_val(s,  event, "pfn", record, &val, 1) < 0)
                return -1;
-       sprintf(ev.pfn, "0x%llx", val);
+       snprintf(ev.pfn, sizeof(ev.pfn), "0x%llx", val);
        trace_seq_printf(s, "pfn=0x%llx ", val);
 
        if (tep_get_field_val(s, event, "type", record, &val, 1) < 0)
index 7d3ceccd98c7ec3a09f97bf161111848b0f1f4df..6d830fd8d3a162ba0a5eb25e0b5bebd33cfef0d6 100644 (file)
@@ -40,7 +40,7 @@ static char *uuid_le(const char *uu)
        static const unsigned char le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
 
        for (i = 0; i < 16; i++) {
-               p += sprintf(p, "%.2x", (unsigned char)uu[le[i]]);
+               p += snprintf(p, sizeof(uuid), "%.2x", (unsigned char)uu[le[i]]);
                switch (i) {
                case 3:
                case 5:
index 058949260c04a74e9e1682575a257bf6a72eb278..d3706c1f5d00facfb5eabaa17e106b2dd1686e36 100644 (file)
@@ -225,7 +225,7 @@ static int do_page_offline(unsigned long long addr, enum otype type)
                return -1;
        }
 
-       sprintf(buf, "%#llx", addr);
+       snprintf(buf, sizeof(buf), "%#llx", addr);
        rc = write(fd, buf, strlen(buf));
        if (rc < 0)
                log(TERM, LOG_ERR,
index ed5bd48291e713a7c390905750d78d30fe248da0..5cc81ea2b623c1d1ca7bfc60d7025e63bdc33e92 100644 (file)
@@ -1107,9 +1107,9 @@ static int __ras_mc_prepare_stmt(struct sqlite3_priv *priv,
 
        for (i = 1; i < db_tab->num_fields; i++) {
                if (i <  db_tab->num_fields - 1)
-                       strcat(sql, "?, ");
+                       strscat(sql, "?, ", sizeof(sql));
                else
-                       strcat(sql, "?)");
+                       strscat(sql, "?)", sizeof(sql));
        }
 
 #ifdef DEBUG_SQL
index 3148315c29cab6fd0de12b00149134ab459d4c19..3898041e82fd2a64badb20d4f8f0dca9d451743f 100644 (file)
@@ -33,7 +33,7 @@ static int setup_report_socket(void)
 
        memset(&addr, 0, sizeof(struct sockaddr_un));
        addr.sun_family = AF_UNIX;
-       strncpy(addr.sun_path, ABRT_SOCKET, sizeof(addr.sun_path));
+       strscpy(addr.sun_path, ABRT_SOCKET, sizeof(addr.sun_path));
        addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
 
        rc = connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
diff --git a/types.h b/types.h
index 5e77967acc9d351bfe726963d9c2e37610ca8990..d0b4ee316c92dd289b941213202c1fa3b26eedfe 100644 (file)
--- a/types.h
+++ b/types.h
 
 /* BIT handling */
 
-#define _AC(X,Y)       (X##Y)
+#define _AC(X, Y)      (X##Y)
 
 #define _UL(x)          (_AC(x, UL))
 #define _ULL(x)         (_AC(x, ULL))