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=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 141F4C63697 for ; Fri, 27 Nov 2020 02:32:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CDE2B221FD for ; Fri, 27 Nov 2020 02:32:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="RdVbIFRv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392254AbgK0CcS (ORCPT ); Thu, 26 Nov 2020 21:32:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392216AbgK0CcR (ORCPT ); Thu, 26 Nov 2020 21:32:17 -0500 Received: from mail-lf1-x142.google.com (mail-lf1-x142.google.com [IPv6:2a00:1450:4864:20::142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D28EC0617A7 for ; Thu, 26 Nov 2020 18:32:17 -0800 (PST) Received: by mail-lf1-x142.google.com with SMTP id z21so4980968lfe.12 for ; Thu, 26 Nov 2020 18:32:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=duKXrMXbO5x97/X4ifJt1mH4DWBuc4Xj0bBfthNtITo=; b=RdVbIFRv325tEKb1KxuR884D6+sUO17uL5VDFRX/mMETlyh/rWSMLOOCkq4JwuwzIP WjByKo44zmV0Uh8tYD/lm/SijRhTovN4/OE9s+UL6M2mNFT9OE0eTUSHz5wrS7h+Gfwe 4bRtAW8ynalT354yFwKhUOZudZHtMXtMZq92s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=duKXrMXbO5x97/X4ifJt1mH4DWBuc4Xj0bBfthNtITo=; b=Dz0izlNYvUFBTs1Rfj258YfkE/G5JifsUJekY8/4UXbGQ9vau0x7gbwzWyovhOe58d 8hRutg0aSrgpLHZsx+wk76In2bUh76zoSehi/685gHSVEVs/ri6kMFaTizdBm+sG+bPA WIVxttaGfKEpC7HlHMDOVMvcNjo/ZX7+JNzLUh5ZYMCi4cbKSAWgGBnrg6L8pNXZyrw2 U4IyrWLWba7f2plwW3GHxcKLpVqhICeE/m5RMmxQM9Fsjq3ZxQX7oKNgoYW5Cex6V3am kBxvHwVJdxxAG5/qB3a3oHDHOIVZnKvOVyZEyeRXIYRFLCxip1COPZgASn5WBV3DZ1h2 xUpw== X-Gm-Message-State: AOAM533cCCFcCkSP1gch0Qa/bK7uzJFzS6t5lenKXIKUtHWszlnxco2b re5gw+mS3V6eBcL5KsjduP7eU03DGkNTlYfP1sR54gyjQxINnglM X-Google-Smtp-Source: ABdhPJyRfHa59VPLJk9D5ukrU4+/2/Vx2g97em7FvUpGC0yhzfjdDvSvAvT99Qfioctitgy9S0KPwt8yv8ZyB1g1TJU= X-Received: by 2002:a05:6512:34d3:: with SMTP id w19mr2235358lfr.418.1606444335787; Thu, 26 Nov 2020 18:32:15 -0800 (PST) MIME-Version: 1.0 References: <20201126165748.1748417-1-revest@google.com> In-Reply-To: <20201126165748.1748417-1-revest@google.com> From: KP Singh Date: Fri, 27 Nov 2020 03:32:04 +0100 Message-ID: Subject: Re: [PATCH bpf-next 1/2] bpf: Add a bpf_kallsyms_lookup helper To: Florent Revest Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Florent Revest , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [...] > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index c3458ec1f30a..670998635eac 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -3817,6 +3817,21 @@ union bpf_attr { > * The **hash_algo** is returned on success, > * **-EOPNOTSUP** if IMA is disabled or **-EINVAL** if > * invalid arguments are passed. > + * > + * long bpf_kallsyms_lookup(u64 address, char *symbol, u32 symbol_size, char *module, u32 module_size) > + * Description > + * Uses kallsyms to write the name of the symbol at *address* > + * into *symbol* of size *symbol_sz*. This is guaranteed to be > + * zero terminated. > + * If the symbol is in a module, up to *module_size* bytes of > + * the module name is written in *module*. This is also > + * guaranteed to be zero-terminated. Note: a module name > + * is always shorter than 64 bytes. > + * Return > + * On success, the strictly positive length of the full symbol > + * name, If this is greater than *symbol_size*, the written > + * symbol is truncated. > + * On error, a negative value. > */ > #define __BPF_FUNC_MAPPER(FN) \ > FN(unspec), \ > @@ -3981,6 +3996,7 @@ union bpf_attr { > FN(bprm_opts_set), \ > FN(ktime_get_coarse_ns), \ > FN(ima_inode_hash), \ > + FN(kallsyms_lookup), \ > /* */ > > /* integer value in 'imm' field of BPF_CALL instruction selects which helper > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index d255bc9b2bfa..9d86e20c2b13 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -17,6 +17,7 @@ > #include > #include > #include > +#include > > #include > > @@ -1260,6 +1261,44 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { > .arg5_type = ARG_ANYTHING, > }; > > +BPF_CALL_5(bpf_kallsyms_lookup, u64, address, char *, symbol, u32, symbol_size, > + char *, module, u32, module_size) > +{ > + char buffer[KSYM_SYMBOL_LEN]; > + unsigned long offset, size; > + const char *name; > + char *modname; > + long ret; > + > + name = kallsyms_lookup(address, &size, &offset, &modname, buffer); > + if (!name) > + return -EINVAL; > + > + ret = strlen(name) + 1; > + if (symbol_size) { > + strncpy(symbol, name, symbol_size); > + symbol[symbol_size - 1] = '\0'; > + } > + > + if (modname && module_size) { > + strncpy(module, modname, module_size); The return value does not seem to be impacted by the truncation of the module name, I wonder if it is better to just use a single buffer. For example, the proc kallsyms shows symbols as: [module_name] https://github.com/torvalds/linux/blob/master/kernel/kallsyms.c#L648 The square brackets do seem to be a waste here, so maybe we could use a single character as a separator? > + module[module_size - 1] = '\0'; > + } > + > + return ret; > +} > + > +const struct bpf_func_proto bpf_kallsyms_lookup_proto = { > + .func = bpf_kallsyms_lookup, > + .gpl_only = false, > + .ret_type = RET_INTEGER, > + .arg1_type = ARG_ANYTHING, > + .arg2_type = ARG_PTR_TO_MEM, > + .arg3_type = ARG_CONST_SIZE, > + .arg4_type = ARG_PTR_TO_MEM, > + .arg5_type = ARG_CONST_SIZE, > +}; > + [...]