Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

EDAC/mc: Replace strcpy(), sprintf() and snprintf() with strscpy() or scnprintf()

strcpy() performs no bounds checking on the destination buffer. This
could result in linear overflows beyond the end of the buffer, leading
to all kinds of misbehavior. The safe replacement is strscpy().
[1][2]

However, to simplify and clarify the code, to concatenate labels use the
scnprintf() function. This way it is not necessary to check the return
value of strscpy() (-E2BIG if the parameter count is 0 or the src was
truncated) since scnprintf() always returns the number of chars written
into the buffer. This function always returns a nul-terminated string
even if it needs to be truncated.

While at it, fix all other broken string generation code that wrongly
interprets snprintf()'s return code or just uses sprintf(), implement
that using scnprintf() here too. Drop breaks in loops around
scnprintf() as it is safe now to loop.

Moreover, the check is not needed: for the case when the buffer is
exhausted, len never gets zero because scnprintf() takes the full buffer
length as input parameter, but excludes the trailing '\0' in its return
code and thus, 1 is the minimum len.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy
[2] https://github.com/KSPP/linux/issues/88

[ rric: Replace snprintf() with scnprintf(), rework sprintf() user,
drop breaks in loops around scnprintf(), introduce 'end' pointer to
reduce pointer arithmetic, use prefix pattern for e->location,
adjust subject and description ]

Co-developed-by: Joe Perches <joe@perches.com>
Signed-off-by: Joe Perches <joe@perches.com>
Signed-off-by: Len Baker <len.baker@gmx.com>
Signed-off-by: Robert Richter <rrichter@amd.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20210903150539.7282-1-len.baker@gmx.com

authored by

Len Baker and committed by
Borislav Petkov
fca61165 6880fa6c

+18 -24
+18 -24
drivers/edac/edac_mc.c
··· 66 66 char *p = buf; 67 67 68 68 for (i = 0; i < mci->n_layers; i++) { 69 - n = snprintf(p, len, "%s %d ", 69 + n = scnprintf(p, len, "%s %d ", 70 70 edac_layer_name[mci->layers[i].type], 71 71 dimm->location[i]); 72 72 p += n; 73 73 len -= n; 74 74 count += n; 75 - if (!len) 76 - break; 77 75 } 78 76 79 77 return count; ··· 339 341 */ 340 342 len = sizeof(dimm->label); 341 343 p = dimm->label; 342 - n = snprintf(p, len, "mc#%u", mci->mc_idx); 344 + n = scnprintf(p, len, "mc#%u", mci->mc_idx); 343 345 p += n; 344 346 len -= n; 345 347 for (layer = 0; layer < mci->n_layers; layer++) { 346 - n = snprintf(p, len, "%s#%u", 347 - edac_layer_name[mci->layers[layer].type], 348 - pos[layer]); 348 + n = scnprintf(p, len, "%s#%u", 349 + edac_layer_name[mci->layers[layer].type], 350 + pos[layer]); 349 351 p += n; 350 352 len -= n; 351 353 dimm->location[layer] = pos[layer]; 352 - 353 - if (len <= 0) 354 - break; 355 354 } 356 355 357 356 /* Link it to the csrows old API data */ ··· 1022 1027 const char *other_detail) 1023 1028 { 1024 1029 struct dimm_info *dimm; 1025 - char *p; 1030 + char *p, *end; 1026 1031 int row = -1, chan = -1; 1027 1032 int pos[EDAC_MAX_LAYERS] = { top_layer, mid_layer, low_layer }; 1028 1033 int i, n_labels = 0; 1029 1034 struct edac_raw_error_desc *e = &mci->error_desc; 1030 1035 bool any_memory = true; 1036 + const char *prefix; 1031 1037 1032 1038 edac_dbg(3, "MC%d\n", mci->mc_idx); 1033 1039 ··· 1083 1087 */ 1084 1088 p = e->label; 1085 1089 *p = '\0'; 1090 + end = p + sizeof(e->label); 1091 + prefix = ""; 1086 1092 1087 1093 mci_for_each_dimm(mci, dimm) { 1088 1094 if (top_layer >= 0 && top_layer != dimm->location[0]) ··· 1112 1114 p = e->label; 1113 1115 *p = '\0'; 1114 1116 } else { 1115 - if (p != e->label) { 1116 - strcpy(p, OTHER_LABEL); 1117 - p += strlen(OTHER_LABEL); 1118 - } 1119 - strcpy(p, dimm->label); 1120 - p += strlen(p); 1117 + p += scnprintf(p, end - p, "%s%s", prefix, dimm->label); 1118 + prefix = OTHER_LABEL; 1121 1119 } 1122 1120 1123 1121 /* ··· 1135 1141 } 1136 1142 1137 1143 if (any_memory) 1138 - strcpy(e->label, "any memory"); 1144 + strscpy(e->label, "any memory", sizeof(e->label)); 1139 1145 else if (!*e->label) 1140 - strcpy(e->label, "unknown memory"); 1146 + strscpy(e->label, "unknown memory", sizeof(e->label)); 1141 1147 1142 1148 edac_inc_csrow(e, row, chan); 1143 1149 1144 1150 /* Fill the RAM location data */ 1145 1151 p = e->location; 1152 + end = p + sizeof(e->location); 1153 + prefix = ""; 1146 1154 1147 1155 for (i = 0; i < mci->n_layers; i++) { 1148 1156 if (pos[i] < 0) 1149 1157 continue; 1150 1158 1151 - p += sprintf(p, "%s:%d ", 1152 - edac_layer_name[mci->layers[i].type], 1153 - pos[i]); 1159 + p += scnprintf(p, end - p, "%s%s:%d", prefix, 1160 + edac_layer_name[mci->layers[i].type], pos[i]); 1161 + prefix = " "; 1154 1162 } 1155 - if (p > e->location) 1156 - *(p - 1) = '\0'; 1157 1163 1158 1164 edac_raw_mc_handle_error(e); 1159 1165 }