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 X-Spam-Level: X-Spam-Status: No, score=-6.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_NEOMUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B664AC28CC0 for ; Thu, 30 May 2019 13:54:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 889C4259CD for ; Thu, 30 May 2019 13:54:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726879AbfE3NyR (ORCPT ); Thu, 30 May 2019 09:54:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:40772 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725870AbfE3NyR (ORCPT ); Thu, 30 May 2019 09:54:17 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id F04F9ADD9; Thu, 30 May 2019 13:54:15 +0000 (UTC) Date: Thu, 30 May 2019 15:54:14 +0200 From: Petr Mladek To: Josh Poimboeuf Cc: Steven Rostedt , Jiri Kosina , Miroslav Benes , Jessica Yu , Joe Lawrence , linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, Johannes Erdfelt , Ingo Molnar Subject: Re: [PATCH] livepatch: Fix ftrace module text permissions race Message-ID: <20190530135414.taftuprranwtowry@pathway.suse.cz> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170912 (1.9.0) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed 2019-05-29 14:02:24, Josh Poimboeuf wrote: > The above panic occurs when loading two modules at the same time with > ftrace enabled, where at least one of the modules is a livepatch module: > > CPU0 CPU1 > klp_enable_patch() > klp_init_object_loaded() > module_disable_ro() > ftrace_module_enable() > ftrace_arch_code_modify_post_process() > set_all_modules_text_ro() > klp_write_object_relocations() > apply_relocate_add() > *patches read-only code* - BOOM This patch looks fine and fixes the race: Reviewed-by: Petr Mladek That said, the semantic of text_mutex is a bit unclear: + It serializes RO/RW setting but not NX + Nothing prevents manipulation of the access rights by external code before the module is ready-enough. I mean before the sections are set RO by the module loader itself. Most sections are ready in MODULE_STATE_COMMING state. Only ro_after_init sections need to stay RW longer, see my question below. > diff --git a/kernel/module.c b/kernel/module.c > index 6e6712b3aaf5..3c056b56aefa 100644 > --- a/kernel/module.c > +++ b/kernel/module.c > @@ -3519,7 +3534,7 @@ static noinline int do_init_module(struct module *mod) > /* Switch to core kallsyms now init is done: kallsyms may be walking! */ > rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms); > #endif > - module_enable_ro(mod, true); > + __module_enable_ro(mod, true); The "true" parameter causes that also ro_after_init section is set read only. What is the purpose of this section, please? I ask because module_enable_ro(mod, true) can be called earlier from klp_init_object_loaded() from do_one_initcall(). For example, it could some MODULE_STATE_LIVE notifier when it requires write access to ro_after_init section. Anyway, the above is a separate problem. This patch looks fine for the original problem. Best Regards, Petr