Received: from eggs.gnu.org ([2001:4830:134:3::10]:55095) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xnt0z-0005e3-F5 for qemu-devel@nongnu.org; Mon, 10 Nov 2014 12:44:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xnt0p-0000ij-MI for qemu-devel@nongnu.org; Mon, 10 Nov 2014 12:44:33 -0500 Received: from cantor2.suse.de ([195.135.220.15]:42897 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xnt0p-0000iJ-G2 for qemu-devel@nongnu.org; Mon, 10 Nov 2014 12:44:23 -0500 From: Alexander Graf Date: Mon, 10 Nov 2014 18:44:21 +0100 Message-Id: <1415641462-52468-3-git-send-email-agraf@suse.de> In-Reply-To: <1415641462-52468-1-git-send-email-agraf@suse.de> References: <1415641462-52468-1-git-send-email-agraf@suse.de> Subject: [Qemu-devel] [PATCH 2.2 v2 2/3] linux-user: Properly handle timer magic offset List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: tommusta@gmail.com, riku.voipio@iki.fi, peter.maydell@linaro.org When creating a timer handle, we give the timer id a special magic offset of 0xcafe0000. However, we never mask that offset out of the timer id before we start using it to dereference our timer array. So we always end up aborting timer operations because the timer id is out of bounds. This was not an issue before my patch e52a99f756e ("linux-user: Simplify timerid checks on g_posix_timers range") because before we would blindly mask anything above the first 16 bits. This patch is superior to the plain masking in that it also adds validity checks against the timer id to ensure we're always dealing with an actual timer id created by QEMU. Reported-by: Tom Musta Signed-off-by: Alexander Graf --- v1 -> v2: - Abort when magic is missing --- linux-user/syscall.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index d012c71..b7f12b6 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -9573,6 +9573,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, } #endif +#define TIMER_MAGIC 0xcafe0000 +#define TIMER_MAGIC_MASK 0xffff0000 + #ifdef TARGET_NR_timer_create case TARGET_NR_timer_create: { @@ -9600,7 +9603,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, if (ret) { phtimer = NULL; } else { - if (put_user(0xcafe0000 | timer_index, arg3, target_timer_t)) { + if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) { goto efault; } } @@ -9616,6 +9619,14 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, * struct itimerspec * old_value */ target_ulong timerid = arg1; + /* Convert QEMU provided timer ID back to internal 16bit index format */ + if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) { + timerid &= 0xffff; + } else { + ret = -TARGET_EINVAL; + break; + } + if (arg3 == 0 || timerid >= ARRAY_SIZE(g_posix_timers)) { ret = -TARGET_EINVAL; } else { @@ -9637,6 +9648,14 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, /* args: timer_t timerid, struct itimerspec *curr_value */ target_ulong timerid = arg1; + /* Convert QEMU provided timer ID back to internal 16bit index format */ + if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) { + timerid &= 0xffff; + } else { + ret = -TARGET_EINVAL; + break; + } + if (!arg2) { return -TARGET_EFAULT; } else if (timerid >= ARRAY_SIZE(g_posix_timers)) { -- 1.7.12.4