From patchwork Fri Aug 16 04:55:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 267542 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0B30D2C02CA for ; Fri, 16 Aug 2013 14:56:13 +1000 (EST) Received: from localhost ([::1]:56584 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VAC53-0004D8-Jy for incoming@patchwork.ozlabs.org; Fri, 16 Aug 2013 00:56:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VAC4g-0004AZ-Ln for qemu-devel@nongnu.org; Fri, 16 Aug 2013 00:55:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VAC4Z-0003Eb-8h for qemu-devel@nongnu.org; Fri, 16 Aug 2013 00:55:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58302) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VAC4Y-0003DD-O0; Fri, 16 Aug 2013 00:55:38 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r7G4taOR016296 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 16 Aug 2013 00:55:36 -0400 Received: from bling.home (ovpn-113-74.phx2.redhat.com [10.3.113.74]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r7G4tZ93013267; Fri, 16 Aug 2013 00:55:35 -0400 To: qemu-devel@nongnu.org From: Alex Williamson Date: Thu, 15 Aug 2013 22:55:35 -0600 Message-ID: <20130816044811.3049.77085.stgit@bling.home> User-Agent: StGit/0.16 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: qemu-stable@nongnu.org, rth@twiddle.net Subject: [Qemu-devel] [PATCH] exec: Fix non-power-of-2 sized accesses 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 Since commit 23326164 we align access sizes to match the alignment of the address, but we don't align the access size itself. This means we let illegal access sizes (ex. 3) slip through if the address is sufficiently aligned (ex. 4). This results in an abort which would be easy for a guest to trigger. Account for aligning the access size. Signed-off-by: Alex Williamson Cc: qemu-stable@nongnu.org Reviewed-by: Laszlo Ersek --- In the example I saw the guest was doing a 4-byte read at I/O port 0xcd7. We satisfy the first byte with a 1-byte read leaving 3 bytes remaining at an 8-byte aligned address... boom. ffs() caused weird stack smashing errors here, so I just did a loop since it can only run for a few iterations max. exec.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/exec.c b/exec.c index 3ca9381..652fc3a 100644 --- a/exec.c +++ b/exec.c @@ -1924,6 +1924,13 @@ static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr) } } + /* Size must be a power of 2 */ + if (l & (l - 1)) { + while (l & (access_size_max - 1) && access_size_max > 1) { + access_size_max >>= 1; + } + } + /* Don't attempt accesses larger than the maximum. */ if (l > access_size_max) { l = access_size_max;