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: 9379279 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 76E94607D4 for ; Mon, 17 Oct 2016 13:29:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A03629367 for ; Mon, 17 Oct 2016 13:29:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5E47229373; Mon, 17 Oct 2016 13:29:45 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 2A9E72936F for ; Mon, 17 Oct 2016 13:29:44 +0000 (UTC) Received: from localhost ([::1]:33071 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bw7z5-0006y1-Ap for patchwork-qemu-devel@patchwork.kernel.org; Mon, 17 Oct 2016 09:29:43 -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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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(-) 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