Linux PCI subsystem development
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dave@treblig.org>
To: mj@ucw.cz
Cc: linux-pci@vger.kernel.org
Subject: pciutils pci_open seg with invalid arg to -i
Date: Tue, 4 Dec 2012 14:20:08 +0000	[thread overview]
Message-ID: <20121204142008.GB6117@gallifrey> (raw)

Hi Martin,
  I was looking at a bug reported in the Ubuntu bug database against lspci:

https://bugs.launchpad.net/ubuntu/+source/pciutils/+bug/690431

The reporter had called lspci -vvii    which ends up trying to open the
file 'i' as the pci-id and seg faults.

I can see a fix, but I can't quite see what the code is trying to do;
lib/names-parse.c:

 23 static pci_file pci_open(struct pci_access *a)
 24 {
 25   pci_file result;
 26   size_t len;
 27   char *new_name;
 28 
 29   result = gzopen(a->id_file_name, "rb");
 30   if (result)
 31     return result;
 32   len = strlen(a->id_file_name);
 33   if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
 34     return result;
 35   new_name = malloc(len - 2);
 36   memcpy(new_name, a->id_file_name, len - 3);
 37   new_name[len - 3] = 0;
 38   pci_set_name_list_path(a, new_name, 1);
 39   return gzopen(a->id_file_name, "rb");
 40 }

The problem here is for a short filename we drop down to line 35 and
do an allocate of a -ve length, and then try and memcpy into it.

So I was going to suggest something like:

 33   if (len < 3 || memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
 34     return result;

which would mean that it would drop out if it was a short filename; but
then I started to try and understand the code more; it looks to me like:

   try and gzopen it.
   if that fails
     check it ended with a .gz (else fail)
     strip the .gz off the end
     gzopen it without the .gz

But why bother?  According to the zlib manual for gzopen :
  ( http://www.zlib.net/manual.html )
     
' gzopen can be used to read a file which is not in gzip format; in this case
gzread will directly read from the file without decompression. When reading,
this will be detected automatically by looking for the magic two-byte gzip
header. '

So what are lines 32-39 trying to do?

Dave (not subscribed to linux-pci, so please cc)
-- 
 -----Open up your eyes, open up your mind, open up your code -------   
/ Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \ 
\ gro.gilbert @ treblig.org |                               | In Hex /
 \ _________________________|_____ http://www.treblig.org   |_______/

             reply	other threads:[~2012-12-04 14:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-04 14:20 Dr. David Alan Gilbert [this message]
2012-12-05 20:36 ` pciutils pci_open seg with invalid arg to -i Martin Mares
2012-12-06 16:08 ` Martin Mares

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121204142008.GB6117@gallifrey \
    --to=dave@treblig.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mj@ucw.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox