Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1742134ybl; Sat, 11 Jan 2020 01:54:09 -0800 (PST) X-Google-Smtp-Source: APXvYqwh1fQklkbm4sJ0GvZ4RL+bptpXgaI+4b2NUBi67kRqVvfKghDX86zvGGoxo9qI2Xy60Hxm X-Received: by 2002:aca:4dd6:: with SMTP id a205mr5500026oib.43.1578736448917; Sat, 11 Jan 2020 01:54:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578736448; cv=none; d=google.com; s=arc-20160816; b=Cu5RTH6txiHWHn8ToPqMiIY1IreiHOz5UsD1UVPS9uTbSFtbT8vpZ/yySWI8SZPfmK rDq4yGdKT2c8QdxznOXQ2kLh3+9X+FzW+30CcXfaurSnF0emcnkDvAHDXQwBc29XpSne 1McZxbbuuI32qDd8mhC0OIoUSqwoThTP0pomq+1F/VfnyMCxWstAoL3rnFulmDEOxgJL pFj8a4IOtka83hkk6QdCkkRRhiGNbVI+ojzZcK2Z/Su6k6lH186RJxbkpEu+UNBSw89W iI9vdp1YxVF0gNjC4rK8ugPIfsFPXkJp7y5Y6Lujb/aJBL4pLIBqeF8qS1q042zLt0HU QXnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=i0nFadrGYipvBY3ANatlmzsgm87HwfcxMCllDizlC1k=; b=kxIB1GK44D2OyvzRmigI3Sa9JXAToTO3hDkb73IW3AmbFN+22sYsEGbZbWAwUxfD1v llFax3ayZ2tbr4u0uCKDr2L20n8txmVJ6iteW9+xZHv9O1JPhvW0dSgDLuW5MlUNbhgv ywVtb9pmW7Q+Khj8t3PZx+jQxsq+H4QF/xZaTGCmZ0i8IM7XqaUdP7X+wHvIs8leOFx3 bUvDWOWJ0/EfLSoBOR1+g236WlQ5bBx6c+BeIX8Hhe2ZIsB3neT+pVxYXdMwX+w/kylN 3XPg6l1Rq0rFdNv+RH1vJzops+MQ/wBeiaMTKRGb5htrEgUCLD+NBxok018yphNdsIO6 KlAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ZYChWGtv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v5si3463808otn.155.2020.01.11.01.53.58; Sat, 11 Jan 2020 01:54:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ZYChWGtv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729009AbgAKJxF (ORCPT + 99 others); Sat, 11 Jan 2020 04:53:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:39530 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728752AbgAKJxF (ORCPT ); Sat, 11 Jan 2020 04:53:05 -0500 Received: from localhost (unknown [62.119.166.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AC7C620842; Sat, 11 Jan 2020 09:53:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578736384; bh=wL6HCTVAlcZDf7Nmfb5nhq3M1MQucxU/iSCj/9IbWXI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZYChWGtvng+ZM0CPQU9V+7biwweTdItcgcEK9WCP2xDMobbM+WJKL8A9Z6pp4xgIa QnZ+fXde0vuEO1cB1/rxKU6tmXaiYhJmHRCmf11OxUGg80CAxX22lB0Q8roMUj23/u bdX4JyaXmmGdBLSVQjmvUcofrUvN6Gsf2799EiqY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Axtens , Michael Ellerman , Sasha Levin Subject: [PATCH 4.4 27/59] powerpc/pseries/hvconsole: Fix stack overread via udbg Date: Sat, 11 Jan 2020 10:49:36 +0100 Message-Id: <20200111094844.025396036@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200111094835.417654274@linuxfoundation.org> References: <20200111094835.417654274@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Axtens [ Upstream commit 934bda59f286d0221f1a3ebab7f5156a996cc37d ] While developing KASAN for 64-bit book3s, I hit the following stack over-read. It occurs because the hypercall to put characters onto the terminal takes 2 longs (128 bits/16 bytes) of characters at a time, and so hvc_put_chars() would unconditionally copy 16 bytes from the argument buffer, regardless of supplied length. However, udbg_hvc_putc() can call hvc_put_chars() with a single-byte buffer, leading to the error. ================================================================== BUG: KASAN: stack-out-of-bounds in hvc_put_chars+0xdc/0x110 Read of size 8 at addr c0000000023e7a90 by task swapper/0 CPU: 0 PID: 0 Comm: swapper Not tainted 5.2.0-rc2-next-20190528-02824-g048a6ab4835b #113 Call Trace: dump_stack+0x104/0x154 (unreliable) print_address_description+0xa0/0x30c __kasan_report+0x20c/0x224 kasan_report+0x18/0x30 __asan_report_load8_noabort+0x24/0x40 hvc_put_chars+0xdc/0x110 hvterm_raw_put_chars+0x9c/0x110 udbg_hvc_putc+0x154/0x200 udbg_write+0xf0/0x240 console_unlock+0x868/0xd30 register_console+0x970/0xe90 register_early_udbg_console+0xf8/0x114 setup_arch+0x108/0x790 start_kernel+0x104/0x784 start_here_common+0x1c/0x534 Memory state around the buggy address: c0000000023e7980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0000000023e7a00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 >c0000000023e7a80: f1 f1 01 f2 f2 f2 00 00 00 00 00 00 00 00 00 00 ^ c0000000023e7b00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0000000023e7b80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ================================================================== Document that a 16-byte buffer is requred, and provide it in udbg. Signed-off-by: Daniel Axtens Signed-off-by: Michael Ellerman Signed-off-by: Sasha Levin --- arch/powerpc/platforms/pseries/hvconsole.c | 2 +- drivers/tty/hvc/hvc_vio.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/platforms/pseries/hvconsole.c b/arch/powerpc/platforms/pseries/hvconsole.c index 849b29b3e9ae..954ef27128f2 100644 --- a/arch/powerpc/platforms/pseries/hvconsole.c +++ b/arch/powerpc/platforms/pseries/hvconsole.c @@ -62,7 +62,7 @@ EXPORT_SYMBOL(hvc_get_chars); * @vtermno: The vtermno or unit_address of the adapter from which the data * originated. * @buf: The character buffer that contains the character data to send to - * firmware. + * firmware. Must be at least 16 bytes, even if count is less than 16. * @count: Send this number of characters. */ int hvc_put_chars(uint32_t vtermno, const char *buf, int count) diff --git a/drivers/tty/hvc/hvc_vio.c b/drivers/tty/hvc/hvc_vio.c index f575a9b5ede7..1d671d058dcb 100644 --- a/drivers/tty/hvc/hvc_vio.c +++ b/drivers/tty/hvc/hvc_vio.c @@ -122,6 +122,14 @@ static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count) return got; } +/** + * hvterm_raw_put_chars: send characters to firmware for given vterm adapter + * @vtermno: The virtual terminal number. + * @buf: The characters to send. Because of the underlying hypercall in + * hvc_put_chars(), this buffer must be at least 16 bytes long, even if + * you are sending fewer chars. + * @count: number of chars to send. + */ static int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count) { struct hvterm_priv *pv = hvterm_privs[vtermno]; @@ -234,6 +242,7 @@ static const struct hv_ops hvterm_hvsi_ops = { static void udbg_hvc_putc(char c) { int count = -1; + unsigned char bounce_buffer[16]; if (!hvterm_privs[0]) return; @@ -244,7 +253,12 @@ static void udbg_hvc_putc(char c) do { switch(hvterm_privs[0]->proto) { case HV_PROTOCOL_RAW: - count = hvterm_raw_put_chars(0, &c, 1); + /* + * hvterm_raw_put_chars requires at least a 16-byte + * buffer, so go via the bounce buffer + */ + bounce_buffer[0] = c; + count = hvterm_raw_put_chars(0, bounce_buffer, 1); break; case HV_PROTOCOL_HVSI: count = hvterm_hvsi_put_chars(0, &c, 1); -- 2.20.1