Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp235179pxb; Wed, 11 Nov 2020 02:15:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJxC/y/zrBjA9F3apq9KjGfyJAVp7dDjhzCCxQTpDA6m5MjhlWFYWs02P13ON4WFICcw5sCJ X-Received: by 2002:a17:906:2ddb:: with SMTP id h27mr24156245eji.213.1605089714293; Wed, 11 Nov 2020 02:15:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605089714; cv=none; d=google.com; s=arc-20160816; b=Mjjt0TX3CeNRQspr3q6FnXvL7y+pwWl2CH7RnvY9sFQnzW6HjLH6Ri6TpV6L3IDEV0 2T1i4yXsdZQkCk/GDICa9N9lIBazEZRy6QqQzPBOa2V5kGbKHU+3499LrK4ZjQ0bROcu yWQgfWDNA4PyFWUSOweDClB9GbDOO99WHTCdA+YGT8c86owFbtJUz7iWW3Ce+JScvRWf txV4cEvhhuoKqrBQpY1kUAq5iABKXYkdHBpFeaxtUBUOlwZ3K6E7+eCG7EA/d9EChRiI 35AV0BfiRFu7Kc2U2as0NrHMeoXTi1PWCL5qYGDKAWyVaTk3y4OmbZzvGI1yN08p7Nre Cy7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=5HMvbgr+T4p3gpTrWfskz5T5BfYrkalniGeXrQQR+ek=; b=yNceur8FWKSagKcnUwkjKJBaVg8lzNWCO57o1vQTjZl0SYdvnMWJjb7IIqlmI1W0qT iugaj63wDshF5NxPiYfKdZ2+b0miz52d1OiVHW6yjfiVMnhjYx0EzEBBlWuQHt0pvrOc ko+/CG0S9ulK4N+TCw4lSa6nz/RuCohHuuT4Nn9CC9V2VlvW7Yl6sr97dGfNE4Te3iUy j1WFew6ZYJHQrv0pLUT7KoIIvBfmJWpY4g7K6FohiADl8Yri7VerjhyQqqq27kMZZbXN k2jlEtg3spi5tVGVhAHSw4IyWxFlVKqkRfnt7dJwTfh3QFQrXHXRHskrqzGC1QRCLVWm Yf1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dsoqw4Gt; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bi26si1171975edb.553.2020.11.11.02.14.49; Wed, 11 Nov 2020 02:15:14 -0800 (PST) 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=@kernel.org header.s=default header.b=dsoqw4Gt; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727332AbgKKKNX (ORCPT + 99 others); Wed, 11 Nov 2020 05:13:23 -0500 Received: from mail.kernel.org ([198.145.29.99]:45770 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726725AbgKKKNX (ORCPT ); Wed, 11 Nov 2020 05:13:23 -0500 Received: from linux-8ccs (p57a236d4.dip0.t-ipconnect.de [87.162.54.212]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3B33920759; Wed, 11 Nov 2020 10:13:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605089602; bh=xxLTRoJd+SPxyQexcJlzZu0LoiFHq0I4V40FKZ6INoo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dsoqw4Gt+Z0PcB9erzVCdthLSJMuysR33Xo6sd8U16TU2TxIS8AKN65N3en8kZbzD WOG4gIg1vYYVy17PriOWMQMoiI9BTNqAbtjVqRVLdugO+fa90PIT28A9XLkxdB0VXJ 2c+cVdtJ7cHWZi0sz8dvl7udMo5qTvLqAFO834uY= Date: Wed, 11 Nov 2020 11:13:17 +0100 From: Jessica Yu To: Andrii Nakryiko Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, ast@fb.com, daniel@iogearbox.net, kernel-team@fb.com, linux-kernel@vger.kernel.org, rafael@kernel.org, Arnaldo Carvalho de Melo , Greg Kroah-Hartman Subject: Re: [PATCH v4 bpf-next 4/5] bpf: load and verify kernel module BTFs Message-ID: <20201111101316.GA5304@linux-8ccs> References: <20201110011932.3201430-1-andrii@kernel.org> <20201110011932.3201430-5-andrii@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20201110011932.3201430-5-andrii@kernel.org> X-OS: Linux linux-8ccs 4.12.14-lp150.12.61-default x86_64 User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +++ Andrii Nakryiko [09/11/20 17:19 -0800]: [snipped] >diff --git a/kernel/module.c b/kernel/module.c >index a4fa44a652a7..f2996b02ab2e 100644 >--- a/kernel/module.c >+++ b/kernel/module.c >@@ -380,6 +380,35 @@ static void *section_objs(const struct load_info *info, > return (void *)info->sechdrs[sec].sh_addr; > } > >+/* Find a module section: 0 means not found. Ignores SHF_ALLOC flag. */ >+static unsigned int find_any_sec(const struct load_info *info, const char *name) >+{ >+ unsigned int i; >+ >+ for (i = 1; i < info->hdr->e_shnum; i++) { >+ Elf_Shdr *shdr = &info->sechdrs[i]; >+ if (strcmp(info->secstrings + shdr->sh_name, name) == 0) >+ return i; >+ } >+ return 0; >+} >+ >+/* >+ * Find a module section, or NULL. Fill in number of "objects" in section. >+ * Ignores SHF_ALLOC flag. >+ */ >+static __maybe_unused void *any_section_objs(const struct load_info *info, >+ const char *name, >+ size_t object_size, >+ unsigned int *num) >+{ >+ unsigned int sec = find_any_sec(info, name); >+ >+ /* Section 0 has sh_addr 0 and sh_size 0. */ >+ *num = info->sechdrs[sec].sh_size / object_size; >+ return (void *)info->sechdrs[sec].sh_addr; >+} >+ Hm, I see this patchset has already been applied to bpf-next, but I guess that doesn't preclude any follow-up patches :-) I am not a huge fan of the code duplication here, and also the fact that they're only called in one place. any_section_objs() and find_any_sec() are pretty much identical to section_objs() and find_sec(), other than the fact the former drops the SHF_ALLOC check. Moreover, since it appears that the ".BTF" section is not marked SHF_ALLOC, I think this will leave mod->btf_data as a dangling pointer after the module is done loading and the module's load_info has been deallocated, since SHF_ALLOC sections are not allocated nor copied to the module's final location in memory. Why not simply mark the ".BTF" section in the module SHF_ALLOC? We already do some sh_flags rewriting in rewrite_section_headers(). Then the module loader knows to keep the section in memory and you can use section_objs(). And since the .BTF section stays in module memory, that might save you the memcpy() to btf->data in btf_parse_module() (unless that is still needed for some reason). Thanks, Jessica > /* Provided by the linker */ > extern const struct kernel_symbol __start___ksymtab[]; > extern const struct kernel_symbol __stop___ksymtab[]; >@@ -3250,6 +3279,9 @@ static int find_module_sections(struct module *mod, struct load_info *info) > sizeof(*mod->bpf_raw_events), > &mod->num_bpf_raw_events); > #endif >+#ifdef CONFIG_DEBUG_INFO_BTF_MODULES >+ mod->btf_data = any_section_objs(info, ".BTF", 1, &mod->btf_data_size); >+#endif > #ifdef CONFIG_JUMP_LABEL > mod->jump_entries = section_objs(info, "__jump_table", > sizeof(*mod->jump_entries), >-- >2.24.1 >