From patchwork Thu Mar 23 02:55:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eddie Kovsky X-Patchwork-Id: 9640395 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 239FB6020B for ; Thu, 23 Mar 2017 02:56:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B31F2847F for ; Thu, 23 Mar 2017 02:56:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E55C28491; Thu, 23 Mar 2017 02:56:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id A5C3C2847F for ; Thu, 23 Mar 2017 02:56:35 +0000 (UTC) Received: (qmail 28518 invoked by uid 550); 23 Mar 2017 02:56:34 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 28500 invoked from network); 23 Mar 2017 02:56:33 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=edkovsky.org; s=mail; t=1490237781; bh=ssD7xKpOJvBXYO22y1MErzDich+GRcwvmj7wMXalKaM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WpeXzvyaH6pvTHk/kUAQnd0E46IaG8x/MQBtQ4eYryfaSQZGorBCh+eNcW/Z97pa/ WDsEl+FplWoqUTG7uFxjzq+QHPjvl49W60i1H6HOHGT4vkK3gIDtGbU88PsrD9Xnra 22v8ElZy2vJ0i7sQzE6Rc2a+f0NDSIMYu97bq/jE= From: Eddie Kovsky To: jeyu@redhat.com, rusty@rustcorp.com.au, keescook@chromium.org Cc: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Wed, 22 Mar 2017 20:55:48 -0600 Message-Id: <20170323025549.19588-2-ewk@edkovsky.org> X-Mailer: git-send-email 2.12.1 In-Reply-To: <20170323025549.19588-1-ewk@edkovsky.org> References: <20170323025549.19588-1-ewk@edkovsky.org> X-Virus-Scanned: clamav-milter 0.99.2 at olympus X-Virus-Status: Clean Subject: [kernel-hardening] [PATCH v3 1/2] module: verify address is read-only X-Virus-Scanned: ClamAV using ClamSMTP Implement a mechanism to check if a module's address is in the rodata or ro_after_init sections. It mimics the exsiting functions that test if an address is inside a module's text section. Functions that take a module as an argument will be able to verify that the module is in a read-only section. Signed-off-by: Eddie Kovsky Reviewed-by: Kees Cook --- Changes in v3: - Change function name is_module_ro_address() to is_module_rodata_address(). - Improve comments on is_module_rodata_address(). - Add a !CONFIG_MODULES stub for is_module_rodata_address. - Correct and simplify the check for the read-only memory regions in the module address. include/linux/module.h | 12 ++++++++++++ kernel/module.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) -- 2.12.0 diff --git a/include/linux/module.h b/include/linux/module.h index 9ad68561d8c2..66b7fd321334 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -492,7 +492,9 @@ static inline int module_is_live(struct module *mod) struct module *__module_text_address(unsigned long addr); struct module *__module_address(unsigned long addr); +struct module *__module_ro_address(unsigned long addr); bool is_module_address(unsigned long addr); +bool is_module_rodata_address(unsigned long addr); bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr); bool is_module_percpu_address(unsigned long addr); bool is_module_text_address(unsigned long addr); @@ -646,6 +648,16 @@ static inline struct module *__module_address(unsigned long addr) return NULL; } +static inline struct module *__module_ro_address(unsigned long addr) +{ + return NULL; +} + +static inline bool is_module_rodata_address(unsigned long addr) +{ + return false; +} + static inline struct module *__module_text_address(unsigned long addr) { return NULL; diff --git a/kernel/module.c b/kernel/module.c index 8ffcd29a4245..99ada1257aaa 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -4292,6 +4292,59 @@ struct module *__module_text_address(unsigned long addr) } EXPORT_SYMBOL_GPL(__module_text_address); +/** + * is_module_rodata_address - is this address inside read-only module code? + * @addr: the address to check. + * + */ +bool is_module_rodata_address(unsigned long addr) +{ + bool ret; + + preempt_disable(); + ret = __module_ro_address(addr) != NULL; + preempt_enable(); + + return ret; +} + +/* + * __module_ro_address - get the module whose rodata/ro_after_init sections + * contain the given address. + * @addr: the address. + * + * Must be called with preempt disabled or module mutex held so that + * module doesn't get freed during this. + */ +struct module *__module_ro_address(unsigned long addr) +{ + struct module *mod = __module_address(addr); + + void *init_base = mod->init_layout.base; + unsigned int init_text_size = mod->init_layout.text_size; + unsigned int init_ro_after_init_size = mod->init_layout.ro_after_init_size; + + void *core_base = mod->core_layout.base; + unsigned int core_text_size = mod->core_layout.text_size; + unsigned int core_ro_after_init_size = mod->core_layout.ro_after_init_size; + + /* + * Make sure module is within the read-only section. + * range(base + text_size, base + ro_after_init_size) + * encompasses both the rodata and ro_after_init regions. + * See comment above frob_text() for the layout diagram. + */ + if (mod) { + if (!within(addr, init_base + init_text_size, + init_ro_after_init_size - init_text_size) + && !within(addr, core_base + core_text_size, + core_ro_after_init_size - core_text_size)) + mod = NULL; + } + return mod; +} +EXPORT_SYMBOL_GPL(__module_ro_address); + /* Don't grab lock, we're oopsing. */ void print_modules(void) {