platform: fix crash in binary search for _link_type_from_rtnl_type(), _link_type_from_devtype()

When searching an element that is lower than the first list element (for
example RTNL type "batadv"), imax will be -1 after the last iteration.

Use int instead of unsigned to make the termination condition imin > imax
work in this case. This fixes NetworkManager crashing due to an
out-of-bounds array access whenever interfaces of such types exist.

Fixes: 19ad044359 ('platform: use binary search to lookup NMLinkType for rtnl_type')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/515
This commit is contained in:
Matthias Schiffer 2020-05-24 01:13:05 +02:00 committed by Thomas Haller
parent 41d431e0f8
commit 2b54202089
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -726,9 +726,9 @@ _link_type_from_rtnl_type (const char *name) \
}
{
unsigned imin = 0;
unsigned imax = (G_N_ELEMENTS (LIST) - 1);
unsigned imid = (G_N_ELEMENTS (LIST) - 1) / 2;
int imin = 0;
int imax = (G_N_ELEMENTS (LIST) - 1);
int imid = (G_N_ELEMENTS (LIST) - 1) / 2;
for (;;) {
const int cmp = strcmp (link_descs[LIST[imid]].rtnl_type, name);
@ -787,9 +787,9 @@ _link_type_from_devtype (const char *name) \
}
{
unsigned imin = 0;
unsigned imax = (G_N_ELEMENTS (LIST) - 1);
unsigned imid = (G_N_ELEMENTS (LIST) - 1) / 2;
int imin = 0;
int imax = (G_N_ELEMENTS (LIST) - 1);
int imid = (G_N_ELEMENTS (LIST) - 1) / 2;
for (;;) {
const int cmp = strcmp (link_descs[LIST[imid]].devtype, name);