Commit 5b71fbdc authored by Jan Beulich's avatar Jan Beulich Committed by Konrad Rzeszutek Wilk

xen-pciback: simplify and tighten parsing of device IDs

Now that at least one of the conformance problems of the kernel's
sscanf() was addressed (commit da99075c),
we can improve the parsing done in xen-pciback both in terms of code
readability and correctness (in particular properly rejecting input
strings not well formed).
Signed-off-by: default avatarJan Beulich <jbeulich@suse.com>
Signed-off-by: default avatarKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>
parent 9d02b43d
...@@ -897,42 +897,35 @@ static struct pci_driver xen_pcibk_pci_driver = { ...@@ -897,42 +897,35 @@ static struct pci_driver xen_pcibk_pci_driver = {
static inline int str_to_slot(const char *buf, int *domain, int *bus, static inline int str_to_slot(const char *buf, int *domain, int *bus,
int *slot, int *func) int *slot, int *func)
{ {
int err; int parsed = 0;
char wc = '*';
err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func); switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
switch (err) { &parsed)) {
case 3: case 3:
*func = -1; *func = -1;
err = sscanf(buf, " %x:%x:%x.%c", domain, bus, slot, &wc); sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
break; break;
case 2: case 2:
*slot = *func = -1; *slot = *func = -1;
err = sscanf(buf, " %x:%x:*.%c", domain, bus, &wc); sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
if (err >= 2)
++err;
break; break;
} }
if (err == 4 && wc == '*') if (parsed && !buf[parsed])
return 0; return 0;
else if (err < 0)
return -EINVAL;
/* try again without domain */ /* try again without domain */
*domain = 0; *domain = 0;
wc = '*'; switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
err = sscanf(buf, " %x:%x.%x", bus, slot, func);
switch (err) {
case 2: case 2:
*func = -1; *func = -1;
err = sscanf(buf, " %x:%x.%c", bus, slot, &wc); sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
break; break;
case 1: case 1:
*slot = *func = -1; *slot = *func = -1;
err = sscanf(buf, " %x:*.%c", bus, &wc) + 1; sscanf(buf, " %x:*.* %n", bus, &parsed);
break; break;
} }
if (err == 3 && wc == '*') if (parsed && !buf[parsed])
return 0; return 0;
return -EINVAL; return -EINVAL;
...@@ -941,13 +934,20 @@ static inline int str_to_slot(const char *buf, int *domain, int *bus, ...@@ -941,13 +934,20 @@ static inline int str_to_slot(const char *buf, int *domain, int *bus,
static inline int str_to_quirk(const char *buf, int *domain, int *bus, int static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
*slot, int *func, int *reg, int *size, int *mask) *slot, int *func, int *reg, int *size, int *mask)
{ {
int err; int parsed = 0;
sscanf(buf, " %4x:%2x:%2x.%d-%8x:%1x:%8x %n", domain, bus, slot, func,
reg, size, mask, &parsed);
if (parsed && !buf[parsed])
return 0;
err = /* try again without domain */
sscanf(buf, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain, bus, slot, *domain = 0;
func, reg, size, mask); sscanf(buf, " %2x:%2x.%d-%8x:%1x:%8x %n", bus, slot, func, reg, size,
if (err == 7) mask, &parsed);
if (parsed && !buf[parsed])
return 0; return 0;
return -EINVAL; return -EINVAL;
} }
...@@ -1339,8 +1339,6 @@ static int __init pcistub_init(void) ...@@ -1339,8 +1339,6 @@ static int __init pcistub_init(void)
if (pci_devs_to_hide && *pci_devs_to_hide) { if (pci_devs_to_hide && *pci_devs_to_hide) {
do { do {
char wc = '*';
parsed = 0; parsed = 0;
err = sscanf(pci_devs_to_hide + pos, err = sscanf(pci_devs_to_hide + pos,
...@@ -1349,51 +1347,48 @@ static int __init pcistub_init(void) ...@@ -1349,51 +1347,48 @@ static int __init pcistub_init(void)
switch (err) { switch (err) {
case 3: case 3:
func = -1; func = -1;
err = sscanf(pci_devs_to_hide + pos, sscanf(pci_devs_to_hide + pos,
" (%x:%x:%x.%c) %n", " (%x:%x:%x.*) %n",
&domain, &bus, &slot, &wc, &domain, &bus, &slot, &parsed);
&parsed);
break; break;
case 2: case 2:
slot = func = -1; slot = func = -1;
err = sscanf(pci_devs_to_hide + pos, sscanf(pci_devs_to_hide + pos,
" (%x:%x:*.%c) %n", " (%x:%x:*.*) %n",
&domain, &bus, &wc, &parsed) + 1; &domain, &bus, &parsed);
break; break;
} }
if (err != 4 || wc != '*') { if (!parsed) {
domain = 0; domain = 0;
wc = '*';
err = sscanf(pci_devs_to_hide + pos, err = sscanf(pci_devs_to_hide + pos,
" (%x:%x.%x) %n", " (%x:%x.%x) %n",
&bus, &slot, &func, &parsed); &bus, &slot, &func, &parsed);
switch (err) { switch (err) {
case 2: case 2:
func = -1; func = -1;
err = sscanf(pci_devs_to_hide + pos, sscanf(pci_devs_to_hide + pos,
" (%x:%x.%c) %n", " (%x:%x.*) %n",
&bus, &slot, &wc, &bus, &slot, &parsed);
&parsed);
break; break;
case 1: case 1:
slot = func = -1; slot = func = -1;
err = sscanf(pci_devs_to_hide + pos, sscanf(pci_devs_to_hide + pos,
" (%x:*.%c) %n", " (%x:*.*) %n",
&bus, &wc, &parsed) + 1; &bus, &parsed);
break; break;
} }
if (err != 3 || wc != '*')
goto parse_error;
} }
if (parsed <= 0)
goto parse_error;
err = pcistub_device_id_add(domain, bus, slot, func); err = pcistub_device_id_add(domain, bus, slot, func);
if (err) if (err)
goto out; goto out;
/* if parsed<=0, we've reached the end of the string */
pos += parsed; pos += parsed;
} while (parsed > 0 && pci_devs_to_hide[pos]); } while (pci_devs_to_hide[pos]);
} }
/* If we're the first PCI Device Driver to register, we're the /* If we're the first PCI Device Driver to register, we're the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment