Skip to content

libmultipath: fix buffer overflow and partial device number match - #168

Open
wuguanghao3 wants to merge 2 commits into
opensvc:masterfrom
wuguanghao3:master
Open

wuguanghao3 wants to merge 2 commits into
opensvc:masterfrom
wuguanghao3:master

Conversation

@wuguanghao3

Copy link
Copy Markdown
Contributor

No description provided.

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 bmarzins left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]".

Comment thread libmultipath/devmapper.c
strcat(n, p);
free(newtable);
while ((p = strstr(p, dev_t)) != NULL) {
if ((p == table || !isdigit(p[-1])) && !isdigit(p[len]))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread libmultipath/devmapper.c
while ((p = strstr(p, dev_t)) != NULL) {
if ((p == table || !isdigit(p[-1])) && !isdigit(p[len]))
return p;
p++;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can increment py by len here.

Comment thread libmultipath/devmapper.c
* 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread libmultipath/devmapper.c
newparams = dm_reassign_deps(params, old, new);
if (!newparams) {
condlog(3, "%s: failed to replace target %s, "
"out of memory", name, target);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of manually adding "out of memory", you should use the "%m" format specifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants