From patchwork Fri Aug 17 09:14:26 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Zuepke X-Patchwork-Id: 178396 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C4F192C008D for ; Sat, 18 Aug 2012 07:13:52 +1000 (EST) Received: from localhost ([::1]:49676 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T2Tra-0005MS-0H for incoming@patchwork.ozlabs.org; Fri, 17 Aug 2012 17:13:50 -0400 Received: from eggs.gnu.org ([208.118.235.92]:37296) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T2IdP-0006wg-SR for qemu-devel@nongnu.org; Fri, 17 Aug 2012 05:14:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T2IdO-0003pL-Qm for qemu-devel@nongnu.org; Fri, 17 Aug 2012 05:14:27 -0400 Received: from mail1.sysgo.com ([176.9.26.183]:54807) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T2IdO-0003oo-Kc for qemu-devel@nongnu.org; Fri, 17 Aug 2012 05:14:26 -0400 Received: from lantia.sysgo.com (unknown [172.22.2.7]) by mail1.sysgo.com (Postfix) with ESMTP id 580D2462DD for ; Fri, 17 Aug 2012 11:14:24 +0200 (CEST) Received: by lantia.sysgo.com (Postfix, from userid 113) id 16C732660C0; Fri, 17 Aug 2012 11:14:23 +0200 (CEST) Received: from [172.22.40.10] (azu.sysgo.com [172.22.40.10]) by lantia.sysgo.com (Postfix) with ESMTPS id 38F632660C3 for ; Fri, 17 Aug 2012 11:14:23 +0200 (CEST) Message-ID: <502E0B72.3030701@sysgo.com> Date: Fri, 17 Aug 2012 11:14:26 +0200 From: Alex ZUEPKE User-Agent: Thunderbird 2.0.0.24 (X11/20101027) MIME-Version: 1.0 To: qemu-devel@nongnu.org X-Enigmail-Version: 0.95.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 176.9.26.183 X-Mailman-Approved-At: Fri, 17 Aug 2012 17:13:44 -0400 Subject: [Qemu-devel] [PATCH] x86: enforce DPL checking on task gate switches invoked through IDT X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Hi, x86 software emulation (non-KVM mode) does not check privilege levels on task gate switches ... so one can invoke a kernel's double fault handler from user space -- very bad. Expected behaviour (testcase works with any linux distribution + gcc): $ cat test.c int main(void) { __asm__ volatile ("int $8"); } $ gcc test.c $ ./a.out Segmentation fault $ ... and not a kernel panic (double fault) Forgive me for sending this patch as attachment, I'm not used to git. Best Regards, Alex x86 software emulation (non-KVM mode) does not check privilege levels on task gate switches ... so one can invoke a kernel's double fault handler from user space. Signed-off-by: Alex Zuepke diff --git a/target-i386/seg_helper.c b/target-i386/seg_helper.c index 5fff8d5..23c5542 100644 --- a/target-i386/seg_helper.c +++ b/target-i386/seg_helper.c @@ -578,12 +578,17 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, e2 = cpu_ldl_kernel(env, ptr + 4); /* check gate type */ type = (e2 >> DESC_TYPE_SHIFT) & 0x1f; + dpl = (e2 >> DESC_DPL_SHIFT) & 3; + cpl = env->hflags & HF_CPL_MASK; switch (type) { case 5: /* task gate */ /* must do that check here to return the correct error code */ if (!(e2 & DESC_P_MASK)) { raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2); } + /* check privilege if software int */ + if (is_int && dpl < cpl) + raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); switch_tss(env, intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip); if (has_error_code) { int type; @@ -616,8 +621,6 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); break; } - dpl = (e2 >> DESC_DPL_SHIFT) & 3; - cpl = env->hflags & HF_CPL_MASK; /* check privilege if software int */ if (is_int && dpl < cpl) { raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);