From patchwork Tue Oct 9 07:29:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kan Li X-Patchwork-Id: 981282 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sina.com Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Tz8v1Q5pz9s9G for ; Wed, 10 Oct 2018 00:48:47 +1100 (AEDT) Received: from localhost ([::1]:51656 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9sNQ-0006n5-Nn for incoming@patchwork.ozlabs.org; Tue, 09 Oct 2018 09:48:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48341) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9mSg-0005ZK-2p for qemu-devel@nongnu.org; Tue, 09 Oct 2018 03:29:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9mSc-0000VV-JS for qemu-devel@nongnu.org; Tue, 09 Oct 2018 03:29:46 -0400 Received: from mail7-212.sinamail.sina.com.cn ([202.108.7.212]:48990) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1g9mSc-0000S4-8g for qemu-devel@nongnu.org; Tue, 09 Oct 2018 03:29:42 -0400 Received: from c-24-130-69-251.hsd1.ca.comcast.net (HELO 0929ab6a2822.hsd1.ca.comcast.net)([24.130.69.251]) by sina.com with ESMTP id 5BBC58D3000053CF; Tue, 9 Oct 2018 15:29:34 +0800 (CST) X-Sender: likan_999.student@sina.com X-Auth-ID: likan_999.student@sina.com X-SMAIL-MID: 892393393893 From: Kan Li To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 07:29:23 +0000 Message-Id: <20181009072923.871-1-likan_999.student@sina.com> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 202.108.7.212 X-Mailman-Approved-At: Tue, 09 Oct 2018 09:45:57 -0400 Subject: [Qemu-devel] [PATCH] Fix linux-user crashes in ioctl(SIOCGIFCONF) when ifc_buf is NULL. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 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" Summary: This is to fix bug https://bugs.launchpad.net/qemu/+bug/1796754. It is valid for ifc_buf to be NULL according to http://man7.org/linux/man-pages/man7/netdevice.7.html. Signed-off-by: Kan Li --- linux-user/syscall.c | 56 ++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index ae3c0dfef7..fbab98d4f7 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -4134,28 +4134,33 @@ static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, unlock_user(argptr, arg, 0); host_ifconf = (struct ifconf *)(unsigned long)buf_temp; - target_ifc_len = host_ifconf->ifc_len; target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf; - target_ifreq_size = thunk_type_size(ifreq_arg_type, 0); - nb_ifreq = target_ifc_len / target_ifreq_size; - host_ifc_len = nb_ifreq * sizeof(struct ifreq); + if (target_ifc_buf != 0) { + target_ifc_len = host_ifconf->ifc_len; - outbufsz = sizeof(*host_ifconf) + host_ifc_len; - if (outbufsz > MAX_STRUCT_SIZE) { - /* We can't fit all the extents into the fixed size buffer. - * Allocate one that is large enough and use it instead. - */ - host_ifconf = malloc(outbufsz); - if (!host_ifconf) { - return -TARGET_ENOMEM; + target_ifreq_size = thunk_type_size(ifreq_arg_type, 0); + nb_ifreq = target_ifc_len / target_ifreq_size; + host_ifc_len = nb_ifreq * sizeof(struct ifreq); + + outbufsz = sizeof(*host_ifconf) + host_ifc_len; + if (outbufsz > MAX_STRUCT_SIZE) { + /* We can't fit all the extents into the fixed size buffer. + * Allocate one that is large enough and use it instead. + */ + host_ifconf = malloc(outbufsz); + if (!host_ifconf) { + return -TARGET_ENOMEM; + } + memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf)); + free_buf = 1; } - memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf)); - free_buf = 1; - } - host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf); + host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf); - host_ifconf->ifc_len = host_ifc_len; + host_ifconf->ifc_len = host_ifc_len; + } else { + host_ifc_buf = NULL; + } host_ifconf->ifc_buf = host_ifc_buf; ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf)); @@ -4178,15 +4183,16 @@ static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET); unlock_user(argptr, arg, target_size); - /* copy ifreq[] to target user */ - - argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0); - for (i = 0; i < nb_ifreq ; i++) { - thunk_convert(argptr + i * target_ifreq_size, - host_ifc_buf + i * sizeof(struct ifreq), - ifreq_arg_type, THUNK_TARGET); + if (target_ifc_buf != 0) { + /* copy ifreq[] to target user */ + argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0); + for (i = 0; i < nb_ifreq ; i++) { + thunk_convert(argptr + i * target_ifreq_size, + host_ifc_buf + i * sizeof(struct ifreq), + ifreq_arg_type, THUNK_TARGET); + } + unlock_user(argptr, target_ifc_buf, target_ifc_len); } - unlock_user(argptr, target_ifc_buf, target_ifc_len); } if (free_buf) {