From patchwork Fri Dec 19 13:25:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 422882 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C3B3E140082 for ; Sat, 20 Dec 2014 00:25:51 +1100 (AEDT) Received: from localhost ([::1]:58534 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y1xZ0-0002vA-18 for incoming@patchwork.ozlabs.org; Fri, 19 Dec 2014 08:25:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54917) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y1xYU-0002ZP-MT for qemu-devel@nongnu.org; Fri, 19 Dec 2014 08:25:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y1xYO-0002eb-5J for qemu-devel@nongnu.org; Fri, 19 Dec 2014 08:25:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56254) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y1xYN-0002eK-Uq for qemu-devel@nongnu.org; Fri, 19 Dec 2014 08:25:12 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sBJDPBFn013082 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 19 Dec 2014 08:25:11 -0500 Received: from air.redhat.com (vpn1-113-187.nay.redhat.com [10.66.113.187]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sBJDP3og023257; Fri, 19 Dec 2014 08:25:05 -0500 From: Amos Kong To: qemu-devel@nongnu.org Date: Fri, 19 Dec 2014 21:25:02 +0800 Message-Id: <1418995502-14908-1-git-send-email-akong@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: jasowang@redhat.com, stefanha@redhat.com, mst@redhat.com Subject: [Qemu-devel] [PATCH] check return value of fcntl() to detect invalid fd X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Passing some invalid fds in QEMU commandline, the fds don't exist. QEMU will get error "TUNGETIFF ioctl() failed: Bad file descriptor", and coredump in setting queues. This patch checked return value of first operate to fd, QEMU will report error and exit without coredump. It's effected for both netdev fds and vhost_net fds. Signed-off-by: Amos Kong --- net/tap.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/net/tap.c b/net/tap.c index bde6b58..039280a 100644 --- a/net/tap.c +++ b/net/tap.c @@ -688,7 +688,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name, NetClientState *peer) { const NetdevTapOptions *tap; - int fd, vnet_hdr = 0, i = 0, queues; + int fd, vnet_hdr = 0, i = 0, queues, ret; /* for the no-fd, no-helper case */ const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */ const char *downscript = NULL; @@ -722,7 +722,12 @@ int net_init_tap(const NetClientOptions *opts, const char *name, return -1; } - fcntl(fd, F_SETFL, O_NONBLOCK); + ret = fcntl(fd, F_SETFL, O_NONBLOCK); + if (ret < 0) { + error_report("Fail to set file status to nonblock, " + "%s", strerror(-ret)); + return -1; + } vnet_hdr = tap_probe_vnet_hdr(fd); @@ -761,7 +766,12 @@ int net_init_tap(const NetClientOptions *opts, const char *name, return -1; } - fcntl(fd, F_SETFL, O_NONBLOCK); + ret = fcntl(fd, F_SETFL, O_NONBLOCK); + if (ret < 0) { + error_report("Fail to set file status to nonblock, " + "%s", strerror(-ret)); + return -1; + } if (i == 0) { vnet_hdr = tap_probe_vnet_hdr(fd);