Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp357533pxb; Wed, 14 Apr 2021 17:43:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwywj1/3D/mO+eTpEVFNV8lbqTsI0ZGokZ0Je+HHhHzqJyb60vZmAqf4bqCMzU9LglQWfHE X-Received: by 2002:a17:906:81d5:: with SMTP id e21mr774504ejx.134.1618447429934; Wed, 14 Apr 2021 17:43:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618447429; cv=none; d=google.com; s=arc-20160816; b=owW0QtITDuQlEjDOr+IzsuGw1ZKDIbyexVMpfHQ6ZL5m1FxCLrTOWR19/gw3HqcCJr qxJ8KGvfSwkKyiAPK1sGJ0+cm4buzAloICtm8852An6pagFz7NxlK2800B2uCcJBR8SA s7lRBOTyS58CIplFbpP4e4Ev0HUyH804N9G5wwmmEuIp27s2BNltMLEi8/QCiadjma3H A9qoQ4xRDio+rYSVJqvA3KKfZRpxQvoecmeB1hvSZyUWF5nb8z8hqBlK41kzpigIMvQN N6eKuCjlFGui9L5pw3EIFPC3FE/SP0j7K5F7EQs4qADL+fyuvrNYzRgmlp6GyUj4fm9u 2gYQ== 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=+5LgeKpusYSyxX8qx1DrgdRoJ4W0OuV8svaN4mMwLYQ=; b=nn7GThW8kNdina5RJ7rRnOHuDCQC9C/TQDqsWgVMrbahINbwug0iIl9GCm/TgNisrb q0g5aVcpj43M1PwzXyQTqClBJ1NRv536IhpQX6ryXcWGaam/Q+fCn89G1CBddKxdWs75 5BYxHRBr6bxUmfjh4UECW4o2FM7LwHZvwliI+L7l6YqPYF+kTi1Mjoi8NkA3R0Kfa+LG QZKYxLV0YB076ImrZoLWphMzCbt42moK5GFnVxOkBDkFV/PCo8L8KlTaTnz14/jSlMMM p+TEE3dZcTwCP5O8Ui33Kuhr+vo4EShXq/Qj1f2zl10e9271Z2sfqOYhaMjv/Ko3K2L3 gMAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=awkOVaY3; 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 l25si887813eds.452.2021.04.14.17.43.27; Wed, 14 Apr 2021 17:43:49 -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=awkOVaY3; 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 S234683AbhDNSyk (ORCPT + 99 others); Wed, 14 Apr 2021 14:54:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230398AbhDNSyj (ORCPT ); Wed, 14 Apr 2021 14:54:39 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F380AC061574 for ; Wed, 14 Apr 2021 11:54:16 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id r7so8864401wrm.1 for ; Wed, 14 Apr 2021 11:54:16 -0700 (PDT) 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=+5LgeKpusYSyxX8qx1DrgdRoJ4W0OuV8svaN4mMwLYQ=; b=awkOVaY3FBqbwSPvcQFmJMgiBgkPDfbnZZ2JQ+y8NXjsQiUsY8xnqDYZRA7DOHDzuc UnjikV7YvVHMPseWobwAnaIsaRLDLQz8H7o7TGRDab0w9LD2ea9DMUCM3HO2M2z+GBBQ A1EZN8u4whHzQLSmfAp6fFr7v0csq+Jqe79dw= 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=+5LgeKpusYSyxX8qx1DrgdRoJ4W0OuV8svaN4mMwLYQ=; b=EUTjVO33WYEbl15hsZpZZHm8317fpl62bvvLAqZdc+5byCwhL7B7Txj13owHQ/ZCPs eQ4xAGX4L0xwE0NmHyRMLc0/OZxRGvm2l1LtBM3AfzH5H8m/Wuepymg2LCETtj3ockC4 4RWkKOJ4qQLsDUajtVkeK5CdTTI4qk7pvhuByJR3JlvtpIvdXiVeGvhS4rStVOB3KUaF xrmHmMolyeV7knQTYHhuJDgBVrHSLmTsXISiMVIu7a/FPHLZWchlumeVvrDZKTdGQD91 ObDAPNt0eMMbkRfW8UpdK3tZO2094kT2QlnObQxexlbayYs2twWmBuTP9KkizxOEhdi3 bDFQ== X-Gm-Message-State: AOAM5312SM3QslSEtf1aS4QBmFhIGeyiZViXiLKheXsM35jbt0B9oyBc Nn9b1W3x/QnhLX/iH+D6QsnbPw== X-Received: by 2002:adf:f510:: with SMTP id q16mr5741957wro.343.1618426455698; Wed, 14 Apr 2021 11:54:15 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:42:204:8b2a:41bd:9d62:10d5]) by smtp.gmail.com with ESMTPSA id f12sm253131wrr.61.2021.04.14.11.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Apr 2021 11:54:15 -0700 (PDT) From: Florent Revest To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, yhs@fb.com, kpsingh@kernel.org, jackmanb@chromium.org, linux-kernel@vger.kernel.org, Florent Revest Subject: [PATCH bpf-next v4 0/6] Add a snprintf eBPF helper Date: Wed, 14 Apr 2021 20:54:00 +0200 Message-Id: <20210414185406.917890-1-revest@chromium.org> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We have a usecase where we want to audit symbol names (if available) in callback registration hooks. (ex: fentry/nf_register_net_hook) A few months back, I proposed a bpf_kallsyms_lookup series but it was decided in the reviews that a more generic helper, bpf_snprintf, would be more useful. This series implements the helper according to the feedback received in https://lore.kernel.org/bpf/20201126165748.1748417-1-revest@google.com/T/#u - A new arg type guarantees the NULL-termination of string arguments and lets us pass format strings in only one arg - A new helper is implemented using that guarantee. Because the format string is known at verification time, the format string validation is done by the verifier - To implement a series of tests for bpf_snprintf, the logic for marshalling variadic args in a fixed-size array is reworked as per: https://lore.kernel.org/bpf/20210310015455.1095207-1-revest@chromium.org/T/#u --- Changes in v4: - Moved bpf_snprintf, bpf_printf_prepare and bpf_printf_cleanup to kernel/bpf/helpers.c so that they get built without CONFIG_BPF_EVENTS - Added negative test cases (various invalid format strings) - Renamed put_fmt_tmp_buf() as bpf_printf_cleanup() - Fixed a mistake that caused temporary buffers to be unconditionally freed in bpf_printf_prepare - Fixed a mistake that caused missing 0 character to be ignored - Fixed a warning about integer to pointer conversion - Misc cleanups --- Changes in v3: - Simplified temporary buffer acquisition with try_get_fmt_tmp_buf() - Made zero-termination check more consistent - Allowed NULL output_buffer - Simplified the BPF_CAST_FMT_ARG macro - Three new test cases: number padding, simple string with no arg and string length extraction only with a NULL output buffer - Clarified helper's description for edge cases (eg: str_size == 0) - Lots of cosmetic changes --- Changes in v2: - Extracted the format validation/argument sanitization in a generic way for all printf-like helpers. - bpf_snprintf's str_size can now be 0 - bpf_snprintf is now exposed to all BPF program types - We now preempt_disable when using a per-cpu temporary buffer - Addressed a few cosmetic changes Florent Revest (6): bpf: Factorize bpf_trace_printk and bpf_seq_printf bpf: Add a ARG_PTR_TO_CONST_STR argument type bpf: Add a bpf_snprintf helper libbpf: Initialize the bpf_seq_printf parameters array field by field libbpf: Introduce a BPF_SNPRINTF helper macro selftests/bpf: Add a series of tests for bpf_snprintf include/linux/bpf.h | 22 ++ include/uapi/linux/bpf.h | 28 ++ kernel/bpf/helpers.c | 304 ++++++++++++++ kernel/bpf/verifier.c | 82 ++++ kernel/trace/bpf_trace.c | 373 ++---------------- tools/include/uapi/linux/bpf.h | 28 ++ tools/lib/bpf/bpf_tracing.h | 58 ++- .../selftests/bpf/prog_tests/snprintf.c | 124 ++++++ .../selftests/bpf/progs/test_snprintf.c | 73 ++++ .../bpf/progs/test_snprintf_single.c | 20 + 10 files changed, 767 insertions(+), 345 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf_single.c -- 2.31.1.295.g9ea45b61b8-goog