From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB5BAC433FE for ; Wed, 13 Oct 2021 07:05:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A255260E78 for ; Wed, 13 Oct 2021 07:05:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238095AbhJMHHb (ORCPT ); Wed, 13 Oct 2021 03:07:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238178AbhJMHHb (ORCPT ); Wed, 13 Oct 2021 03:07:31 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8597FC061570 for ; Wed, 13 Oct 2021 00:05:28 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id g13-20020a17090a3c8d00b00196286963b9so3690867pjc.3 for ; Wed, 13 Oct 2021 00:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6tGvcqjwGnkwXapVdxREWLfq3SeiEzYltujninCiyHk=; b=Y9iLi2JrztwgqJU7WxouRqkcs19pQmlvc+dwqmTt/LsB2A/+LkmdmebziJqWx90+1l QGUBkBxPvUXkDNRhn2WfhIRnIA//lJRWBR6Sy4+Bvt3AZnPK5owInFUImk8i6ZlkNfXx 3PbBkcLhCoweDrO9NiqjhNF4c3MLWR35u5DiA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6tGvcqjwGnkwXapVdxREWLfq3SeiEzYltujninCiyHk=; b=mE5qWfrWpGRXjOAur5h2NAOG8k/nQ13b5uwLyW8CFEWE4UjzwXIU0URJWI3kcmDpcs UIocXcY+IfNqWnMPylaQwlRkfBoNXVhBLy1p0h4XydZpRjU4TLSlBMFTHkFsZWbMKbtY AOzu6qEZUZmnXX/foZYnbDfHyxwSHfV9rO+krBqfgA34lZJDMk6q+B4mwYD3OOacQoNe leNf/NCtyxSOZWkEWBgDOrHD5lFDHJr+sJS001B8FLIbfD9ybfi8gGeX/nPi+yiqmGLb gyLwwyYz8UqOG+YJ9OnCWyD1W5aNTNfM3DspZs9D0Y8KDnji02Kx5/7q9g4u0uqIOFoL y4Lg== X-Gm-Message-State: AOAM532P5xHt4ifMf4XoLBFD95tlp3+Ub7iWOxv6uOvz5xXuRk9251Uw /cXFzwUxZXe562nJkzXQWr8YDw== X-Google-Smtp-Source: ABdhPJxyo+lKj4DVYPaWC7XDi86I8yDjK4zmQZScEiHVyq9xnd6FexVoNrQNr9v1X26KMPGowX7ckA== X-Received: by 2002:a17:90b:3b44:: with SMTP id ot4mr11523975pjb.73.1634108727909; Wed, 13 Oct 2021 00:05:27 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id ip10sm5334805pjb.40.2021.10.13.00.05.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Oct 2021 00:05:27 -0700 (PDT) Date: Wed, 13 Oct 2021 00:05:26 -0700 From: Kees Cook To: Christophe Leroy Cc: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Andrew Morton , "James E.J. Bottomley" , Helge Deller , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-ia64@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN Message-ID: <202110130004.880A6C841@keescook> References: <624940395e5d81967246f911a65740b9a15b5a70.1633964380.git.christophe.leroy@csgroup.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <624940395e5d81967246f911a65740b9a15b5a70.1633964380.git.christophe.leroy@csgroup.eu> Precedence: bulk List-ID: X-Mailing-List: linux-arch@vger.kernel.org On Mon, Oct 11, 2021 at 05:25:35PM +0200, Christophe Leroy wrote: > WRITE_KERN is supposed to overwrite some kernel text, namely > do_overwritten() function. > > But at the time being it overwrites do_overwritten() function > descriptor, not function text. > > Fix it by dereferencing the function descriptor to obtain > function text pointer. > > And make do_overwritten() noinline so that it is really > do_overwritten() which is called by lkdtm_WRITE_KERN(). > > Signed-off-by: Christophe Leroy > --- > drivers/misc/lkdtm/perms.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c > index 60b3b2fe929d..442d60ed25ef 100644 > --- a/drivers/misc/lkdtm/perms.c > +++ b/drivers/misc/lkdtm/perms.c > @@ -5,6 +5,7 @@ > * even non-readable regions. > */ > #include "lkdtm.h" > +#include Why not #include instead here? > #include > #include > #include > @@ -37,7 +38,7 @@ static noinline void do_nothing(void) > } > > /* Must immediately follow do_nothing for size calculuations to work out. */ > -static void do_overwritten(void) > +static noinline void do_overwritten(void) > { > pr_info("do_overwritten wasn't overwritten!\n"); > return; > @@ -113,8 +114,9 @@ void lkdtm_WRITE_KERN(void) > size_t size; > volatile unsigned char *ptr; > > - size = (unsigned long)do_overwritten - (unsigned long)do_nothing; > - ptr = (unsigned char *)do_overwritten; > + size = (unsigned long)dereference_symbol_descriptor(do_overwritten) - > + (unsigned long)dereference_symbol_descriptor(do_nothing); > + ptr = dereference_symbol_descriptor(do_overwritten); But otherwise, yup, I expect there will be a bunch of things like this to clean up in LKDTM. :| Sorry about that! Acked-by: Kees Cook > > pr_info("attempting bad %zu byte write at %px\n", size, ptr); > memcpy((void *)ptr, (unsigned char *)do_nothing, size); > -- > 2.31.1 > -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kees Cook Date: Wed, 13 Oct 2021 07:05:26 +0000 Subject: Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN Message-Id: <202110130004.880A6C841@keescook> List-Id: References: <624940395e5d81967246f911a65740b9a15b5a70.1633964380.git.christophe.leroy@csgroup.eu> In-Reply-To: <624940395e5d81967246f911a65740b9a15b5a70.1633964380.git.christophe.leroy@csgroup.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Christophe Leroy Cc: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Andrew Morton , "James E.J. Bottomley" , Helge Deller , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-ia64@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org On Mon, Oct 11, 2021 at 05:25:35PM +0200, Christophe Leroy wrote: > WRITE_KERN is supposed to overwrite some kernel text, namely > do_overwritten() function. > > But at the time being it overwrites do_overwritten() function > descriptor, not function text. > > Fix it by dereferencing the function descriptor to obtain > function text pointer. > > And make do_overwritten() noinline so that it is really > do_overwritten() which is called by lkdtm_WRITE_KERN(). > > Signed-off-by: Christophe Leroy > --- > drivers/misc/lkdtm/perms.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c > index 60b3b2fe929d..442d60ed25ef 100644 > --- a/drivers/misc/lkdtm/perms.c > +++ b/drivers/misc/lkdtm/perms.c > @@ -5,6 +5,7 @@ > * even non-readable regions. > */ > #include "lkdtm.h" > +#include Why not #include instead here? > #include > #include > #include > @@ -37,7 +38,7 @@ static noinline void do_nothing(void) > } > > /* Must immediately follow do_nothing for size calculuations to work out. */ > -static void do_overwritten(void) > +static noinline void do_overwritten(void) > { > pr_info("do_overwritten wasn't overwritten!\n"); > return; > @@ -113,8 +114,9 @@ void lkdtm_WRITE_KERN(void) > size_t size; > volatile unsigned char *ptr; > > - size = (unsigned long)do_overwritten - (unsigned long)do_nothing; > - ptr = (unsigned char *)do_overwritten; > + size = (unsigned long)dereference_symbol_descriptor(do_overwritten) - > + (unsigned long)dereference_symbol_descriptor(do_nothing); > + ptr = dereference_symbol_descriptor(do_overwritten); But otherwise, yup, I expect there will be a bunch of things like this to clean up in LKDTM. :| Sorry about that! Acked-by: Kees Cook > > pr_info("attempting bad %zu byte write at %px\n", size, ptr); > memcpy((void *)ptr, (unsigned char *)do_nothing, size); > -- > 2.31.1 > -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66610C4332F for ; Wed, 13 Oct 2021 07:06:14 +0000 (UTC) Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D29A261027 for ; Wed, 13 Oct 2021 07:06:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D29A261027 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.ozlabs.org Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4HTk8h2f31z3c9R for ; Wed, 13 Oct 2021 18:06:12 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=Y9iLi2Jr; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=chromium.org (client-ip=2607:f8b0:4864:20::1031; helo=mail-pj1-x1031.google.com; envelope-from=keescook@chromium.org; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=Y9iLi2Jr; dkim-atps=neutral Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4HTk7x3Xrhz2xr4 for ; Wed, 13 Oct 2021 18:05:30 +1100 (AEDT) Received: by mail-pj1-x1031.google.com with SMTP id qe4-20020a17090b4f8400b0019f663cfcd1so3703951pjb.1 for ; Wed, 13 Oct 2021 00:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6tGvcqjwGnkwXapVdxREWLfq3SeiEzYltujninCiyHk=; b=Y9iLi2JrztwgqJU7WxouRqkcs19pQmlvc+dwqmTt/LsB2A/+LkmdmebziJqWx90+1l QGUBkBxPvUXkDNRhn2WfhIRnIA//lJRWBR6Sy4+Bvt3AZnPK5owInFUImk8i6ZlkNfXx 3PbBkcLhCoweDrO9NiqjhNF4c3MLWR35u5DiA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6tGvcqjwGnkwXapVdxREWLfq3SeiEzYltujninCiyHk=; b=KFse9PBqkt/x48AaU28gfXBpY/Ld2N9GxChsfge9uH00Ua9GQF1DJqAUCikN8JmPzE TeIAIFEHpfJfasmeSNgnPaYG4aNhwDzEszcqVf6kVnPPhCvMVym9jhviJAjVRFq1hFbS D37SEdyeDk+5KVG0T+IgCdzCpQnGwRbDQL+h61MwwV2p+LaxVCY0H0Vu6Rbx0vBDVGzE mlOOmoQSW2gBBeivsAWjl+1tZesTZkwMJ0N0DJ9dJufYjQdH0WgyENOk7Di4iSrmEQ0H IrSymdYwroa0rlW/dPjsN9bG2YVXDy5SSU46R/qwFU1MOMWfwtS2WOAUo/Ml2hH0yt7j ps2A== X-Gm-Message-State: AOAM533Yb7WvpSo54ID0jDQ790IMaw9grPwonum4OZMuvl5mhDqLI9xR gDMH2ZC0wa0S8YSsz+bhPCwYWQ== X-Google-Smtp-Source: ABdhPJxyo+lKj4DVYPaWC7XDi86I8yDjK4zmQZScEiHVyq9xnd6FexVoNrQNr9v1X26KMPGowX7ckA== X-Received: by 2002:a17:90b:3b44:: with SMTP id ot4mr11523975pjb.73.1634108727909; Wed, 13 Oct 2021 00:05:27 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id ip10sm5334805pjb.40.2021.10.13.00.05.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Oct 2021 00:05:27 -0700 (PDT) Date: Wed, 13 Oct 2021 00:05:26 -0700 From: Kees Cook To: Christophe Leroy Subject: Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN Message-ID: <202110130004.880A6C841@keescook> References: <624940395e5d81967246f911a65740b9a15b5a70.1633964380.git.christophe.leroy@csgroup.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <624940395e5d81967246f911a65740b9a15b5a70.1633964380.git.christophe.leroy@csgroup.eu> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, linux-ia64@vger.kernel.org, linux-parisc@vger.kernel.org, Arnd Bergmann , Greg Kroah-Hartman , Helge Deller , linux-kernel@vger.kernel.org, "James E.J. Bottomley" , linux-mm@kvack.org, Paul Mackerras , Andrew Morton , linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On Mon, Oct 11, 2021 at 05:25:35PM +0200, Christophe Leroy wrote: > WRITE_KERN is supposed to overwrite some kernel text, namely > do_overwritten() function. > > But at the time being it overwrites do_overwritten() function > descriptor, not function text. > > Fix it by dereferencing the function descriptor to obtain > function text pointer. > > And make do_overwritten() noinline so that it is really > do_overwritten() which is called by lkdtm_WRITE_KERN(). > > Signed-off-by: Christophe Leroy > --- > drivers/misc/lkdtm/perms.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c > index 60b3b2fe929d..442d60ed25ef 100644 > --- a/drivers/misc/lkdtm/perms.c > +++ b/drivers/misc/lkdtm/perms.c > @@ -5,6 +5,7 @@ > * even non-readable regions. > */ > #include "lkdtm.h" > +#include Why not #include instead here? > #include > #include > #include > @@ -37,7 +38,7 @@ static noinline void do_nothing(void) > } > > /* Must immediately follow do_nothing for size calculuations to work out. */ > -static void do_overwritten(void) > +static noinline void do_overwritten(void) > { > pr_info("do_overwritten wasn't overwritten!\n"); > return; > @@ -113,8 +114,9 @@ void lkdtm_WRITE_KERN(void) > size_t size; > volatile unsigned char *ptr; > > - size = (unsigned long)do_overwritten - (unsigned long)do_nothing; > - ptr = (unsigned char *)do_overwritten; > + size = (unsigned long)dereference_symbol_descriptor(do_overwritten) - > + (unsigned long)dereference_symbol_descriptor(do_nothing); > + ptr = dereference_symbol_descriptor(do_overwritten); But otherwise, yup, I expect there will be a bunch of things like this to clean up in LKDTM. :| Sorry about that! Acked-by: Kees Cook > > pr_info("attempting bad %zu byte write at %px\n", size, ptr); > memcpy((void *)ptr, (unsigned char *)do_nothing, size); > -- > 2.31.1 > -- Kees Cook