Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp703854ybh; Wed, 15 Jul 2020 12:53:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyA9ROJlsYAjSIEuBHSVUBj137vWL7JkuX3lkcbndyLcSr/1ycZbF2w/g6FtFZfZh06x5JL X-Received: by 2002:a17:907:20b4:: with SMTP id pw20mr556099ejb.225.1594842836293; Wed, 15 Jul 2020 12:53:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594842836; cv=none; d=google.com; s=arc-20160816; b=kNkXSm+8SWL0lU7dsUKSHH04rnjZAtQgJPKoMzO1YB/K4ZYp5USB2QCjw/J1c52U/h A/tn/zk4vkZ2Wh5sesHqAH9AliCEOkGf5BkKf4J2ACcYhnCOUm6a5g71hLydGTAu5aEl fe7x1sVJ159GRDXH6CRftAT5zL5+oUAXMNUdBH7Ob4JgLgBNJYzHPRm0wgrdQHjdRN3j TKcSJq1wZwd0ZG0ZOOZZAeD+VDK1UPxz4umX/CI0qBZUfnKPbXgNbvTzqWRw5ZMGeGYJ 4/gZc8F7BSGVSztpCMT8gDVfQNAy75yDAyMTIbI8e9qI4usCe0IOfefBadXCrv5PDlI1 5QEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=9JohOJfniBpoVsObcPj1lUWx7GOTVdGb79jGgvP71lo=; b=EKNp3qPRK2c6MjsGCO0X5kNw32LS1m5/njochk6V+G/l5w7qZ4WsF3Uq+GGkW7mPmM CK7PGgzdOA+IJpZDb78K44hvKH5jQ1AgmIKPKt5OOXEJtkodQCG7GcBA/GD5TfmgK2kS 39ZkFKkcC/nc+zfrAq87RXnabrNICFbRF9WqQWAsUidLW9hAKNWIrh0rFCFTAq4+SvBs 6fki10i3goSW1BAUvH2bOYGqo4r+L9urDS4y3U4JJUa3A6TGmBns5grd8lK9uM06HMwh ZRq1dOGmwhvmx6UTJVVyHrdPATFtPYsf86dlbeY1UZvhxJT8GUelC1vXKNE6PxeC4mmE kWsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="jbY1Z/ME"; 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 g16si2074475ejf.492.2020.07.15.12.53.33; Wed, 15 Jul 2020 12:53:56 -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="jbY1Z/ME"; 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 S1726986AbgGOTv0 (ORCPT + 99 others); Wed, 15 Jul 2020 15:51:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726864AbgGOTvZ (ORCPT ); Wed, 15 Jul 2020 15:51:25 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86467C08C5CE for ; Wed, 15 Jul 2020 12:51:25 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id a14so2550442pfi.2 for ; Wed, 15 Jul 2020 12:51:25 -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=9JohOJfniBpoVsObcPj1lUWx7GOTVdGb79jGgvP71lo=; b=jbY1Z/MEEo7lZDPPFvdqaESsTErbULuzQ2o7XOJQEu3R7wPlnDYtTP0kbWlMVoJQ1i /k1apyD0iOzOuWoUP9wFyrQE7izrWG9QaJbvN1bsKY3OdWcAvYVZ8ILLhC9aNjofvZv/ iS1hlqyDWhpIZ6w8peHSwSjNTfMnQ/gI9hMN4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=9JohOJfniBpoVsObcPj1lUWx7GOTVdGb79jGgvP71lo=; b=sKU/LpkLyI2+n3Go7ZV7v6B+iqtaATbdMU/5hF5jsb7C1HlTOspsakg6vnwzwGvHkx RT/x/se1RS1FZHJscqVNSkD7CTEoXW/1s76fYwP5Me7O7J9/dgZvHnN4wbpaYhu4li/h sti5jOyFv5JbYTy6Hjvlc3HhYZDyKJy3ljSlPBed40HE+FCvSlDeivM6lPJEHRPXOLXg Wt56p6p+KwdD+sHAmfSIKiM+Iy/Ui950Uz6IkcDLqcJoOAW/EXdINMH0yBX39lf3u4ye R1azCvyTzVcR0VdXEhztAuazZ2ngObrYgWpbCPSdmEG/Vtf90oF0CWtI0SYjEYCO/IC7 w8yw== X-Gm-Message-State: AOAM531phVgsU1LGmvcvvuX1rjgEALZ/ldX6mzTXS4c3DQ5ZSeDdjPFt 95xr+LM8ZeAWQHXQuv70Y7xiYA== X-Received: by 2002:a62:7790:: with SMTP id s138mr699734pfc.65.1594842685051; Wed, 15 Jul 2020 12:51:25 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 16sm2794211pjb.48.2020.07.15.12.51.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2020 12:51:24 -0700 (PDT) Date: Wed, 15 Jul 2020 12:51:23 -0700 From: Kees Cook To: Mike Rapoport Cc: Jarkko Sakkinen , linux-kernel@vger.kernel.org, x86@vger.kernel.org, Andi Kleen , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Masami Hiramatsu , Jessica Yu , Andrew Morton , "Aneesh Kumar K.V" , Will Deacon , Sami Tolvanen , Alexandre Ghiti , Masahiro Yamada , Peter Collingbourne , Frederic Weisbecker , Krzysztof Kozlowski , Arnd Bergmann , Stephen Boyd , Andy Lutomirski , Josh Poimboeuf , Miroslav Benes , Babu Moger , Omar Sandoval , Nayna Jain , Marco Elver , Brian Gerst , Jiri Kosina , Joe Lawrence Subject: Re: [PATCH v3 1/3] kprobes: Add text_alloc() and text_free() Message-ID: <202007151250.DE6C4B417@keescook> References: <20200714223239.1543716-1-jarkko.sakkinen@linux.intel.com> <20200714223239.1543716-2-jarkko.sakkinen@linux.intel.com> <202007151232.0DA220B2@keescook> <20200715194933.GC1166045@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200715194933.GC1166045@linux.ibm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 15, 2020 at 10:49:33PM +0300, Mike Rapoport wrote: > On Wed, Jul 15, 2020 at 12:36:02PM -0700, Kees Cook wrote: > > On Wed, Jul 15, 2020 at 01:32:27AM +0300, Jarkko Sakkinen wrote: > > > Introduce new API for allocating space for code generaed at run-time > > > leveraging from module_alloc() and module_memfree() code. Use this to > > > perform memory allocations in the kprobes code in order to loose the > > > bound between kprobes and modules subsystem. > > > > > > Initially, use this API only with arch/x86 and define a new config > > > flag CONFIG_ARCH_HAS_TEXT_ALLOC to promote the availability of the > > > new API. > > > [...] > > > diff --git a/include/linux/text.h b/include/linux/text.h > > > new file mode 100644 > > > index 000000000000..a27d4a42ecda > > > --- /dev/null > > > +++ b/include/linux/text.h > > > @@ -0,0 +1,17 @@ > > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > > + > > > +#ifndef _LINUX_TEXT_H > > > +#define _LINUX_TEXT_H > > > + > > > +/* > > > + * An allocator used for allocating modules, core sections and init sections. > > > + * Returns NULL on failure. > > > + */ > > > +void *text_alloc(unsigned long size); > > > + > > > +/* > > > + * Free memory returned from text_alloc(). > > > + */ > > > +void text_free(void *region); > > > + > > > +#endif /* _LINUX_TEXT_H */ > > > diff --git a/kernel/kprobes.c b/kernel/kprobes.c > > > index 2e97febeef77..fa7687eb0c0e 100644 > > > --- a/kernel/kprobes.c > > > +++ b/kernel/kprobes.c > > > @@ -35,6 +35,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > > > > #include > > > #include > > > @@ -111,12 +112,20 @@ enum kprobe_slot_state { > > > > > > void __weak *alloc_insn_page(void) > > > { > > > +#ifdef CONFIG_ARCH_HAS_TEXT_ALLOC > > > + return text_alloc(PAGE_SIZE); > > > +#else > > > return module_alloc(PAGE_SIZE); > > > +#endif > > > > This seems like it shouldn't be needed. I think text_alloc() should > > always be visible. In the ARCH_HAS... case it will call the arch > > implementation, and without it should just call module_alloc(): > > > > For example: > > void *text_alloc(unsigned long size) > > { > > #ifdef CONFIG_ARCH_HAS_TEXT_ALLOC > > return arch_text_alloc(size); > > #else > > return module_alloc(size); > > #endif > > } > > This inverts the dependcy chain, IMHO, module_alloc() is a special case > of text_alloc() and not vice versa. Okay, sure. That's fine too. Whatever the case is, I want to make sure we keep the KASLR offset though. I don't think that should be unique to the modules logic. -- Kees Cook