From patchwork Tue Jul 9 07:50:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 168728 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp8211377ilk; Tue, 9 Jul 2019 00:53:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqxyU0T/LA99zA+HXTr++CE9x1QVsdH/H68umUeD7IyC2uKmSUzxOftYQ+opFw//9ST1zkxC X-Received: by 2002:a17:906:3c1:: with SMTP id c1mr20231549eja.221.1562658811366; Tue, 09 Jul 2019 00:53:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562658811; cv=none; d=google.com; s=arc-20160816; b=XufgtxAoTFabX5HrF8lDF2ExqzK0ukc3tzxQQaXXP/2+VSTx92dblpzBi4f+eESVs+ C3HlqWQvJGzskEnep/Ie8PBGnqBPFaa01cYZf2ABGTaSGO0Fks25CLZ48TO5KXFS2KLw ZcuyJrHEwjmfCntRjt5cxMwh+8qbab3rgE9vFiId70UwTWDwTTH4cIXYWpZhQB9aRTAB r1NbPz/JpI150gPvn3qFKNUhiSAl4UcEqoZqvehIDgYy9jHDlSghLssFrkL/LSl6R0Ug G7zpmBTjofBy8TWOAV9CMeIrdoaUN4NEXPsvnrRGzzZAZxReSPxRXdbrsIlcu0QvPWra +XAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=PqJ1ISuGV2u+IdiY8ngyBhS17daHeZo+xRM6Jaz465M=; b=p9e1j9HwTtY2Hrvf43jkEVYPxMLy8GEWHT7ZOb31F7qctJ9sPOv1zzlecgmcQTl4W6 G1SDJTR7QAsPbAKYCEs+9+C+5YO0pZTcqYvsJqyz6avgNh84zR/AF78ikTEq4ITq+vDY vreY7nma/UQoVsWxJ8B+BI7LAKIaBtpjhKW6LduysLZXmqSuQyKALnGupbLWW6ED5ZqI ijGyF4goXaO8VSWevGr/9opf2SggZXaHa9RVuojabHoH/b1BptkPrtK8+zKxxvB5tKcV CgCbRn7zow83ahuR2pLrCd2fq49ZRUl36aQPu32dW1kfAICVxNrCQ+NSdyXEmHIp13Vm 4n7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=R2Xf7bB9; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id fx1si11600365ejb.226.2019.07.09.00.53.31 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Jul 2019 00:53:31 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=R2Xf7bB9; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:47592 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkkwM-0008RW-CJ for patch@linaro.org; Tue, 09 Jul 2019 03:53:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52136) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkktx-0005jg-PK for qemu-devel@nongnu.org; Tue, 09 Jul 2019 03:51:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkktw-0000ze-5H for qemu-devel@nongnu.org; Tue, 09 Jul 2019 03:51:01 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]:34030) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hkktv-0000wr-Ue for qemu-devel@nongnu.org; Tue, 09 Jul 2019 03:51:00 -0400 Received: by mail-pf1-x42d.google.com with SMTP id b13so4279538pfo.1 for ; Tue, 09 Jul 2019 00:50:58 -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=PqJ1ISuGV2u+IdiY8ngyBhS17daHeZo+xRM6Jaz465M=; b=R2Xf7bB9WfosM0jErAXQugyS5r4ryza934b8npGVReYjwEUT7ULGB6tOBeETgl17dR HwxmfSHSawfR3T9RuvpwOeR4VTTgj3ulT7Opoq0/TRAgPapKRbaneT5N0Xfpv5JVmrJi MxwFjP1lWoL2B54rlwWIkTLSYfQj9ReJdFq4ls7OnIB8CoVdB3yWYkTJSDreyazQKl6v Adn7x93E9BWhjgc1cx4cEVV2uZtG4jPABkyOqw2wPuRy0lccD6Vdhjx765tpBWAtJ+Dv LckpdR4f9BjJFwe2XYn1d9GKAbFH66tKtChV1PpgAj50WSFplqKKG53hSaL2RXIwkb+n OJcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PqJ1ISuGV2u+IdiY8ngyBhS17daHeZo+xRM6Jaz465M=; b=rez0O7vgMIq/FuyFXbegIZbrkBGW2QtRHFZKmabDgf3dTZMAqTps1zSp0hcFGk0Xlb Mbo+S+R3uQ0BWdWBVqtfgBMmXgO4wkNeikHCuEYFgpcOuJlwgoOWjtoIr1zBhK78PWyo ggyV2L9beuawEHIXvZokW8aBiQH9j5drhuopACah14FnwLAfWlJgBSf730GykNfOwFTW /9cCOijXhi6ZpW5eIkbxJ87OyMzl7ua2I35fADdmSMvSOnAcAjHgZRxYr5K2XyszWSGQ k6Sz8mIAB3LIVRthpRhQ5UCRryTjEDl7vzfgBcrwU4JTHNNYw45RJF7Kl0SQWQC+6Emv hvSw== X-Gm-Message-State: APjAAAXzqKAhXYz4n5N+8/YSGIDgRyRzOHg8qVTN9Y2sW99mPageikWe AdXhdq+QSJlwyDosFr0wT8C8aOFGiUJuCQ== X-Received: by 2002:a63:6eca:: with SMTP id j193mr28399881pgc.74.1562658656595; Tue, 09 Jul 2019 00:50:56 -0700 (PDT) Received: from localhost.localdomain ([172.56.12.212]) by smtp.gmail.com with ESMTPSA id y22sm28237375pfo.39.2019.07.09.00.50.53 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 09 Jul 2019 00:50:56 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 9 Jul 2019 09:50:38 +0200 Message-Id: <20190709075042.13941-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190709075042.13941-1-richard.henderson@linaro.org> References: <20190709075042.13941-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::42d Subject: [Qemu-devel] [PULL 1/3] tcg: Fix mmap lock assert on translation failure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Check page flags before letting an invalid pc cause a SIGSEGV. Prepare for eventially validating PROT_EXEC. The current wrinkle being that we have a problem with our implementation of signals. We should be using a vdso like the kernel, but we instead put the trampoline on the stack. In the meantime, let PROT_READ match PROT_EXEC. Fixes: https://bugs.launchpad.net/qemu/+bug/1832353 Signed-off-by: Richard Henderson --- include/exec/cpu-all.h | 1 + include/exec/cpu_ldst_useronly_template.h | 8 +++++-- accel/tcg/translate-all.c | 29 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) -- 2.17.1 diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index 536ea58f81..58b8915617 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -259,6 +259,7 @@ int walk_memory_regions(void *, walk_memory_regions_fn); int page_get_flags(target_ulong address); void page_set_flags(target_ulong start, target_ulong end, int flags); int page_check_range(target_ulong start, target_ulong len, int flags); +void validate_exec_access(CPUArchState *env, target_ulong s, target_ulong l); #endif CPUArchState *cpu_copy(CPUArchState *env); diff --git a/include/exec/cpu_ldst_useronly_template.h b/include/exec/cpu_ldst_useronly_template.h index bc45e2b8d4..f095415149 100644 --- a/include/exec/cpu_ldst_useronly_template.h +++ b/include/exec/cpu_ldst_useronly_template.h @@ -64,7 +64,9 @@ static inline RES_TYPE glue(glue(cpu_ld, USUFFIX), MEMSUFFIX)(CPUArchState *env, abi_ptr ptr) { -#if !defined(CODE_ACCESS) +#ifdef CODE_ACCESS + validate_exec_access(env, ptr, DATA_SIZE); +#else trace_guest_mem_before_exec( env_cpu(env), ptr, trace_mem_build_info(SHIFT, false, MO_TE, false)); @@ -88,7 +90,9 @@ glue(glue(glue(cpu_ld, USUFFIX), MEMSUFFIX), _ra)(CPUArchState *env, static inline int glue(glue(cpu_lds, SUFFIX), MEMSUFFIX)(CPUArchState *env, abi_ptr ptr) { -#if !defined(CODE_ACCESS) +#ifdef CODE_ACCESS + validate_exec_access(env, ptr, DATA_SIZE); +#else trace_guest_mem_before_exec( env_cpu(env), ptr, trace_mem_build_info(SHIFT, true, MO_TE, false)); diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 5d1e08b169..1d4a8a260f 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -2600,10 +2600,39 @@ int page_check_range(target_ulong start, target_ulong len, int flags) } } } + /* + * FIXME: We place the signal trampoline on the stack, + * even when the guest expects that to be in the vdso. + * Until we fix that, allow execute on any readable page. + */ + if ((flags & PAGE_EXEC) && !(p->flags & (PAGE_EXEC | PAGE_READ))) { + return -1; + } } return 0; } +/* + * Called for each code read, longjmp out to issue SIGSEGV if the page(s) + * do not have execute access. + */ +void validate_exec_access(CPUArchState *env, + target_ulong ptr, target_ulong len) +{ + if (page_check_range(ptr, len, PAGE_EXEC) < 0) { + CPUState *cs = env_cpu(env); + CPUClass *cc = CPU_GET_CLASS(cs); + + /* Like tb_gen_code, release the memory lock before cpu_loop_exit. */ + assert_memory_lock(); + mmap_unlock(); + + /* This is user-only. The target must raise an exception. */ + cc->tlb_fill(cs, ptr, 0, MMU_INST_FETCH, MMU_USER_IDX, false, 0); + g_assert_not_reached(); + } +} + /* called from signal handler: invalidate the code and unprotect the * page. Return 0 if the fault was not handled, 1 if it was handled, * and 2 if it was handled but the caller must cause the TB to be