netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2] ip route: check ftell, fseek return value
@ 2016-09-06  6:39 Hangbin Liu
  2016-09-06 10:05 ` Phil Sutter
  2016-09-06 11:18 ` David Laight
  0 siblings, 2 replies; 4+ messages in thread
From: Hangbin Liu @ 2016-09-06  6:39 UTC (permalink / raw)
  To: netdev; +Cc: Phil Sutter, Stephen Hemminger, Hangbin Liu

ftell() may return -1 in error case, which is not handled and therefore pass a
negative offset to fseek(). The return code of fseek() is also not checked.

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
 ip/iproute.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/ip/iproute.c b/ip/iproute.c
index 3da23af..ba877dc 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -1859,7 +1859,11 @@ static int iproute_restore(void)
 	if (route_dump_check_magic())
 		exit(-1);
 
-	pos = ftell(stdin);
+	if ((pos = ftell(stdin)) == -1) {
+		perror("Failed to restore: ftell");
+		exit(errno);
+	}
+
 	for (prio = 0; prio < 3; prio++) {
 		int err;
 
@@ -1867,7 +1871,10 @@ static int iproute_restore(void)
 		if (err)
 			exit(err);
 
-		fseek(stdin, pos, SEEK_SET);
+		if (fseek(stdin, pos, SEEK_SET) == -1) {
+			perror("Failed to restore: fseek");
+			exit(errno);
+		}
 	}
 
 	exit(0);
-- 
2.5.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH iproute2] ip route: check ftell, fseek return value
  2016-09-06  6:39 [PATCH iproute2] ip route: check ftell, fseek return value Hangbin Liu
@ 2016-09-06 10:05 ` Phil Sutter
  2016-09-06 11:18 ` David Laight
  1 sibling, 0 replies; 4+ messages in thread
From: Phil Sutter @ 2016-09-06 10:05 UTC (permalink / raw)
  To: Hangbin Liu; +Cc: netdev, Stephen Hemminger

On Tue, Sep 06, 2016 at 02:39:50PM +0800, Hangbin Liu wrote:
> ftell() may return -1 in error case, which is not handled and therefore pass a
> negative offset to fseek(). The return code of fseek() is also not checked.
> 
> Reported-by: Phil Sutter <phil@nwl.cc>
> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>

Acked-by: Phil Sutter <phil@nwl.cc>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH iproute2] ip route: check ftell, fseek return value
  2016-09-06  6:39 [PATCH iproute2] ip route: check ftell, fseek return value Hangbin Liu
  2016-09-06 10:05 ` Phil Sutter
@ 2016-09-06 11:18 ` David Laight
  2016-09-07 13:50   ` Hangbin Liu
  1 sibling, 1 reply; 4+ messages in thread
From: David Laight @ 2016-09-06 11:18 UTC (permalink / raw)
  To: 'Hangbin Liu', netdev@vger.kernel.org
  Cc: Phil Sutter, Stephen Hemminger

From: Hangbin Liu
> Sent: 06 September 2016 07:40
> ftell() may return -1 in error case, which is not handled and therefore pass a
> negative offset to fseek(). The return code of fseek() is also not checked.
> 
> Reported-by: Phil Sutter <phil@nwl.cc>
> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
> ---
>  ip/iproute.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/ip/iproute.c b/ip/iproute.c
> index 3da23af..ba877dc 100644
> --- a/ip/iproute.c
> +++ b/ip/iproute.c
> @@ -1859,7 +1859,11 @@ static int iproute_restore(void)
>  	if (route_dump_check_magic())
>  		exit(-1);
> 
> -	pos = ftell(stdin);
> +	if ((pos = ftell(stdin)) == -1) {

Don't put assignments in conditionals.

> +		perror("Failed to restore: ftell");
> +		exit(errno);

errno is not a valid argument to exit().

...

Actually WTF is this code trying to do.
stdin is very likely to be a pipe, so expecting to seek on it
seems very likely to fail.

	David

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH iproute2] ip route: check ftell, fseek return value
  2016-09-06 11:18 ` David Laight
@ 2016-09-07 13:50   ` Hangbin Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Hangbin Liu @ 2016-09-07 13:50 UTC (permalink / raw)
  To: David Laight
  Cc: 'Hangbin Liu', netdev@vger.kernel.org, Phil Sutter,
	Stephen Hemminger

On Tue, Sep 06, 2016 at 11:18:00AM +0000, David Laight wrote:
> From: Hangbin Liu
> > Sent: 06 September 2016 07:40
> > ftell() may return -1 in error case, which is not handled and therefore pass a
> > negative offset to fseek(). The return code of fseek() is also not checked.
> > 
> > Reported-by: Phil Sutter <phil@nwl.cc>
> > Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
> > ---
> >  ip/iproute.c | 11 +++++++++--
> >  1 file changed, 9 insertions(+), 2 deletions(-)
> > 
> > diff --git a/ip/iproute.c b/ip/iproute.c
> > index 3da23af..ba877dc 100644
> > --- a/ip/iproute.c
> > +++ b/ip/iproute.c
> > @@ -1859,7 +1859,11 @@ static int iproute_restore(void)
> >  	if (route_dump_check_magic())
> >  		exit(-1);
> > 
> > -	pos = ftell(stdin);
> > +	if ((pos = ftell(stdin)) == -1) {
> 
> Don't put assignments in conditionals.
> 
> > +		perror("Failed to restore: ftell");
> > +		exit(errno);
> 
> errno is not a valid argument to exit().

Hi David,

Thanks for the advice. I will send PATCHv2 to fix them.

> 
> ...
> 
> Actually WTF is this code trying to do.
> stdin is vere likely to be a pipe, so expecting to seek on it
> seems very likely to fail.

I will fix the return code handle issue first and try investigate the pipe
issue later.

Regards
Hangbin

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-09-07 13:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-06  6:39 [PATCH iproute2] ip route: check ftell, fseek return value Hangbin Liu
2016-09-06 10:05 ` Phil Sutter
2016-09-06 11:18 ` David Laight
2016-09-07 13:50   ` Hangbin Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).