Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp685908pxu; Thu, 26 Nov 2020 09:03:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJxozIq0WRKa4vb2ckC5nOH4Yx0Mu2AZJEMnetGh+fjRQmqb2JDLFlrjtUWIIpsobwfwsmhA X-Received: by 2002:a17:906:a458:: with SMTP id cb24mr3505785ejb.321.1606410219265; Thu, 26 Nov 2020 09:03:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606410219; cv=none; d=google.com; s=arc-20160816; b=pnouzxf2NwJRPlbzdeZU0M842GnnWAZo9MXxMQUViyIYP6MIE670owwhfEp6kYIcQr m/74kx8pAnRe8Epn5TXeuFqLbOT2ptXsvjsE4fEEnBpW/5Kwve6lEY3FFxh31QTnTlTK 36MRly3m+GYMC+g05jcE92Ack3cvmYwTX3wWmvI4FL2dizLMexeez8CKWaY7xDu2TWeu STFx0QUG2saiNiw5Yw5SzMZO+OnzWloyX6cd8aRTPe2P2PQ+8G3K9vmizHoXrsPOkFoo +F4oxMJ5yKB84M8QMQpZudeyXi8xTtraG4+sSphJWA+fn7R56SA7QkF6UrGXniBMG67Q l6JQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=0L00r4rPHemcbzcKizDl5R/fNH5uAoaWPolJKjNNL9c=; b=lSqruGpoRGIHH2ES+KK/JrVYgG6l9SO6Gm3iOulNhyhMKrmLaGUN+uoBQtYpwwMmmt 8xP2Izk3dOK33SyZm0shg/T1cqT8qdpJJLVpX32RI51Oq4gtI/SnmW2zQML9ot0sfgcW R+QuJbXmRP2eGtQGP5i7HUysS0x93u/82/Qw6EWa/YeX87C3oazam5SL41LB68h/Xbuy buFCmHybQw3osQNoIgivxkdqM4AcNPJcMkISFPBzEda2qpDhjdmJL+44nSnRxTNbExBk Is/Z8tggt70w8/fOq+xM6PGmZFfDAN6Hzn07VxHZdywL8P8uCdilGZI10IkVC9FQ/5BF QL8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EivVOmLN; 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 n29si473134edb.324.2020.11.26.09.03.15; Thu, 26 Nov 2020 09:03:39 -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=EivVOmLN; 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 S2403901AbgKZQ7F (ORCPT + 99 others); Thu, 26 Nov 2020 11:59:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403871AbgKZQ7E (ORCPT ); Thu, 26 Nov 2020 11:59:04 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 646DCC0613D4 for ; Thu, 26 Nov 2020 08:59:04 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id 3so334135wmj.4 for ; Thu, 26 Nov 2020 08:59:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0L00r4rPHemcbzcKizDl5R/fNH5uAoaWPolJKjNNL9c=; b=EivVOmLNk176rC1BaDYKctnghwXfBJ8z7Vtqp3kgoYE9GywHRyCXiLuUXCFTUOrUxa /l5zZGyjrAbnUwyPs1Y9iN/ilkHw2xWvXklsmM8mKMvuW9Q+H9ispA2Wt6bxLoWZ8Ba/ g/vBv3cyAQaJSj+Ve66wA52pJQBf8mNihZ/cQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0L00r4rPHemcbzcKizDl5R/fNH5uAoaWPolJKjNNL9c=; b=r9a+b5tGTxhPaXt+qhYDJU4t4tCVxg6tIJO+2yNdnkL0+9GevvKTcatFvCqCo7pOms viXFh6o51alTkoFDgYf+rrumJv3iex385wc69DMOrk3ZTRc2hFdYNfDLnUH8O4dWqMWW d20aePMffu+XwpQQOfBb5ERvQpPUlXngRa2LXphwAhHiq1Y/J1mxfdjQup2cFyHYUCY0 SCKUMGmEF+HQGupmvL834fq9rCvYFdJzGcpCii9at6OdbrKOdz60tElpDdgX37wL5IdG 0QhGaVzuSxeen+A5/NL7wZYk5HHFnHOFqbVq7kB5V+B2youHae3Ygo02TWj+4ToatvYJ MriQ== X-Gm-Message-State: AOAM5317ql1yYC4YN7k1Ee+MS8hQpTOxKkPZQ5p3tU3fezceFO6NpuB5 wuq1iwEklewcJsTu2DZHds/GgQ== X-Received: by 2002:a1c:1f16:: with SMTP id f22mr4243742wmf.108.1606409943063; Thu, 26 Nov 2020 08:59:03 -0800 (PST) Received: from revest.zrh.corp.google.com ([2a00:79e0:42:204:f693:9fff:fef4:a569]) by smtp.gmail.com with ESMTPSA id 17sm8768032wmf.48.2020.11.26.08.59.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Nov 2020 08:59:02 -0800 (PST) From: Florent Revest X-Google-Original-From: Florent Revest To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kpsingh@chromium.org, revest@google.com, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 1/2] bpf: Add a bpf_kallsyms_lookup helper Date: Thu, 26 Nov 2020 17:57:47 +0100 Message-Id: <20201126165748.1748417-1-revest@google.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This helper exposes the kallsyms_lookup function to eBPF tracing programs. This can be used to retrieve the name of the symbol at an address. For example, when hooking into nf_register_net_hook, one can audit the name of the registered netfilter hook and potentially also the name of the module in which the symbol is located. Signed-off-by: Florent Revest --- include/uapi/linux/bpf.h | 16 +++++++++++++ kernel/trace/bpf_trace.c | 41 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 16 +++++++++++++ 3 files changed, 73 insertions(+) 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); + 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, +}; + const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1356,6 +1395,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_per_cpu_ptr_proto; case BPF_FUNC_bpf_this_cpu_ptr: return &bpf_this_cpu_ptr_proto; + case BPF_FUNC_kallsyms_lookup: + return &bpf_kallsyms_lookup_proto; default: return NULL; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index c3458ec1f30a..670998635eac 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/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 -- 2.29.2.454.gaff20da3a2-goog