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 49DB6C433EF for ; Fri, 11 Feb 2022 01:01:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A63E6B0071; Thu, 10 Feb 2022 20:01:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 954A86B0073; Thu, 10 Feb 2022 20:01:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F5826B0074; Thu, 10 Feb 2022 20:01:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id 709D26B0071 for ; Thu, 10 Feb 2022 20:01:10 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 22BC1180AC33A for ; Fri, 11 Feb 2022 01:01:10 +0000 (UTC) X-FDA: 79128695100.26.A776F0A Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf25.hostedemail.com (Postfix) with ESMTP id BCC63A0002 for ; Fri, 11 Feb 2022 01:01:09 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id w1so3355606plb.6 for ; Thu, 10 Feb 2022 17:01:09 -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:in-reply-to; bh=RdgcHpK2QxPcOYOUkcGQd2wPLDeTg88ycTZ52tqwOpM=; b=ayaQeq+iHp1StJk08sGOB8wL9hF2ax+YroHj/Q2xe57EsF2tmwRM+bRcoP5B6RJSao zVRGQxURnYafJmjj2pjUpW4Gbc4ywJAnHBFGFsrK1qUnpQSCr8sghtVn51G6hSguVMPD lHPaakslQ0USKodMgm+Qx4Kb22euGUvZ0qMSM= 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=RdgcHpK2QxPcOYOUkcGQd2wPLDeTg88ycTZ52tqwOpM=; b=K+9Lpp7tzUzWGbrkWkQ1DGg42TDDj8FzouVrabPW3IxBUZcyj2k3KWECMOEJFq926+ x/Pp7tHUNBBV84f/QE1t2qINi1jmP6H2C8mFMEWo+W9fWH3Yq8lt84gqqsc7QE7mszwV O8a/p5+BO73hmy/QZeOcBL0pVNRRXWQvorh1W51E28hUfEG0lI/roDDBygQxR6N4s5rK j333TnJPpqbe8IwQLbQFsB5TCYmCjH/dYi/lPyi+f2aOWOoZQiPrE0BnTDoPG6HXPTCj LF+IkdHHfRfSjwrnYMjFLZU55CNJDvMpKFdklMK5LJdOp3VXJC/DHAeLsUs8MjYsB3Ju RMIA== X-Gm-Message-State: AOAM530o9Nk2tkxfpC8fsXIgmWAi9d5aEKGYz8P1As5JAoGP/+omq088 ln5Y54PCavAh3ycDzqaSL1RitQ== X-Google-Smtp-Source: ABdhPJy2kB/YH9xMcBfAS96T6mrN64LEHeytd1RwomHu8ZQbb2OHvCkkk5zZkV2I8D5+PzLBVw0c7Q== X-Received: by 2002:a17:902:a413:: with SMTP id p19mr10224104plq.35.1644541268697; Thu, 10 Feb 2022 17:01:08 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c20sm13253330pfl.46.2022.02.10.17.01.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Feb 2022 17:01:08 -0800 (PST) Date: Thu, 10 Feb 2022 17:01:07 -0800 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 v3 11/12] lkdtm: Fix execute_[user]_location() Message-ID: <202202101700.985C29A@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BCC63A0002 X-Stat-Signature: o3gdia5k699yohbhpf3gzkni9bnpdnuy Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ayaQeq+i; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf25.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.182 as permitted sender) smtp.mailfrom=keescook@chromium.org X-HE-Tag: 1644541269-2852 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 Sun, Oct 17, 2021 at 02:38:24PM +0200, Christophe Leroy wrote: > 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. > > 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. > > Also fix the displayed addresses by dereferencing do_nothing() > function descriptor. > > Signed-off-by: Christophe Leroy This looks good. I might rename variables in the future (e.g. to avoid the churn from adding _text) but also, that does help keep it clear. :) Acked-by: Kees Cook -Kees > --- > drivers/misc/lkdtm/perms.c | 37 ++++++++++++++++++++++++++++--------- > 1 file changed, 28 insertions(+), 9 deletions(-) > > 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) > return; > } > > +static void *setup_function_descriptor(func_desc_t *fdesc, void *dst) > +{ > + if (!have_function_descriptors()) > + return dst; > + > + memcpy(fdesc, do_nothing, sizeof(*fdesc)); > + fdesc->addr = (unsigned long)dst; > + barrier(); > + > + return fdesc; > +} > + > static noinline void execute_location(void *dst, bool write) > { > - void (*func)(void) = dst; > + void (*func)(void); > + func_desc_t fdesc; > + void *do_nothing_text = dereference_function_descriptor(do_nothing); > > - pr_info("attempting ok execution at %px\n", do_nothing); > + pr_info("attempting ok execution at %px\n", do_nothing_text); > do_nothing(); > > if (write == CODE_WRITE) { > - memcpy(dst, do_nothing, EXEC_SIZE); > + memcpy(dst, do_nothing_text, EXEC_SIZE); > flush_icache_range((unsigned long)dst, > (unsigned long)dst + EXEC_SIZE); > } > - pr_info("attempting bad execution at %px\n", func); > + pr_info("attempting bad execution at %px\n", dst); > + func = setup_function_descriptor(&fdesc, dst); > func(); > pr_err("FAIL: func returned\n"); > } > @@ -66,16 +81,19 @@ static void execute_user_location(void *dst) > int copied; > > /* Intentionally crossing kernel/user memory boundary. */ > - void (*func)(void) = dst; > + void (*func)(void); > + func_desc_t fdesc; > + void *do_nothing_text = dereference_function_descriptor(do_nothing); > > - pr_info("attempting ok execution at %px\n", do_nothing); > + pr_info("attempting ok execution at %px\n", do_nothing_text); > do_nothing(); > > - copied = access_process_vm(current, (unsigned long)dst, do_nothing, > + copied = access_process_vm(current, (unsigned long)dst, do_nothing_text, > EXEC_SIZE, FOLL_WRITE); > if (copied < EXEC_SIZE) > return; > - pr_info("attempting bad execution at %px\n", func); > + pr_info("attempting bad execution at %px\n", dst); > + func = setup_function_descriptor(&fdesc, dst); > func(); > pr_err("FAIL: func returned\n"); > } > @@ -153,7 +171,8 @@ void lkdtm_EXEC_VMALLOC(void) > > void lkdtm_EXEC_RODATA(void) > { > - execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS); > + execute_location(dereference_function_descriptor(lkdtm_rodata_do_nothing), > + CODE_AS_IS); > } > > void lkdtm_EXEC_USERSPACE(void) > -- > 2.31.1 > -- Kees Cook