From patchwork Tue Jul 19 12:54:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Riku Voipio X-Patchwork-Id: 72309 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp68959qga; Tue, 19 Jul 2016 06:05:50 -0700 (PDT) X-Received: by 10.31.137.8 with SMTP id l8mr14314347vkd.97.1468933550893; Tue, 19 Jul 2016 06:05:50 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id h4si13382786qtc.54.2016.07.19.06.05.50 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 19 Jul 2016 06:05:50 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:55730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPUic-0001br-AI for patch@linaro.org; Tue, 19 Jul 2016 09:05:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42894) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPUXh-0007ZW-U5 for qemu-devel@nongnu.org; Tue, 19 Jul 2016 08:54:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bPUXf-0003OY-LH for qemu-devel@nongnu.org; Tue, 19 Jul 2016 08:54:33 -0400 Received: from mail-lf0-x235.google.com ([2a00:1450:4010:c07::235]:34456) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPUXf-0003OP-Dr for qemu-devel@nongnu.org; Tue, 19 Jul 2016 08:54:31 -0400 Received: by mail-lf0-x235.google.com with SMTP id l69so13413303lfg.1 for ; Tue, 19 Jul 2016 05:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hs40IW2YADE7m69Kc+ZeuOFEudO/N2AsXC21RL39JcI=; b=AtVQOqpF4kjwnSMDyd0XEyiLgyTkXY2RNllMCQeVgsW+KfyMK93VS2EH6jaJugTym5 UXdstYh0qZKtFwPJNEVx0eRqdQJDcV6AM4V1HyOGp4sZNRqpYgwxnKGUEcmtedeIBIMH mb9dRfxXtKxYiNSHSepRGCA60TvmcBDmMcRAA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hs40IW2YADE7m69Kc+ZeuOFEudO/N2AsXC21RL39JcI=; b=hjn+1LQBslKql1XzTiyviEXduUxQn+kzEEdNxzx5Hm7tyvp/I5CAqrCwvuPymv1zcd yN4XzexHSR9kAOQFAMCWTfJsKy9BAgCJ3Vl8e2cEM1sSs3F7ku+OqtDht0pd8ulOPJbR YjjMwB+iujptdYq9SJ+uZ0ZlZ4lUVhWxY7BC6meNExpOmGEPvYsDgl24JQKSt4njIXUc qESMtxYUKkYAztZgdN0kttpg4OrMENnJempmYf5k0azJhwtbwcBsVZZUWKulXbxoRn7V 9msSfjSLQ53dgoiyw8u93eLz0JpDfjALWSWg8f5JqC7zeVZqV7bBEOi9lsJ/HhJcTv5s uy3w== X-Gm-Message-State: ALyK8tISud2V2cjVVaFKN6wnHcYVyhZ73DUEiiv1fQBPAs5gyxU9uSYWbyCga559DfWt/VaD X-Received: by 10.25.16.212 with SMTP id 81mr16390541lfq.174.1468932870279; Tue, 19 Jul 2016 05:54:30 -0700 (PDT) Received: from beaming.home (91-157-170-157.elisa-laajakaista.fi. [91.157.170.157]) by smtp.gmail.com with ESMTPSA id g3sm2248898lfe.14.2016.07.19.05.54.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 19 Jul 2016 05:54:29 -0700 (PDT) From: riku.voipio@linaro.org To: qemu-devel@nongnu.org Date: Tue, 19 Jul 2016 15:54:09 +0300 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::235 Subject: [Qemu-devel] [PULL 11/16] linux-user: Handle short lengths in host_to_target_sockaddr() 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: Peter Maydell Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell If userspace specifies a short buffer for a target sockaddr, the kernel will only copy in as much as it has space for (or none at all if the length is zero) -- see the kernel move_addr_to_user() function. Mimic this in QEMU's host_to_target_sockaddr() routine. In particular, this fixes a segfault running the LTP recvfrom01 test, where the guest makes a recvfrom() call with a bad buffer pointer and other parameters which cause the kernel to set the addrlen to zero; because we did not skip the attempt to swap the sa_family field we segfaulted on the bad address. Signed-off-by: Peter Maydell Signed-off-by: Riku Voipio --- linux-user/syscall.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) -- 2.1.4 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 0a99af8..ca6a2b4 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -1376,12 +1376,19 @@ static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, { struct target_sockaddr *target_saddr; + if (len == 0) { + return 0; + } + target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0); if (!target_saddr) return -TARGET_EFAULT; memcpy(target_saddr, addr, len); - target_saddr->sa_family = tswap16(addr->sa_family); - if (addr->sa_family == AF_NETLINK) { + if (len >= offsetof(struct target_sockaddr, sa_family) + + sizeof(target_saddr->sa_family)) { + target_saddr->sa_family = tswap16(addr->sa_family); + } + if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) { struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr; target_nl->nl_pid = tswap32(target_nl->nl_pid); target_nl->nl_groups = tswap32(target_nl->nl_groups);