From patchwork Thu Apr 27 09:43:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Kubecek X-Patchwork-Id: 755925 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3wDBpw2hlRz9sDC for ; Thu, 27 Apr 2017 19:43:52 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938085AbdD0Jnu (ORCPT ); Thu, 27 Apr 2017 05:43:50 -0400 Received: from mx2.suse.de ([195.135.220.15]:37579 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932592AbdD0Jnt (ORCPT ); Thu, 27 Apr 2017 05:43:49 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 9A8C8AC39; Thu, 27 Apr 2017 09:43:47 +0000 (UTC) Received: by unicorn.suse.cz (Postfix, from userid 1000) id 3EB63A0F1C; Thu, 27 Apr 2017 11:43:47 +0200 (CEST) From: Michal Kubecek Subject: [PATCH iproute2] routel: fix infinite loop in line parser To: Stephen Hemminger Cc: netdev@vger.kernel.org Message-Id: <20170427094347.3EB63A0F1C@unicorn.suse.cz> Date: Thu, 27 Apr 2017 11:43:47 +0200 (CEST) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- 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 ' ' '