-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEZH8oZUiU471FcZm+ONu9yGCSaT4FAmUOqSMACgkQONu9yGCS aT6xIg//SVVT7zeyVcdNSchMLT6N1sJKtnplNnhyM6oFPlnyRJbgm608p394osx9 bMkz8QNPugdJz075nFt1blC2qqh2GqNkgaAM1bSKrVmUhBR3ouaO2vKfTamd1qkQ uHjE2+4NSlJu0zeqF+D+xmYYo3W32XXfDjn64p3dYiEVFtM4J0r633OpkNTZL3KR b8Ooj0sE6WtG5Lt4I64z74/p8QjK8ESW7N7hYUjADadoycn7ms5wwED6KbXwO+Ed 3piSteS8bddtx+s6pblRwHvRcOMU3NX0rVG8x3lBtdnjAk32/HEsUm7mAycqJdsJ TQ67UJ4gyqzrCtDfrbhZ9hKpaEHGuy6nnjKfXtnlSKZ+8h4uuxK0rIwFlZuS+sjH Xm99yiA6KK+CbdR9/ltgQyr5kaTcIqauA6VTjbqqJ3Fuj4OWEz3N2ALUpWeLPNpe Enl7b5/eQ4B0sDOYDVG4HsjRTt7ZgNVGFxRRp8ZulDKgX9G4M0K2khq/b3PM9aEQ gkgWDxLt3H0EO+6mRgCA0J3a/TSC6gPgV8t8iNcg5rzlXngJzAajdgi7HBMnhPdl 8y8JCfojtA+RuHWHOEmPXJG1AmwQ4df7szVxbv8WDuidIqv2tb09POo38s/UWHeN NGM5nh1WSCs4hQBfkx4wk58xSZ/jAh4/Uq6g3GasmqlknhA8TjQ= =dWOv -----END PGP SIGNATURE----- Merge 5.4.257 into android11-5.4-lts Changes in 5.4.257 erofs: ensure that the post-EOF tails are all zeroed ARM: pxa: remove use of symbol_get() mmc: au1xmmc: force non-modular build and remove symbol_get usage net: enetc: use EXPORT_SYMBOL_GPL for enetc_phc_index rtc: ds1685: use EXPORT_SYMBOL_GPL for ds1685_rtc_poweroff modules: only allow symbol_get of EXPORT_SYMBOL_GPL modules USB: serial: option: add Quectel EM05G variant (0x030e) USB: serial: option: add FOXCONN T99W368/T99W373 product HID: wacom: remove the battery when the EKR is off staging: rtl8712: fix race condition Bluetooth: btsdio: fix use after free bug in btsdio_remove due to race condition serial: sc16is7xx: fix bug when first setting GPIO direction firmware: stratix10-svc: Fix an NULL vs IS_ERR() bug in probe fsi: master-ast-cf: Add MODULE_FIRMWARE macro nilfs2: fix general protection fault in nilfs_lookup_dirty_data_buffers() nilfs2: fix WARNING in mark_buffer_dirty due to discarded buffer reuse pinctrl: amd: Don't show `Invalid config param` errors 9p: virtio: make sure 'offs' is initialized in zc_request ASoC: da7219: Flush pending AAD IRQ when suspending ASoC: da7219: Check for failure reading AAD IRQ events ethernet: atheros: fix return value check in atl1c_tso_csum() vxlan: generalize vxlan_parse_gpe_hdr and remove unused args m68k: Fix invalid .section syntax s390/dasd: use correct number of retries for ERP requests s390/dasd: fix hanging device after request requeue fs/nls: make load_nls() take a const parameter ASoc: codecs: ES8316: Fix DMIC config ASoC: atmel: Fix the 8K sample parameter in I2SC master platform/x86: intel: hid: Always call BTNL ACPI method platform/x86: huawei-wmi: Silence ambient light sensor security: keys: perform capable check only on privileged operations clk: fixed-mmio: make COMMON_CLK_FIXED_MMIO depend on HAS_IOMEM net: usb: qmi_wwan: add Quectel EM05GV2 idmaengine: make FSL_EDMA and INTEL_IDMA64 depends on HAS_IOMEM scsi: qedi: Fix potential deadlock on &qedi_percpu->p_work_lock netlabel: fix shift wrapping bug in netlbl_catmap_setlong() bnx2x: fix page fault following EEH recovery sctp: handle invalid error codes without calling BUG() cifs: add a warning when the in-flight count goes negative scsi: storvsc: Always set no_report_opcodes ALSA: seq: oss: Fix racy open/close of MIDI devices platform/mellanox: Fix mlxbf-tmfifo not handling all virtio CONSOLE notifications net: Avoid address overwrite in kernel_connect powerpc/32s: Fix assembler warning about r0 udf: Check consistency of Space Bitmap Descriptor udf: Handle error when adding extent to a file Revert "net: macsec: preserve ingress frame ordering" reiserfs: Check the return value from __getblk() eventfd: Export eventfd_ctx_do_read() eventfd: prevent underflow for eventfd semaphores new helper: lookup_positive_unlocked() fs: Fix error checking for d_hash_and_lookup() tmpfs: verify {g,u}id mount options correctly OPP: Fix passing 0 to PTR_ERR in _opp_attach_genpd() x86/asm: Make more symbols local x86/boot: Annotate local functions x86/decompressor: Don't rely on upper 32 bits of GPRs being preserved perf/imx_ddr: don't enable counter0 if none of 4 counters are used cpufreq: powernow-k8: Use related_cpus instead of cpus in driver.exit() bpf: Clear the probe_addr for uprobe tcp: tcp_enter_quickack_mode() should be static regmap: rbtree: Use alloc_flags for memory allocations spi: tegra20-sflash: fix to check return value of platform_get_irq() in tegra_sflash_probe() can: gs_usb: gs_usb_receive_bulk_callback(): count RX overflow errors also in case of OOM wifi: mwifiex: Fix OOB and integer underflow when rx packets mwifiex: switch from 'pci_' to 'dma_' API wifi: mwifiex: fix error recovery in PCIE buffer descriptor management crypto: stm32 - Properly handle pm_runtime_get failing Bluetooth: nokia: fix value check in nokia_bluetooth_serdev_probe() crypto: caam - fix unchecked return value error hwrng: iproc-rng200 - use semicolons rather than commas to separate statements hwrng: iproc-rng200 - Implement suspend and resume calls lwt: Fix return values of BPF xmit ops lwt: Check LWTUNNEL_XMIT_CONTINUE strictly fs: ocfs2: namei: check return value of ocfs2_add_entry() wifi: mwifiex: fix memory leak in mwifiex_histogram_read() wifi: mwifiex: Fix missed return in oob checks failed path wifi: ath9k: fix races between ath9k_wmi_cmd and ath9k_wmi_ctrl_rx wifi: ath9k: protect WMI command response buffer replacement with a lock wifi: mwifiex: avoid possible NULL skb pointer dereference wifi: ath9k: use IS_ERR() with debugfs_create_dir() net: arcnet: Do not call kfree_skb() under local_irq_disable() mlxsw: i2c: Fix chunk size setting in output mailbox buffer mlxsw: i2c: Limit single transaction buffer size net/sched: sch_hfsc: Ensure inner classes have fsc curve netrom: Deny concurrent connect(). drm/bridge: tc358764: Fix debug print parameter order quota: avoid increasing DQST_LOOKUPS when iterating over dirty/inuse list quota: factor out dquot_write_dquot() quota: rename dquot_active() to inode_quota_active() quota: add new helper dquot_active() quota: fix dqput() to follow the guarantees dquot_srcu should provide drm/amdgpu: avoid integer overflow warning in amdgpu_device_resize_fb_bar() ARM: dts: BCM53573: Drop nonexistent "default-off" LED trigger ARM: dts: BCM53573: Add cells sizes to PCIe node ARM: dts: BCM53573: Use updated "spi-gpio" binding properties drm/etnaviv: fix dumping of active MMU context ARM: dts: s3c6410: move fixed clocks under root node in Mini6410 ARM: dts: s3c6410: align node SROM bus node name with dtschema in Mini6410 ARM: dts: s3c64xx: align pinctrl with dtschema ARM: dts: samsung: s3c6410-mini6410: correct ethernet reg addresses (split) ARM: dts: s5pv210: add RTC 32 KHz clock in SMDKV210 ARM: dts: s5pv210: use defines for IRQ flags in SMDKV210 ARM: dts: s5pv210: correct ethernet unit address in SMDKV210 ARM: dts: s5pv210: add dummy 5V regulator for backlight on SMDKv210 ARM: dts: samsung: s5pv210-smdkv210: correct ethernet reg addresses (split) drm: adv7511: Fix low refresh rate register for ADV7533/5 ARM: dts: BCM53573: Fix Ethernet info for Luxul devices arm64: dts: qcom: sdm845: Add missing RPMh power domain to GCC drm/amdgpu: Update min() to min_t() in 'amdgpu_info_ioctl' md/bitmap: don't set max_write_behind if there is no write mostly device md/md-bitmap: hold 'reconfig_mutex' in backlog_store() drm/tegra: Remove superfluous error messages around platform_get_irq() drm/tegra: dpaux: Fix incorrect return value of platform_get_irq of: unittest: fix null pointer dereferencing in of_unittest_find_node_by_name() drm/armada: Fix off-by-one error in armada_overlay_get_property() drm/panel: simple: Add missing connector type and pixel format for AUO T215HVN01 ima: Remove deprecated IMA_TRUSTED_KEYRING Kconfig drm/msm/mdp5: Don't leak some plane state smackfs: Prevent underflow in smk_set_cipso() audit: fix possible soft lockup in __audit_inode_child() drm/mediatek: Fix potential memory leak if vmap() fail of: unittest: Fix overlay type in apply/revert check ALSA: ac97: Fix possible error value of *rac97 ipmi:ssif: Add check for kstrdup ipmi:ssif: Fix a memory leak when scanning for an adapter drivers: clk: keystone: Fix parameter judgment in _of_pll_clk_init() clk: sunxi-ng: Modify mismatched function name PCI: Mark NVIDIA T4 GPUs to avoid bus reset PCI: pciehp: Use RMW accessors for changing LNKCTL PCI/ASPM: Use RMW accessors for changing LNKCTL clk: imx: composite-8m: fix clock pauses when set_rate would be a no-op powerpc/fadump: reset dump area size if fadump memory reserve fails PCI: Add #defines for Enter Compliance, Transmit Margin drm/amdgpu: Correct Transmit Margin masks drm/amdgpu: Replace numbers with PCI_EXP_LNKCTL2 definitions drm/amdgpu: Prefer pcie_capability_read_word() drm/amdgpu: Use RMW accessors for changing LNKCTL drm/radeon: Correct Transmit Margin masks drm/radeon: Replace numbers with PCI_EXP_LNKCTL2 definitions drm/radeon: Prefer pcie_capability_read_word() drm/radeon: Use RMW accessors for changing LNKCTL wifi: ath10k: Use RMW accessors for changing LNKCTL nfs/blocklayout: Use the passed in gfp flags powerpc/iommu: Fix notifiers being shared by PCI and VIO buses jfs: validate max amount of blocks before allocation. fs: lockd: avoid possible wrong NULL parameter NFSD: da_addr_body field missing in some GETDEVICEINFO replies NFS: Guard against READDIR loop when entry names exceed MAXNAMELEN media: v4l2-fwnode: fix v4l2_fwnode_parse_link handling media: v4l2-fwnode: simplify v4l2_fwnode_parse_link media: v4l2-core: Fix a potential resource leak in v4l2_fwnode_parse_link() drivers: usb: smsusb: fix error handling code in smsusb_init_device media: dib7000p: Fix potential division by zero media: dvb-usb: m920x: Fix a potential memory leak in m920x_i2c_xfer() media: cx24120: Add retval check for cx24120_message_send() media: mediatek: vcodec: Return NULL if no vdec_fb is found usb: phy: mxs: fix getting wrong state with mxs_phy_is_otg_host() scsi: iscsi: Add strlen() check in iscsi_if_set{_host}_param() scsi: be2iscsi: Add length check when parsing nlattrs scsi: qla4xxx: Add length check when parsing nlattrs serial: sprd: getting port index via serial aliases only serial: sprd: remove redundant sprd_port cleanup serial: sprd: Assign sprd_port after initialized to avoid wrong access serial: sprd: Fix DMA buffer leak issue x86/APM: drop the duplicate APM_MINOR_DEV macro scsi: qedf: Do not touch __user pointer in qedf_dbg_stop_io_on_error_cmd_read() directly scsi: qedf: Do not touch __user pointer in qedf_dbg_debug_cmd_read() directly scsi: qedf: Do not touch __user pointer in qedf_dbg_fp_int_cmd_read() directly coresight: tmc: Explicit type conversions to prevent integer overflow dma-buf/sync_file: Fix docs syntax driver core: test_async: fix an error code IB/uverbs: Fix an potential error pointer dereference iommu/vt-d: Fix to flush cache of PASID directory table media: go7007: Remove redundant if statement USB: gadget: f_mass_storage: Fix unused variable warning media: i2c: ov5640: Configure HVP lines in s_power callback media: ov5640: Enable MIPI interface in ov5640_set_power_mipi() media: i2c: ov2680: Set V4L2_CTRL_FLAG_MODIFY_LAYOUT on flips media: ov2680: Remove auto-gain and auto-exposure controls media: ov2680: Fix ov2680_bayer_order() media: ov2680: Fix vflip / hflip set functions media: ov2680: Fix regulators being left enabled on ov2680_power_on() errors scsi: core: Use 32-bit hostnum in scsi_host_lookup() scsi: fcoe: Fix potential deadlock on &fip->ctlr_lock serial: tegra: handle clk prepare error in tegra_uart_hw_init() amba: bus: fix refcount leak Revert "IB/isert: Fix incorrect release of isert connection" RDMA/siw: Balance the reference of cep->kref in the error path RDMA/siw: Correct wrong debug message HID: logitech-dj: Fix error handling in logi_dj_recv_switch_to_dj_mode() HID: multitouch: Correct devm device reference for hidinput input_dev name x86/speculation: Mark all Skylake CPUs as vulnerable to GDS tracing: Fix race issue between cpu buffer write and swap phy/rockchip: inno-hdmi: use correct vco_div_5 macro on rk3328 phy/rockchip: inno-hdmi: round fractal pixclock in rk3328 recalc_rate phy/rockchip: inno-hdmi: do not power on rk3328 post pll on reg write rpmsg: glink: Add check for kstrdup mtd: rawnand: fsmc: handle clk prepare error in fsmc_nand_resume() um: Fix hostaudio build errors dmaengine: ste_dma40: Add missing IRQ check in d40_probe cpufreq: Fix the race condition while updating the transition_task of policy virtio_ring: fix avail_wrap_counter in virtqueue_add_packed igmp: limit igmpv3_newpack() packet size to IP_MAX_MTU netfilter: ipset: add the missing IP_SET_HASH_WITH_NET0 macro for ip_set_hash_netportnet.c netfilter: xt_u32: validate user space input netfilter: xt_sctp: validate the flag_info count skbuff: skb_segment, Call zero copy functions before using skbuff frags igb: set max size RX buffer when store bad packet is enabled PM / devfreq: Fix leak in devfreq_dev_release() ALSA: pcm: Fix missing fixup call in compat hw_refine ioctl ipmi_si: fix a memleak in try_smi_init() ARM: OMAP2+: Fix -Warray-bounds warning in _pwrdm_state_switch() backlight/gpio_backlight: Compare against struct fb_info.device backlight/bd6107: Compare against struct fb_info.device backlight/lv5207lp: Compare against struct fb_info.device xtensa: PMU: fix base address for the newer hardware media: dvb: symbol fixup for dvb_attach() ntb: Drop packets when qp link is down ntb: Clean up tx tail index on link down ntb: Fix calculation ntb_transport_tx_free_entry() Revert "PCI: Mark NVIDIA T4 GPUs to avoid bus reset" procfs: block chmod on /proc/thread-self/comm parisc: Fix /proc/cpuinfo output for lscpu dlm: fix plock lookup when using multiple lockspaces dccp: Fix out of bounds access in DCCP error handler X.509: if signature is unsupported skip validation net: handle ARPHRD_PPP in dev_is_mac_header_xmit() fsverity: skip PKCS#7 parser when keyring is empty pstore/ram: Check start of empty przs during init s390/ipl: add missing secure/has_secure file to ipl type 'unknown' crypto: stm32 - fix loop iterating through scatterlist for DMA cpufreq: brcmstb-avs-cpufreq: Fix -Warray-bounds bug sc16is7xx: Set iobase to device index serial: sc16is7xx: fix broken port 0 uart init usb: typec: tcpci: clear the fault status bit udf: initialize newblock to 0 drm: fix double free for gbo in drm_gem_vram_init and drm_gem_vram_create net/ipv6: SKB symmetric hash should incorporate transport ports scsi: qla2xxx: fix inconsistent TMF timeout scsi: qla2xxx: Fix erroneous link up failure scsi: qla2xxx: Turn off noisy message log scsi: qla2xxx: Remove unsupported ql2xenabledif option fbdev/ep93xx-fb: Do not assign to struct fb_info.dev drm/ast: Fix DRAM init on AST2200 lib/test_meminit: allocate pages up to order MAX_ORDER parisc: led: Fix LAN receive and transmit LEDs parisc: led: Reduce CPU overhead for disk & lan LED computation clk: qcom: gcc-mdm9615: use proper parent for pll0_vote clock soc: qcom: qmi_encdec: Restrict string length in decode NFSv4/pnfs: minor fix for cleanup path in nfs4_get_device_info kconfig: fix possible buffer overflow perf annotate bpf: Don't enclose non-debug code with an assert() x86/virt: Drop unnecessary check on extended CPUID level in cpu_has_svm() perf top: Don't pass an ERR_PTR() directly to perf_session__delete() watchdog: intel-mid_wdt: add MODULE_ALIAS() to allow auto-load pwm: lpc32xx: Remove handling of PWM channels sctp: annotate data-races around sk->sk_wmem_queued ipv4: annotate data-races around fi->fib_dead net: read sk->sk_family once in sk_mc_loop() igb: disable virtualization features on 82580 veth: Fixing transmit return status for dropped packets net: ipv6/addrconf: avoid integer underflow in ipv6_create_tempaddr af_unix: Fix data-races around user->unix_inflight. af_unix: Fix data-race around unix_tot_inflight. af_unix: Fix data-races around sk->sk_shutdown. af_unix: Fix data race around sk->sk_err. net: sched: sch_qfq: Fix UAF in qfq_dequeue() kcm: Destroy mutex in kcm_exit_net() igc: Change IGC_MIN to allow set rx/tx value between 64 and 80 igbvf: Change IGBVF_MIN to allow set rx/tx value between 64 and 80 igb: Change IGB_MIN to allow set rx/tx value between 64 and 80 s390/zcrypt: don't leak memory if dev_set_name() fails idr: fix param name in idr_alloc_cyclic() doc ip_tunnels: use DEV_STATS_INC() netfilter: nfnetlink_osf: avoid OOB read net: hns3: fix the port information display when sfp is absent sh: boards: Fix CEU buffer size passed to dma_declare_coherent_memory() ata: sata_gemini: Add missing MODULE_DESCRIPTION ata: pata_ftide010: Add missing MODULE_DESCRIPTION fuse: nlookup missing decrement in fuse_direntplus_link btrfs: don't start transaction when joining with TRANS_JOIN_NOSTART btrfs: use the correct superblock to compare fsid in btrfs_validate_super mtd: rawnand: brcmnand: Fix crash during the panic_write mtd: rawnand: brcmnand: Fix potential out-of-bounds access in oob write mtd: rawnand: brcmnand: Fix potential false time out warning perf hists browser: Fix hierarchy mode header perf tools: Handle old data in PERF_RECORD_ATTR usb: typec: tcpm: Refactor tcpm_handle_vdm_request payload handling usb: typec: tcpm: Refactor tcpm_handle_vdm_request usb: typec: bus: verify partner exists in typec_altmode_attention ARM: dts: BCM5301X: Extend RAM to full 256MB for Linksys EA6500 V2 clk: imx8mm: Move 1443X/1416X PLL clock structure to common place net: ipv4: fix one memleak in __inet_del_ifa() net: ethernet: mvpp2_main: fix possible OOB write in mvpp2_ethtool_get_rxnfc() net: ethernet: mtk_eth_soc: fix possible NULL pointer dereference in mtk_hwlro_get_fdir_all() r8152: check budget for r8152_poll() kcm: Fix memory leak in error path of kcm_sendmsg() platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if no more descriptors mlxbf-tmfifo: sparse tags for config access platform/mellanox: mlxbf-tmfifo: Drop jumbo frames net/tls: do not free tls_rec on async operation in bpf_exec_tx_verdict() ixgbe: fix timestamp configuration code kcm: Fix error handling for SOCK_DGRAM in kcm_sendmsg(). drm/amd/display: Fix a bug when searching for insert_above_mpcc parisc: Drop loops_per_jiffy from per_cpu struct autofs: fix memory leak of waitqueues in autofs_catatonic_mode btrfs: output extra debug info if we failed to find an inline backref locks: fix KASAN: use-after-free in trace_event_raw_event_filelock_lock ACPICA: Add AML_NO_OPERAND_RESOLVE flag to Timer kernel/fork: beware of __put_task_struct() calling context ACPI: video: Add backlight=native DMI quirk for Lenovo Ideapad Z470 perf/smmuv3: Enable HiSilicon Erratum 162001900 quirk for HIP08/09 hw_breakpoint: fix single-stepping when using bpf_overflow_handler devlink: remove reload failed checks in params get/set callbacks wifi: ath9k: fix printk specifier wifi: mwifiex: fix fortify warning crypto: lib/mpi - avoid null pointer deref in mpi_cmp_ui() tpm_tis: Resend command to recover from data transfer errors mmc: sdhci-esdhc-imx: improve ESDHC_FLAG_ERR010450 alx: fix OOB-read compiler warning wifi: mac80211_hwsim: drop short frames drm/exynos: fix a possible null-pointer dereference due to data race in exynos_drm_crtc_atomic_disable() bus: ti-sysc: Configure uart quirks for k3 SoC md: raid1: fix potential OOB in raid1_remove_disk() ext2: fix datatype of block number in ext2_xattr_set2() fs/jfs: prevent double-free in dbUnmount() after failed jfs_remount() jfs: fix invalid free of JFS_IP(ipimap)->i_imap in diUnmount powerpc/pseries: fix possible memory leak in ibmebus_bus_init() media: dvb-usb-v2: af9035: Fix null-ptr-deref in af9035_i2c_master_xfer media: dw2102: Fix null-ptr-deref in dw2102_i2c_transfer() media: af9005: Fix null-ptr-deref in af9005_i2c_xfer media: anysee: fix null-ptr-deref in anysee_master_xfer media: az6007: Fix null-ptr-deref in az6007_i2c_xfer() media: tuners: qt1010: replace BUG_ON with a regular error media: pci: cx23885: replace BUG with error return usb: gadget: fsl_qe_udc: validate endpoint index for ch9 udc scsi: target: iscsi: Fix buffer overflow in lio_target_nacl_info_show() serial: cpm_uart: Avoid suspicious locking media: pci: ipu3-cio2: Initialise timing struct to avoid a compiler warning kobject: Add sanity check for kset->kobj.ktype in kset_register() tools features: Add feature test to check if libbfd has buildid support perf jevents: Make build dependency on test JSONs perf tools: Add an option to build without libbfd btrfs: move btrfs_pinned_by_swapfile prototype into volumes.h btrfs: add a helper to read the superblock metadata_uuid btrfs: compare the correct fsid/metadata_uuid in btrfs_validate_super selftests: tracing: Fix to unmount tracefs for recovering environment md/raid1: fix error: ISO C90 forbids mixed declarations attr: block mode changes of symlinks btrfs: fix lockdep splat and potential deadlock after failure running delayed items tracing: Have current_trace inc the trace array ref count tracing: Have option files inc the trace array ref count nfsd: fix change_info in NFSv4 RENAME replies tracefs: Add missing lockdown check to tracefs_create_dir() i2c: aspeed: Reset the i2c controller when timeout occurs scsi: megaraid_sas: Fix deadlock on firmware crashdump ext4: fix rec_len verify error mtd: rawnand: brcmnand: Fix ECC level field setting for v7.2 controller drm/amdgpu: fix amdgpu_cs_p1_user_fence net/sched: Retire rsvp classifier Linux 5.4.257 Change-Id: I99f6978fc0d802b5803005fe903a90aed315d88d Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
1529 lines
40 KiB
C
1529 lines
40 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
|
|
* Copyright (c) 2016 Facebook
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/types.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/bpf.h>
|
|
#include <linux/bpf_perf_event.h>
|
|
#include <linux/filter.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/ctype.h>
|
|
#include <linux/kprobes.h>
|
|
#include <linux/syscalls.h>
|
|
#include <linux/error-injection.h>
|
|
|
|
#include <asm/tlb.h>
|
|
|
|
#include "trace_probe.h"
|
|
#include "trace.h"
|
|
|
|
#define bpf_event_rcu_dereference(p) \
|
|
rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
|
|
|
|
#ifdef CONFIG_MODULES
|
|
struct bpf_trace_module {
|
|
struct module *module;
|
|
struct list_head list;
|
|
};
|
|
|
|
static LIST_HEAD(bpf_trace_modules);
|
|
static DEFINE_MUTEX(bpf_module_mutex);
|
|
|
|
static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
|
|
{
|
|
struct bpf_raw_event_map *btp, *ret = NULL;
|
|
struct bpf_trace_module *btm;
|
|
unsigned int i;
|
|
|
|
mutex_lock(&bpf_module_mutex);
|
|
list_for_each_entry(btm, &bpf_trace_modules, list) {
|
|
for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
|
|
btp = &btm->module->bpf_raw_events[i];
|
|
if (!strcmp(btp->tp->name, name)) {
|
|
if (try_module_get(btm->module))
|
|
ret = btp;
|
|
goto out;
|
|
}
|
|
}
|
|
}
|
|
out:
|
|
mutex_unlock(&bpf_module_mutex);
|
|
return ret;
|
|
}
|
|
#else
|
|
static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
|
|
{
|
|
return NULL;
|
|
}
|
|
#endif /* CONFIG_MODULES */
|
|
|
|
u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
|
|
u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
|
|
|
|
/**
|
|
* trace_call_bpf - invoke BPF program
|
|
* @call: tracepoint event
|
|
* @ctx: opaque context pointer
|
|
*
|
|
* kprobe handlers execute BPF programs via this helper.
|
|
* Can be used from static tracepoints in the future.
|
|
*
|
|
* Return: BPF programs always return an integer which is interpreted by
|
|
* kprobe handler as:
|
|
* 0 - return from kprobe (event is filtered out)
|
|
* 1 - store kprobe event into ring buffer
|
|
* Other values are reserved and currently alias to 1
|
|
*/
|
|
unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
|
|
{
|
|
unsigned int ret;
|
|
|
|
if (in_nmi()) /* not supported yet */
|
|
return 1;
|
|
|
|
preempt_disable();
|
|
|
|
if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
|
|
/*
|
|
* since some bpf program is already running on this cpu,
|
|
* don't call into another bpf program (same or different)
|
|
* and don't send kprobe event into ring-buffer,
|
|
* so return zero here
|
|
*/
|
|
ret = 0;
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
|
|
* to all call sites, we did a bpf_prog_array_valid() there to check
|
|
* whether call->prog_array is empty or not, which is
|
|
* a heurisitc to speed up execution.
|
|
*
|
|
* If bpf_prog_array_valid() fetched prog_array was
|
|
* non-NULL, we go into trace_call_bpf() and do the actual
|
|
* proper rcu_dereference() under RCU lock.
|
|
* If it turns out that prog_array is NULL then, we bail out.
|
|
* For the opposite, if the bpf_prog_array_valid() fetched pointer
|
|
* was NULL, you'll skip the prog_array with the risk of missing
|
|
* out of events when it was updated in between this and the
|
|
* rcu_dereference() which is accepted risk.
|
|
*/
|
|
ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
|
|
|
|
out:
|
|
__this_cpu_dec(bpf_prog_active);
|
|
preempt_enable();
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(trace_call_bpf);
|
|
|
|
#ifdef CONFIG_BPF_KPROBE_OVERRIDE
|
|
BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
|
|
{
|
|
regs_set_return_value(regs, rc);
|
|
override_function_with_return(regs);
|
|
return 0;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_override_return_proto = {
|
|
.func = bpf_override_return,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_ANYTHING,
|
|
};
|
|
#endif
|
|
|
|
BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
|
|
{
|
|
int ret;
|
|
|
|
ret = security_locked_down(LOCKDOWN_BPF_READ);
|
|
if (ret < 0)
|
|
goto out;
|
|
|
|
ret = probe_kernel_read(dst, unsafe_ptr, size);
|
|
if (unlikely(ret < 0))
|
|
out:
|
|
memset(dst, 0, size);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_probe_read_proto = {
|
|
.func = bpf_probe_read,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
|
|
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
|
|
.arg3_type = ARG_ANYTHING,
|
|
};
|
|
|
|
BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
|
|
u32, size)
|
|
{
|
|
/*
|
|
* Ensure we're in user context which is safe for the helper to
|
|
* run. This helper has no business in a kthread.
|
|
*
|
|
* access_ok() should prevent writing to non-user memory, but in
|
|
* some situations (nommu, temporary switch, etc) access_ok() does
|
|
* not provide enough validation, hence the check on KERNEL_DS.
|
|
*
|
|
* nmi_uaccess_okay() ensures the probe is not run in an interim
|
|
* state, when the task or mm are switched. This is specifically
|
|
* required to prevent the use of temporary mm.
|
|
*/
|
|
|
|
if (unlikely(in_interrupt() ||
|
|
current->flags & (PF_KTHREAD | PF_EXITING)))
|
|
return -EPERM;
|
|
if (unlikely(uaccess_kernel()))
|
|
return -EPERM;
|
|
if (unlikely(!nmi_uaccess_okay()))
|
|
return -EPERM;
|
|
|
|
return probe_user_write(unsafe_ptr, src, size);
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_probe_write_user_proto = {
|
|
.func = bpf_probe_write_user,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_ANYTHING,
|
|
.arg2_type = ARG_PTR_TO_MEM,
|
|
.arg3_type = ARG_CONST_SIZE,
|
|
};
|
|
|
|
static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
|
|
{
|
|
pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
|
|
current->comm, task_pid_nr(current));
|
|
|
|
return &bpf_probe_write_user_proto;
|
|
}
|
|
|
|
/*
|
|
* Only limited trace_printk() conversion specifiers allowed:
|
|
* %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
|
|
*/
|
|
BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
|
|
u64, arg2, u64, arg3)
|
|
{
|
|
bool str_seen = false;
|
|
int mod[3] = {};
|
|
int fmt_cnt = 0;
|
|
u64 unsafe_addr;
|
|
char buf[64];
|
|
int i;
|
|
|
|
/*
|
|
* bpf_check()->check_func_arg()->check_stack_boundary()
|
|
* guarantees that fmt points to bpf program stack,
|
|
* fmt_size bytes of it were initialized and fmt_size > 0
|
|
*/
|
|
if (fmt[--fmt_size] != 0)
|
|
return -EINVAL;
|
|
|
|
/* check format string for allowed specifiers */
|
|
for (i = 0; i < fmt_size; i++) {
|
|
if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
|
|
return -EINVAL;
|
|
|
|
if (fmt[i] != '%')
|
|
continue;
|
|
|
|
if (fmt_cnt >= 3)
|
|
return -EINVAL;
|
|
|
|
/* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
|
|
i++;
|
|
if (fmt[i] == 'l') {
|
|
mod[fmt_cnt]++;
|
|
i++;
|
|
} else if (fmt[i] == 'p' || fmt[i] == 's') {
|
|
mod[fmt_cnt]++;
|
|
/* disallow any further format extensions */
|
|
if (fmt[i + 1] != 0 &&
|
|
!isspace(fmt[i + 1]) &&
|
|
!ispunct(fmt[i + 1]))
|
|
return -EINVAL;
|
|
fmt_cnt++;
|
|
if (fmt[i] == 's') {
|
|
if (str_seen)
|
|
/* allow only one '%s' per fmt string */
|
|
return -EINVAL;
|
|
str_seen = true;
|
|
|
|
switch (fmt_cnt) {
|
|
case 1:
|
|
unsafe_addr = arg1;
|
|
arg1 = (long) buf;
|
|
break;
|
|
case 2:
|
|
unsafe_addr = arg2;
|
|
arg2 = (long) buf;
|
|
break;
|
|
case 3:
|
|
unsafe_addr = arg3;
|
|
arg3 = (long) buf;
|
|
break;
|
|
}
|
|
buf[0] = 0;
|
|
strncpy_from_unsafe(buf,
|
|
(void *) (long) unsafe_addr,
|
|
sizeof(buf));
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (fmt[i] == 'l') {
|
|
mod[fmt_cnt]++;
|
|
i++;
|
|
}
|
|
|
|
if (fmt[i] != 'i' && fmt[i] != 'd' &&
|
|
fmt[i] != 'u' && fmt[i] != 'x')
|
|
return -EINVAL;
|
|
fmt_cnt++;
|
|
}
|
|
|
|
/* Horrid workaround for getting va_list handling working with different
|
|
* argument type combinations generically for 32 and 64 bit archs.
|
|
*/
|
|
#define __BPF_TP_EMIT() __BPF_ARG3_TP()
|
|
#define __BPF_TP(...) \
|
|
__trace_printk(0 /* Fake ip */, \
|
|
fmt, ##__VA_ARGS__)
|
|
|
|
#define __BPF_ARG1_TP(...) \
|
|
((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
|
|
? __BPF_TP(arg1, ##__VA_ARGS__) \
|
|
: ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
|
|
? __BPF_TP((long)arg1, ##__VA_ARGS__) \
|
|
: __BPF_TP((u32)arg1, ##__VA_ARGS__)))
|
|
|
|
#define __BPF_ARG2_TP(...) \
|
|
((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
|
|
? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
|
|
: ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
|
|
? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
|
|
: __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
|
|
|
|
#define __BPF_ARG3_TP(...) \
|
|
((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
|
|
? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
|
|
: ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
|
|
? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
|
|
: __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
|
|
|
|
return __BPF_TP_EMIT();
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_trace_printk_proto = {
|
|
.func = bpf_trace_printk,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_MEM,
|
|
.arg2_type = ARG_CONST_SIZE,
|
|
};
|
|
|
|
const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
|
|
{
|
|
/*
|
|
* this program might be calling bpf_trace_printk,
|
|
* so allocate per-cpu printk buffers
|
|
*/
|
|
trace_printk_init_buffers();
|
|
|
|
return &bpf_trace_printk_proto;
|
|
}
|
|
|
|
static __always_inline int
|
|
get_map_perf_counter(struct bpf_map *map, u64 flags,
|
|
u64 *value, u64 *enabled, u64 *running)
|
|
{
|
|
struct bpf_array *array = container_of(map, struct bpf_array, map);
|
|
unsigned int cpu = smp_processor_id();
|
|
u64 index = flags & BPF_F_INDEX_MASK;
|
|
struct bpf_event_entry *ee;
|
|
|
|
if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
|
|
return -EINVAL;
|
|
if (index == BPF_F_CURRENT_CPU)
|
|
index = cpu;
|
|
if (unlikely(index >= array->map.max_entries))
|
|
return -E2BIG;
|
|
|
|
ee = READ_ONCE(array->ptrs[index]);
|
|
if (!ee)
|
|
return -ENOENT;
|
|
|
|
return perf_event_read_local(ee->event, value, enabled, running);
|
|
}
|
|
|
|
BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
|
|
{
|
|
u64 value = 0;
|
|
int err;
|
|
|
|
err = get_map_perf_counter(map, flags, &value, NULL, NULL);
|
|
/*
|
|
* this api is ugly since we miss [-22..-2] range of valid
|
|
* counter values, but that's uapi
|
|
*/
|
|
if (err)
|
|
return err;
|
|
return value;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_perf_event_read_proto = {
|
|
.func = bpf_perf_event_read,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_CONST_MAP_PTR,
|
|
.arg2_type = ARG_ANYTHING,
|
|
};
|
|
|
|
BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
|
|
struct bpf_perf_event_value *, buf, u32, size)
|
|
{
|
|
int err = -EINVAL;
|
|
|
|
if (unlikely(size != sizeof(struct bpf_perf_event_value)))
|
|
goto clear;
|
|
err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
|
|
&buf->running);
|
|
if (unlikely(err))
|
|
goto clear;
|
|
return 0;
|
|
clear:
|
|
memset(buf, 0, size);
|
|
return err;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
|
|
.func = bpf_perf_event_read_value,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_CONST_MAP_PTR,
|
|
.arg2_type = ARG_ANYTHING,
|
|
.arg3_type = ARG_PTR_TO_UNINIT_MEM,
|
|
.arg4_type = ARG_CONST_SIZE,
|
|
};
|
|
|
|
static __always_inline u64
|
|
__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
|
|
u64 flags, struct perf_sample_data *sd)
|
|
{
|
|
struct bpf_array *array = container_of(map, struct bpf_array, map);
|
|
unsigned int cpu = smp_processor_id();
|
|
u64 index = flags & BPF_F_INDEX_MASK;
|
|
struct bpf_event_entry *ee;
|
|
struct perf_event *event;
|
|
|
|
if (index == BPF_F_CURRENT_CPU)
|
|
index = cpu;
|
|
if (unlikely(index >= array->map.max_entries))
|
|
return -E2BIG;
|
|
|
|
ee = READ_ONCE(array->ptrs[index]);
|
|
if (!ee)
|
|
return -ENOENT;
|
|
|
|
event = ee->event;
|
|
if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
|
|
event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
|
|
return -EINVAL;
|
|
|
|
if (unlikely(event->oncpu != cpu))
|
|
return -EOPNOTSUPP;
|
|
|
|
return perf_event_output(event, sd, regs);
|
|
}
|
|
|
|
/*
|
|
* Support executing tracepoints in normal, irq, and nmi context that each call
|
|
* bpf_perf_event_output
|
|
*/
|
|
struct bpf_trace_sample_data {
|
|
struct perf_sample_data sds[3];
|
|
};
|
|
|
|
static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
|
|
static DEFINE_PER_CPU(int, bpf_trace_nest_level);
|
|
BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
|
|
u64, flags, void *, data, u64, size)
|
|
{
|
|
struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
|
|
int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
|
|
struct perf_raw_record raw = {
|
|
.frag = {
|
|
.size = size,
|
|
.data = data,
|
|
},
|
|
};
|
|
struct perf_sample_data *sd;
|
|
int err;
|
|
|
|
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
|
|
err = -EBUSY;
|
|
goto out;
|
|
}
|
|
|
|
sd = &sds->sds[nest_level - 1];
|
|
|
|
if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
|
|
err = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
perf_sample_data_init(sd, 0, 0);
|
|
sd->raw = &raw;
|
|
|
|
err = __bpf_perf_event_output(regs, map, flags, sd);
|
|
|
|
out:
|
|
this_cpu_dec(bpf_trace_nest_level);
|
|
return err;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_perf_event_output_proto = {
|
|
.func = bpf_perf_event_output,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_CONST_MAP_PTR,
|
|
.arg3_type = ARG_ANYTHING,
|
|
.arg4_type = ARG_PTR_TO_MEM,
|
|
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
|
|
};
|
|
|
|
static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
|
|
struct bpf_nested_pt_regs {
|
|
struct pt_regs regs[3];
|
|
};
|
|
static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
|
|
static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
|
|
|
|
u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
|
|
void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
|
|
{
|
|
int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
|
|
struct perf_raw_frag frag = {
|
|
.copy = ctx_copy,
|
|
.size = ctx_size,
|
|
.data = ctx,
|
|
};
|
|
struct perf_raw_record raw = {
|
|
.frag = {
|
|
{
|
|
.next = ctx_size ? &frag : NULL,
|
|
},
|
|
.size = meta_size,
|
|
.data = meta,
|
|
},
|
|
};
|
|
struct perf_sample_data *sd;
|
|
struct pt_regs *regs;
|
|
u64 ret;
|
|
|
|
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
|
|
ret = -EBUSY;
|
|
goto out;
|
|
}
|
|
sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
|
|
regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
|
|
|
|
perf_fetch_caller_regs(regs);
|
|
perf_sample_data_init(sd, 0, 0);
|
|
sd->raw = &raw;
|
|
|
|
ret = __bpf_perf_event_output(regs, map, flags, sd);
|
|
out:
|
|
this_cpu_dec(bpf_event_output_nest_level);
|
|
return ret;
|
|
}
|
|
|
|
BPF_CALL_0(bpf_get_current_task)
|
|
{
|
|
return (long) current;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_get_current_task_proto = {
|
|
.func = bpf_get_current_task,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
};
|
|
|
|
BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
|
|
{
|
|
struct bpf_array *array = container_of(map, struct bpf_array, map);
|
|
struct cgroup *cgrp;
|
|
|
|
if (unlikely(idx >= array->map.max_entries))
|
|
return -E2BIG;
|
|
|
|
cgrp = READ_ONCE(array->ptrs[idx]);
|
|
if (unlikely(!cgrp))
|
|
return -EAGAIN;
|
|
|
|
return task_under_cgroup_hierarchy(current, cgrp);
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
|
|
.func = bpf_current_task_under_cgroup,
|
|
.gpl_only = false,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_CONST_MAP_PTR,
|
|
.arg2_type = ARG_ANYTHING,
|
|
};
|
|
|
|
BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
|
|
const void *, unsafe_ptr)
|
|
{
|
|
int ret;
|
|
|
|
ret = security_locked_down(LOCKDOWN_BPF_READ);
|
|
if (ret < 0)
|
|
goto out;
|
|
|
|
/*
|
|
* The strncpy_from_unsafe() call will likely not fill the entire
|
|
* buffer, but that's okay in this circumstance as we're probing
|
|
* arbitrary memory anyway similar to bpf_probe_read() and might
|
|
* as well probe the stack. Thus, memory is explicitly cleared
|
|
* only in error case, so that improper users ignoring return
|
|
* code altogether don't copy garbage; otherwise length of string
|
|
* is returned that can be used for bpf_perf_event_output() et al.
|
|
*/
|
|
ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
|
|
if (unlikely(ret < 0))
|
|
out:
|
|
memset(dst, 0, size);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_probe_read_str_proto = {
|
|
.func = bpf_probe_read_str,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
|
|
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
|
|
.arg3_type = ARG_ANYTHING,
|
|
};
|
|
|
|
struct send_signal_irq_work {
|
|
struct irq_work irq_work;
|
|
struct task_struct *task;
|
|
u32 sig;
|
|
};
|
|
|
|
static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
|
|
|
|
static void do_bpf_send_signal(struct irq_work *entry)
|
|
{
|
|
struct send_signal_irq_work *work;
|
|
|
|
work = container_of(entry, struct send_signal_irq_work, irq_work);
|
|
group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, PIDTYPE_TGID);
|
|
}
|
|
|
|
BPF_CALL_1(bpf_send_signal, u32, sig)
|
|
{
|
|
struct send_signal_irq_work *work = NULL;
|
|
|
|
/* Similar to bpf_probe_write_user, task needs to be
|
|
* in a sound condition and kernel memory access be
|
|
* permitted in order to send signal to the current
|
|
* task.
|
|
*/
|
|
if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
|
|
return -EPERM;
|
|
if (unlikely(uaccess_kernel()))
|
|
return -EPERM;
|
|
if (unlikely(!nmi_uaccess_okay()))
|
|
return -EPERM;
|
|
/* Task should not be pid=1 to avoid kernel panic. */
|
|
if (unlikely(is_global_init(current)))
|
|
return -EPERM;
|
|
|
|
if (irqs_disabled()) {
|
|
/* Do an early check on signal validity. Otherwise,
|
|
* the error is lost in deferred irq_work.
|
|
*/
|
|
if (unlikely(!valid_signal(sig)))
|
|
return -EINVAL;
|
|
|
|
work = this_cpu_ptr(&send_signal_work);
|
|
if (work->irq_work.flags & IRQ_WORK_BUSY)
|
|
return -EBUSY;
|
|
|
|
/* Add the current task, which is the target of sending signal,
|
|
* to the irq_work. The current task may change when queued
|
|
* irq works get executed.
|
|
*/
|
|
work->task = current;
|
|
work->sig = sig;
|
|
irq_work_queue(&work->irq_work);
|
|
return 0;
|
|
}
|
|
|
|
return group_send_sig_info(sig, SEND_SIG_PRIV, current, PIDTYPE_TGID);
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_send_signal_proto = {
|
|
.func = bpf_send_signal,
|
|
.gpl_only = false,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_ANYTHING,
|
|
};
|
|
|
|
static const struct bpf_func_proto *
|
|
tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|
{
|
|
switch (func_id) {
|
|
case BPF_FUNC_map_lookup_elem:
|
|
return &bpf_map_lookup_elem_proto;
|
|
case BPF_FUNC_map_update_elem:
|
|
return &bpf_map_update_elem_proto;
|
|
case BPF_FUNC_map_delete_elem:
|
|
return &bpf_map_delete_elem_proto;
|
|
case BPF_FUNC_map_push_elem:
|
|
return &bpf_map_push_elem_proto;
|
|
case BPF_FUNC_map_pop_elem:
|
|
return &bpf_map_pop_elem_proto;
|
|
case BPF_FUNC_map_peek_elem:
|
|
return &bpf_map_peek_elem_proto;
|
|
case BPF_FUNC_probe_read:
|
|
return &bpf_probe_read_proto;
|
|
case BPF_FUNC_ktime_get_ns:
|
|
return &bpf_ktime_get_ns_proto;
|
|
case BPF_FUNC_ktime_get_boot_ns:
|
|
return &bpf_ktime_get_boot_ns_proto;
|
|
case BPF_FUNC_tail_call:
|
|
return &bpf_tail_call_proto;
|
|
case BPF_FUNC_get_current_pid_tgid:
|
|
return &bpf_get_current_pid_tgid_proto;
|
|
case BPF_FUNC_get_current_task:
|
|
return &bpf_get_current_task_proto;
|
|
case BPF_FUNC_get_current_uid_gid:
|
|
return &bpf_get_current_uid_gid_proto;
|
|
case BPF_FUNC_get_current_comm:
|
|
return &bpf_get_current_comm_proto;
|
|
case BPF_FUNC_trace_printk:
|
|
return bpf_get_trace_printk_proto();
|
|
case BPF_FUNC_get_smp_processor_id:
|
|
return &bpf_get_smp_processor_id_proto;
|
|
case BPF_FUNC_get_numa_node_id:
|
|
return &bpf_get_numa_node_id_proto;
|
|
case BPF_FUNC_perf_event_read:
|
|
return &bpf_perf_event_read_proto;
|
|
case BPF_FUNC_probe_write_user:
|
|
return bpf_get_probe_write_proto();
|
|
case BPF_FUNC_current_task_under_cgroup:
|
|
return &bpf_current_task_under_cgroup_proto;
|
|
case BPF_FUNC_get_prandom_u32:
|
|
return &bpf_get_prandom_u32_proto;
|
|
case BPF_FUNC_probe_read_str:
|
|
return &bpf_probe_read_str_proto;
|
|
#ifdef CONFIG_CGROUPS
|
|
case BPF_FUNC_get_current_cgroup_id:
|
|
return &bpf_get_current_cgroup_id_proto;
|
|
#endif
|
|
case BPF_FUNC_send_signal:
|
|
return &bpf_send_signal_proto;
|
|
default:
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
static const struct bpf_func_proto *
|
|
kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|
{
|
|
switch (func_id) {
|
|
case BPF_FUNC_perf_event_output:
|
|
return &bpf_perf_event_output_proto;
|
|
case BPF_FUNC_get_stackid:
|
|
return &bpf_get_stackid_proto;
|
|
case BPF_FUNC_get_stack:
|
|
return &bpf_get_stack_proto;
|
|
case BPF_FUNC_perf_event_read_value:
|
|
return &bpf_perf_event_read_value_proto;
|
|
#ifdef CONFIG_BPF_KPROBE_OVERRIDE
|
|
case BPF_FUNC_override_return:
|
|
return &bpf_override_return_proto;
|
|
#endif
|
|
default:
|
|
return tracing_func_proto(func_id, prog);
|
|
}
|
|
}
|
|
|
|
/* bpf+kprobe programs can access fields of 'struct pt_regs' */
|
|
static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
|
|
const struct bpf_prog *prog,
|
|
struct bpf_insn_access_aux *info)
|
|
{
|
|
if (off < 0 || off >= sizeof(struct pt_regs))
|
|
return false;
|
|
if (type != BPF_READ)
|
|
return false;
|
|
if (off % size != 0)
|
|
return false;
|
|
/*
|
|
* Assertion for 32 bit to make sure last 8 byte access
|
|
* (BPF_DW) to the last 4 byte member is disallowed.
|
|
*/
|
|
if (off + size > sizeof(struct pt_regs))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
const struct bpf_verifier_ops kprobe_verifier_ops = {
|
|
.get_func_proto = kprobe_prog_func_proto,
|
|
.is_valid_access = kprobe_prog_is_valid_access,
|
|
};
|
|
|
|
const struct bpf_prog_ops kprobe_prog_ops = {
|
|
};
|
|
|
|
BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
|
|
u64, flags, void *, data, u64, size)
|
|
{
|
|
struct pt_regs *regs = *(struct pt_regs **)tp_buff;
|
|
|
|
/*
|
|
* r1 points to perf tracepoint buffer where first 8 bytes are hidden
|
|
* from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
|
|
* from there and call the same bpf_perf_event_output() helper inline.
|
|
*/
|
|
return ____bpf_perf_event_output(regs, map, flags, data, size);
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
|
|
.func = bpf_perf_event_output_tp,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_CONST_MAP_PTR,
|
|
.arg3_type = ARG_ANYTHING,
|
|
.arg4_type = ARG_PTR_TO_MEM,
|
|
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
|
|
};
|
|
|
|
BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
|
|
u64, flags)
|
|
{
|
|
struct pt_regs *regs = *(struct pt_regs **)tp_buff;
|
|
|
|
/*
|
|
* Same comment as in bpf_perf_event_output_tp(), only that this time
|
|
* the other helper's function body cannot be inlined due to being
|
|
* external, thus we need to call raw helper function.
|
|
*/
|
|
return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
|
|
flags, 0, 0);
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
|
|
.func = bpf_get_stackid_tp,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_CONST_MAP_PTR,
|
|
.arg3_type = ARG_ANYTHING,
|
|
};
|
|
|
|
BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
|
|
u64, flags)
|
|
{
|
|
struct pt_regs *regs = *(struct pt_regs **)tp_buff;
|
|
|
|
return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
|
|
(unsigned long) size, flags, 0);
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_get_stack_proto_tp = {
|
|
.func = bpf_get_stack_tp,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_PTR_TO_UNINIT_MEM,
|
|
.arg3_type = ARG_CONST_SIZE_OR_ZERO,
|
|
.arg4_type = ARG_ANYTHING,
|
|
};
|
|
|
|
static const struct bpf_func_proto *
|
|
tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|
{
|
|
switch (func_id) {
|
|
case BPF_FUNC_perf_event_output:
|
|
return &bpf_perf_event_output_proto_tp;
|
|
case BPF_FUNC_get_stackid:
|
|
return &bpf_get_stackid_proto_tp;
|
|
case BPF_FUNC_get_stack:
|
|
return &bpf_get_stack_proto_tp;
|
|
default:
|
|
return tracing_func_proto(func_id, prog);
|
|
}
|
|
}
|
|
|
|
static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
|
|
const struct bpf_prog *prog,
|
|
struct bpf_insn_access_aux *info)
|
|
{
|
|
if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
|
|
return false;
|
|
if (type != BPF_READ)
|
|
return false;
|
|
if (off % size != 0)
|
|
return false;
|
|
|
|
BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
|
|
return true;
|
|
}
|
|
|
|
const struct bpf_verifier_ops tracepoint_verifier_ops = {
|
|
.get_func_proto = tp_prog_func_proto,
|
|
.is_valid_access = tp_prog_is_valid_access,
|
|
};
|
|
|
|
const struct bpf_prog_ops tracepoint_prog_ops = {
|
|
};
|
|
|
|
BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
|
|
struct bpf_perf_event_value *, buf, u32, size)
|
|
{
|
|
int err = -EINVAL;
|
|
|
|
if (unlikely(size != sizeof(struct bpf_perf_event_value)))
|
|
goto clear;
|
|
err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
|
|
&buf->running);
|
|
if (unlikely(err))
|
|
goto clear;
|
|
return 0;
|
|
clear:
|
|
memset(buf, 0, size);
|
|
return err;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
|
|
.func = bpf_perf_prog_read_value,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_PTR_TO_UNINIT_MEM,
|
|
.arg3_type = ARG_CONST_SIZE,
|
|
};
|
|
|
|
static const struct bpf_func_proto *
|
|
pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|
{
|
|
switch (func_id) {
|
|
case BPF_FUNC_perf_event_output:
|
|
return &bpf_perf_event_output_proto_tp;
|
|
case BPF_FUNC_get_stackid:
|
|
return &bpf_get_stackid_proto_tp;
|
|
case BPF_FUNC_get_stack:
|
|
return &bpf_get_stack_proto_tp;
|
|
case BPF_FUNC_perf_prog_read_value:
|
|
return &bpf_perf_prog_read_value_proto;
|
|
default:
|
|
return tracing_func_proto(func_id, prog);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
|
|
* to avoid potential recursive reuse issue when/if tracepoints are added
|
|
* inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
|
|
*
|
|
* Since raw tracepoints run despite bpf_prog_active, support concurrent usage
|
|
* in normal, irq, and nmi context.
|
|
*/
|
|
struct bpf_raw_tp_regs {
|
|
struct pt_regs regs[3];
|
|
};
|
|
static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
|
|
static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
|
|
static struct pt_regs *get_bpf_raw_tp_regs(void)
|
|
{
|
|
struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
|
|
int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
|
|
|
|
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
|
|
this_cpu_dec(bpf_raw_tp_nest_level);
|
|
return ERR_PTR(-EBUSY);
|
|
}
|
|
|
|
return &tp_regs->regs[nest_level - 1];
|
|
}
|
|
|
|
static void put_bpf_raw_tp_regs(void)
|
|
{
|
|
this_cpu_dec(bpf_raw_tp_nest_level);
|
|
}
|
|
|
|
BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
|
|
struct bpf_map *, map, u64, flags, void *, data, u64, size)
|
|
{
|
|
struct pt_regs *regs = get_bpf_raw_tp_regs();
|
|
int ret;
|
|
|
|
if (IS_ERR(regs))
|
|
return PTR_ERR(regs);
|
|
|
|
perf_fetch_caller_regs(regs);
|
|
ret = ____bpf_perf_event_output(regs, map, flags, data, size);
|
|
|
|
put_bpf_raw_tp_regs();
|
|
return ret;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
|
|
.func = bpf_perf_event_output_raw_tp,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_CONST_MAP_PTR,
|
|
.arg3_type = ARG_ANYTHING,
|
|
.arg4_type = ARG_PTR_TO_MEM,
|
|
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
|
|
};
|
|
|
|
BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
|
|
struct bpf_map *, map, u64, flags)
|
|
{
|
|
struct pt_regs *regs = get_bpf_raw_tp_regs();
|
|
int ret;
|
|
|
|
if (IS_ERR(regs))
|
|
return PTR_ERR(regs);
|
|
|
|
perf_fetch_caller_regs(regs);
|
|
/* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
|
|
ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
|
|
flags, 0, 0);
|
|
put_bpf_raw_tp_regs();
|
|
return ret;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
|
|
.func = bpf_get_stackid_raw_tp,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_CONST_MAP_PTR,
|
|
.arg3_type = ARG_ANYTHING,
|
|
};
|
|
|
|
BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
|
|
void *, buf, u32, size, u64, flags)
|
|
{
|
|
struct pt_regs *regs = get_bpf_raw_tp_regs();
|
|
int ret;
|
|
|
|
if (IS_ERR(regs))
|
|
return PTR_ERR(regs);
|
|
|
|
perf_fetch_caller_regs(regs);
|
|
ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
|
|
(unsigned long) size, flags, 0);
|
|
put_bpf_raw_tp_regs();
|
|
return ret;
|
|
}
|
|
|
|
static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
|
|
.func = bpf_get_stack_raw_tp,
|
|
.gpl_only = true,
|
|
.ret_type = RET_INTEGER,
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
.arg2_type = ARG_PTR_TO_MEM,
|
|
.arg3_type = ARG_CONST_SIZE_OR_ZERO,
|
|
.arg4_type = ARG_ANYTHING,
|
|
};
|
|
|
|
static const struct bpf_func_proto *
|
|
raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|
{
|
|
switch (func_id) {
|
|
case BPF_FUNC_perf_event_output:
|
|
return &bpf_perf_event_output_proto_raw_tp;
|
|
case BPF_FUNC_get_stackid:
|
|
return &bpf_get_stackid_proto_raw_tp;
|
|
case BPF_FUNC_get_stack:
|
|
return &bpf_get_stack_proto_raw_tp;
|
|
default:
|
|
return tracing_func_proto(func_id, prog);
|
|
}
|
|
}
|
|
|
|
static bool raw_tp_prog_is_valid_access(int off, int size,
|
|
enum bpf_access_type type,
|
|
const struct bpf_prog *prog,
|
|
struct bpf_insn_access_aux *info)
|
|
{
|
|
/* largest tracepoint in the kernel has 12 args */
|
|
if (off < 0 || off >= sizeof(__u64) * 12)
|
|
return false;
|
|
if (type != BPF_READ)
|
|
return false;
|
|
if (off % size != 0)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
|
|
.get_func_proto = raw_tp_prog_func_proto,
|
|
.is_valid_access = raw_tp_prog_is_valid_access,
|
|
};
|
|
|
|
const struct bpf_prog_ops raw_tracepoint_prog_ops = {
|
|
};
|
|
|
|
static bool raw_tp_writable_prog_is_valid_access(int off, int size,
|
|
enum bpf_access_type type,
|
|
const struct bpf_prog *prog,
|
|
struct bpf_insn_access_aux *info)
|
|
{
|
|
if (off == 0) {
|
|
if (size != sizeof(u64) || type != BPF_READ)
|
|
return false;
|
|
info->reg_type = PTR_TO_TP_BUFFER;
|
|
}
|
|
return raw_tp_prog_is_valid_access(off, size, type, prog, info);
|
|
}
|
|
|
|
const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
|
|
.get_func_proto = raw_tp_prog_func_proto,
|
|
.is_valid_access = raw_tp_writable_prog_is_valid_access,
|
|
};
|
|
|
|
const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
|
|
};
|
|
|
|
static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
|
|
const struct bpf_prog *prog,
|
|
struct bpf_insn_access_aux *info)
|
|
{
|
|
const int size_u64 = sizeof(u64);
|
|
|
|
if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
|
|
return false;
|
|
if (type != BPF_READ)
|
|
return false;
|
|
if (off % size != 0) {
|
|
if (sizeof(unsigned long) != 4)
|
|
return false;
|
|
if (size != 8)
|
|
return false;
|
|
if (off % size != 4)
|
|
return false;
|
|
}
|
|
|
|
switch (off) {
|
|
case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
|
|
bpf_ctx_record_field_size(info, size_u64);
|
|
if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
|
|
return false;
|
|
break;
|
|
case bpf_ctx_range(struct bpf_perf_event_data, addr):
|
|
bpf_ctx_record_field_size(info, size_u64);
|
|
if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
|
|
return false;
|
|
break;
|
|
default:
|
|
if (size != sizeof(long))
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
|
|
const struct bpf_insn *si,
|
|
struct bpf_insn *insn_buf,
|
|
struct bpf_prog *prog, u32 *target_size)
|
|
{
|
|
struct bpf_insn *insn = insn_buf;
|
|
|
|
switch (si->off) {
|
|
case offsetof(struct bpf_perf_event_data, sample_period):
|
|
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
|
|
data), si->dst_reg, si->src_reg,
|
|
offsetof(struct bpf_perf_event_data_kern, data));
|
|
*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
|
|
bpf_target_off(struct perf_sample_data, period, 8,
|
|
target_size));
|
|
break;
|
|
case offsetof(struct bpf_perf_event_data, addr):
|
|
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
|
|
data), si->dst_reg, si->src_reg,
|
|
offsetof(struct bpf_perf_event_data_kern, data));
|
|
*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
|
|
bpf_target_off(struct perf_sample_data, addr, 8,
|
|
target_size));
|
|
break;
|
|
default:
|
|
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
|
|
regs), si->dst_reg, si->src_reg,
|
|
offsetof(struct bpf_perf_event_data_kern, regs));
|
|
*insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
|
|
si->off);
|
|
break;
|
|
}
|
|
|
|
return insn - insn_buf;
|
|
}
|
|
|
|
const struct bpf_verifier_ops perf_event_verifier_ops = {
|
|
.get_func_proto = pe_prog_func_proto,
|
|
.is_valid_access = pe_prog_is_valid_access,
|
|
.convert_ctx_access = pe_prog_convert_ctx_access,
|
|
};
|
|
|
|
const struct bpf_prog_ops perf_event_prog_ops = {
|
|
};
|
|
|
|
static DEFINE_MUTEX(bpf_event_mutex);
|
|
|
|
#define BPF_TRACE_MAX_PROGS 64
|
|
|
|
int perf_event_attach_bpf_prog(struct perf_event *event,
|
|
struct bpf_prog *prog)
|
|
{
|
|
struct bpf_prog_array *old_array;
|
|
struct bpf_prog_array *new_array;
|
|
int ret = -EEXIST;
|
|
|
|
/*
|
|
* Kprobe override only works if they are on the function entry,
|
|
* and only if they are on the opt-in list.
|
|
*/
|
|
if (prog->kprobe_override &&
|
|
(!trace_kprobe_on_func_entry(event->tp_event) ||
|
|
!trace_kprobe_error_injectable(event->tp_event)))
|
|
return -EINVAL;
|
|
|
|
mutex_lock(&bpf_event_mutex);
|
|
|
|
if (event->prog)
|
|
goto unlock;
|
|
|
|
old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
|
|
if (old_array &&
|
|
bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
|
|
ret = -E2BIG;
|
|
goto unlock;
|
|
}
|
|
|
|
ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
|
|
if (ret < 0)
|
|
goto unlock;
|
|
|
|
/* set the new array to event->tp_event and set event->prog */
|
|
event->prog = prog;
|
|
rcu_assign_pointer(event->tp_event->prog_array, new_array);
|
|
bpf_prog_array_free(old_array);
|
|
|
|
unlock:
|
|
mutex_unlock(&bpf_event_mutex);
|
|
return ret;
|
|
}
|
|
|
|
void perf_event_detach_bpf_prog(struct perf_event *event)
|
|
{
|
|
struct bpf_prog_array *old_array;
|
|
struct bpf_prog_array *new_array;
|
|
int ret;
|
|
|
|
mutex_lock(&bpf_event_mutex);
|
|
|
|
if (!event->prog)
|
|
goto unlock;
|
|
|
|
old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
|
|
ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
|
|
if (ret == -ENOENT)
|
|
goto unlock;
|
|
if (ret < 0) {
|
|
bpf_prog_array_delete_safe(old_array, event->prog);
|
|
} else {
|
|
rcu_assign_pointer(event->tp_event->prog_array, new_array);
|
|
bpf_prog_array_free(old_array);
|
|
}
|
|
|
|
bpf_prog_put(event->prog);
|
|
event->prog = NULL;
|
|
|
|
unlock:
|
|
mutex_unlock(&bpf_event_mutex);
|
|
}
|
|
|
|
int perf_event_query_prog_array(struct perf_event *event, void __user *info)
|
|
{
|
|
struct perf_event_query_bpf __user *uquery = info;
|
|
struct perf_event_query_bpf query = {};
|
|
struct bpf_prog_array *progs;
|
|
u32 *ids, prog_cnt, ids_len;
|
|
int ret;
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
if (event->attr.type != PERF_TYPE_TRACEPOINT)
|
|
return -EINVAL;
|
|
if (copy_from_user(&query, uquery, sizeof(query)))
|
|
return -EFAULT;
|
|
|
|
ids_len = query.ids_len;
|
|
if (ids_len > BPF_TRACE_MAX_PROGS)
|
|
return -E2BIG;
|
|
ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
|
|
if (!ids)
|
|
return -ENOMEM;
|
|
/*
|
|
* The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
|
|
* is required when user only wants to check for uquery->prog_cnt.
|
|
* There is no need to check for it since the case is handled
|
|
* gracefully in bpf_prog_array_copy_info.
|
|
*/
|
|
|
|
mutex_lock(&bpf_event_mutex);
|
|
progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
|
|
ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
|
|
mutex_unlock(&bpf_event_mutex);
|
|
|
|
if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
|
|
copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
|
|
ret = -EFAULT;
|
|
|
|
kfree(ids);
|
|
return ret;
|
|
}
|
|
|
|
extern struct bpf_raw_event_map __start__bpf_raw_tp[];
|
|
extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
|
|
|
|
struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
|
|
{
|
|
struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
|
|
|
|
for (; btp < __stop__bpf_raw_tp; btp++) {
|
|
if (!strcmp(btp->tp->name, name))
|
|
return btp;
|
|
}
|
|
|
|
return bpf_get_raw_tracepoint_module(name);
|
|
}
|
|
|
|
void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
|
|
{
|
|
struct module *mod;
|
|
|
|
preempt_disable();
|
|
mod = __module_address((unsigned long)btp);
|
|
module_put(mod);
|
|
preempt_enable();
|
|
}
|
|
|
|
static __always_inline
|
|
void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
|
|
{
|
|
rcu_read_lock();
|
|
preempt_disable();
|
|
(void) BPF_PROG_RUN(prog, args);
|
|
preempt_enable();
|
|
rcu_read_unlock();
|
|
}
|
|
|
|
#define UNPACK(...) __VA_ARGS__
|
|
#define REPEAT_1(FN, DL, X, ...) FN(X)
|
|
#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
|
|
#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
|
|
#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
|
|
|
|
#define SARG(X) u64 arg##X
|
|
#define COPY(X) args[X] = arg##X
|
|
|
|
#define __DL_COM (,)
|
|
#define __DL_SEM (;)
|
|
|
|
#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
|
|
|
|
#define BPF_TRACE_DEFN_x(x) \
|
|
void bpf_trace_run##x(struct bpf_prog *prog, \
|
|
REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
|
|
{ \
|
|
u64 args[x]; \
|
|
REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
|
|
__bpf_trace_run(prog, args); \
|
|
} \
|
|
EXPORT_SYMBOL_GPL(bpf_trace_run##x)
|
|
BPF_TRACE_DEFN_x(1);
|
|
BPF_TRACE_DEFN_x(2);
|
|
BPF_TRACE_DEFN_x(3);
|
|
BPF_TRACE_DEFN_x(4);
|
|
BPF_TRACE_DEFN_x(5);
|
|
BPF_TRACE_DEFN_x(6);
|
|
BPF_TRACE_DEFN_x(7);
|
|
BPF_TRACE_DEFN_x(8);
|
|
BPF_TRACE_DEFN_x(9);
|
|
BPF_TRACE_DEFN_x(10);
|
|
BPF_TRACE_DEFN_x(11);
|
|
BPF_TRACE_DEFN_x(12);
|
|
|
|
static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
|
|
{
|
|
struct tracepoint *tp = btp->tp;
|
|
|
|
/*
|
|
* check that program doesn't access arguments beyond what's
|
|
* available in this tracepoint
|
|
*/
|
|
if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
|
|
return -EINVAL;
|
|
|
|
if (prog->aux->max_tp_access > btp->writable_size)
|
|
return -EINVAL;
|
|
|
|
return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
|
|
prog);
|
|
}
|
|
|
|
int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
|
|
{
|
|
return __bpf_probe_register(btp, prog);
|
|
}
|
|
|
|
int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
|
|
{
|
|
return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
|
|
}
|
|
|
|
int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
|
|
u32 *fd_type, const char **buf,
|
|
u64 *probe_offset, u64 *probe_addr)
|
|
{
|
|
bool is_tracepoint, is_syscall_tp;
|
|
struct bpf_prog *prog;
|
|
int flags, err = 0;
|
|
|
|
prog = event->prog;
|
|
if (!prog)
|
|
return -ENOENT;
|
|
|
|
/* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
|
|
if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
|
|
return -EOPNOTSUPP;
|
|
|
|
*prog_id = prog->aux->id;
|
|
flags = event->tp_event->flags;
|
|
is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
|
|
is_syscall_tp = is_syscall_trace_event(event->tp_event);
|
|
|
|
if (is_tracepoint || is_syscall_tp) {
|
|
*buf = is_tracepoint ? event->tp_event->tp->name
|
|
: event->tp_event->name;
|
|
*fd_type = BPF_FD_TYPE_TRACEPOINT;
|
|
*probe_offset = 0x0;
|
|
*probe_addr = 0x0;
|
|
} else {
|
|
/* kprobe/uprobe */
|
|
err = -EOPNOTSUPP;
|
|
#ifdef CONFIG_KPROBE_EVENTS
|
|
if (flags & TRACE_EVENT_FL_KPROBE)
|
|
err = bpf_get_kprobe_info(event, fd_type, buf,
|
|
probe_offset, probe_addr,
|
|
event->attr.type == PERF_TYPE_TRACEPOINT);
|
|
#endif
|
|
#ifdef CONFIG_UPROBE_EVENTS
|
|
if (flags & TRACE_EVENT_FL_UPROBE)
|
|
err = bpf_get_uprobe_info(event, fd_type, buf,
|
|
probe_offset, probe_addr,
|
|
event->attr.type == PERF_TYPE_TRACEPOINT);
|
|
#endif
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
static int __init send_signal_irq_work_init(void)
|
|
{
|
|
int cpu;
|
|
struct send_signal_irq_work *work;
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
work = per_cpu_ptr(&send_signal_work, cpu);
|
|
init_irq_work(&work->irq_work, do_bpf_send_signal);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
subsys_initcall(send_signal_irq_work_init);
|
|
|
|
#ifdef CONFIG_MODULES
|
|
static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
|
|
void *module)
|
|
{
|
|
struct bpf_trace_module *btm, *tmp;
|
|
struct module *mod = module;
|
|
|
|
if (mod->num_bpf_raw_events == 0 ||
|
|
(op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
|
|
return 0;
|
|
|
|
mutex_lock(&bpf_module_mutex);
|
|
|
|
switch (op) {
|
|
case MODULE_STATE_COMING:
|
|
btm = kzalloc(sizeof(*btm), GFP_KERNEL);
|
|
if (btm) {
|
|
btm->module = module;
|
|
list_add(&btm->list, &bpf_trace_modules);
|
|
}
|
|
break;
|
|
case MODULE_STATE_GOING:
|
|
list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
|
|
if (btm->module == module) {
|
|
list_del(&btm->list);
|
|
kfree(btm);
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
mutex_unlock(&bpf_module_mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct notifier_block bpf_module_nb = {
|
|
.notifier_call = bpf_event_notify,
|
|
};
|
|
|
|
static int __init bpf_event_init(void)
|
|
{
|
|
register_module_notifier(&bpf_module_nb);
|
|
return 0;
|
|
}
|
|
|
|
fs_initcall(bpf_event_init);
|
|
#endif /* CONFIG_MODULES */
|