Received: by 2002:aa6:c781:0:b0:139:2837:6657 with SMTP id u1csp1560555lkq; Fri, 3 Sep 2021 09:43:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQuS/qlqt3S9vxkSeGAaFc+Brq3GzmsPNQa2pZ6YbXOTcVGY6ldY0DZRSBGZupN+1UIGE+ X-Received: by 2002:a50:fe8b:: with SMTP id d11mr5032213edt.330.1630687380046; Fri, 03 Sep 2021 09:43:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630687380; cv=none; d=google.com; s=arc-20160816; b=qAG3KeB9IOsuexWQ3j/A+uQAN3K6cBCXZGbgZ8+QdL8LVMwNUNdR+se1YlhXx4xzom vXE9hYC1VvO8Z35MRi3pzsf12ffYj8bVAoJCSQL5Klipx+KxV45JEDk2a3RSfw9WseJA kJqgUK3NjCo9sajlkSxrAkh5hZnJuyAYaBkyfTJHto/FKkGg9My9YiVo6kvivZ0M1g5r lg/5jd89lv2gklrVudcxNAqPrN/pP6vW4NLJMnjROJCeNoeN/gZi7O7YcWTXd6Yc++jb A6ljPUvxgx4isaZyY5tsfBC0Vbm6vk8L+LX1rPzVSLYvlRGCs9V6aOrAPHlbSBbHauey r8Eg== 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=FfOt3SEZ0/w/EILHgIhc/sI685lMnBLBscfmUVoWBxM=; b=PMJBY+BKl2ESJg3t6opn9eUFA8Lm12tv5x04FD4WsIu3bALIWWxZR/w8OQayL0AuVu JgFW1OgmHe2dqajDPcvsgh9GBfjxlFDHwFGDm5q2curtWApI/bOdw4s0tlkevgz6XOjm lTm8dI3aJ6DvQkrLzhH41lP/IuPKDJH6z2o+scF/gY70VTuLzjmwfGNhr4fKGg5St1n4 Z/LNRsFKSyBoWBrFtFjh+WVyL5WfIlFEC0YQ+GELq3h4g94Qx0wfMuaAOP32Ig4fl/TG 0EmZSk2Bykf4LBEo3g6EatcLaZ7zmUacGocu50Gq4oRE5UYppxXHunz0IYV02HARyUke aQvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=U3zD7zTV; 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 y26si6519980edl.469.2021.09.03.09.42.35; Fri, 03 Sep 2021 09:43:00 -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=U3zD7zTV; 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 S1350093AbhICQjp (ORCPT + 99 others); Fri, 3 Sep 2021 12:39:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350013AbhICQjo (ORCPT ); Fri, 3 Sep 2021 12:39:44 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2D77C061764 for ; Fri, 3 Sep 2021 09:38:44 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id bg1so3609704plb.13 for ; Fri, 03 Sep 2021 09:38:44 -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=FfOt3SEZ0/w/EILHgIhc/sI685lMnBLBscfmUVoWBxM=; b=U3zD7zTVZnXyaVHVcepE2ZnBhIVi+C+briI5/GPmfUYXdkLyFmxljAvwjJ2Y/T6FN7 JEKgVwTx7TIQEk8iONrflQBgQEuu9wPi6w1QPNdCALwa6JGDNl9ILIHqvd26pArQ3XuF FBhHN4XJSFB6INWrh3p2QndF/YNVVDAe55YSQ= 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=FfOt3SEZ0/w/EILHgIhc/sI685lMnBLBscfmUVoWBxM=; b=fYdDxTxqrUkIUfUmhH0oJwJQ6GlCDnOTm37CZhYAikWoAXHvWA4N0vqrATqVMo+BSN OW5naNOwGA3SxPmJ3e9Z4HejUGKqhPNJD/NCGmykr5UDVhE+bNyyFomVU6fa9crb2HkT 5vzD52RHcXSrkZBqxi2YFvIXJff4iWFGgnCKO1+9Fpyfge+2aHildY6p1Y/0MQTSib5u dai6RvonF9kRk9E+paMOQYwR+bqv1OKvDtLay7XEAWE/cizR2Po7jfTQRUS5hYNYroHl NQ2B9vFXZRYQza8mME5xKBVMm6HeXFPgC8muIzFosu7raUs6RMkC+7byp8///d5O7rbX v2vw== X-Gm-Message-State: AOAM531QMzET6LRuUMdEMv5KsWviLfhMAeUnumbFMSdRtGYnQKlddzio VzZp1dcLvl+e7fm7jwEnXTepdA== X-Received: by 2002:a17:90b:2212:: with SMTP id kw18mr1653921pjb.59.1630687124160; Fri, 03 Sep 2021 09:38:44 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s31sm5894049pfw.23.2021.09.03.09.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 09:38:43 -0700 (PDT) Date: Fri, 3 Sep 2021 09:38:42 -0700 From: Kees Cook To: Josh Poimboeuf Cc: Arnd Bergmann , Jessica Yu , Peter Zijlstra , linux-arch@vger.kernel.org, Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Alexander Egorenkov , Sven Schnelle , Ilya Leoshkevich , "Steven Rostedt (VMware)" , Ingo Molnar , Sami Tolvanen , linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 3/4] module: Use a list of strings for ro_after_init sections Message-ID: <202109030932.1358C4093@keescook> References: <20210901233757.2571878-1-keescook@chromium.org> <20210901233757.2571878-4-keescook@chromium.org> <20210903064951.to4dhiu7zua7s6dn@treble> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210903064951.to4dhiu7zua7s6dn@treble> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 02, 2021 at 11:49:51PM -0700, Josh Poimboeuf wrote: > On Wed, Sep 01, 2021 at 04:37:56PM -0700, Kees Cook wrote: > > Instead of open-coding the section names, use a list for the sections that > > need to be marked read-only after init. Unfortunately, it seems we can't > > do normal section merging with scripts/module.lds.S as ld.bfd doesn't > > correctly update symbol tables. For more details, see commit 6a3193cdd5e5 > > ("kbuild: lto: Merge module sections if and only if CONFIG_LTO_CLANG > > is enabled"). > > I'm missing what this has to do with section merging. Can you connect > the dots here, i.e. what sections would we want to merge and how would > that help here? Right, sorry, if ld.bfd didn't have this issue, we could use section merging in the module.lds.S file the way we do in vmlinux.lds: #ifndef RO_AFTER_INIT_DATA #define RO_AFTER_INIT_DATA \ . = ALIGN(8); \ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ STATIC_CALL_DATA \ __end_ro_after_init = .; #endif ... . = ALIGN((align)); \ .rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \ __start_rodata = .; \ *(.rodata) *(.rodata.*) \ SCHED_DATA \ RO_AFTER_INIT_DATA /* Read only after init */ \ . = ALIGN(8); \ __start___tracepoints_ptrs = .; \ KEEP(*(__tracepoints_ptrs)) /* Tracepoints: pointer array */ \ __stop___tracepoints_ptrs = .; \ *(__tracepoints_strings)/* Tracepoints: strings */ \ } \ Then jump_table and static_call sections could be collected into a new section, as the module loader would only need to look for that single name. > Instead of hard-coding section names in module.c, I'm wondering if we > can do something like the following to set SHF_RO_AFTER_INIT when first > creating the sections. Completely untested... > > > diff --git a/arch/x86/include/asm/jump_label.h b/arch/x86/include/asm/jump_label.h > index 0449b125d27f..d4ff34c6199c 100644 > --- a/arch/x86/include/asm/jump_label.h > +++ b/arch/x86/include/asm/jump_label.h > @@ -13,7 +13,7 @@ > #include > > #define JUMP_TABLE_ENTRY \ > - ".pushsection __jump_table, \"aw\" \n\t" \ > + ".pushsection __jump_table, \"0x00200003\" \n\t"\ > _ASM_ALIGN "\n\t" \ > ".long 1b - . \n\t" \ > ".long %l[l_yes] - . \n\t" \ > diff --git a/kernel/module.c b/kernel/module.c > index 40ec9a030eec..1dda33c9ae49 100644 > --- a/kernel/module.c > +++ b/kernel/module.c > @@ -3549,15 +3549,6 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) > * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set. > */ > ndx = find_sec(info, ".data..ro_after_init"); > - if (ndx) > - info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT; > - /* > - * Mark the __jump_table section as ro_after_init as well: these data > - * structures are never modified, with the exception of entries that > - * refer to code in the __init section, which are annotated as such > - * at module load time. > - */ > - ndx = find_sec(info, "__jump_table"); > if (ndx) > info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT; > > diff --git a/tools/objtool/check.c b/tools/objtool/check.c > index e5947fbb9e7a..b25ca38179ea 100644 > --- a/tools/objtool/check.c > +++ b/tools/objtool/check.c > @@ -20,6 +20,9 @@ > #include > #include > > +/* cribbed from include/uapi/linux/elf.h */ > +#define SHF_RO_AFTER_INIT 0x00200000 > + > struct alternative { > struct list_head list; > struct instruction *insn; > @@ -466,7 +469,8 @@ static int create_static_call_sections(struct objtool_file *file) > list_for_each_entry(insn, &file->static_call_list, call_node) > idx++; > > - sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, > + sec = elf_create_section(file->elf, ".static_call_sites", > + SHF_WRITE | SHF_RO_AFTER_INIT, > sizeof(struct static_call_site), idx); > if (!sec) > return -1; Interesting! I got the impression from the module code that this wasn't possible since it'd be exposing an internal set of flags to the external linker, and would break the vmlinux section merging (since it _is_ supposed to live in the .rodata section ultimately). The modules handle permissions slightly differently (i.e. more exact temporal), than the kernel though. (Most of the architecture's vmlinux logic starts with everything writable, and only does the read-only-ness after __init, though I think s390 does it "correctly" and as such has a separate area for the ro-after-init section.) -- Kees Cook