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: 78049 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp890052qge; Tue, 18 Oct 2016 06:38:25 -0700 (PDT) X-Received: by 10.55.185.69 with SMTP id j66mr533945qkf.264.1476797905969; Tue, 18 Oct 2016 06:38:25 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id j125si8797707qkc.327.2016.10.18.06.38.25 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 18 Oct 2016 06:38:25 -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]:41576 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwUb3-0003IK-Cz for patch@linaro.org; Tue, 18 Oct 2016 09:38:25 -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+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 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