netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2] routel: fix infinite loop in line parser
@ 2017-04-27  9:43 Michal Kubecek
  2017-04-27 23:46 ` Stephen Hemminger
  0 siblings, 1 reply; 3+ messages in thread
From: Michal Kubecek @ 2017-04-27  9:43 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

As noticed by one of the few users of routel script, it ends up in an
infinite loop when they pull out the cable from the NIC used for some
route. This is caused by its parser expecting the line of "ip route show"
output consists of "key value" pairs (except for the initial target range),
together with an old trap of Bourne style shells that "shift 2" does
nothing if there is only one argument left. Some keywords, e.g. "linkdown",
are not followed by a value.

Improve the parser to

  (1) only set variables for keywords we care about
  (2) recognize (currently) known keywords without value

This is still far from perfect (and certainly not future proof) but to
fully fix the script, one would probably have to rewrite the logic
completely (and I'm not sure it's worth the effort).

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
---
 ip/routel | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/ip/routel b/ip/routel
index 8d1d352abdff..9a30462aa6b2 100644
--- a/ip/routel
+++ b/ip/routel
@@ -32,10 +32,22 @@ ip route list table "$@" |
     esac
     while test $# != 0
     do
-       key=$1
-       val=$2
-       eval "$key=$val"
-       shift 2
+       case "$1" in
+          proto|via|dev|scope|src|table)
+             key=$1
+             val=$2
+             eval "$key='$val'"
+             shift 2
+             ;;
+          dead|onlink|pervasive|offload|notify|linkdown|unresolved)
+             shift
+             ;;
+          *)
+             # avoid infinite loop on unknown keyword without value at line end
+             shift
+             shift
+             ;;
+       esac
     done
     echo "$network	$via	$src	$proto	$scope	$dev	$table"
  done | awk -F '	' '
-- 
2.12.2

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

* Re: [PATCH iproute2] routel: fix infinite loop in line parser
  2017-04-27  9:43 [PATCH iproute2] routel: fix infinite loop in line parser Michal Kubecek
@ 2017-04-27 23:46 ` Stephen Hemminger
  2017-04-28 16:21   ` Michal Kubecek
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Hemminger @ 2017-04-27 23:46 UTC (permalink / raw)
  To: Michal Kubecek; +Cc: netdev

On Thu, 27 Apr 2017 11:43:47 +0200 (CEST)
Michal Kubecek <mkubecek@suse.cz> wrote:

> As noticed by one of the few users of routel script, it ends up in an
> infinite loop when they pull out the cable from the NIC used for some
> route. This is caused by its parser expecting the line of "ip route show"
> output consists of "key value" pairs (except for the initial target range),
> together with an old trap of Bourne style shells that "shift 2" does
> nothing if there is only one argument left. Some keywords, e.g. "linkdown",
> are not followed by a value.
> 
> Improve the parser to
> 
>   (1) only set variables for keywords we care about
>   (2) recognize (currently) known keywords without value
> 
> This is still far from perfect (and certainly not future proof) but to
> fully fix the script, one would probably have to rewrite the logic
> completely (and I'm not sure it's worth the effort).
> 
> Signed-off-by: Michal Kubecek <mkubecek@suse.cz>

Appled, but this really needs to be done better.
Either as a simplified output of route command. See ip -br link
Or ip route should have a json output option and use python/perl/xss
to reformat.

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

* Re: [PATCH iproute2] routel: fix infinite loop in line parser
  2017-04-27 23:46 ` Stephen Hemminger
@ 2017-04-28 16:21   ` Michal Kubecek
  0 siblings, 0 replies; 3+ messages in thread
From: Michal Kubecek @ 2017-04-28 16:21 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

On Thu, Apr 27, 2017 at 04:46:47PM -0700, Stephen Hemminger wrote:
> 
> Appled, but this really needs to be done better.
> Either as a simplified output of route command. See ip -br link
> Or ip route should have a json output option and use python/perl/xss
> to reformat.

Agreed, parsing the output of ip command is certainly not the right way.

I must admit that I had no idea this script exists until the bug report
landed on my table (and I had to use "rpm -qf" to learn that it's part
of iproute2 package). And I suspect I may not be the only one.

Michal Kubecek

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

end of thread, other threads:[~2017-04-28 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-27  9:43 [PATCH iproute2] routel: fix infinite loop in line parser Michal Kubecek
2017-04-27 23:46 ` Stephen Hemminger
2017-04-28 16:21   ` Michal Kubecek

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).