Repository navigation
libmultipath: fix buffer overflow and partial device number match - #168
wuguanghao3 wants to merge 2 commits into
Conversation
dm_reassign_deps() wrote the replacement device number in place into the buffer holding the table string, which is sized to fit the original string only. The replacement (the multipath map's device number, e.g. "253:1234") is usually longer than the old one (a path device, e.g. "8:16"), so dm_reassign_table() could write past the end of the buffer. Make dm_reassign_deps() return a newly allocated string instead, and update dm_reassign_table() to use it.
dm_reassign_deps() used strstr() to find the old device number in the table, so a short device number like "8:0" could match inside a longer one, e.g. "18:0" or "8:03". Replacing such a partial match would leave the table pointing at an arbitrary wrong device number. Match the old device number only if it is a standalone token, i.e. not preceded nor followed by a digit. Apply the same check to the lookup in dm_reassign_table() that decides whether to replace a target, so that both agree.
bmarzins
left a comment
There was a problem hiding this comment.
multipath-tools prefers to use a mailing list workflow. Could you please send the next version of your patches to [email protected], and CC me, "Benjamin Marzinski [email protected]" and "Martin Wilck [email protected]".
| strcat(n, p); | ||
| free(newtable); | ||
| while ((p = strstr(p, dev_t)) != NULL) { | ||
| if ((p == table || !isdigit(p[-1])) && !isdigit(p[len])) |
There was a problem hiding this comment.
Instead of just looking for !isdigit(), dm target tables should always have whitespace() on either side of a device (obviously except at the beginning and end of the table string). It seems safer to look for that.
| while ((p = strstr(p, dev_t)) != NULL) { | ||
| if ((p == table || !isdigit(p[-1])) && !isdigit(p[len])) | ||
| return p; | ||
| p++; |
There was a problem hiding this comment.
You can increment py by len here.
| * The buffer holding "table" is sized to fit that string exactly, and | ||
| * "newdep" is usually longer than "dep" (e.g. "253:1234" vs "8:16"). | ||
| * Therefore the result mustn't be written in place, which would | ||
| * overflow the buffer. |
There was a problem hiding this comment.
I realize that this was already broken, but the code shouldn't just replace the first occurence of "dep". It should replace all the occurences of "dep" in "table". dm target tables can include the same device multiple times.
| newparams = dm_reassign_deps(params, old, new); | ||
| if (!newparams) { | ||
| condlog(3, "%s: failed to replace target %s, " | ||
| "out of memory", name, target); |
There was a problem hiding this comment.
Instead of manually adding "out of memory", you should use the "%m" format specifier.
No description provided.