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 2DACDC433F5 for ; Wed, 13 Oct 2021 07:16:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CF13B600D4 for ; Wed, 13 Oct 2021 07:16:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CF13B600D4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 71EF06B006C; Wed, 13 Oct 2021 03:16:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A8BF900002; Wed, 13 Oct 2021 03:16:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5490F6B0072; Wed, 13 Oct 2021 03:16:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0221.hostedemail.com [216.40.44.221]) by kanga.kvack.org (Postfix) with ESMTP id 44F506B006C for ; Wed, 13 Oct 2021 03:16:35 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 020551813B792 for ; Wed, 13 Oct 2021 07:16:35 +0000 (UTC) X-FDA: 78690556350.39.10EA098 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf02.hostedemail.com (Postfix) with ESMTP id AC48B7001A24 for ; Wed, 13 Oct 2021 07:16:34 +0000 (UTC) Received: by mail-pl1-f180.google.com with SMTP id l6so1175364plh.9 for ; Wed, 13 Oct 2021 00:16:34 -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=Qi2kcK7gbHZUycBDWjYwOBh1qqhZS2gpMfLgpt9Uq+g=; b=DBeVwPHI8FJhK4IF5VHgTTJLctjt6LXWmLThZ2yFnKb+4pyEOAe0Ui9MkqyggUzG0q 28MfwGk11htf2S4E1fmmF13OlhLumeXU3HW0grwDrGEkj60K3HN3BkQgDptITaIdv9Uy kua2TuyZ53AjYkbvnu6ABjlxXEF9G7ygD+r9s= 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=Qi2kcK7gbHZUycBDWjYwOBh1qqhZS2gpMfLgpt9Uq+g=; b=az+00J/kcQFZCzE4zYG9ie+aKVnls6tN5wf7adr2FoV2+rurye6f7HIklqxB/etBvE ipizqOK3c/rHGPw6LFaVSlt7/C1uOMcPFqiR4UojATpd6bamLP8hh5D9nYXSqnqwScIy 4IWbaRi4WGjg2lyRTzlaeo3LDNQPTN7fWUTdjqpzluee3weSxsRwrwXGjxKefnvPjxxz aRL2dRGS8IhYTD5fMosIIAeyfogaXV2yMw6brHRgeWw9lWuSPVGRP//7vrLWjD9ufjwK AQ/xafbCm5ThEM2Jn8rWW4ziwl2wYnGls8P0w2SaP3tguhKkzRC3tESbDN4NWKIXl/1q kYMQ== X-Gm-Message-State: AOAM530khyszd4PsR6bYr2LLycqhvs+YjQmeORBp/IDgMNlwhxGkZtx2 9mYTLb/upn4CsOzFf3IshcdO5w== X-Google-Smtp-Source: ABdhPJyAOXrKW60jwbhdhdic11/bHUj22V/uWz/NG4QXRJ4qpgyk4BVyCru7dvuvTxCLWVGzDuPokg== X-Received: by 2002:a17:90a:8807:: with SMTP id s7mr11433275pjn.226.1634109393567; Wed, 13 Oct 2021 00:16:33 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q14sm4820727pjm.17.2021.10.13.00.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Oct 2021 00:16:33 -0700 (PDT) Date: Wed, 13 Oct 2021 00:16:32 -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 10/10] lkdtm: Fix execute_[user]_location() Message-ID: <202110130012.4608FFD38E@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AC48B7001A24 X-Stat-Signature: 8cs9q43re94xcdjrtn85tfpoo73wgzwo Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=DBeVwPHI; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf02.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.180 as permitted sender) smtp.mailfrom=keescook@chromium.org X-HE-Tag: 1634109394-786000 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 Mon, Oct 11, 2021 at 05:25:37PM +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 > --- > drivers/misc/lkdtm/perms.c | 45 +++++++++++++++++++++++++++++++------- > 1 file changed, 37 insertions(+), 8 deletions(-) > > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c > index da16564e1ecd..1d03cd44c21d 100644 > --- a/drivers/misc/lkdtm/perms.c > +++ b/drivers/misc/lkdtm/perms.c > @@ -44,19 +44,42 @@ static noinline void do_overwritten(void) > return; > } > > +static void *setup_function_descriptor(funct_descr_t *fdesc, void *dst) > +{ > + int err; > + > + if (!__is_defined(HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR)) > + return dst; > + > + err = copy_from_kernel_nofault(fdesc, do_nothing, sizeof(*fdesc)); > + if (err < 0) > + return ERR_PTR(err); > + > + fdesc->addr = (unsigned long)dst; > + barrier(); > + > + return fdesc; > +} > + > static noinline void execute_location(void *dst, bool write) > { > - void (*func)(void) = dst; > + void (*func)(void); > + funct_descr_t fdesc; > + void *do_nothing_text = dereference_symbol_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); > + func = setup_function_descriptor(&fdesc, dst); > + if (IS_ERR(func)) > + return; I think this error case should complain with some details. :) Maybe: pr_err("FAIL: could not build function descriptor for %px\n", dst); > + > + pr_info("attempting bad execution at %px\n", dst); And then leave this pr_info() as it was, before the setup_function_descriptor() call. > func(); > pr_err("FAIL: func returned\n"); > } > @@ -66,16 +89,22 @@ static void execute_user_location(void *dst) > int copied; > > /* Intentionally crossing kernel/user memory boundary. */ > - void (*func)(void) = dst; > + void (*func)(void); > + funct_descr_t fdesc; > + void *do_nothing_text = dereference_symbol_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); > + func = setup_function_descriptor(&fdesc, dst); > + if (IS_ERR(func)) > + return; > + > + pr_info("attempting bad execution at %px\n", dst); Same here. > func(); > pr_err("FAIL: func returned\n"); > } > -- > 2.31.1 > -- Kees Cook