Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5967751pxu; Wed, 23 Dec 2020 09:45:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJyz2X2oR3GZdiBzM7osvOZYVsyAIDofi7AWV9boFwP1fAURP0H1QJEamF/xEQTky+YgIU2g X-Received: by 2002:a17:906:bc9b:: with SMTP id lv27mr24444949ejb.505.1608745537221; Wed, 23 Dec 2020 09:45:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608745537; cv=none; d=google.com; s=arc-20160816; b=IYElR9hbUc+v7+JmQDeUi+M8DCQttO+rpAh4digQGPgAYN2LlincDb2MLvwghZi4Cd WVC+pDAs7Ug2RlcmDfhQG+W5Z0R757aa8nA96t6cE9EZ2XGoztmh+H8vyTQXUK3naRpk WwNsRyp4ZuJKYz6b97wY49lMh67JvUD7pwbZJhYpLvWA0BxE8HWIbNpsM+p23V82BGnX DoQxXO55z4DmF5NHZ3v9MzFwnt/weszeYZqxgP9cebxZ148N5PmReCF8SQD0/7HMPG8U +DhdJa9kE//Doz5tK6jf+Of/lxON6i07K7cWsoAjRTlvoN6VFnR+lzfJx7KMUqWzbWLY yScw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ov/xQx4ghCLA5ZePaeXhY2tH4LZazkldREB7mFa42RA=; b=t740xNdzly6q8OaojJIBVBEPV8GSPgDEfMOxvZiR/otewft3YeWMLt4M4eSd/i7osG IvMH2YhZrBwOUyQwNH5jQ8942Q7UjRDeI6SsMjfWH6QIuJQxq4yGofx7raut9qU/R0qN dTYbjPOm/wevUUiawoXEO6Kw8PB9Yd1M5C2gGEoCBtIHXQb5EtJXoWV+ejlUCmZ7mFhs KSHrQsl2RiFvGc5rvnjYQqmxC5v2aoMs1LApSJu2vVhpjODSyykCfD/eqrRXmSU1VvhI ISnfFnC1dsDF+dJ3cjiFdKF1IvAr9We1A34ZDO2tlLxnnsAJ10fAsfSvY6DBEqGft0Vk A0xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b="j9N/MU7l"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k17si12666019ejq.626.2020.12.23.09.45.12; Wed, 23 Dec 2020 09:45:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b="j9N/MU7l"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728555AbgLWRnt (ORCPT + 99 others); Wed, 23 Dec 2020 12:43:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728245AbgLWRn3 (ORCPT ); Wed, 23 Dec 2020 12:43:29 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4656AC061285 for ; Wed, 23 Dec 2020 09:42:49 -0800 (PST) Received: from zn.tnic (p200300ec2f0de600b74d534b1676c98b.dip0.t-ipconnect.de [IPv6:2003:ec:2f0d:e600:b74d:534b:1676:c98b]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id DBE181EC052A; Wed, 23 Dec 2020 18:42:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1608745368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ov/xQx4ghCLA5ZePaeXhY2tH4LZazkldREB7mFa42RA=; b=j9N/MU7lpiDcolzwCQnZhwFnxiDmP1fogoEmA6TSE3ljz554ahIzuAPLSjdgR8e+Z3H6+T u4fgMzK9AviI8GVLS6xCNixtQ33bjrdyp43ZhORDwT3j8Rrw8IBEAZ72SUyZbXUsvKpXjJ QypZy91v+eKs1iD2MXAwbE0ZYEz1qus= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [PATCH v1 10/19] x86/kprobes: Convert to insn_decode() Date: Wed, 23 Dec 2020 18:42:24 +0100 Message-Id: <20201223174233.28638-11-bp@alien8.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201223174233.28638-1-bp@alien8.de> References: <20201223174233.28638-1-bp@alien8.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, improve decoding error checking. Signed-off-by: Borislav Petkov Acked-by: Masami Hiramatsu --- arch/x86/kernel/kprobes/core.c | 17 +++++++++++------ arch/x86/kernel/kprobes/opt.c | 9 +++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index a65e9e97857f..1cf4b532b798 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -285,6 +285,8 @@ static int can_probe(unsigned long paddr) /* Decode instructions */ addr = paddr - offset; while (addr < paddr) { + int ret; + /* * Check if the instruction has been modified by another * kprobe, in which case we replace the breakpoint by the @@ -296,8 +298,10 @@ static int can_probe(unsigned long paddr) __addr = recover_probed_instruction(buf, addr); if (!__addr) return 0; - kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE); - insn_get_length(&insn); + + ret = insn_decode(&insn, (void *)__addr, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return 0; /* * Another debugging subsystem might insert this breakpoint. @@ -340,8 +344,8 @@ static int is_IF_modifier(kprobe_opcode_t *insn) int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn) { kprobe_opcode_t buf[MAX_INSN_SIZE]; - unsigned long recovered_insn = - recover_probed_instruction(buf, (unsigned long)src); + unsigned long recovered_insn = recover_probed_instruction(buf, (unsigned long)src); + int ret; if (!recovered_insn || !insn) return 0; @@ -351,8 +355,9 @@ int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn) MAX_INSN_SIZE)) return 0; - kernel_insn_init(insn, dest, MAX_INSN_SIZE); - insn_get_length(insn); + ret = insn_decode(insn, dest, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return 0; /* We can not probe force emulate prefixed instruction */ if (insn_has_emulate_prefix(insn)) diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c index 08eb23074f92..4299fc865732 100644 --- a/arch/x86/kernel/kprobes/opt.c +++ b/arch/x86/kernel/kprobes/opt.c @@ -312,6 +312,8 @@ static int can_optimize(unsigned long paddr) addr = paddr - offset; while (addr < paddr - offset + size) { /* Decode until function end */ unsigned long recovered_insn; + int ret; + if (search_exception_tables(addr)) /* * Since some fixup code will jumps into this function, @@ -321,8 +323,11 @@ static int can_optimize(unsigned long paddr) recovered_insn = recover_probed_instruction(buf, addr); if (!recovered_insn) return 0; - kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE); - insn_get_length(&insn); + + ret = insn_decode(&insn, (void *)recovered_insn, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return 0; + /* * In the case of detecting unknown breakpoint, this could be * a padding INT3 between functions. Let's check that all the -- 2.29.2