From patchwork Thu Jun 16 19:01:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 9181501 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3263860573 for ; Thu, 16 Jun 2016 19:02:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 018642012F for ; Thu, 16 Jun 2016 19:02:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA71528382; Thu, 16 Jun 2016 19:02:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3F2EC2012F for ; Thu, 16 Jun 2016 19:02:32 +0000 (UTC) Received: from localhost ([::1]:51674 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDcYg-0002hr-0c for patchwork-qemu-devel@patchwork.kernel.org; Thu, 16 Jun 2016 15:02:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37240) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDcYD-0002cv-O3 for qemu-devel@nongnu.org; Thu, 16 Jun 2016 15:02:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDcY9-0006ia-Jj for qemu-devel@nongnu.org; Thu, 16 Jun 2016 15:02:00 -0400 Received: from mout.kundenserver.de ([212.227.126.130]:65119) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDcY9-0006iV-9U for qemu-devel@nongnu.org; Thu, 16 Jun 2016 15:01:57 -0400 Received: from Quad.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue001) with ESMTPSA (Nemesis) id 0LrEdQ-1bPU2R2g9c-0139Cq; Thu, 16 Jun 2016 21:01:47 +0200 From: Laurent Vivier To: Riku Voipio Date: Thu, 16 Jun 2016 21:01:37 +0200 Message-Id: <1466103697-27279-3-git-send-email-laurent@vivier.eu> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1466103697-27279-1-git-send-email-laurent@vivier.eu> References: <1466103697-27279-1-git-send-email-laurent@vivier.eu> X-Provags-ID: V03:K0:0Hr0NW7kOVyo7gfmK2QQm/O1aQrhbIoIzzCZtjFqJcdSNhZsbGY JuqKUfpanaeQC60s10khMo1d/xStQgnoOBfQwDcd57rk33X/UGnL95a88JbgpN4wj2/zU00 efQe5+L7SEhwQcB0T0JP4uItVqOd2egw5ap08z/3ujZIeylUMhDaFhef4smL7wKqYYcqZHs T4MBGobCJt/NvwCAXrJlQ== X-UI-Out-Filterresults: notjunk:1; V01:K0:0k4enZbG6es=:y0hrGnwMu7jQYjZLpLS4Ua IJw/DOlQlwMT1RWfoY5vggZQAiFAe47csIpqvVTT8u2KpWC6T86z2AcsrpV/rNxaFGM63X1mc p/KoEkgCC7ZVuQeQ3Pfz/xuKYF6eLF34Ibv70Vvv6ltrvVP/x/IdGzhjxpcD3QvAGyH3X8IeJ TvmN2W8dWuY6jeZ2IMcxtt29tw8jKU4syAZRTZelvw5jRxfFJlfxqJ8QapXdxLCgYCGfd6NV7 PMA6vALhyHDzeEo8VTF8k54tIPOq2UAczrYg9uSMbobdSLEqyd1Rsj6Vi2FE9aAcSUAfWVdyb ZDubJJMKlhPsOrANGZhxorcvWmSogJ/MWA5OV7K+SkBc1uI9jO0EAzNh34t7a/836ChuGnvZg O2n1ou4a5kgsmlJWnvqoxZqMT29RJC3Js/WC6YIU2pWfLL+1vDqrparCC86gku5ZQ3AAp4DKB QkUqcSeoLEArinFkDIPMx8TdA+ja22UoDrQbG/FkvsxM7WAZ7wuAq9c8lL8Q0TjCcQDZAd+rb cllLqiVMArU07byqzkO9YW5MLUb5LnhfHfi+x6k188IsltWkDIt0Gtil+dSzdm9ONWwB9LJLm Oc+S0/HwDbcfxb5HQF3Ba9uW34QV8XuiAPgmjVrfswRbDc56/8wfJzp2vLh+M5KB1taVlQppm juq+P84nqEsyiUNcObXlXz3RvpCMrIDaTQt1LrlSp/4BMUy9PbOxjPJUcdzbHLXJlJgY= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.126.130 Subject: [Qemu-devel] [PATCH 2/2] linux-user: don't swap NLMSG_DATA() fields 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: , Cc: qemu-devel@nongnu.org, Laurent Vivier Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP If the structure pointed by NLMSG_DATA() is bigger than the size of NLMSG_DATA(), don't swap its fields to avoid memory corruption. Signed-off-by: Laurent Vivier Reviewed-by: Peter Maydell --- linux-user/syscall.c | 72 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 3c30437..a0a3e65 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -1944,29 +1944,35 @@ static abi_long host_to_target_data_route(struct nlmsghdr *nlh) case RTM_NEWLINK: case RTM_DELLINK: case RTM_GETLINK: - ifi = NLMSG_DATA(nlh); - ifi->ifi_type = tswap16(ifi->ifi_type); - ifi->ifi_index = tswap32(ifi->ifi_index); - ifi->ifi_flags = tswap32(ifi->ifi_flags); - ifi->ifi_change = tswap32(ifi->ifi_change); - host_to_target_link_rtattr(IFLA_RTA(ifi), - nlmsg_len - NLMSG_LENGTH(sizeof(*ifi))); + if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) { + ifi = NLMSG_DATA(nlh); + ifi->ifi_type = tswap16(ifi->ifi_type); + ifi->ifi_index = tswap32(ifi->ifi_index); + ifi->ifi_flags = tswap32(ifi->ifi_flags); + ifi->ifi_change = tswap32(ifi->ifi_change); + host_to_target_link_rtattr(IFLA_RTA(ifi), + nlmsg_len - NLMSG_LENGTH(sizeof(*ifi))); + } break; case RTM_NEWADDR: case RTM_DELADDR: case RTM_GETADDR: - ifa = NLMSG_DATA(nlh); - ifa->ifa_index = tswap32(ifa->ifa_index); - host_to_target_addr_rtattr(IFA_RTA(ifa), - nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); + if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) { + ifa = NLMSG_DATA(nlh); + ifa->ifa_index = tswap32(ifa->ifa_index); + host_to_target_addr_rtattr(IFA_RTA(ifa), + nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); + } break; case RTM_NEWROUTE: case RTM_DELROUTE: case RTM_GETROUTE: - rtm = NLMSG_DATA(nlh); - rtm->rtm_flags = tswap32(rtm->rtm_flags); - host_to_target_route_rtattr(RTM_RTA(rtm), - nlmsg_len - NLMSG_LENGTH(sizeof(*rtm))); + if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) { + rtm = NLMSG_DATA(nlh); + rtm->rtm_flags = tswap32(rtm->rtm_flags); + host_to_target_route_rtattr(RTM_RTA(rtm), + nlmsg_len - NLMSG_LENGTH(sizeof(*rtm))); + } break; default: return -TARGET_EINVAL; @@ -2082,30 +2088,36 @@ static abi_long target_to_host_data_route(struct nlmsghdr *nlh) break; case RTM_NEWLINK: case RTM_DELLINK: - ifi = NLMSG_DATA(nlh); - ifi->ifi_type = tswap16(ifi->ifi_type); - ifi->ifi_index = tswap32(ifi->ifi_index); - ifi->ifi_flags = tswap32(ifi->ifi_flags); - ifi->ifi_change = tswap32(ifi->ifi_change); - target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len - - NLMSG_LENGTH(sizeof(*ifi))); + if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) { + ifi = NLMSG_DATA(nlh); + ifi->ifi_type = tswap16(ifi->ifi_type); + ifi->ifi_index = tswap32(ifi->ifi_index); + ifi->ifi_flags = tswap32(ifi->ifi_flags); + ifi->ifi_change = tswap32(ifi->ifi_change); + target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len - + NLMSG_LENGTH(sizeof(*ifi))); + } break; case RTM_GETADDR: case RTM_NEWADDR: case RTM_DELADDR: - ifa = NLMSG_DATA(nlh); - ifa->ifa_index = tswap32(ifa->ifa_index); - target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len - - NLMSG_LENGTH(sizeof(*ifa))); + if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) { + ifa = NLMSG_DATA(nlh); + ifa->ifa_index = tswap32(ifa->ifa_index); + target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len - + NLMSG_LENGTH(sizeof(*ifa))); + } break; case RTM_GETROUTE: break; case RTM_NEWROUTE: case RTM_DELROUTE: - rtm = NLMSG_DATA(nlh); - rtm->rtm_flags = tswap32(rtm->rtm_flags); - target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len - - NLMSG_LENGTH(sizeof(*rtm))); + if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) { + rtm = NLMSG_DATA(nlh); + rtm->rtm_flags = tswap32(rtm->rtm_flags); + target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len - + NLMSG_LENGTH(sizeof(*rtm))); + } break; default: return -TARGET_EOPNOTSUPP;