From patchwork Mon Oct 17 13:24:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Riku Voipio X-Patchwork-Id: 77748 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp396916qge; Mon, 17 Oct 2016 06:31:47 -0700 (PDT) X-Received: by 10.55.17.74 with SMTP id b71mr29269188qkh.93.1476711107700; Mon, 17 Oct 2016 06:31:47 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id s59si17946123qtd.74.2016.10.17.06.31.47 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 17 Oct 2016 06:31:47 -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]:33080 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bw815-000881-5T for patch@linaro.org; Mon, 17 Oct 2016 09:31:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38397) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bw7uV-0003B7-3v for qemu-devel@nongnu.org; Mon, 17 Oct 2016 09:25:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bw7uU-0003Dz-5E for qemu-devel@nongnu.org; Mon, 17 Oct 2016 09:24:59 -0400 Received: from mail-lf0-x22a.google.com ([2a00:1450:4010:c07::22a]:36012) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bw7uT-0003Dp-TD for qemu-devel@nongnu.org; Mon, 17 Oct 2016 09:24:58 -0400 Received: by mail-lf0-x22a.google.com with SMTP id b75so288595535lfg.3 for ; Mon, 17 Oct 2016 06:24:57 -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=HSFIXPZIeTMvxJcq9tOTaD5DZBpyBcDqi0TL780xUPE=; b=DJOjGP/C60Gu9MeqOfMm2jAxlhhcK7Dn93Xvt8DtFTpbewHLQ306pHE32WuDCJtUkF MSvu7Qt8BHKOpbc3iuU3cUYuEBOuSeYx9/pzfawbyhnBonjW18gkifc9TbTeP4UrFSdG YDTNehCg2spp5mTT5C7yc6W+InWRUXdK7fp1E= 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=HSFIXPZIeTMvxJcq9tOTaD5DZBpyBcDqi0TL780xUPE=; b=NuU3g1zKfHlaUQk4JWi0/P29mE77FlUBvbfcE2DfOXiOhiCa1IeNRFqwuVFQlNOI3k OelytUssOAG/iH6YvicwYXJAePKvRw3dDf8x5Nh1huMw9wvgPedmsuFl/w4ZF3Z78NtE Hy2XxFTuvHksOAhz1S28c9sjc4+Yi16VRspreLDhLGp0QraljAby0DokxpRbV5+M/3V5 WvvHs2Ojl2oj2yJttztUN7ScMZWY+GMJ+lxKbmZ1hixLxF8dB+mcfpTXT8x6ZwCdm5o1 s6CGvpv/Kz+WSylV1pajOTbxkPPyvfIQVeqwiSVt5H2E5kQ4zvd4/e9xpFMhVdtaKkDN iOxQ== X-Gm-Message-State: AA6/9RlFQrfOyJ3uC3pJS8+DnKKmtJNSU5wvhMh9PfydG0lWqzqWecSPDL4jPwkMcel8GzMO X-Received: by 10.25.18.211 with SMTP id 80mr15014550lfs.89.1476710696462; Mon, 17 Oct 2016 06:24:56 -0700 (PDT) Received: from beaming.home (91-157-170-157.elisa-laajakaista.fi. [91.157.170.157]) by smtp.gmail.com with ESMTPSA id y81sm7782857lff.29.2016.10.17.06.24.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Oct 2016 06:24:55 -0700 (PDT) From: riku.voipio@linaro.org To: qemu-devel@nongnu.org Date: Mon, 17 Oct 2016 16:24:29 +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::22a Subject: [Qemu-devel] [PULL 11/22] linux-user: Don't use alloca() for epoll_wait's epoll event array 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 The epoll event array which epoll_wait() allocates has a size determined by the guest which could potentially be quite large. Use g_try_new() rather than alloca() so that we can fail more cleanly if the guest hands us an oversize value. (ENOMEM is not a documented return value for epoll_wait() but in practice some kernel configurations can return it -- see for instance sys_oabi_epoll_wait() on ARM.) This rearrangement includes fixing a bug where we were incorrectly passing a negative length to unlock_user() in the error-exit codepath. Signed-off-by: Peter Maydell Signed-off-by: Riku Voipio --- linux-user/syscall.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) -- 2.1.4 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index d66efa1..4e557a6 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -11795,7 +11795,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, goto efault; } - ep = alloca(maxevents * sizeof(struct epoll_event)); + ep = g_try_new(struct epoll_event, maxevents); + if (!ep) { + unlock_user(target_ep, arg2, 0); + ret = -TARGET_ENOMEM; + break; + } switch (num) { #if defined(TARGET_NR_epoll_pwait) @@ -11813,8 +11818,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, target_set = lock_user(VERIFY_READ, arg5, sizeof(target_sigset_t), 1); if (!target_set) { - unlock_user(target_ep, arg2, 0); - goto efault; + ret = -TARGET_EFAULT; + break; } target_to_host_sigset(set, target_set); unlock_user(target_set, arg5, 0); @@ -11842,8 +11847,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, target_ep[i].events = tswap32(ep[i].events); target_ep[i].data.u64 = tswap64(ep[i].data.u64); } + unlock_user(target_ep, arg2, + ret * sizeof(struct target_epoll_event)); + } else { + unlock_user(target_ep, arg2, 0); } - unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event)); + g_free(ep); break; } #endif