From patchwork Sat Feb 18 05:58:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eddie Kovsky X-Patchwork-Id: 9580995 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 144E3604A4 for ; Sat, 18 Feb 2017 05:59:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04A632878D for ; Sat, 18 Feb 2017 05:59:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC76528793; Sat, 18 Feb 2017 05:59:43 +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 28E7F2878D for ; Sat, 18 Feb 2017 05:59:42 +0000 (UTC) Received: (qmail 5932 invoked by uid 550); 18 Feb 2017 05:59:41 -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 5912 invoked from network); 18 Feb 2017 05:59:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=edkovsky.org; s=mail; t=1487397569; bh=+qZj5VCfyjyXNjsorua3pSng79nQeTONSXUOzEpDjrk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qVs8Ubfu6QRocN3wj3Fo/zHmfzbe+wP1yd2XjRHN34NjH0GbMnp/aaEVwzwWd6I6Q S3RfpDDYEj6Rg5TcnCP0z/YpuIZPBDEdby8eh5/+v0kgtSIgqUA7dTnRVXoRpmIlbe jcfMjNW7kxuExcCc8Y4zdKqy6g7y+NZtej3tSgdk= From: Eddie Kovsky To: jeyu@redhat.com, rusty@rustcorp.com.au, keescook@chromium.org, kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com Cc: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Fri, 17 Feb 2017 22:58:42 -0700 Message-Id: <20170218055844.1457-2-ewk@edkovsky.org> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170218055844.1457-1-ewk@edkovsky.org> References: <20170218055844.1457-1-ewk@edkovsky.org> X-Virus-Scanned: clamav-milter 0.99.2 at olympus X-Virus-Status: Clean Subject: [kernel-hardening] [PATCH v2 1/3] 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. Signed-off-by: Eddie Kovsky --- include/linux/module.h | 7 +++++++ kernel/module.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) -- 2.11.1 diff --git a/include/linux/module.h b/include/linux/module.h index 0297c5cd7cdf..1608d3570ee2 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_ro_address(unsigned long addr); bool is_module_percpu_address(unsigned long addr); bool is_module_text_address(unsigned long addr); @@ -645,6 +647,11 @@ 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 struct module *__module_text_address(unsigned long addr) { return NULL; diff --git a/kernel/module.c b/kernel/module.c index 7eba6dea4f41..298cfe4645b1 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -4275,6 +4275,50 @@ struct module *__module_text_address(unsigned long addr) } EXPORT_SYMBOL_GPL(__module_text_address); +/** + * is_module_text_ro_address - is this address inside read-only module code? + * @addr: the address to check. + * + */ +bool is_module_ro_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 code contains a read-only 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); + + if (mod) { + /* Make sure it's within the read-only section. */ + if (!within(addr, mod->init_layout.base, + mod->init_layout.ro_size) + && !within(addr, mod->core_layout.base, + mod->core_layout.ro_size)) + mod = NULL; + if (!within(addr, mod->init_layout.base, + mod->init_layout.ro_after_init_size) + && !within(addr, mod->core_layout.base, + mod->core_layout.ro_after_init_size)) + mod = NULL; + } + return mod; +} +EXPORT_SYMBOL_GPL(__module_ro_address); + /* Don't grab lock, we're oopsing. */ void print_modules(void) {