Pino Toscano
2014-Aug-11 16:35 UTC
[Libguestfs] [PATCH] p2v: check results of strndup and sscanf
---
p2v/ssh.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/p2v/ssh.c b/p2v/ssh.c
index 1e9b05c..ff906df 100644
--- a/p2v/ssh.c
+++ b/p2v/ssh.c
@@ -505,7 +505,16 @@ open_data_connection (struct config *config, int
*local_port, int *remote_port)
}, ovector, ovecsize)) {
case 100: /* Ephemeral port. */
port_str = strndup (&h->buffer[ovector[2]], ovector[3]-ovector[2]);
- sscanf (port_str, "%d", remote_port);
+ if (port_str == NULL) {
+ set_ssh_error ("not enough memory for strndup");
+ mexp_close (h);
+ return NULL;
+ }
+ if (sscanf (port_str, "%d", remote_port) != 1) {
+ set_ssh_error ("cannot extract the port number from
'%s'", port_str);
+ mexp_close (h);
+ return NULL;
+ }
break;
case MEXP_EOF:
--
1.9.3
Richard W.M. Jones
2014-Aug-12 09:54 UTC
Re: [Libguestfs] [PATCH] p2v: check results of strndup and sscanf
On Mon, Aug 11, 2014 at 06:35:50PM +0200, Pino Toscano wrote:> --- > p2v/ssh.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/p2v/ssh.c b/p2v/ssh.c > index 1e9b05c..ff906df 100644 > --- a/p2v/ssh.c > +++ b/p2v/ssh.c > @@ -505,7 +505,16 @@ open_data_connection (struct config *config, int *local_port, int *remote_port) > }, ovector, ovecsize)) { > case 100: /* Ephemeral port. */ > port_str = strndup (&h->buffer[ovector[2]], ovector[3]-ovector[2]); > - sscanf (port_str, "%d", remote_port); > + if (port_str == NULL) { > + set_ssh_error ("not enough memory for strndup"); > + mexp_close (h); > + return NULL; > + } > + if (sscanf (port_str, "%d", remote_port) != 1) { > + set_ssh_error ("cannot extract the port number from '%s'", port_str); > + mexp_close (h); > + return NULL; > + } > break;ACK, although the regex ought to guarantee this already? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
Pino Toscano
2014-Aug-12 10:51 UTC
Re: [Libguestfs] [PATCH] p2v: check results of strndup and sscanf
On Tuesday 12 August 2014 10:54:08 Richard W.M. Jones wrote:> On Mon, Aug 11, 2014 at 06:35:50PM +0200, Pino Toscano wrote: > > --- > > > > p2v/ssh.c | 11 ++++++++++- > > 1 file changed, 10 insertions(+), 1 deletion(-) > > > > diff --git a/p2v/ssh.c b/p2v/ssh.c > > index 1e9b05c..ff906df 100644 > > --- a/p2v/ssh.c > > +++ b/p2v/ssh.c > > @@ -505,7 +505,16 @@ open_data_connection (struct config *config, > > int *local_port, int *remote_port)> > > }, ovector, ovecsize)) { > > > > case 100: /* Ephemeral port. */ > > > > port_str = strndup (&h->buffer[ovector[2]], > > ovector[3]-ovector[2]); > > > > - sscanf (port_str, "%d", remote_port); > > + if (port_str == NULL) { > > + set_ssh_error ("not enough memory for strndup"); > > + mexp_close (h); > > + return NULL; > > + } > > + if (sscanf (port_str, "%d", remote_port) != 1) { > > + set_ssh_error ("cannot extract the port number from '%s'", > > port_str); + mexp_close (h); > > + return NULL; > > + } > > > > break; > > ACK, although the regex ought to guarantee this already?Yes, it does -- this is just a small safety net for sscanf. Thanks, -- Pino Toscano
Maybe Matching Threads
- [PATCH] p2v: improve error message for sudo with password
- [PATCH 0/4] p2v: Send ^C to remote end to cancel the conversion.
- [PATCH] p2v: require a non-interative sudo (RHBZ#1340809)
- [PATCH 0/2] Remove virt-p2v from libguestfs
- [PATCH] p2v: Test for sudo requiring a password first (RHBZ#1500673).