From patchwork Mon Feb 4 10:40:12 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 217911 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 994E42C02AB for ; Tue, 5 Feb 2013 00:17:31 +1100 (EST) Received: from localhost ([::1]:34353 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2JUn-0006NE-2q for incoming@patchwork.ozlabs.org; Mon, 04 Feb 2013 05:41:53 -0500 Received: from eggs.gnu.org ([208.118.235.92]:50676) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2JUR-0006CB-LL for qemu-devel@nongnu.org; Mon, 04 Feb 2013 05:41:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U2JUO-0000qp-O5 for qemu-devel@nongnu.org; Mon, 04 Feb 2013 05:41:31 -0500 Received: from isrv.corpit.ru ([86.62.121.231]:59141) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2JUO-0000qd-Em; Mon, 04 Feb 2013 05:41:28 -0500 Received: from gandalf.tls.msk.ru (mjt.vpn.tls.msk.ru [192.168.177.99]) by isrv.corpit.ru (Postfix) with ESMTP id C45C2A03E8; Mon, 4 Feb 2013 14:41:27 +0400 (MSK) Received: by gandalf.tls.msk.ru (Postfix, from userid 1000) id 64F4B1A4; Mon, 4 Feb 2013 14:41:26 +0400 (MSK) From: Michael Tokarev To: qemu-devel@nongnu.org Date: Mon, 4 Feb 2013 14:40:12 +0400 Message-Id: <1359974470-17044-3-git-send-email-mjt@msgid.tls.msk.ru> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1359974470-17044-1-git-send-email-mjt@msgid.tls.msk.ru> References: <1359974470-17044-1-git-send-email-mjt@msgid.tls.msk.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 86.62.121.231 Cc: Anthony Liguori , Michael Tokarev , qemu-stable@nongnu.org, David Gibson Subject: [Qemu-devel] [PATCH 02/60] qemu-char: BUGFIX, don't call FD_ISSET with negative 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 From: David Gibson tcp_chr_connect(), unlike for example udp_chr_update_read_handler() does not check if the fd it is using is valid (>= 0) before passing it to qemu_set_fd_handler2(). If using e.g. a TCP serial port, which is not initially connected, this can result in -1 being passed to FD_ISSET, which has undefined behaviour. On x86 it seems to harmlessly return 0, but on PowerPC, it causes a fortify buffer overflow error to be thrown. This patch fixes this by putting an extra test in tcp_chr_connect(), and also adds an assert qemu_set_fd_handler2() to catch other such errors on all platforms, rather than just some. Signed-off-by: David Gibson Signed-off-by: Anthony Liguori (cherry picked from commit bbdd2ad0814ea0911076419ea21b7957505cf1cc) Signed-off-by: Michael Tokarev --- iohandler.c | 2 ++ qemu-char.c | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/iohandler.c b/iohandler.c index dea4355..a2d871b 100644 --- a/iohandler.c +++ b/iohandler.c @@ -56,6 +56,8 @@ int qemu_set_fd_handler2(int fd, { IOHandlerRecord *ioh; + assert(fd >= 0); + if (!fd_read && !fd_write) { QLIST_FOREACH(ioh, &io_handlers, next) { if (ioh->fd == fd) { diff --git a/qemu-char.c b/qemu-char.c index fe1126f..3a68430 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2321,8 +2321,10 @@ static void tcp_chr_connect(void *opaque) TCPCharDriver *s = chr->opaque; s->connected = 1; - qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, - tcp_chr_read, NULL, chr); + if (s->fd >= 0) { + qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, + tcp_chr_read, NULL, chr); + } qemu_chr_generic_open(chr); }