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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B680C433F5 for ; Wed, 19 Jan 2022 22:00:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6185E6B0071; Wed, 19 Jan 2022 17:00:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C80F6B0073; Wed, 19 Jan 2022 17:00:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4686A6B0074; Wed, 19 Jan 2022 17:00:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0084.hostedemail.com [216.40.44.84]) by kanga.kvack.org (Postfix) with ESMTP id 375B86B0071 for ; Wed, 19 Jan 2022 17:00:35 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id E503C96F17 for ; Wed, 19 Jan 2022 22:00:34 +0000 (UTC) X-FDA: 79048406388.20.10EE854 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf01.hostedemail.com (Postfix) with ESMTP id 2D6BA4001B for ; Wed, 19 Jan 2022 22:00:29 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id 78so3514523pfu.10 for ; Wed, 19 Jan 2022 14:00:28 -0800 (PST) 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:content-transfer-encoding:in-reply-to; bh=JI5jEcI1hiEGc8WLPgbqQTxkl08Ju0kSWlXY2eIoY9Q=; b=Qxk8ZlOzgx4vMG2yojzX5wqvtn5rpGheXm11Z2NyGGBIUXeWLr4XOg3b0q3SyZigtH iGaQnWl3zb1OvgUv/kyJhvoC8mXZPAhUz50q7uhfasP7KXx2JeaPhKPNgtg4s8H9BDUo vvaHlGGVz9UU7HVblsspusc51ErXeXhgqq8Bk= 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:content-transfer-encoding :in-reply-to; bh=JI5jEcI1hiEGc8WLPgbqQTxkl08Ju0kSWlXY2eIoY9Q=; b=QsQBiA29zMBRSk22Byp9CRcMB1qyrXkVeh9wqcZvsjk7AZ27lbLuFqBqkn31oRqix8 c2n137OXZKt5UgAcxiFbojjoMqvG1vk4zt/QlV0aHHtXgKK5+jd/MDkyXomqb9IevCH5 bN9WVbLHNCZOzOCQFXhqGXePwisz8J9vJHM1gaCJkdeDj3xmV3QKTP8f6WeLRwij0bUO FUcHQxeO3zgMv6SRrMr9HfR28erWYB1sUiabmzqLCX8Fy0jL+/SGjjky0dtCpy+TbMz/ 7nuCw3cp5C8V0ZL3eBzGSLDaUynFgDFhT5wqyhEd9obqMg4BEzJHuc9yHQoAcj6DDU5+ /DAw== X-Gm-Message-State: AOAM533MQTenTuv86lfDIwW4FvKfIixrU7UEtXOhYR2E+dXrHihhaY1x kFN+iB0JEmptkTeGMU27heEktg== X-Google-Smtp-Source: ABdhPJwt4Nv5793SP6zTE20w2tmeoTfB5V156ftaoTtLpJsdxvELruQabzZX/RUlM9ATmwCRik8jUg== X-Received: by 2002:aa7:918e:0:b0:4bb:793:b7a7 with SMTP id x14-20020aa7918e000000b004bb0793b7a7mr32876692pfa.71.1642629627636; Wed, 19 Jan 2022 14:00:27 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k12sm602597pfc.107.2022.01.19.14.00.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jan 2022 14:00:26 -0800 (PST) Date: Wed, 19 Jan 2022 14:00:26 -0800 From: Kees Cook To: Christophe Leroy Cc: Helge Deller , Benjamin Herrenschmidt , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org, linux-ia64@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, "James E.J. Bottomley" , Paul Mackerras , Arnd Bergmann , Michael Ellerman Subject: Re: [PATCH v3 11/12] lkdtm: Fix execute_[user]_location() Message-ID: <202201191359.5E67E74A@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: X-Stat-Signature: 9xgbz6tnd4eteandxjz6qfmfshdfqqog Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Qxk8ZlOz; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf01.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.177 as permitted sender) smtp.mailfrom=keescook@chromium.org X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 2D6BA4001B X-HE-Tag: 1642629629-196669 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Jan 19, 2022 at 08:28:54PM +0100, Christophe Leroy wrote: > Hi Kees, >=20 >=20 > Le 17/12/2021 =E0 12:49, Christophe Leroy a =E9crit=A0: > > Hi Kees, > >=20 > > Le 17/10/2021 =E0 14:38, Christophe Leroy a =E9crit=A0: > > > execute_location() and execute_user_location() intent > > > to copy do_nothing() text and execute it at a new location. > > > However, at the time being it doesn't copy do_nothing() function > > > but do_nothing() function descriptor which still points to the > > > original text. So at the end it still executes do_nothing() at > > > its original location allthough using a copied function descriptor. > > >=20 > > > So, fix that by really copying do_nothing() text and build a new > > > function descriptor by copying do_nothing() function descriptor and > > > updating the target address with the new location. > > >=20 > > > Also fix the displayed addresses by dereferencing do_nothing() > > > function descriptor. > > >=20 > > > Signed-off-by: Christophe Leroy > >=20 > > Do you have any comment to this patch and to patch 12 ? > >=20 > > If not, is it ok to get your acked-by ? >=20 > Any feedback please, even if it's to say no feedback ? Hi! Thanks for the ping; I haven't had time yet to look at this, but with -rc1 coming, I should be able to task-switch back to LKDTM for the dev cycle and I can give some feedback. -Kees >=20 > Many thanks, > Christophe >=20 > >=20 > > Thanks > > Christophe > >=20 > > > --- > > > =A0 drivers/misc/lkdtm/perms.c | 37 ++++++++++++++++++++++++++++---= ------ > > > =A0 1 file changed, 28 insertions(+), 9 deletions(-) > > >=20 > > > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.= c > > > index 035fcca441f0..1cf24c4a79e9 100644 > > > --- a/drivers/misc/lkdtm/perms.c > > > +++ b/drivers/misc/lkdtm/perms.c > > > @@ -44,19 +44,34 @@ static noinline void do_overwritten(void) > > > =A0=A0=A0=A0=A0 return; > > > =A0 } > > > +static void *setup_function_descriptor(func_desc_t *fdesc, void *d= st) > > > +{ > > > +=A0=A0=A0 if (!have_function_descriptors()) > > > +=A0=A0=A0=A0=A0=A0=A0 return dst; > > > + > > > +=A0=A0=A0 memcpy(fdesc, do_nothing, sizeof(*fdesc)); > > > +=A0=A0=A0 fdesc->addr =3D (unsigned long)dst; > > > +=A0=A0=A0 barrier(); > > > + > > > +=A0=A0=A0 return fdesc; > > > +} > > > + > > > =A0 static noinline void execute_location(void *dst, bool write) > > > =A0 { > > > -=A0=A0=A0 void (*func)(void) =3D dst; > > > +=A0=A0=A0 void (*func)(void); > > > +=A0=A0=A0 func_desc_t fdesc; > > > +=A0=A0=A0 void *do_nothing_text =3D dereference_function_descripto= r(do_nothing); > > > -=A0=A0=A0 pr_info("attempting ok execution at %px\n", do_nothing); > > > +=A0=A0=A0 pr_info("attempting ok execution at %px\n", do_nothing_t= ext); > > > =A0=A0=A0=A0=A0 do_nothing(); > > > =A0=A0=A0=A0=A0 if (write =3D=3D CODE_WRITE) { > > > -=A0=A0=A0=A0=A0=A0=A0 memcpy(dst, do_nothing, EXEC_SIZE); > > > +=A0=A0=A0=A0=A0=A0=A0 memcpy(dst, do_nothing_text, EXEC_SIZE); > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0 flush_icache_range((unsigned long)dst, > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (unsig= ned long)dst + EXEC_SIZE); > > > =A0=A0=A0=A0=A0 } > > > -=A0=A0=A0 pr_info("attempting bad execution at %px\n", func); > > > +=A0=A0=A0 pr_info("attempting bad execution at %px\n", dst); > > > +=A0=A0=A0 func =3D setup_function_descriptor(&fdesc, dst); > > > =A0=A0=A0=A0=A0 func(); > > > =A0=A0=A0=A0=A0 pr_err("FAIL: func returned\n"); > > > =A0 } > > > @@ -66,16 +81,19 @@ static void execute_user_location(void *dst) > > > =A0=A0=A0=A0=A0 int copied; > > > =A0=A0=A0=A0=A0 /* Intentionally crossing kernel/user memory bounda= ry. */ > > > -=A0=A0=A0 void (*func)(void) =3D dst; > > > +=A0=A0=A0 void (*func)(void); > > > +=A0=A0=A0 func_desc_t fdesc; > > > +=A0=A0=A0 void *do_nothing_text =3D dereference_function_descripto= r(do_nothing); > > > -=A0=A0=A0 pr_info("attempting ok execution at %px\n", do_nothing); > > > +=A0=A0=A0 pr_info("attempting ok execution at %px\n", do_nothing_t= ext); > > > =A0=A0=A0=A0=A0 do_nothing(); > > > -=A0=A0=A0 copied =3D access_process_vm(current, (unsigned long)dst= , do_nothing, > > > +=A0=A0=A0 copied =3D access_process_vm(current, (unsigned long)dst= , > > > do_nothing_text, > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 EXEC_S= IZE, FOLL_WRITE); > > > =A0=A0=A0=A0=A0 if (copied < EXEC_SIZE) > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0 return; > > > -=A0=A0=A0 pr_info("attempting bad execution at %px\n", func); > > > +=A0=A0=A0 pr_info("attempting bad execution at %px\n", dst); > > > +=A0=A0=A0 func =3D setup_function_descriptor(&fdesc, dst); > > > =A0=A0=A0=A0=A0 func(); > > > =A0=A0=A0=A0=A0 pr_err("FAIL: func returned\n"); > > > =A0 } > > > @@ -153,7 +171,8 @@ void lkdtm_EXEC_VMALLOC(void) > > > =A0 void lkdtm_EXEC_RODATA(void) > > > =A0 { > > > -=A0=A0=A0 execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS); > > > + execute_location(dereference_function_descriptor(lkdtm_rodata_= do_nothing), > > >=20 > > > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 CODE_AS_IS); > > > =A0 } > > > =A0 void lkdtm_EXEC_USERSPACE(void) > > >=20 --=20 Kees Cook