Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2599519pxb; Sun, 17 Oct 2021 20:07:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxiUPX/JJH8WjHaYIrVV+X4RbeZqy07/9ML4plDKZc449A3ey4riPpQ1ijy0o5HDYouzO6f X-Received: by 2002:a63:f145:: with SMTP id o5mr21358242pgk.273.1634526432717; Sun, 17 Oct 2021 20:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634526432; cv=none; d=google.com; s=arc-20160816; b=D1+8EocmaAkqjTxDi0UDORGwWBMBDcoog9bQKxti7+NrQTyel7CR62xxKqF+Bn154w LrPjc4ZcqcKoLAZrvtm++q5T86NqD+JoEx3fH73HNtIYxJNkEkx4E7EqW3MhYzGn0Szr ss5zMLHatw+vlK/UCpkelTw9x/Ey19DPAA8/nUO0SgUDXvAgKG34pZz2XsLbu79Wbx6u TgJUjNAZbvNa9XKTHsirFLFhWFW7Vf7OMMQSIHUwWuVGIBskxT4oTRq8NlMLJSWrEnBb iJP6aH0kwi11jLp7FrjT8YMeA97YEFMOTRRTuaAdsur5nq1lFokTqUxkXDwBrhaslFBQ CF7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=kKloKGIwGWmZY9+Er8BC6aPtcIV5M1fZmtHmnCWa8iU=; b=DIBcLJNWNqed6o0ZuqK3DWxe0s67h4eWwDLYB7+erYhEKP8s7wEw5S194+vVH+rcyP 4zo5rEm55SrYulWVuCEfnAHtajnEgKIFeH2N7PRpaTcDT2Z1QpwBxblK8P1sh2AbG2TR o6d9xSBPUxK9Jd3fgvbsCo7nVPTBBK6Jc+wS/urQtq03fikg0q7IOEECRXwGKLbGLTAU QJMX2SSymF35ehBj0PKS0TTeQYooNIam3tFlJ+Zu4HVvHFIqnuW9DOHVkn+d7t0cJxMG lFntTlC0USiWBH8SbMl4yNo5zJs5LDeVeEnkjl2n7Uzd+8Q9V2YjIPLuVmzCWX9pIlzF 88KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JeYYnn82; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j6si15040425pgg.211.2021.10.17.20.07.00; Sun, 17 Oct 2021 20:07:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JeYYnn82; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238499AbhJOVdn (ORCPT + 99 others); Fri, 15 Oct 2021 17:33:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238555AbhJOVdm (ORCPT ); Fri, 15 Oct 2021 17:33:42 -0400 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB0E6C061765 for ; Fri, 15 Oct 2021 14:31:35 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id c4so2664535pgv.11 for ; Fri, 15 Oct 2021 14:31:35 -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=kKloKGIwGWmZY9+Er8BC6aPtcIV5M1fZmtHmnCWa8iU=; b=JeYYnn823k8n2zSxtjokkfqoZ5ilS26HSi3+OKWmuzF1ap1aszmc7JLPZWsF4HCG0w 31EKzOJ0R9W8h1AKhQ/AK6BIP9nPFEBX8Fhn+ihNhFPlklATnjeMva6cxWbMA+SX6L+t CU/dKvCcEPU/V6JRuGbgX3x4m1PI5C82Th35I= 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=kKloKGIwGWmZY9+Er8BC6aPtcIV5M1fZmtHmnCWa8iU=; b=YzmhppzSz01dfuA5LLl2cJQvPQmY+qQD5IyzYOXvxC8XK9gRn+2jAcreYYUptjXkcb 7myZhp1H3JpprrgPNwWT8BEjKBYNiWTPCVkAYmgtKlWXwz28mVg1+yhndEuvcRIwNxX/ i3d6ZHyaMXwLChBqk2wjkT8bstDgJPDpTYRf6sLMfGL+KVERbfQhy/ctKnuTvbG0PPkK MyYbya8MnYMMNL3UFYH1RVEtllrXHYQnwHdHHks3eG9Mu64B881dCCmy+yBFsOen2/ho iqI2jFU+3KbSIg5xkSSy/WaAPRwNpBKyu/ja9fSo0/qBmu9k5oj7IZ2QFs72JET+DNZc WjZw== X-Gm-Message-State: AOAM531i7ajHBTYLYrW2kTmhZuEsfB3CdtR/Z6fdIKbxXaIwhxZ7bPim eud+eCXripmHaImd2S1DZNONmA== X-Received: by 2002:a05:6a00:992:b0:44d:8981:37f6 with SMTP id u18-20020a056a00099200b0044d898137f6mr9473913pfg.76.1634333495151; Fri, 15 Oct 2021 14:31:35 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id gf23sm6006874pjb.26.2021.10.15.14.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Oct 2021 14:31:34 -0700 (PDT) Date: Fri, 15 Oct 2021 14:31:33 -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 v2 12/13] lkdtm: Fix execute_[user]_location() Message-ID: <202110151428.187B1CF@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 14, 2021 at 07:50:01AM +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 | 25 +++++++++++++++++++++---- > include/asm-generic/sections.h | 5 +++++ > 2 files changed, 26 insertions(+), 4 deletions(-) > > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c > index 5266dc28df6e..96b3ebfcb8ed 100644 > --- a/drivers/misc/lkdtm/perms.c > +++ b/drivers/misc/lkdtm/perms.c > @@ -44,19 +44,32 @@ static noinline void do_overwritten(void) > return; > } > > +static void *setup_function_descriptor(func_desc_t *fdesc, void *dst) > +{ > + memcpy(fdesc, do_nothing, sizeof(*fdesc)); > + fdesc->addr = (unsigned long)dst; > + barrier(); > + > + return fdesc; > +} How about collapsing the "have_function_descriptors()" check into setup_function_descriptor()? static void *setup_function_descriptor(func_desc_t *fdesc, void *dst) { if (__is_defined(HAVE_FUNCTION_DESCRIPTORS)) { memcpy(fdesc, do_nothing, sizeof(*fdesc)); fdesc->addr = (unsigned long)dst; barrier(); return fdesc; } else { return dst; } } > + > static noinline void execute_location(void *dst, bool write) > { > void (*func)(void) = dst; > + 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); > + if (have_function_descriptors()) > + func = setup_function_descriptor(&fdesc, dst); > func(); > pr_err("FAIL: func returned\n"); > } > @@ -67,15 +80,19 @@ static void execute_user_location(void *dst) > > /* Intentionally crossing kernel/user memory boundary. */ > void (*func)(void) = dst; > + 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); > + if (have_function_descriptors()) > + func = setup_function_descriptor(&fdesc, dst); > func(); > pr_err("FAIL: func returned\n"); > } > diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h > index 76163883c6ff..d225318538bd 100644 > --- a/include/asm-generic/sections.h > +++ b/include/asm-generic/sections.h > @@ -70,6 +70,11 @@ typedef struct { > } func_desc_t; > #endif > > +static inline bool have_function_descriptors(void) > +{ > + return __is_defined(HAVE_FUNCTION_DESCRIPTORS); > +} > + > /* random extra sections (if any). Override > * in asm/sections.h */ > #ifndef arch_is_kernel_text This hunk seems like it should live in a separate patch. -- Kees Cook