Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1159571pxu; Fri, 27 Nov 2020 00:48:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJyBVHPi0q7NFJepzk2wBCYDhN6yUS1y/s7/0dbt5dMUCuTLXc+It330cW6BJe2rj3tkHoo9 X-Received: by 2002:a17:906:4495:: with SMTP id y21mr6719268ejo.209.1606466910489; Fri, 27 Nov 2020 00:48:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606466910; cv=none; d=google.com; s=arc-20160816; b=qL9lhDYcW9PCPEsa622MG2bCP/oFUw1vYk31LSfVX0lcufoVmmEvmbJ/ljH0NFz5r7 LBOpUyeClhkH67WxEAo8v/FIJ5zGgwBoUzDpbQn6nTY9v5UAvqDsZvREtwXhIFJNd3VC L6UxqgtjWdKwut5Gu3ZqVgOVHTFM7L4EbaRfnQTufAnFQQHMtIiOicFGt2bT18uT9DK8 dzKR1Kx/JVvnNbThokvW0bDMP+SyyxIEHUWRkJzJtl0OnPDGsC+7W5Er6KDoSYM2HfFW dBb1iaJVj/F6SSTr3y8YaXZZh9On4GrGU9z3Q11E/izWzsHQvywKYjCTHfYg/EF3rVgS X1uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=duKXrMXbO5x97/X4ifJt1mH4DWBuc4Xj0bBfthNtITo=; b=J1CIVQu8BbSbnmSBvKqbgDEs479tDLrONwxLSd+UK47+Z/7gU682H9hTBmq7ngBKrC I2807GVQLWWeQ4e4lCNmgpstbuxdjzbJcVliUU9kY35WnvQr8rTVFExPHm8DoQJ0ZPbk lTClTVhuRqcUPD92KP+f5IMSEOXeLePWuJJF2pd+YF5wOXyFJEkfFKvOaF2lrx3h/X7L nWK3PwifV9sMiqc2RjR8AT7Ed8nhYuak1BpuKx6GzU/6i8ryTyM/i8m4Irus9vy1sN9w JvKqCHPXUPO2LUT/m64wauOBGQhKLXqtXtm3d+3UV8qKfQvh9n/8s1jPodpolQdU2ulD qLOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RdVbIFRv; 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 z10si5518870edp.346.2020.11.27.00.48.08; Fri, 27 Nov 2020 00:48:30 -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=@chromium.org header.s=google header.b=RdVbIFRv; 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 S2392250AbgK0CcS (ORCPT + 99 others); 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-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, > +}; > + [...]