From patchwork Tue Oct 18 13:21:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Riku Voipio X-Patchwork-Id: 9382147 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 A1E05600CA for ; Tue, 18 Oct 2016 13:36:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90EF8295AD for ; Tue, 18 Oct 2016 13:36:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 83DD7295DE; Tue, 18 Oct 2016 13:36:34 +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 1D0EA295AD for ; Tue, 18 Oct 2016 13:36:33 +0000 (UTC) Received: from localhost ([::1]:41569 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwUZE-0001xV-EM for patchwork-qemu-devel@patchwork.kernel.org; Tue, 18 Oct 2016 09:36:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53845) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwULK-0006Jw-P2 for qemu-devel@nongnu.org; Tue, 18 Oct 2016 09:22:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bwULJ-0000hL-VF for qemu-devel@nongnu.org; Tue, 18 Oct 2016 09:22:10 -0400 Received: from mail-lf0-x229.google.com ([2a00:1450:4010:c07::229]:34830) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bwULJ-0000gP-NR for qemu-devel@nongnu.org; Tue, 18 Oct 2016 09:22:09 -0400 Received: by mail-lf0-x229.google.com with SMTP id l131so25238728lfl.2 for ; Tue, 18 Oct 2016 06:22:09 -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=Pc+LO26ptlDCVztJK6ctFa2Ciu80QAyhFeoVTe97+CI=; b=Andyo5QPEWQ9RLpNYIFp9Ao0TqQ2bi4C8fN539sinkYbTAuNjnySEHa/LQaY7TUoxU orFvBbjys2rFPwcsjJLwrjPefMmxV8BXfCzJ5ZmKl1xJfjgfzep6gVrE5/NSK9zkpScW X6gYBbvv9ZD4WhYJXorsXFxf+NL0pmc7gGrgM= 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=Pc+LO26ptlDCVztJK6ctFa2Ciu80QAyhFeoVTe97+CI=; b=GrPwLyPLQy5OpotnQYj/Lyn7P5fRh9LBPQc3QKHj/csJCRql7cCbm6lLP9tv4WoQmp N/uuu+RPLQIXv2Swati07VYmvj9ZE/OGxE/FIEMcpuN0/gRWiKWzf9eAiidLV5TfnSal CK5CM/xZzCsUrP3H2WCV9zDC5OK5w4ORb95Xibbx1aHRJFmTVAeCDHihquR0GEvb1a8o T1CnPUJJ0bKfzieVrwHmONABSo7e7NZNX0kTFAS/6y5AmdHkxiSLXPzy+Q8kRIg6fpMN vxnMd+VxxczD6Ze6guXR83rqKsdfhWCN72CoBAAVjQqRZyvSz62J6Kv1zo2tD7VB834f diHA== X-Gm-Message-State: AA6/9RnLbIjatwyanrAVMyeCekNho+1n8aZlxDR3flrCZ5mANx4NstOlKAF2Lk8I4aQR3xT3 X-Received: by 10.25.210.5 with SMTP id j5mr583208lfg.14.1476796925314; Tue, 18 Oct 2016 06:22:05 -0700 (PDT) Received: from beaming.home (91-157-170-157.elisa-laajakaista.fi. [91.157.170.157]) by smtp.gmail.com with ESMTPSA id 201sm9535359ljf.48.2016.10.18.06.22.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Oct 2016 06:22:04 -0700 (PDT) From: riku.voipio@linaro.org To: qemu-devel@nongnu.org Date: Tue, 18 Oct 2016 16:21:39 +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::229 Subject: [Qemu-devel] [PULL v2 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 bbdf316..fb56fed 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -11796,7 +11796,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) @@ -11814,8 +11819,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); @@ -11843,8 +11848,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