7.1.5-rc1 review
Subsystems (15)
Greg Kroah-Hartman
maintainerPATCH 1775/2077 dmaengine: dw-edma: Add spinlock to protect DONE_INT_MASK and ABORT_INT_MASK
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Frank Li <[email protected]>
commit 8ffba0171c6bbce5f093c6dba5a02c0805b31203 upstream.
The DONE_INT_MASK and ABORT_INT_MASK registers are shared by all DMA channels, and modifying them requires a read-modify-write sequence. Because this operation is not atomic, concurrent calls to dw_edma_v0_core_start() can introduce race conditions if two channels update these registers simultaneously.
Add a spinlock to serialize access to these registers and prevent race conditions.
--- drivers/dma/dw-edma/dw-edma-core.h | 2 +- drivers/dma/dw-edma/dw-edma-v0-core.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
drivers/dma/dw-edma/dw-edma-core.h +1 -1
@@ -109,7 +109,7 @@ struct dw_edma {struct dw_edma_chan *chan; -raw_spinlock_t lock;/* Only for legacy */ +raw_spinlock_t lock;/* Protect v0 shared registers */ struct dw_edma_chip *chip;
drivers/dma/dw-edma/dw-edma-v0-core.c +6 -0
@@ -364,6 +364,7 @@ static void dw_edma_v0_core_start(struct {struct dw_edma_chan *chan = chunk->chan;struct dw_edma *dw = chan->dw; +unsigned long flags;u32 tmp;dw_edma_v0_core_write_chunk (chunk);
@@ -408,6 +409,8 @@ static void dw_edma_v0_core_start(struct } }/* Interrupt unmask - done, abort */ +raw_spin_lock_irqsave (&dw->lock, flags); + tmp =GET_RW_32 (dw, chan->dir, int_mask); tmp &= ~FIELD_PREP (EDMA_V0_DONE_INT_MASK ,BIT (chan->id)); tmp &= ~FIELD_PREP (EDMA_V0_ABORT_INT_MASK ,BIT (chan->id));
@@ -416,6 +419,9 @@ static void dw_edma_v0_core_start(struct tmp =GET_RW_32 (dw, chan->dir, linked_list_err_en); tmp |=FIELD_PREP (EDMA_V0_LINKED_LIST_ERR_MASK ,BIT (chan->id));SET_RW_32 (dw, chan->dir, linked_list_err_en, tmp); + +raw_spin_unlock_irqrestore (&dw->lock, flags); +/* Channel control */ SET_CH_32 (dw, chan->dir, chan->id, ch_control1, (DW_EDMA_V0_CCS |DW_EDMA_V0_LLE ));
Greg Kroah-Hartman
maintainerPATCH 1776/2077 dmaengine: sh: rz-dmac: Fix incorrect NULL check for list_first_entry()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Claudiu Beznea <[email protected]>
commit 5fbf3a2a3b96ef5810e6e0fbc601f82067629bc5 upstream.
list_first_entry() does not return NULL when the list is empty, making the existing NULL check invalid. Use list_first_entry_or_null() instead.
--- drivers/dma/sh/rz-dmac.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/sh/rz-dmac.c +2 -2
@@ -723,8 +723,8 @@ static u32 rz_dmac_chan_get_residue(struct rz_dmac_chan *channel,u32 crla, crtb, i;/* Get current processing virtual descriptor */ - current_desc =list_first_entry (&channel->ld_active, - struct rz_dmac_desc, node); + current_desc =list_first_entry_or_null (&channel->ld_active, + struct rz_dmac_desc, node);if (!current_desc)return 0 ;
Signature · 1 line
2.55.0
Greg Kroah-Hartman
maintainerPATCH 1777/2077 dmaengine: dw-edma-pcie: Reject devices without driver data
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Koichiro Den <[email protected]>
commit 11d7cfe0c119691b2dafbb699bbca90258c678aa upstream.
dw_edma_pcie_probe() treats the PCI device ID driver_data as the template for the controller layout and copies it unconditionally. A device bound dynamically via sysfs can match the driver without that data, which leads to a NULL pointer dereference.
Reject such matches before enabling the device.
--- drivers/dma/dw-edma/dw-edma-pcie.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/dma/dw-edma/dw-edma-pcie.c +3 -0
@@ -314,6 +314,9 @@ static int dw_edma_pcie_probe(struct pciint i, mask;bool non_ll = false; +if (!pdata) +return -ENODEV ; + struct dw_edma_pcie_data *vsec_data __free(kfree) =kmalloc_obj (*vsec_data);if (!vsec_data)
Greg Kroah-Hartman
maintainerPATCH 1778/2077 dmaengine: sh: rz-dmac: Move interrupt request after everything is set up
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Claudiu Beznea <[email protected]>
commit 731712403ddb39d1a76a11abf339a0615bc85de7 upstream.
Once the interrupt is requested, the interrupt handler may run immediately. Since the IRQ handler can access channel->ch_base, which is initialized only after requesting the IRQ, this may lead to invalid memory access. Likewise, the IRQ thread may access uninitialized data (the ld_free, ld_queue, and ld_active lists), which may also lead to issues.
Request the interrupts only after everything is set up. To keep the error path simpler, use dmam_alloc_coherent() instead of dma_alloc_coherent().
--- drivers/dma/sh/rz-dmac.c | 88 +++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 55 deletions(-)
drivers/dma/sh/rz-dmac.c +33 -55
@@ -981,25 +981,6 @@ static int rz_dmac_chan_probe(struct rz_ channel->index = index; channel->mid_rid = -EINVAL ; -/* Request the channel interrupt. */ -scnprintf (pdev_irqname,sizeof (pdev_irqname),"ch%u" , index); - irq =platform_get_irq_byname (pdev, pdev_irqname); -if (irq <0 ) -return irq; - - irqname =devm_kasprintf (dmac->dev,GFP_KERNEL ,"%s:%u" , -dev_name (dmac->dev), index); -if (!irqname) -return -ENOMEM ; - - ret =devm_request_threaded_irq (dmac->dev, irq, rz_dmac_irq_handler, - rz_dmac_irq_handler_thread,0 , - irqname, channel); -if (ret) { -dev_err (dmac->dev,"failed to request IRQ %u (%d)\n" , irq, ret); -return ret; - } -/* Set io base address for each channel */ if (index <8 ) { channel->ch_base = dmac->base + CHANNEL_0_7_OFFSET +
@@ -1012,9 +993,9 @@ static int rz_dmac_chan_probe(struct rz_ }/* Allocate descriptors */ - lmdesc =dma_alloc_coherent (&pdev->dev, -sizeof (struct rz_lmdesc ) *DMAC_NR_LMDESC , - &channel->lmdesc.base_dma,GFP_KERNEL ); + lmdesc =dmam_alloc_coherent (&pdev->dev, +sizeof (struct rz_lmdesc ) *DMAC_NR_LMDESC , + &channel->lmdesc.base_dma,GFP_KERNEL );if (!lmdesc) {dev_err (&pdev->dev,"Can't allocate memory (lmdesc)\n" );return -ENOMEM ;
@@ -1030,7 +1011,24 @@ static int rz_dmac_chan_probe(struct rz_INIT_LIST_HEAD (&channel->ld_free);INIT_LIST_HEAD (&channel->ld_active); -return 0 ; +/* Request the channel interrupt. */ +scnprintf (pdev_irqname,sizeof (pdev_irqname),"ch%u" , index); + irq =platform_get_irq_byname (pdev, pdev_irqname); +if (irq <0 ) +return irq; + + irqname =devm_kasprintf (dmac->dev,GFP_KERNEL ,"%s:%u" , +dev_name (dmac->dev), index); +if (!irqname) +return -ENOMEM ; + + ret =devm_request_threaded_irq (dmac->dev, irq, rz_dmac_irq_handler, + rz_dmac_irq_handler_thread,0 , + irqname, channel); +if (ret) +dev_err (dmac->dev,"failed to request IRQ %u (%d)\n" , irq, ret); + +return ret; }static void rz_dmac_put_device (void *_dev)
@@ -1099,7 +1097,6 @@ static int rz_dmac_probe(struct platformconst char *irqname ="error" ;struct dma_device *engine;struct rz_dmac *dmac; -int channel_num;int ret;int irq;u8 i;
@@ -1132,18 +1129,6 @@ static int rz_dmac_probe(struct platformreturn PTR_ERR (dmac->ext_base); } -/* Register interrupt handler for error */ - irq =platform_get_irq_byname_optional (pdev, irqname); -if (irq >0 ) { - ret =devm_request_irq (&pdev->dev, irq, rz_dmac_irq_handler,0 , - irqname,NULL ); -if (ret) { -dev_err (&pdev->dev,"failed to request IRQ %u (%d)\n" , - irq, ret); -return ret; - } - } -/* Initialize the channels. */ INIT_LIST_HEAD (&dmac->engine.channels);
@@ -1169,6 +1154,18 @@ static int rz_dmac_probe(struct platform goto err; } +/* Register interrupt handler for error */ + irq =platform_get_irq_byname_optional (pdev, irqname); +if (irq >0 ) { + ret =devm_request_irq (&pdev->dev, irq, rz_dmac_irq_handler,0 , + irqname,NULL ); +if (ret) { +dev_err (&pdev->dev,"failed to request IRQ %u (%d)\n" , + irq, ret); + goto err; + } + } +/* Register the DMAC as a DMA provider for DT. */ ret =of_dma_controller_register (pdev->dev.of_node, rz_dmac_of_xlate,NULL );
@@ -1210,16 +1207,6 @@ static int rz_dmac_probe(struct platform dma_register_err:of_dma_controller_free (pdev->dev.of_node); err: - channel_num = i ? i -1 :0 ; -for (i =0 ; i < channel_num; i++) { -struct rz_dmac_chan *channel = &dmac->channels[i]; - -dma_free_coherent (&pdev->dev, -sizeof (struct rz_lmdesc ) *DMAC_NR_LMDESC , - channel->lmdesc.base, - channel->lmdesc.base_dma); - } -reset_control_assert (dmac->rstc); err_pm_runtime_put:pm_runtime_put (&pdev->dev);
@@ -1232,18 +1219,9 @@ err_pm_disable:static void rz_dmac_remove (struct platform_device *pdev) {struct rz_dmac *dmac =platform_get_drvdata (pdev); -unsigned int i;dma_async_device_unregister (&dmac->engine);of_dma_controller_free (pdev->dev.of_node); -for (i =0 ; i < dmac->n_channels; i++) { -struct rz_dmac_chan *channel = &dmac->channels[i]; - -dma_free_coherent (&pdev->dev, -sizeof (struct rz_lmdesc ) *DMAC_NR_LMDESC , - channel->lmdesc.base, - channel->lmdesc.base_dma); - }reset_control_assert (dmac->rstc);pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev);
Greg Kroah-Hartman
maintainerPATCH 1779/2077 platform/x86/intel/tpmi: use cleanup helpers in mem_write()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: ZhaoJinming <[email protected]>
commit a221557958e3a82d8565729d445a7385963f30b6 upstream.
In mem_write(), the temporary array returned by parse_int_array_user() must be released on all exit paths. Convert the array variable to use cleanup.h scope-based cleanup so it is freed automatically on return.
This also moves the array declaration next to parse_int_array_user() as required by cleanup.h usage guidelines.
--- drivers/platform/x86/intel/vsec_tpmi.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -16
@@ -50,6 +50,7 @@#include <linux/auxiliary_bus.h> #include <linux/bitfield.h> #include <linux/debugfs.h> +#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/intel_tpmi.h> #include <linux/intel_vsec.h>
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t lstruct seq_file *m = file->private_data;struct intel_tpmi_pm_feature *pfs = m->private;u32 addr, value, punit, size; -u32 num_elems, *array; +u32 num_elems;void __iomem *mem;int ret;
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t lif (!size)return -EIO ; + u32 *array __free(kfree) = NULL; ret =parse_int_array_user (userbuf, len, (int **)&array);if (ret <0 )return ret; num_elems = *array; -if (num_elems !=3 ) { - ret = -EINVAL ; - goto exit_write; - } +if (num_elems !=3 ) +return -EINVAL ; punit = array[1 ]; addr = array[2 ];
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t lif (!IS_ALIGNED (addr,sizeof (u32)))return -EINVAL ; -if (punit >= pfs->pfs_header.num_entries) { - ret = -EINVAL ; - goto exit_write; - } +if (punit >= pfs->pfs_header.num_entries) +return -EINVAL ; -if (addr >= size) { - ret = -EINVAL ; - goto exit_write; - } +if (addr >= size) +return -EINVAL ;mutex_lock (&tpmi_dev_lock);
@@ -525,9 +521,6 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l unlock_mem_write:mutex_unlock (&tpmi_dev_lock); -exit_write: -kfree (array); -return ret; }
Signature · 1 line
2.55.0
Greg Kroah-Hartman
maintainerPATCH 1780/2077 platform/x86: dell-laptop: fix missing cleanups in init error path
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Haoxiang Li <[email protected]>
commit 6e9cab2247e5b243ae2d907ce7c948a8a9c8d61a upstream.
dell_init() initializes several resources after dell_setup_rfkill(), including the optional touchpad LED, keyboard backlight LED, battery hook, debugfs directory and dell-laptop notifier.
If a later LED or backlight registration fails, the error path only tears down the battery hook and rfkill resources. This leaves the notifier, debugfs directory, keyboard backlight LED and optional touchpad LED registered after dell_init() returns an error.
Add the missing cleanup calls before tearing down rfkill.
--- drivers/platform/x86/dell/dell-laptop.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/platform/x86/dell/dell-laptop.c +5 -0
@@ -2551,7 +2551,12 @@ fail_backlight:if (mute_led_registered)led_classdev_unregister (&mute_led_cdev); fail_led: +dell_laptop_unregister_notifier (&dell_laptop_notifier); +debugfs_remove_recursive (dell_laptop_dir);dell_battery_exit (); +kbd_led_exit (); +if (quirks && quirks->touchpad_led) +touchpad_led_exit ();dell_cleanup_rfkill (); fail_rfkill:platform_device_del (platform_device);
Greg Kroah-Hartman
maintainerPATCH 1781/2077 platform/x86: hp-wmi: Add support for Omen 16-ap0xxx (8D26)
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Krishna Chomal <[email protected]>
commit 0aab31d47c2e857ca05028d718d1e0d239e683ad upstream.
The HP Omen 16-ap0xxx (board ID: 8D26) has the same WMI interface as other Victus S boards, but requires quirks for correctly switching thermal profile.
Add the DMI board name to victus_s_thermal_profile_boards[] table and map it to omen_v1_legacy_thermal_params.
Testing on board 8D26 confirmed that platform profile is registered successfully and fan RPMs are readable and controllable.
--- drivers/platform/x86/hp/hp-wmi.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +4 -0
@@ -250,6 +250,10 @@ static const struct dmi_system_id victus .driver_data = (void *)&victus_s_thermal_params, }, { + .matches = { DMI_MATCH(DMI_BOARD_NAME, "8D26") }, + .driver_data = (void *)&omen_v1_legacy_thermal_params, + }, + { .matches = { DMI_MATCH(DMI_BOARD_NAME, "8D41") }, .driver_data = (void *)&omen_v1_no_ec_thermal_params, },
Greg Kroah-Hartman
maintainerPATCH 1782/2077 platform/x86: hp-wmi: Add support for Omen 16-ap0xxx (8E35)
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Krishna Chomal <[email protected]>
commit 56b7981c6f21670c0a1a62e6d2f9afb380e2596d upstream.
The HP Omen 16-ap0xxx (board ID: 8E35) has the same WMI interface as other Victus S boards, but requires quirks for correctly switching thermal profile.
Add the DMI board name to victus_s_thermal_profile_boards[] table and map it to omen_v1_legacy_thermal_params.
Testing on board 8E35 confirmed that platform profile is registered successfully and fan RPMs are readable and controllable.
--- drivers/platform/x86/hp/hp-wmi.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +4 -0
@@ -261,6 +261,10 @@ static const struct dmi_system_id victus .matches = { DMI_MATCH(DMI_BOARD_NAME, "8D87") }, .driver_data = (void *)&omen_v1_no_ec_thermal_params, }, + { + .matches = { DMI_MATCH(DMI_BOARD_NAME, "8E35") }, + .driver_data = (void *)&omen_v1_legacy_thermal_params, + }, {}, };
Greg Kroah-Hartman
maintainerPATCH 1783/2077 platform/x86: ISST: Restore SST-PP control to all domains
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Srinivas Pandruvada <[email protected]>
commit 2565a28cdcdcb035e151d285efcba26bccb3726e upstream.
The SST-PP control offset is only restored to power domain 0 after resume. During suspend, control values are read and stored for all power domains.
Use pd_info->sst_base instead of power_domain_info->sst_base, which only points to power domain 0 base address.
--- drivers/platform/x86/intel/speed_select_if/isst_tpmi_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/intel/speed_select_if/isst_tpmi_core.c +1 -1
@@ -1804,7 +1804,7 @@ process_pp_resume:if (!(pd_info->sst_header.cap_mask &SST_PP_CAP_PP_ENABLE ))continue ; -writeq (pd_info->saved_pp_control, power_domain_info->sst_base + +writeq (pd_info->saved_pp_control, pd_info->sst_base + pd_info->sst_header.pp_offset +SST_PP_CONTROL_OFFSET ); } }
Greg Kroah-Hartman
maintainerPATCH 1784/2077 platform/x86/amd/pmc: Check for intermediate wakeup in function
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Daniel Gibson <[email protected]>
commit 3bdd6fc11fbfa8249483f4b716ead51e43e3a0cd upstream.
Refactor code introduced by commit 9f5595d5f03f ("pmc: Require at least 2.5 seconds between HW sleep cycles") to allow adding different conditions for that delay in an upcoming change.
--- drivers/platform/x86/amd/pmc/pmc.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/platform/x86/amd/pmc/pmc.c +14 -3
@@ -598,6 +598,19 @@ static int amd_pmc_verify_czn_rtc(structreturn rc; } +static bool amd_pmc_intermediate_wakeup_need_delay (struct amd_pmc_dev *pdev) +{ +/* +* Starting a new HW sleep cycle right after waking from one +* can cause electrical problems triggering the over voltage protection. +* That is avoided by delaying the next suspend a bit, see also +* https://lore.kernel.org/all/[email protected]/ +*/ +struct smu_metrics table; + +return get_metrics_table (pdev, &table) ==0 && table.s0i3_last_entry_status; +} +static void amd_pmc_s2idle_prepare (void ) {struct amd_pmc_dev *pdev = &pmc;
@@ -632,11 +645,9 @@ static void amd_pmc_s2idle_prepare(void)static void amd_pmc_s2idle_check (void ) {struct amd_pmc_dev *pdev = &pmc; -struct smu_metrics table;int rc; -/* Avoid triggering OVP */ -if (!get_metrics_table (pdev, &table) && table.s0i3_last_entry_status) +if (amd_pmc_intermediate_wakeup_need_delay (pdev))msleep (2500 );/* Dump the IdleMask before we add to the STB */
Greg Kroah-Hartman
maintainerPATCH 1785/2077 platform/x86/amd/pmc: Delay suspend for some Lenovo Laptops
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Daniel Gibson <[email protected]>
commit 9b9e60dd31da054a37d601e9fcabdfd8a2bff354 upstream.
Some IdeaPad Slim 3 devices and similar with AMD CPUs have a nonfunctional keyboard and lid switch after s2idle.
It helps to delay suspend by 2.5 seconds so the EC has some time to do whatever it needs to get done before suspend - unfortunately at least on my 16ABR8 waking it with a timer (wakealarm) still triggers the issue, but at least normal resume via keypress or lid works fine. On the 14ARP10 wakealarm has been reported to also work fine with this patch.
This issue has been reported for many different devices, this patch has been tested with the Zen3-based IdeaPad Slim 3 16ABR8 (82XR) and the Zen3+-based IdeaPad Slim 3 14ARP10 (83K6) and IdeaPad Slim 3 15ARP10 (83MM).
--- drivers/platform/x86/amd/pmc/pmc-quirks.c | 39 ++++++++++++++++++++++++++++++ drivers/platform/x86/amd/pmc/pmc.c | 24 +++++++++++++++++- drivers/platform/x86/amd/pmc/pmc.h | 1 3 files changed, 63 insertions(+), 1 deletion(-)
drivers/platform/x86/amd/pmc/pmc-quirks.c +39 -0
@@ -18,6 +18,7 @@struct quirk_entry {u32 s2idle_bug_mmio;bool spurious_8042; +bool need_suspend_delay; };static struct quirk_entry quirk_s2idle_bug = {
@@ -33,6 +34,10 @@ static struct quirk_entry quirk_s2idle_s .spurious_8042 = true, }; +static struct quirk_entry quirk_s2idle_need_suspend_delay = { + .need_suspend_delay = true, +}; +static const struct dmi_system_id fwbug_list[] = { { .ident ="L14 Gen2 AMD" ,
@@ -203,6 +208,35 @@ static const struct dmi_system_id fwbug_ DMI_MATCH(DMI_PRODUCT_NAME, "82XQ"), } }, +/* https://bugzilla.kernel.org/show_bug.cgi?id=221383 */ + { + .ident = "Zen3-based IdeaPad Slim and similar", + .driver_data = &quirk_s2idle_need_suspend_delay, + .matches = { +DMI_MATCH (DMI_BOARD_VENDOR ,"LENOVO" ), +/* +* Note: there are also some Zen2-based 82X* devices that +* need different quirks, they're already handled above +*/ + DMI_MATCH(DMI_PRODUCT_NAME, "82X"), + } + }, + { + .ident ="Zen3+-based IdeaPad Slim and similar" , + .driver_data = &quirk_s2idle_need_suspend_delay, + .matches = { +DMI_MATCH (DMI_BOARD_VENDOR ,"LENOVO" ), + DMI_MATCH(DMI_PRODUCT_NAME, "83K"), + } + }, + { + .ident ="IdeaPad Slim 3 15ARP10 (83MM)" , + .driver_data = &quirk_s2idle_need_suspend_delay, + .matches = { +DMI_MATCH (DMI_BOARD_VENDOR ,"LENOVO" ), + DMI_MATCH(DMI_PRODUCT_NAME, "83MM"), + } + }, /* https://bugzilla.kernel.org/show_bug.cgi?id=221273 */ { .ident = "Thinkpad L14 Gen3",
@@ -356,6 +390,11 @@ void amd_pmc_process_restore_quirks(struamd_pmc_skip_nvme_smi_handler (dev->quirks->s2idle_bug_mmio); } +bool amd_pmc_quirk_need_suspend_delay (struct amd_pmc_dev *dev) +{ +return dev->quirks && dev->quirks->need_suspend_delay; +} +void amd_pmc_quirks_init (struct amd_pmc_dev *dev) {const struct dmi_system_id *dmi_id;
drivers/platform/x86/amd/pmc/pmc.c +23 -1
@@ -611,6 +611,27 @@ static bool amd_pmc_intermediate_wakeup_return get_metrics_table (pdev, &table) ==0 && table.s0i3_last_entry_status; } +static bool amd_pmc_want_suspend_delay (struct amd_pmc_dev *pdev) +{ +/* +* Some Lenovo Laptops (like different IdeaPad 3 Slims) need some +* me-time before sleeping or they get uncooperative after waking +* up and don't send events for keyboard and lid switch anymore. +* +* Unfortunately this doesn't entirely fix the problem: It can still +* happen when resuming with a timer (wakealarm), but at least the +* more common usecases (wakeup by opening lid or pressing a key) +* work fine with this workaround. +* +* See https://bugzilla.kernel.org/show_bug.cgi?id=221383 +*/ +if (!disable_workarounds &&amd_pmc_quirk_need_suspend_delay (pdev)) { +dev_info (pdev->dev,"Delaying suspend by 2.5s to avoid platform bug\n" ); +return true; + } +return false; +} +static void amd_pmc_s2idle_prepare (void ) {struct amd_pmc_dev *pdev = &pmc;
@@ -647,7 +668,8 @@ static void amd_pmc_s2idle_check(void)struct amd_pmc_dev *pdev = &pmc;int rc; -if (amd_pmc_intermediate_wakeup_need_delay (pdev)) +if (amd_pmc_intermediate_wakeup_need_delay (pdev) || +amd_pmc_want_suspend_delay (pdev))msleep (2500 );/* Dump the IdleMask before we add to the STB */
drivers/platform/x86/amd/pmc/pmc.h +1 -0
@@ -147,6 +147,7 @@ enum amd_pmc_def { };void amd_pmc_process_restore_quirks (struct amd_pmc_dev *dev); +bool amd_pmc_quirk_need_suspend_delay (struct amd_pmc_dev *dev);void amd_pmc_quirks_init (struct amd_pmc_dev *dev);void amd_mp2_stb_init (struct amd_pmc_dev *dev);void amd_mp2_stb_deinit (struct amd_pmc_dev *dev);
Greg Kroah-Hartman
maintainerPATCH 1786/2077 platform/x86/amd/pmc: Add delay_suspend module parameter
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Daniel Gibson <[email protected]>
commit 428b9fd2dce50b4dc5cd9ade10b92efcf57ce7aa upstream.
Enabling the new delay_suspend module parameter delays suspend for 2.5 seconds which is known to help for some AMD-based Lenovo Laptops that otherwise failed to send/receive events for key presses or the lid switch after s2idle. Apparently the EC needs to do some things in the background before suspend or it gets into a bad state.
There are many reports of AMD-based laptops (mostly but not exclusively IdeaPads) about similar issues on the web; this parameter gives affected users an easy way to try out if their issues have the same root cause and to work around them until their specific device is added to the quirks list.
The parameter description has a note encouraging users to report their device so it can be added to the quirks list, inspired by a similar request in parameter descriptions of the ideapad-laptop module.
The module parameter can be set to "1" to explicitly enable it, "0" to disable it even on devices that are assumed to be affected, or -1 (the default) to enable it if the device is assumed to be affected (according to fwbug_list[])
--- drivers/platform/x86/amd/pmc/pmc.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
drivers/platform/x86/amd/pmc/pmc.c +23 -2
@@ -16,6 +16,7 @@#include <linux/bits.h> #include <linux/debugfs.h> #include <linux/delay.h> +#include <linux/dmi.h> #include <linux/io.h> #include <linux/iopoll.h> #include <linux/limits.h>
@@ -89,6 +90,11 @@ static bool disable_workarounds;module_param (disable_workarounds, bool,0644 );MODULE_PARM_DESC (disable_workarounds,"Disable workarounds for platform bugs" ); +static int delay_suspend =-1 ; +module_param (delay_suspend, int,0644 ); +MODULE_PARM_DESC (delay_suspend, +"Delays s2idle by 2.5 seconds to work around buggy ECs, often causing keyboard issues after suspend. 0: don't delay, 1: do delay, -1 (default): let amd_pmc decide. If you need this please report this to: [email protected]" ); +static struct amd_pmc_dev pmc;static inline u32 amd_pmc_reg_read (struct amd_pmc_dev *dev,int reg_offset)
@@ -625,8 +631,23 @@ static bool amd_pmc_want_suspend_delay(s* * See https://bugzilla.kernel.org/show_bug.cgi?id=221383 */ -if (!disable_workarounds &&amd_pmc_quirk_need_suspend_delay (pdev)) { -dev_info (pdev->dev,"Delaying suspend by 2.5s to avoid platform bug\n" ); +if (amd_pmc_quirk_need_suspend_delay (pdev)) { +/* +* delay_suspend=1 force-enables this, otherwise it can be +* disabled with disable_workarounds or delay_suspend=0 +*/ +if (delay_suspend ==1 || (delay_suspend ==-1 && !disable_workarounds)) { +dev_info (pdev->dev,"Delaying suspend by 2.5s to avoid platform bug\n" ); +return true; + } +dev_info (pdev->dev,"Not delaying suspend because of module parameter, even though your device is assumed to need it!\n" ); + }else if (delay_suspend ==1 ) { +dev_info (pdev->dev,"Delaying suspend by 2.5s because delay_suspend=1. If this solves problems on your machine, please report this whole line to: [email protected] so it can be automatically detected as affected in the future. System Vendor: \"%s\" Product Name: \"%s\" Product Family: \"%s\" Board Vendor: \"%s\" Board Name: \"%s\"\n" , +dmi_get_system_info (DMI_SYS_VENDOR ), +dmi_get_system_info (DMI_PRODUCT_NAME ), +dmi_get_system_info (DMI_PRODUCT_FAMILY ), +dmi_get_system_info (DMI_BOARD_VENDOR ), +dmi_get_system_info (DMI_BOARD_NAME ));return true; }return false;
Greg Kroah-Hartman
maintainerPATCH 1787/2077 platform/x86/amd/pmc: Dont log during intermediate wakeups
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Daniel Gibson <[email protected]>
commit 037f0b03c663a247366673a807834389107995b7 upstream.
The ECs in the IdeaPads that need the delay_suspend quirk send lots of messages when charging, which not only causes intermediate wakeups when suspended, but also prevents the device from reaching the deepest suspend state.
Because of this amd_pmc_intermediate_wakeup_need_delay() returns false during intermediate wakeups and amd_pmc_want_suspend_delay() is called. So far it always logged its "Delaying suspend by 2.5s ..." messages then, which spams dmesg. This commit makes sure that those messages are only logged once per suspend.
--- drivers/platform/x86/amd/pmc/pmc.c | 39 +++++++++++++++++++++++++++++-------- drivers/platform/x86/amd/pmc/pmc.h | 1 2 files changed, 32 insertions(+), 8 deletions(-)
drivers/platform/x86/amd/pmc/pmc.c +31 -8
@@ -620,6 +620,20 @@ static bool amd_pmc_intermediate_wakeup_static bool amd_pmc_want_suspend_delay (struct amd_pmc_dev *pdev) {/* +* intermediate_wakeup implies that the machine didn't get to deepest sleep +* state before - otherwise this function isn't called in amd_pmc_s2idle_check() +* because amd_pmc_intermediate_wakeup_need_delay() returns true first. +* On some IdeaPads that happens when charging, because the EC seems +* to send lots of messages then that wake the machine. +* +* But even in that case, the sleep here is necessary (on those IdeaPads), +* otherwise they wake up completely (resume) after a few seconds. +* So this variable is only used to avoid spamming dmesg on each +* intermediate wakeup. +*/ +bool intermediate_wakeup = !pdev->is_first_check_after_suspend; + +/* * Some Lenovo Laptops (like different IdeaPad 3 Slims) need some * me-time before sleeping or they get uncooperative after waking * up and don't send events for keyboard and lid switch anymore.
@@ -637,17 +651,20 @@ static bool amd_pmc_want_suspend_delay(s* disabled with disable_workarounds or delay_suspend=0 */ if (delay_suspend ==1 || (delay_suspend ==-1 && !disable_workarounds)) { -dev_info (pdev->dev,"Delaying suspend by 2.5s to avoid platform bug\n" ); +if (!intermediate_wakeup) +dev_info (pdev->dev,"Delaying suspend by 2.5s to avoid platform bug\n" );return true; } -dev_info (pdev->dev,"Not delaying suspend because of module parameter, even though your device is assumed to need it!\n" ); +if (!intermediate_wakeup) +dev_info (pdev->dev,"Not delaying suspend because of module parameter, even though your device is assumed to need it!\n" ); }else if (delay_suspend ==1 ) { -dev_info (pdev->dev,"Delaying suspend by 2.5s because delay_suspend=1. If this solves problems on your machine, please report this whole line to: [email protected] so it can be automatically detected as affected in the future. System Vendor: \"%s\" Product Name: \"%s\" Product Family: \"%s\" Board Vendor: \"%s\" Board Name: \"%s\"\n" , -dmi_get_system_info (DMI_SYS_VENDOR ), -dmi_get_system_info (DMI_PRODUCT_NAME ), -dmi_get_system_info (DMI_PRODUCT_FAMILY ), -dmi_get_system_info (DMI_BOARD_VENDOR ), -dmi_get_system_info (DMI_BOARD_NAME )); +if (!intermediate_wakeup) +dev_info (pdev->dev,"Delaying suspend by 2.5s because delay_suspend=1. If this solves problems on your machine, please report this whole line to: [email protected] so it can be automatically detected as affected in the future. System Vendor: \"%s\" Product Name: \"%s\" Product Family: \"%s\" Board Vendor: \"%s\" Board Name: \"%s\"\n" , +dmi_get_system_info (DMI_SYS_VENDOR ), +dmi_get_system_info (DMI_PRODUCT_NAME ), +dmi_get_system_info (DMI_PRODUCT_FAMILY ), +dmi_get_system_info (DMI_BOARD_VENDOR ), +dmi_get_system_info (DMI_BOARD_NAME ));return true; }return false;
@@ -660,6 +677,9 @@ static void amd_pmc_s2idle_prepare(void)u8 msg;u32 arg =1 ; +/* Reset this variable because this is a fresh suspend */ + pdev->is_first_check_after_suspend = true; +/* Reset and Start SMU logging - to monitor the s0i3 stats */ amd_pmc_setup_smu_logging (pdev);
@@ -699,6 +719,9 @@ static void amd_pmc_s2idle_check(void) rc =amd_stb_write (pdev,AMD_PMC_STB_S2IDLE_CHECK );if (rc)dev_err (pdev->dev,"error writing to STB: %d\n" , rc); + +/* remember that first check after suspend is done (until next prepare) */ + pdev->is_first_check_after_suspend = false; }static int amd_pmc_dump_data (struct amd_pmc_dev *pdev)
drivers/platform/x86/amd/pmc/pmc.h +1 -0
@@ -114,6 +114,7 @@ struct amd_pmc_dev {struct dentry *dbgfs_dir;struct quirk_entry *quirks;bool disable_8042_wakeup; +bool is_first_check_after_suspend;struct amd_mp2_dev *mp2;struct stb_arg stb_arg; };
Greg Kroah-Hartman
maintainerPATCH 1788/2077 pkey: Move keytype check from pkey api to handler
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Holger Dengler <[email protected]>
commit 754e9e49b76fd5be339172aa98544182ed3ca75e upstream.
The PKEY_VERIFYPROTK ioctl takes data from user-space and verifies the contained protected key. While checking the integrity of the ioctl request structure is the responsibility of the generic pkey_api code, the verification of the contained protected key is the responsibility of the pkey handler.
The keytype verification (based on the calculated bitsize of the key) is part of the protected key verification and therefore the responsibility of the pkey handler (which already verifies it). Therefore the keytype verification is removed from the generic pkey_api code.
As the calculation of the key bitsize is currently wrong, the removal of the keytype check in pkey_api also removes this wrong calculation. For this reason, the commit is flagged with the Fixes:
tag.
--- drivers/s390/crypto/pkey_api.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/s390/crypto/pkey_api.c +1 -10
@@ -327,7 +327,6 @@ static int pkey_ioctl_verifyprotk(struct {struct pkey_verifyprotk kvp;struct protaeskeytoken *t; -u32 keytype;u8 *tmpbuf;int rc;
@@ -341,14 +340,6 @@ static int pkey_ioctl_verifyprotk(structreturn -EINVAL ; } - keytype =pkey_aes_bitsize_to_keytype (8 * kvp.protkey.len); -if (!keytype) { -PKEY_DBF_ERR ("%s unknown/unsupported protkey length %u\n" , - __func__, kvp.protkey.len); -memzero_explicit (&kvp,sizeof (kvp)); -return -EINVAL ; - } -/* build a 'protected key token' from the raw protected key */ tmpbuf =kzalloc (sizeof (*t),GFP_KERNEL );if (!tmpbuf) {
@@ -358,7 +349,7 @@ static int pkey_ioctl_verifyprotk(struct t = (struct protaeskeytoken *)tmpbuf; t->type =TOKTYPE_NON_CCA ; t->version =TOKVER_PROTECTED_KEY ; - t->keytype = keytype; + t->keytype = kvp.protkey.type; t->len = kvp.protkey.len;memcpy (t->protkey, kvp.protkey.protkey, kvp.protkey.len);
Greg Kroah-Hartman
maintainerPATCH 1789/2077 smb: client: use kvzalloc() for megabyte buffer in simple fallocate
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Fredric Cover <[email protected]>
commit 806c00c23e3ce8eae397a40ced536ef88ae4e012 upstream.
Currently in smb3_simple_fallocate_range(), a 1 MB buffer is allocated using kzalloc(). Under heavy memory fragmentation, a contiguous 1 MB block of physical memory (an order-8 allocation) may not be available, causing the allocation to fail.
This failure was observed during xfstests generic/013 on a 4GB RAM test machine running fsstress:
fsstress: page allocation failure: order:8,
mode:0x40dc0(GFP_KERNEL|__GFP_ZERO|__GFP_COMP),
nodemask=(null),cpuset=/,mems_allowed=0
Call Trace: <TASK> dump_stack_lvl+0x5d/0x80 warn_alloc+0x163/0x190 __alloc_pages_slowpath.constprop.0+0x71b/0x12f0 __alloc_frozen_pages_noprof+0x2f6/0x340 alloc_pages_mpol+0xb6/0x170 ___kmalloc_large_node+0xb3/0xd0 __kmalloc_large_noprof+0x1e/0xc0 smb3_simple_falloc.isra.0+0x62b/0x960 cifs_fallocate+0xed/0x180 vfs_fallocate+0x165/0x3c0 __x64_sys_fallocate+0x48/0xa0 do_syscall_64+0xe1/0x640 entry_SYSCALL_64_after_hwframe+0x76/0x7e </TASK>
Node 0 Normal: 3375*4kB ... 7*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB
Since this scratch buffer does not require physically contiguous memory, switch the allocation to kvzalloc(). This retains the performance benefits of kmalloc() under normal conditions, while gracefully falling back to virtually contiguous memory when physical allocation fails.
--- fs/smb/client/smb2ops.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/smb/client/smb2ops.c +2 -2
@@ -3595,7 +3595,7 @@ static int smb3_simple_fallocate_range(uif (rc) goto out; - buf =kzalloc (1024 *1024 ,GFP_KERNEL ); + buf =kvzalloc (1024 *1024 ,GFP_KERNEL );if (buf ==NULL ) { rc = -ENOMEM ; goto out;
@@ -3652,7 +3652,7 @@ static int smb3_simple_fallocate_range(u out:kfree (out_data); -kfree (buf); +kvfree (buf);return rc; }
Greg Kroah-Hartman
maintainerPATCH 1790/2077 ksmbd: fix integer overflow in set_file_allocation_info()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Ibrahim Hashimov <[email protected]>
commit 1c0ae3df692ea2a4ce992f786346154e75a3f0d5 upstream.
set_file_allocation_info() converts the client-supplied FILE_ALLOCATION_INFORMATION::AllocationSize into a 512-byte block count with:
alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
AllocationSize is a fully client-controlled __le64 field; the only validation performed by the caller (smb2_set_info_file(), case FILE_ALLOCATION_INFORMATION) is that the fixed buffer is at least sizeof(struct smb2_file_alloc_info) == 8 bytes. The value itself is never range-checked before this arithmetic.
When AllocationSize is close to U64_MAX (e.g. 0xffffffffffffffff), "AllocationSize + 511" wraps around mod 2^64 to a small number (0xffffffffffffffff + 511 = 510), so alloc_blks becomes 0. Since any existing regular file has stat.blocks > 0, the function then takes the "shrink" branch and calls:
ksmbd_vfs_truncate(work, fp, alloc_blks * 512); /* == 0 */
silently truncating the file to size 0, even though the client asked to grow the allocation to (what looks like) the maximum possible size. The trailing "if (size < alloc_blks * 512) i_size_write(inode, size);" restore is guarded by a comparison that is never true once alloc_blks == 0, so the truncation is not undone. This lets an authenticated SMB client that already holds an open handle with FILE_WRITE_DATA on a file silently truncate that same file to size 0 via a single crafted SET_INFO(FILE_ALLOCATION_INFORMATION) request advertising a near-U64_MAX AllocationSize, even though the request asks to grow the file's allocation rather than shrink it. This is a functional/data-loss bug, not a privilege-boundary
violation: the same client could already truncate the file via FILE_END_OF_FILE_INFORMATION or a plain write.
Fix it by validating AllocationSize against MAX_LFS_FILESIZE, the same upper bound the VFS itself uses to reject unrepresentable file sizes, before doing the "+511" rounding, and rejecting oversized values with -EINVAL. Bounding AllocationSize to MAX_LFS_FILESIZE - 511 guarantees the "+511" addition cannot wrap, and that the subsequent "alloc_blks * 512" values passed to vfs_fallocate() and ksmbd_vfs_truncate() stay within a representable loff_t as well.
No legitimate SMB client asks for an allocation size anywhere near 2^64 bytes, so this only rejects a value that was previously silently misinterpreted as zero.
Runtime-verified on a v6.19 KASAN test stand: sending SET_INFO (FILE_ALLOCATION_INFORMATION) with AllocationSize = 0xffffffffffffffff against ksmbd now returns -EINVAL and leaves the target file's size unchanged, where the unpatched kernel truncated it from 4096 to 0 bytes.
--- fs/smb/server/smb2pdu.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +14 -1
@@ -6382,6 +6382,7 @@ static int set_file_allocation_info(stru*/ loff_t alloc_blks; +u64 alloc_size;struct inode *inode;struct kstat stat;int rc;
@@ -6397,7 +6398,19 @@ static int set_file_allocation_info(struif (rc)return rc; - alloc_blks = (le64_to_cpu (file_alloc_info->AllocationSize) +511 ) >>9 ; +/* +* AllocationSize is fully client-controlled (the caller only +* validates the fixed 8-byte buffer length). Reject values that +* would overflow the "round up to 512-byte blocks" conversion +* below instead of silently wrapping it to a tiny block count, +* which would truncate the file to a size the client never +* asked for. +*/ + alloc_size =le64_to_cpu (file_alloc_info->AllocationSize); +if (alloc_size >MAX_LFS_FILESIZE -511 ) +return -EINVAL ; + + alloc_blks = (alloc_size +511 ) >>9 ; inode =file_inode (fp->filp);if (alloc_blks > stat.blocks) {
Greg Kroah-Hartman
maintainerPATCH 1791/2077 hwmon: (ltc2992) add missing select REGMAP_I2C to Kconfig
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Joshua Crofts <[email protected]>
commit a35a6f1b20100057c66b7be5a8f6864661c3945c upstream.
The Kconfig entry for the LTC2992 sensor doesn't contain a select REGMAP_I2C parameter, causing build failures if regmap isn't selected previously during the build process.
--- drivers/hwmon/Kconfig | 1 + 1 file changed, 1 insertion(+)
drivers/hwmon/Kconfig +1 -0
@@ -1076,6 +1076,7 @@ config SENSORS_LTC2992 tristate "Linear Technology LTC2992" depends on I2C depends on GPIOLIB + select REGMAP_I2C help If you say yes here you get support for Linear Technology LTC2992 I2C System Monitor. The LTC2992 measures current, voltage, and
Greg Kroah-Hartman
maintainerPATCH 1792/2077 hwmon: (max6697) add missing select REGMAP_I2C to Kconfig
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Joshua Crofts <[email protected]>
commit ed576f2f4eef8cbe2c110da503825a8dc4717030 upstream.
The Kconfig entry for the MAX6697 sensor doesn't contain a select REGMAP_I2C parameter, causing build failures if regmap isn't selected previously during the build process.
--- drivers/hwmon/Kconfig | 1 + 1 file changed, 1 insertion(+)
drivers/hwmon/Kconfig +1 -0
@@ -1333,6 +1333,7 @@ config SENSORS_MAX6650 config SENSORS_MAX6697 tristate "Maxim MAX6697 and compatibles" depends on I2C + select REGMAP_I2C help If you say yes here you get support for MAX6581, MAX6602, MAX6622, MAX6636, MAX6689, MAX6693, MAX6694, MAX6697, MAX6698, and MAX6699
Greg Kroah-Hartman
maintainerPATCH 1793/2077 i2c: imx: fix locked bus on SMBus block-read of 0 (atomic)
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Vincent Jardin <[email protected]>
commit cb2fc37857693b55909fb77dc2c87cfbc1cdc476 upstream.
SMBus 3.1 6.5.7 allows a Block Read byte count of 0, but the atomic (polling) path rejects it as -EPROTO. Worse, it returns without a NACK+STOP: the next receive cycle has already started, so the target keeps holding SDA and the bus stays stuck until a power cycle for this i2c controller.
Reading I2DR to obtain the count likewise arms the next byte on the count > I2C_SMBUS_BLOCK_MAX path, which also returned -EPROTO directly and left the bus held.
Handle both: NACK the in-flight dummy byte (TXAK) and extend msgs->len so the existing last-byte handling emits STOP; the dummy byte is discarded. A count of 0 is a valid empty block read; a count above I2C_SMBUS_BLOCK_MAX is still reported as -EPROTO, but only after the bus has been released.
The interrupt-driven path has the same flaw from a later commit and is fixed separately, as it carries a different Fixes: tag and stable range.
--- drivers/i2c/busses/i2c-imx.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-imx.c +16 -3
@@ -1415,6 +1415,7 @@ static int i2c_imx_atomic_read(struct imint i, result;unsigned int temp;int block_data = msgs->flags &I2C_M_RECV_LEN ; +int block_err =0 ; result =i2c_imx_prepare_read (i2c_imx, msgs, false);if (result)
@@ -1436,8 +1437,20 @@ static int i2c_imx_atomic_read(struct im*/ if ((!i) && block_data) { len =imx_i2c_read_reg (i2c_imx,IMX_I2C_I2DR ); -if ((len ==0 ) || (len >I2C_SMBUS_BLOCK_MAX )) -return -EPROTO ; +if ((len ==0 ) || (len >I2C_SMBUS_BLOCK_MAX )) { +/* +* SMBus 3.1 6.5.7: support count byte of 0. +* I2C_SMBUS_BLOCK_MAX case should not hold the SDA either. +*/ +if (len >I2C_SMBUS_BLOCK_MAX ) + block_err = -EPROTO ; + temp =imx_i2c_read_reg (i2c_imx,IMX_I2C_I2CR ); + temp |=I2CR_TXAK ; +imx_i2c_write_reg (temp, i2c_imx,IMX_I2C_I2CR ); + msgs->buf[0 ] =0 ; + msgs->len =2 ; +continue ; + }dev_dbg (&i2c_imx->adapter.dev,"<%s> read length: 0x%X\n" , __func__, len);
@@ -1485,7 +1498,7 @@ static int i2c_imx_atomic_read(struct im"<%s> read byte: B%d=0x%X\n" , __func__, i, msgs->buf[i]); } -return 0 ; +return block_err; } static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs,
Greg Kroah-Hartman
maintainerPATCH 1794/2077 i2c: imx: fix locked bus on SMBus block-read of 0 (IRQ)
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Vincent Jardin <[email protected]>
commit 07fd9385f0d87dff4b34f355f68adf701080cb24 upstream.
SMBus 3.1 6.5.7 allows a Block Read byte count of 0, but the interrupt-driven block-read state machine rejects it as -EPROTO. Worse, it returns without a NACK+STOP: the next receive cycle has already started, so the target keeps holding SDA and the bus stays stuck until a power cycle of this i2c controller.
Accept count=0: NACK the in-flight dummy byte (TXAK) and set msg->len to 2 so i2c_imx_isr_read_continue() emits STOP via its normal last-byte path. The dummy byte is discarded; block-read callers only consume buf[0..count-1].
Reading I2DR has likewise already armed the next byte on the count > I2C_SMBUS_BLOCK_MAX error path, so NACK it (TXAK) before aborting with -EPROTO; otherwise the failing transfer's STOP cannot complete and the bus stays held.
The atomic path regressed earlier (v3.16) and is fixed separately; this patch covers only the v6.13 state-machine rework.
--- drivers/i2c/busses/i2c-imx.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/i2c/busses/i2c-imx.c +17 -0
@@ -1061,11 +1061,28 @@ static inline enum imx_i2c_state i2c_imxstatic inline void i2c_imx_isr_read_block_data_len (struct imx_i2c_struct *i2c_imx) {u8 len =imx_i2c_read_reg (i2c_imx,IMX_I2C_I2DR ); +unsigned int temp;if (len ==0 || len >I2C_SMBUS_BLOCK_MAX ) { +/* +* SMBus 3.1 6.5.7: support count byte of 0. +* I2C_SMBUS_BLOCK_MAX case should not hold the SDA either. +* So NACK it (TXAK) to not hold the bus. +*/ + temp =imx_i2c_read_reg (i2c_imx,IMX_I2C_I2CR ); + temp |=I2CR_TXAK ; +imx_i2c_write_reg (temp, i2c_imx,IMX_I2C_I2CR ); + +if (len ==0 ) { + i2c_imx->msg->buf[i2c_imx->msg_buf_idx++] =0 ; + i2c_imx->msg->len =2 ; +return ; + } + i2c_imx->isr_result = -EPROTO ; i2c_imx->state =IMX_I2C_STATE_FAILED ;wake_up (&i2c_imx->queue); +return ; } i2c_imx->msg->len += len; i2c_imx->msg->buf[i2c_imx->msg_buf_idx++] = len;
Greg Kroah-Hartman
maintainerPATCH 1795/2077 i2c: mediatek: fix WRRD for SoCs without auto_restart option
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Roman Vivchar <[email protected]>
commit deb35336b5bfed5db9231b5348bc1514db930797 upstream.
MediaTek mt65xx family SoCs have no auto restart, however, they still support the WRRD mode in the hardware. Because auto_restart is set to 0, the WRRD mode will be never enabled, leading to read errors.
Fix this by removing auto_restart check from the WRRD enable path.
--- drivers/i2c/busses/i2c-mt65xx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-mt65xx.c +1 -1
@@ -1258,7 +1258,7 @@ static int mtk_i2c_transfer(struct i2c_a i2c->auto_restart = i2c->dev_comp->auto_restart;/* checking if we can skip restart and optimize using WRRD mode */ -if (i2c->auto_restart && num ==2 ) { +if (num ==2 ) {if (!(msgs[0 ].flags &I2C_M_RD ) && (msgs[1 ].flags &I2C_M_RD ) && msgs[0 ].addr == msgs[1 ].addr) { i2c->auto_restart =0 ;
Greg Kroah-Hartman
maintainerPATCH 1796/2077 i2c: mlxbf: Fix use-after-free in mlxbf_i2c_init_resource()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Xuanqiang Luo <[email protected]>
commit 71356737a7a55c76fee847563e3d33f8e6dc6b6d upstream.
If devm_platform_get_and_ioremap_resource() returns an error, mlxbf_i2c_init_resource() frees tmp_res before reading tmp_res->io to get the error code. This results in a use-after-free.
Save the error code before freeing tmp_res.
--- drivers/i2c/busses/i2c-mlxbf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-mlxbf.c +3 -1
@@ -1051,8 +1051,10 @@ static int mlxbf_i2c_init_resource(struc tmp_res->io =devm_platform_get_and_ioremap_resource (pdev, type, &tmp_res->params);if (IS_ERR (tmp_res->io)) { +int ret =PTR_ERR (tmp_res->io); +devm_kfree (dev, tmp_res); -return PTR_ERR (tmp_res->io); +return ret; } tmp_res->type = type;
Greg Kroah-Hartman
maintainerPATCH 1797/2077 i2c: spacemit: fix spurious IRQ handling returning IRQ_HANDLED
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Pei Xiao <[email protected]>
commit 9db20d23aac7916ff49be409a4bfd48fe7cbbfb4 upstream.
When the interrupt status register reads zero, the handler should return IRQ_NONE instead of IRQ_HANDLED. What the return value actually feeds into is the spurious interrupt accounting in note_interrupt(): falsely claiming IRQ_HANDLED defeats the "irq XX:
nobody cared" detection, so a stuck interrupt source would never be caught.
--- drivers/i2c/busses/i2c-k1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-k1.c +1 -1
@@ -596,7 +596,7 @@ static irqreturn_t spacemit_i2c_irq_hand status =readl (i2c->base +SPACEMIT_ISR );if (!status) -return IRQ_HANDLED ; +return IRQ_NONE ; i2c->status = status;
Greg Kroah-Hartman
maintainerPATCH 1798/2077 ice: fix ice_init_link() error return preventing probe
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Paul Greenwalt <[email protected]>
commit eb509638686b0f8a98a0dd9c809f6a8db4d73a45 upstream.
ice_init_link() can return an error status from ice_update_link_info() or ice_init_phy_user_cfg(), causing probe to fail.
An incorrect NVM update procedure can result in link/PHY errors, and the recommended resolution is to update the NVM using the correct procedure. If the driver fails probe due to link errors, the user cannot update the NVM to recover. The link/PHY errors logged are
non-fatal: they are already annotated as 'not a fatal error if this fails'.
Since none of the errors inside ice_init_link() should prevent probe from completing, convert it to void and remove the error check in the caller. All failures are already logged; callers have no meaningful recovery path for link init errors.
--- drivers/net/ethernet/intel/ice/ice_main.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
drivers/net/ethernet/intel/ice/ice_main.c +5 -11
@@ -4789,16 +4789,14 @@ static void ice_init_wakeup(struct ice_pdevice_set_wakeup_enable (ice_pf_to_dev (pf), false); } -static int ice_init_link (struct ice_pf *pf) +static void ice_init_link (struct ice_pf *pf) {struct device *dev =ice_pf_to_dev (pf);int err; err =ice_init_link_events (pf->hw.port_info); -if (err) { +if (err)dev_err (dev,"ice_init_link_events failed: %d\n" , err); -return err; - }/* not a fatal error if this fails */ err =ice_init_nvm_phy_type (pf->hw.port_info);
@@ -4838,8 +4836,6 @@ static int ice_init_link(struct ice_pf * }else {set_bit (ICE_FLAG_NO_MEDIA , pf->flags); } - -return err; }static int ice_init_pf_sw (struct ice_pf *pf)
@@ -4982,13 +4978,11 @@ static int ice_init(struct ice_pf *pf)ice_init_wakeup (pf); - err =ice_init_link (pf); -if (err) - goto err_init_link; +ice_init_link (pf); err =ice_send_version (pf);if (err) - goto err_init_link; + goto err_deinit_pf_sw;ice_verify_cacheline_size (pf);
@@ -5007,7 +5001,7 @@ static int ice_init(struct ice_pf *pf)return 0 ; -err_init_link: +err_deinit_pf_sw:ice_deinit_pf_sw (pf); err_init_pf_sw:ice_dealloc_vsis (pf);
Greg Kroah-Hartman
maintainerPATCH 1799/2077 tcp: defer md5sig_info kfree past RCU grace period in tcp_connect
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Michael Bommarito <[email protected]>
commit b74cd55038905d5e74c1de109ab78a30b2ea0e1f upstream.
The md5+ao reconciliation in tcp_connect() (net/ipv4/tcp_output.c) has two symmetric branches:
if (needs_md5) {
tcp_ao_destroy_sock(sk, false);
} else if (needs_ao) {
tcp_clear_md5_list(sk);
kfree(rcu_replace_pointer(tp->md5sig_info, NULL, ...));
}Both branches free a per-socket auth-info object while the socket is in TCP_SYN_SENT and is already on the inet ehash (inserted by inet_hash_connect() in tcp_v4_connect()). Both branches are reachable by softirq RX-path readers that load the corresponding info pointer via implicit RCU before bh_lock_sock_nested() is taken.
The needs_md5 branch is fixed in the prior patch by re-introducing the call_rcu() free in tcp_ao_destroy_sock(): the equivalent per-key loop runs inside tcp_ao_info_free_rcu(), the RCU callback, so by the time it frees each tcp_ao_key all softirq readers that captured the container have already completed rcu_read_unlock().
The needs_ao branch is not symmetric in the same way. The container free can be deferred via kfree_rcu(md5sig, rcu) -- struct tcp_md5sig_info already has the required rcu member (include/net/tcp.h:1999-2002), and the rest of the tree already does this in the tcp_md5sig_info_add() rollback paths (net/ipv4/tcp_ipv4.c:1410, 1436). But the per-key teardown is done by tcp_clear_md5_list() in process context BEFORE the container's RCU grace period: it walks &md5sig->head and frees each tcp_md5sig_key with bare hlist_del + kfree. A concurrent softirq reader in __tcp_md5_do_lookup() / __tcp_md5_do_lookup_exact() (tcp_ipv4.c:1253, 1298) walks the same list via hlist_for_each_entry_rcu() and races with that bare kfree on the keys themselves -- a per-key slab use-after-free of the same class as the TCP-AO bug, on the same race window.
Fix this in two halves:
1. Convert the bare kfree() in tcp_connect() to kfree_rcu() so the md5sig_info container joins the rest of the md5sig lifecycle. The local-variable lift is mechanical and required because kfree_rcu() is a macro that expects an lvalue.
2. Make tcp_clear_md5_list() RCU-safe by replacing hlist_del + kfree(key) with hlist_del_rcu + kfree_rcu(key, rcu). struct tcp_md5sig_key already carries the rcu member (include/net/tcp.h:1995) and tcp_md5_do_del() (net/ipv4/tcp_ipv4.c:1456) already uses kfree_rcu, so this restores the lifecycle invariant the rest of the file follows rather than introducing a one-off.
The other caller of tcp_clear_md5_list() is tcp_md5_destruct_sock() (net/ipv4/tcp.c:412), which runs from the sock destructor when the socket is already unhashed and unreachable; the extra grace period there is unnecessary but harmless. Making the helper unconditionally RCU-safe is the cleaner contract.
The needs_ao branch is not reachable by the userns reproducer used to demonstrate the AO-side splat (the repro installs both keys but ends up in the needs_md5 branch because the connect peer matches the MD5 key, not the AO key); however the symmetric race exists and a maintainer touching this code should not have to think about which branch escapes RCU and which one does not.
--- net/ipv4/tcp_ipv4.c | 4 ++-- net/ipv4/tcp_output.c | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-)
net/ipv4/tcp_ipv4.c +2 -2
@@ -1467,9 +1467,9 @@ void tcp_clear_md5_list(struct sock *sk) md5sig =rcu_dereference_protected (tp->md5sig_info,1 );hlist_for_each_entry_safe (key, n, &md5sig->head, node) { -hlist_del (&key->node); +hlist_del_rcu (&key->node);atomic_sub (sizeof (*key), &sk->sk_omem_alloc); -kfree (key); +kfree_rcu (key, rcu); } }
net/ipv4/tcp_output.c +6 -2
@@ -4328,9 +4328,13 @@ int tcp_connect(struct sock *sk)if (needs_md5) {tcp_ao_destroy_sock (sk, false); }else if (needs_ao) { +struct tcp_md5sig_info *md5sig; +tcp_clear_md5_list (sk); -kfree (rcu_replace_pointer (tp->md5sig_info,NULL , -lockdep_sock_is_held (sk))); + md5sig =rcu_replace_pointer (tp->md5sig_info,NULL , +lockdep_sock_is_held (sk)); +if (md5sig) +kfree_rcu (md5sig, rcu); } }#endif
Greg Kroah-Hartman
maintainerPATCH 1800/2077 tcp: Decrement tcp_md5_needed static branch
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Safonov <[email protected]>
commit 6f6e860e370c9e4e919b92118a25e9e1f82e9180 upstream.
In case of early freeing an unwanted TCP-MD5 key on TCP-AO connect(), md5sig_info is freed right away (and set to NULL). Later, at the moment of socket destruction, the static branch counter is not getting decremented.
Add a missing decrement for TCP-MD5 static branch.
--- net/ipv4/tcp_output.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_output.c +2 -2
@@ -4333,8 +4333,8 @@ int tcp_connect(struct sock *sk)tcp_clear_md5_list (sk); md5sig =rcu_replace_pointer (tp->md5sig_info,NULL ,lockdep_sock_is_held (sk)); -if (md5sig) - kfree_rcu(md5sig, rcu); +kfree_rcu (md5sig, rcu); + static_branch_slow_dec_deferred(&tcp_md5_needed); } }#endif
Greg Kroah-Hartman
maintainerPATCH 1801/2077 ufs: core: tracing: Do not dereference pointers in TP_printk()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Steven Rostedt <[email protected]>
commit 535fcf4b8a261fbb8cc4f91e4597343c135a90f2 upstream.
The trace events in drivers/ufs/core/ufs_trace.h were converted to take a pointer to the hba structure as an argument for the tracepoint and then in TP_printk() the printing of the dev_name from the ring buffer was converted to using the dev dereferenced pointer from the hba saved pointer.
This is not allowed as the TP_printk() is executed at the time the trace event is read from /sys/kernel/tracing/trace file. That can happen literally, seconds, minutes, hours, weeks, days, or even months later! There is no guarantee that the hba pointer will still exist by the time it is dereferenced when the "trace" file is read.
Instead, save the device name from the hba pointer at the time the tracepoint is called and place it into the ring buffer event. Then the TP_printk() can read the name directly from the ring buffer and remove the possibility that it will read a freed pointer and crash the kernel.
This was detected when testing the trace event code that looks for TP_printk() parameters doing illegal derferences[1]
[1] https://lore.kernel.org/all/[email protected]/
--- drivers/ufs/core/ufs_trace.h | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-)
drivers/ufs/core/ufs_trace.h +27 -9
@@ -89,16 +89,18 @@ TRACE_EVENT(ufshcd_clk_gating,TP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev))__field (int, state) ),TP_fast_assign ( + __assign_str(dev_name); __entry->hba = hba; __entry->state = state; ),TP_printk ("%s: gating state changed to %s" , -dev_name (__entry->hba->dev), +__get_str (dev_name),__print_symbolic (__entry->state,UFSCHD_CLK_GATING_STATES )) );
@@ -111,6 +113,7 @@ TRACE_EVENT(ufshcd_clk_scaling,TP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev))__string (state, state)__string (clk, clk) __field(u32, prev_state)
@@ -119,6 +122,7 @@ TRACE_EVENT(ufshcd_clk_scaling,TP_fast_assign ( __entry->hba = hba; +__assign_str (dev_name);__assign_str (state);__assign_str (clk); __entry->prev_state = prev_state;
@@ -126,7 +130,7 @@ TRACE_EVENT(ufshcd_clk_scaling, ),TP_printk ("%s: %s %s from %u to %u Hz" , -dev_name (__entry->hba->dev),__get_str (state),__get_str (clk), +__get_str (dev_name),__get_str (state),__get_str (clk), __entry->prev_state, __entry->curr_state) );
@@ -138,16 +142,18 @@ TRACE_EVENT(ufshcd_auto_bkops_state,TP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev))__string (state, state) ),TP_fast_assign ( __entry->hba = hba; +__assign_str (dev_name);__assign_str (state); ),TP_printk ("%s: auto bkops - %s" , -dev_name (__entry->hba->dev),__get_str (state)) +__get_str (dev_name),__get_str (state)) ); DECLARE_EVENT_CLASS(ufshcd_profiling_template,
@@ -158,6 +164,7 @@ DECLARE_EVENT_CLASS(ufshcd_profiling_temTP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev))__string (profile_info, profile_info)__field (s64, time_us) __field(int, err)
@@ -165,13 +172,14 @@ DECLARE_EVENT_CLASS(ufshcd_profiling_temTP_fast_assign ( __entry->hba = hba; +__assign_str (dev_name);__assign_str (profile_info); __entry->time_us = time_us; __entry->err = err; ),TP_printk ("%s: %s: took %lld usecs, err %d" , -dev_name (__entry->hba->dev),__get_str (profile_info), +__get_str (dev_name),__get_str (profile_info), __entry->time_us, __entry->err) );
@@ -200,6 +208,7 @@ DECLARE_EVENT_CLASS(ufshcd_template, __field(s64, usecs) __field(int, err) __field(struct ufs_hba *, hba) + __string(dev_name, dev_name(hba->dev)) __field(int, dev_state) __field(int, link_state) ),
@@ -208,13 +217,14 @@ DECLARE_EVENT_CLASS(ufshcd_template, __entry->usecs = usecs; __entry->err = err; __entry->hba = hba; +__assign_str (dev_name); __entry->dev_state = dev_state; __entry->link_state = link_state; ), TP_printk( "%s: took %lld usecs, dev_state: %s, link_state: %s, err %d", - dev_name(__entry->hba->dev), + __get_str(dev_name), __entry->usecs, __print_symbolic(__entry->dev_state, UFS_PWR_MODES), __print_symbolic(__entry->link_state, UFS_LINK_STATES),
@@ -279,6 +289,7 @@ TRACE_EVENT(ufshcd_command, TP_STRUCT__entry( __field(struct scsi_device *, sdev) __field(struct ufs_hba *, hba) + __string(dev_name, dev_name(&sdev->sdev_dev)) __field(enum ufs_trace_str_t, str_t) __field(unsigned int, tag) __field(u32, doorbell)
@@ -291,6 +302,7 @@ TRACE_EVENT(ufshcd_command, ), TP_fast_assign( + __assign_str(dev_name); __entry->sdev = sdev; __entry->hba = hba; __entry->str_t = str_t;
@@ -307,7 +319,7 @@ TRACE_EVENT(ufshcd_command,TP_printk ("%s: %s: tag: %u, DB: 0x%x, size: %d, IS: %u, LBA: %llu, opcode: 0x%x (%s), group_id: 0x%x, hwq_id: %d" ,show_ufs_cmd_trace_str (__entry->str_t), -dev_name (&__entry->sdev->sdev_dev), __entry->tag, +__get_str (dev_name), __entry->tag, __entry->doorbell, __entry->transfer_len, __entry->intr, __entry->lba, (u32 )__entry->opcode,str_opcode (__entry->opcode), (u32)__entry->group_id, __entry->hwq_id
@@ -322,6 +334,7 @@ TRACE_EVENT(ufshcd_uic_command,TP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev)) __field(enum ufs_trace_str_t, str_t)__field (u32, cmd) __field(u32, arg1)
@@ -331,6 +344,7 @@ TRACE_EVENT(ufshcd_uic_command,TP_fast_assign ( __entry->hba = hba; +__assign_str (dev_name); __entry->str_t = str_t; __entry->cmd = cmd; __entry->arg1 = arg1;
@@ -340,7 +354,7 @@ TRACE_EVENT(ufshcd_uic_command,TP_printk ("%s: %s: cmd: 0x%x, arg1: 0x%x, arg2: 0x%x, arg3: 0x%x" , -show_ufs_cmd_trace_str (__entry->str_t),dev_name (__entry->hba->dev), +show_ufs_cmd_trace_str (__entry->str_t),__get_str (dev_name), __entry->cmd, __entry->arg1, __entry->arg2, __entry->arg3 ) );
@@ -353,6 +367,7 @@ TRACE_EVENT(ufshcd_upiu,TP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev)) __field(enum ufs_trace_str_t, str_t) __array(unsigned char, hdr, 12) __array(unsigned char, tsf, 16)
@@ -361,6 +376,7 @@ TRACE_EVENT(ufshcd_upiu,TP_fast_assign ( __entry->hba = hba; +__assign_str (dev_name); __entry->str_t = str_t;memcpy (__entry->hdr, hdr,sizeof (__entry->hdr));memcpy (__entry->tsf, tsf,sizeof (__entry->tsf));
@@ -369,7 +385,7 @@ TRACE_EVENT(ufshcd_upiu,TP_printk ("%s: %s: HDR:%s, %s:%s" , -show_ufs_cmd_trace_str (__entry->str_t),dev_name (__entry->hba->dev), +show_ufs_cmd_trace_str (__entry->str_t),__get_str (dev_name),__print_hex (__entry->hdr,sizeof (__entry->hdr)),show_ufs_cmd_trace_tsf (__entry->tsf_t), __print_hex(__entry->tsf, sizeof(__entry->tsf))
@@ -384,16 +400,18 @@ TRACE_EVENT(ufshcd_exception_event,TP_STRUCT__entry (__field (struct ufs_hba *,hba ) + __string(dev_name, dev_name(hba->dev))__field (u16, status) ),TP_fast_assign ( __entry->hba = hba; +__assign_str (dev_name); __entry->status = status; ),TP_printk ("%s: status 0x%x" , -dev_name (__entry->hba->dev), __entry->status +__get_str (dev_name), __entry->status ) );
Greg Kroah-Hartman
maintainerPATCH 1802/2077 xen/gntdev: fix error handling in ioctl
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Wentao Liang <[email protected]>
commit 45ca1afe2fd14c04e37227e79d3f8455831d8408 upstream.
When gntdev_ioctl_map_grant_ref() fails to copy the operation result back to userspace after successfully adding the mapping to the list, the error path returns -EFAULT without releasing the reference acquired by gntdev_alloc_map(). The mapping remains in priv->maps with a refcount of 1, causing a memory leak and a dangling list entry.
Additionally, gntdev_add_map() may modify map->index to avoid overlap with existing mappings. Therefore, the index returned to userspace must be obtained after gntdev_add_map() completes.
Fix this by holding the mutex across gntdev_add_map(), retrieving the correct index, and copy_to_user(). If copy_to_user() fails, remove the mapping from the list and release the reference while still holding the lock.
Fix these issues by properly handling all error cases.
Message-ID: <[email protected]>
--- drivers/xen/gntdev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/xen/gntdev.c +6 -2
@@ -670,11 +670,15 @@ static long gntdev_ioctl_map_grant_ref(smutex_lock (&priv->lock);gntdev_add_map (priv, map); op.index = map->index <<PAGE_SHIFT ; -mutex_unlock (&priv->lock); -if (copy_to_user (u, &op,sizeof (op)) !=0 ) +if (copy_to_user (u, &op,sizeof (op)) !=0 ) { +list_del (&map->next); +mutex_unlock (&priv->lock); +gntdev_put_map (priv, map);return -EFAULT ; + } +mutex_unlock (&priv->lock);return 0 ; }
Greg Kroah-Hartman
maintainerPATCH 1803/2077 xfrm: nat_keepalive: avoid double free on send error
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Qianyu Luo <[email protected]>
commit 226f4a490d1a938fc838d8f8c46a4eca864c0d78 upstream.
nat_keepalive_send() frees the keepalive skb whenever the IPv4 or IPv6 send helper reports an error.
That cleanup is only correct before the skb is handed to the output path. Once ip_build_and_send_pkt() or ip6_xmit() takes ownership, the networking stack may already have consumed the skb before returning an error, so freeing it again is unsafe.
Handle the pre-handoff failure cases inside nat_keepalive_send_ipv4() and nat_keepalive_send_ipv6(), where the caller still owns the skb, and keep nat_keepalive_send() responsible only for family dispatch and the unsupported-family cleanup path.
--- net/xfrm/xfrm_nat_keepalive.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
net/xfrm/xfrm_nat_keepalive.c +9 -6
@@ -55,8 +55,10 @@ static int nat_keepalive_send_ipv4(struc ka->encap_sport, sock_net_uid(net, NULL)); rt =ip_route_output_key (net, &fl4); -if (IS_ERR (rt)) +if (IS_ERR (rt)) { +kfree_skb (skb);return PTR_ERR (rt); + }skb_dst_set (skb, &rt->dst);
@@ -101,6 +103,7 @@ static int nat_keepalive_send_ipv6(struc dst =ip6_dst_lookup_flow (net, sk, &fl6,NULL );if (IS_ERR (dst)) {local_unlock_nested_bh (&nat_keepalive_sk_ipv6.bh_lock); +kfree_skb (skb);return PTR_ERR (dst); }
@@ -118,7 +121,6 @@ static void nat_keepalive_send(struct na sizeof(struct ipv6hdr)) +sizeof (struct udphdr );const u8 nat_ka_payload =0xFF ; -int err = -EAFNOSUPPORT ;struct sk_buff *skb;struct udphdr *uh;
@@ -140,16 +142,17 @@ static void nat_keepalive_send(struct naswitch (ka->family) {case AF_INET : - err =nat_keepalive_send_ipv4 (skb, ka); +nat_keepalive_send_ipv4 (skb, ka);break ;#if IS_ENABLED (CONFIG_IPV6 )case AF_INET6 : - err =nat_keepalive_send_ipv6 (skb, ka, uh); +nat_keepalive_send_ipv6 (skb, ka, uh);break ;#endif - } -if (err) +default :kfree_skb (skb); +break ; + } }struct nat_keepalive_work_ctx {
Greg Kroah-Hartman
maintainerPATCH 1804/2077 xfrm: use compat translator only for u64 alignment mismatch
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Sanman Pradhan <[email protected]>
commit 355fbcbdc2539cca7890b0d0914d4ce0f985ad74 upstream.
The XFRM compat layer (CONFIG_XFRM_USER_COMPAT) translates 32-bit xfrm netlink and setsockopt messages into the native 64-bit layout. It is only needed on architectures where the 32-bit and 64-bit ABIs disagree on u64 alignment, which the kernel encodes as COMPAT_FOR_U64_ALIGNMENT.
That symbol is defined only by arch/x86. XFRM_USER_COMPAT depends on it, so the translator can never be built on any other architecture, including arm64, which still provides a 32-bit compat ABI (CONFIG_COMPAT) for AArch32 EL0 userspace. On arm64 the AArch32 EABI already aligns u64 to 8 bytes, identical to the AArch64 ABI, so no translation is required and the native code path is correct for 32-bit tasks.
However, xfrm_user_rcv_msg() and xfrm_user_policy() gate on in_compat_syscall() alone and then call xfrm_get_translator(), which returns NULL when no translator is registered. On arm64 that is always the case, so every xfrm netlink message and the XFRM_POLICY setsockopt issued by a 32-bit task returns -EOPNOTSUPP. A 32-bit userspace process on arm64 (and on any other arch with CONFIG_COMPAT but without COMPAT_FOR_U64_ALIGNMENT) therefore cannot configure XFRM state or policy through the XFRM_USER netlink API, and cannot use the XFRM_POLICY setsockopt path, because both fail before reaching the native parser.
The translator series replaced the blanket compat rejection with a translator lookup. That made the path usable on x86 when the translator is available, but left architectures that cannot build the translator permanently rejected even when their compat layout already matches the native layout. Let those architectures use the native parser instead.
Gate the translator requirement on COMPAT_FOR_U64_ALIGNMENT instead of on in_compat_syscall() alone. Gating on the ABI property rather than on CONFIG_XFRM_USER_COMPAT is deliberate: on x86 with IA32_EMULATION=y but XFRM_USER_COMPAT=n, a 32-bit task must still be rejected rather than routed through the native parser, which would misread genuinely 4-byte-aligned x86-32 messages. COMPAT_FOR_U64_ALIGNMENT is the ABI property that makes the XFRM translator mandatory.
Only the receive/input direction needs the guard. The send, dump and notification paths already call the translator as "if (xtr) { ... }" with no error on NULL, so on arches without a translator they no-op and the kernel emits native 64-bit-layout messages, which is what an AArch32 task expects.
Tested on Juniper SRX hardware: with the fix, 32-bit IPsec userspace netlink and XFRM_POLICY setsockopt operations that previously failed with -EOPNOTSUPP now succeed; x86 behaviour is unchanged by inspection.
--- net/xfrm/xfrm_state.c | 2 +- net/xfrm/xfrm_user.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_state.c +1 -1
@@ -2984,7 +2984,7 @@ int xfrm_user_policy(struct sock *sk, inif (IS_ERR (data))return PTR_ERR (data); -if (in_compat_syscall ()) { +if (IS_ENABLED (CONFIG_COMPAT_FOR_U64_ALIGNMENT ) &&in_compat_syscall ()) {struct xfrm_translator *xtr =xfrm_get_translator ();if (!xtr) {
net/xfrm/xfrm_user.c +1 -1
@@ -3472,7 +3472,7 @@ static int xfrm_user_rcv_msg(struct sk_bif (!netlink_net_capable (skb,CAP_NET_ADMIN ))return -EPERM ; -if (in_compat_syscall ()) { +if (IS_ENABLED (CONFIG_COMPAT_FOR_U64_ALIGNMENT ) &&in_compat_syscall ()) {struct xfrm_translator *xtr =xfrm_get_translator ();if (!xtr)
Greg Kroah-Hartman
maintainerPATCH 1805/2077 xfrm: xfrm_interface: require CAP_NET_ADMIN in the device netns for changelink
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Maoyi Xie <[email protected]>
commit 095515d89b19b6cc19dfcdc846f97403ed1ebce3 upstream.
xfrmi_changelink() operates on at most two netns, dev_net(dev) and the interface link netns xi->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in xi->net can rewrite an interface that lives in xi->net.
Gate xfrmi_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed.
--- net/xfrm/xfrm_interface_core.c | 3 +++ 1 file changed, 3 insertions(+)
net/xfrm/xfrm_interface_core.c +3 -0
@@ -869,6 +869,9 @@ static int xfrmi_changelink(struct net_dstruct net *net = xi->net;struct xfrm_if_parms p = {}; +if (!rtnl_dev_link_net_capable (dev, net)) +return -EPERM ; +xfrmi_netlink_parms (data, &p);if (!p.if_id) {NL_SET_ERR_MSG (extack,"if_id must be non zero" );
Greg Kroah-Hartman
maintainerPATCH 1806/2077 tpm: fix event_size output in tpm1_binary_bios_measurements_show
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Thorsten Blum <[email protected]>
commit 1a58f6115bfb34eabcc7de8a3a9745b219179781 upstream.
Commit 186d124f07da ("tpm_eventlog.c: fix binary_bios_measurements") split the output to write the endian-converted event header first and then the variable-length event data.
However, the split was at sizeof(struct tcpa_event) - 1, even though event_data was a zero-length array, and later a flexible array member, both of which already excluded the event data.
Therefore, the current code writes the first three bytes of event_size from the endian-converted header and then the last byte from the raw header, which can emit a corrupted event_size on PPC64, where do_endian_conversion() maps to be32_to_cpu().
Split one byte later to write the full endian-converted header first, followed by the variable-length event->event_data.
--- drivers/char/tpm/eventlog/tpm1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/tpm/eventlog/tpm1.c +2 -2
@@ -236,12 +236,12 @@ static int tpm1_binary_bios_measurements temp_ptr = (char *) &temp_event; -for (i =0 ; i < (sizeof (struct tcpa_event ) -1 ) ; i++) +for (i =0 ; i <sizeof (struct tcpa_event ); i++)seq_putc (m, temp_ptr[i]); temp_ptr = (char *) v; -for (i = (sizeof (struct tcpa_event ) -1 ); +for (i =sizeof (struct tcpa_event ); i < (sizeof (struct tcpa_event ) + temp_event.event_size); i++)seq_putc (m, temp_ptr[i]);
Greg Kroah-Hartman
maintainerPATCH 1807/2077 tpm: Make the TPM character devices non-seekable
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Jaewon Yang <[email protected]>
commit f20d61c22bcaf172d6790b6500e3838e532e71c8 upstream.
The TPM character devices expose a sequential command/response interface, but their open handlers leave FMODE_PREAD and FMODE_PWRITE enabled.
After a command leaves a response pending, pread(fd, buf, 16, 0x1400) passes 0x1400 as *off to tpm_common_read(). The transfer length is bounded by response_length, but the offset is used unchecked when forming data_buffer + *off. A sufficiently large offset therefore causes an out-of-bounds heap read through copy_to_user() and, if the copy succeeds, an out-of-bounds zero-write through the following memset().
Positional I/O does not provide coherent semantics for this interface. An arbitrary pread offset cannot represent how much of a response has been consumed sequentially. The write callback always stores a command at the start of data_buffer, while pwrite() does not update file->f_pos and can leave the sequential read cursor stale.
Call nonseekable_open() from both open handlers. This removes FMODE_PREAD and FMODE_PWRITE, causing positional reads and writes to fail with -ESPIPE before reaching the TPM callbacks, and explicitly marks the files non-seekable. Normal read() and write() continue to use the existing sequential f_pos cursor, leaving the response state machine unchanged.
Tested on Linux 6.12 with KASAN and a swtpm TPM2 device:
- sequential partial reads returned the complete response
- pread() and preadv() with offset 0x1400 returned -ESPIPE
- pwrite() and pwritev() with offset zero returned -ESPIPE
- the pending response remained intact after the rejected operations
- a subsequent normal command/response cycle completed normally
- no KASAN report was produced.
--- drivers/char/tpm/tpm-dev.c | 2 +- drivers/char/tpm/tpmrm-dev.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm-dev.c +1 -1
@@ -36,7 +36,7 @@ static int tpm_open(struct inode *inode,tpm_common_open (file, chip, priv,NULL ); -return 0 ; +return nonseekable_open (inode, file); out:clear_bit (0 , &chip->is_open);
drivers/char/tpm/tpmrm-dev.c +1 -1
@@ -29,7 +29,7 @@ static int tpmrm_open(struct inode *inodtpm_common_open (file, chip, &priv->priv, &priv->space); -return 0 ; +return nonseekable_open (inode, file); }static int tpmrm_release (struct inode *inode,struct file *file)
Greg Kroah-Hartman
maintainerPATCH 1808/2077 time: Fix off-by-one in compat settimeofday() usec validation
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Wang Yan <[email protected]>
commit 269f2b43fae692d1f3988c9f888a6301aa537b82 upstream.
The compat version of settimeofday() uses '>' instead of '>=' when validating tv_usec against USEC_PER_SEC, allowing the value 1000000 to pass the check. After the subsequent conversion to nanoseconds (tv_nsec *= NSEC_PER_USEC), this results in tv_nsec == NSEC_PER_SEC, which violates the timespec invariant that tv_nsec must be strictly less than NSEC_PER_SEC.
The native settimeofday() was already fixed in commit ce4abda5e126 ("time:
Fix off-by-one in settimeofday() usec validation"), but the compat counterpart was missed.
Fix it by using '>=' to reject tv_usec values outside the valid range [0, USEC_PER_SEC - 1].
--- kernel/time/time.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/time.c +1 -1
@@ -251,7 +251,7 @@ COMPAT_SYSCALL_DEFINE2(settimeofday, str get_user(new_ts.tv_nsec, &tv->tv_usec))return -EFAULT ; -if (new_ts.tv_nsec >USEC_PER_SEC || new_ts.tv_nsec <0 ) +if (new_ts.tv_nsec >=USEC_PER_SEC || new_ts.tv_nsec <0 )return -EINVAL ; new_ts.tv_nsec *=NSEC_PER_USEC ;
Greg Kroah-Hartman
maintainerPATCH 1809/2077 spi: imx: reconfigure for PIO when DMA cannot be started
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Javier Fernandez Pastrana <[email protected]>
commit 245404c26563aafb36aafb01298f148db1851be3 upstream.
When spi_imx_can_dma() selects DMA, the ECSPI is configured for DMA:spi_imx_setupxfer() sets CTRL.SMC and clears dynamic_burst, and spi_imx_dma_transfer() programs the dynamic-burst BURST_LENGTH and the SDMA watermarks.
If the DMA descriptor cannot be prepared (dmaengine_prep_slave_single() returns NULL), the transfer is failed with SPI_TRANS_FAIL_NO_START and falls back to PIO. The dynamic-burst DMA path uses its own bounce buffers instead of the SPI core's mapping, so xfer->{tx,rx}_sg_mapped are not set and the core's DMA->PIO retry is skipped; the driver falls back to PIO internally. But none of the DMA-mode configuration is undone, so the PIO transfer runs with CTRL.SMC set, the wrong burst length and dynamic_burst cleared, and the transferred data is corrupted.
This is easily hit on i.MX8MP boards that describe ECSPI DMA in the device tree but run SDMA on ROM firmware (no external sdma-imx7d.bin):
every ECSPI DMA prepare fails. An Infineon SLB9670 TPM on ECSPI1 then returns shifted TPM2_GetCapability data, is flagged "field failure mode", /dev/tpmrm0 is never created.
Set controller->fallback before re-running spi_imx_setupxfer() so the ECSPI is reconfigured exactly like a normal PIO transfer. With controller->fallback set, spi_imx_setupxfer() sees spi_imx_can_dma() return false, so it clears spi_imx->usedma and reprograms the controller (clears CTRL.SMC, restores dynamic_burst and the PIO burst length). No explicit spi_imx->usedma = false is needed: setupxfer() already updates it from the can_dma() result.
--- drivers/spi/spi-imx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/spi/spi-imx.c +2 -1
@@ -2152,7 +2152,8 @@ static int spi_imx_transfer_one(struct spi_controller *controller,if (spi_imx->usedma) { ret =spi_imx_dma_transfer (spi_imx, transfer);if (transfer->error &SPI_TRANS_FAIL_NO_START ) { - spi_imx->usedma = false; + controller->fallback = true; +spi_imx_setupxfer (spi, transfer);if (spi_imx->target_mode)return spi_imx_pio_transfer_target (spi, transfer);else
Signature · 1 line
2.55.0
Greg Kroah-Hartman
maintainerPATCH 1810/2077 spi: uniphier: Fix completion initialization order before devm_request_irq()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Kunihiko Hayashi <[email protected]>
commit f3ad1c87d8201e54b66bd6072442f0b5d5a308ee upstream.
The driver calls devm_request_irq() before initializing the completion used by the interrupt handler. Because the interrupt may occur immediately after devm_request_irq(), the handler may execute before init_completion().
This may result in calling complete() on an uninitialized completion, causing undefined behavior. This has been observed with KASAN.
Fix this by initializing the completion before registering the IRQ.
--- drivers/spi/spi-uniphier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/spi/spi-uniphier.c +2 -2
@@ -659,6 +659,8 @@ static int uniphier_spi_probe(struct pla priv->host = host; priv->is_save_param = false; +init_completion (&priv->xfer_done); + priv->base =devm_platform_get_and_ioremap_resource (pdev,0 , &res);if (IS_ERR (priv->base)) { ret =PTR_ERR (priv->base);
@@ -686,8 +688,6 @@ static int uniphier_spi_probe(struct pla goto out_host_put; } -init_completion (&priv->xfer_done); - clk_rate =clk_get_rate (priv->clk); host->max_speed_hz =DIV_ROUND_UP (clk_rate,SSI_MIN_CLK_DIVIDER );
Greg Kroah-Hartman
maintainerPATCH 1811/2077 sctp: validate STALE_COOKIE cause length before reading staleness
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Weiming Shi <[email protected]>
commit 1cd23ca80784223fa2204e16203f754da4e821f8 upstream.
When an ERROR chunk with a STALE_COOKIE cause is received in the COOKIE_ECHOED state, sctp_sf_do_5_2_6_stale() reads the 4-byte Measure of Staleness that follows the cause header:
err = (struct sctp_errhdr *)(chunk->skb->data); stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
err is the first cause in the chunk, not the STALE_COOKIE cause that caused the dispatch, and nothing guarantees the staleness field is present. sctp_walk_errors() only requires a cause to be as long as the 4-byte header, so for a STALE_COOKIE cause of length 4 the read runs past the cause, and for a minimal ERROR chunk past skb->tail. The value is echoed to the peer in the Cookie Preservative of the reply INIT, leaking uninitialized memory.
sctp_sf_cookie_echoed_err() already walks to the STALE_COOKIE cause, so check its length there and pass it to sctp_sf_do_5_2_6_stale(), which reads that cause instead of the first one. A STALE_COOKIE cause too short to hold the staleness field is discarded.
The read is reachable by any peer that can drive an association into COOKIE_ECHOED, including an unprivileged process using a raw SCTP socket in a user and network namespace.
--- net/sctp/sm_statefuns.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
net/sctp/sm_statefuns.c +14 -9
@@ -74,7 +74,8 @@ static enum sctp_disposition sctp_sf_do_const struct sctp_association *asoc, const union sctp_subtype type, void *arg, - struct sctp_cmd_seq *commands); + struct sctp_cmd_seq *commands, + struct sctp_errhdr *err); static enum sctp_disposition sctp_sf_shut_8_4_5( struct net *net, const struct sctp_endpoint *ep,
@@ -2529,9 +2530,15 @@ enum sctp_disposition sctp_sf_cookie_ech* errors. */ sctp_walk_errors(err, chunk->chunk_hdr) { -if (SCTP_ERROR_STALE_COOKIE == err->cause) -return sctp_sf_do_5_2_6_stale (net, ep, asoc, type, - arg, commands); +if (err->cause !=SCTP_ERROR_STALE_COOKIE ) +continue ; +/* The staleness is only meaningful if the cause is long +* enough to hold it; a shorter one is malformed. +*/ +if (ntohs (err->length) <sizeof (*err) +sizeof (__be32)) +break ; +return sctp_sf_do_5_2_6_stale (net, ep, asoc, type, + arg, commands, err); }/* It is possible to have malformed error causes, and that
@@ -2573,13 +2580,13 @@ static enum sctp_disposition sctp_sf_do_const struct sctp_association *asoc, const union sctp_subtype type, void *arg, - struct sctp_cmd_seq *commands) + struct sctp_cmd_seq *commands, + struct sctp_errhdr *err) { int attempts = asoc->init_err_counter + 1; -struct sctp_chunk *chunk = arg, *reply;struct sctp_cookie_preserve_param bht;struct sctp_bind_addr *bp; -struct sctp_errhdr *err; +struct sctp_chunk *reply;u32 stale;if (attempts > asoc->max_init_attempts) {
@@ -2590,8 +2597,6 @@ static enum sctp_disposition sctp_sf_do_return SCTP_DISPOSITION_DELETE_TCB ; } - err = (struct sctp_errhdr *)(chunk->skb->data); -/* When calculating the time extension, an implementation * SHOULD use the RTT information measured based on the * previous COOKIE ECHO / ERROR exchange, and should add no
Greg Kroah-Hartman
maintainerPATCH 1812/2077 NFS: Charge unstable writes by request size, not folio size
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Benjamin Coddington <[email protected]>
commit 27934d02cbeb8a957dd11c985a579e58d30c5270 upstream.
nfs_folio_mark_unstable() and nfs_folio_clear_commit() charge and uncharge NR_WRITEBACK/WB_WRITEBACK by folio_nr_pages(folio) once per *request* added to or removed from a commit list. This is correct only when a folio has a single associated request. When pg_test splits a folio into N sub-folio requests (e.g. pNFS flexfiles striping with a stripe unit smaller than the folio size, or plain wsize-limited splitting), each of the N requests independently charges the whole folio's page count, inflating the accounting by a factor of N per folio. With large folios and small stripe units this reaches multiple orders of magnitude: a 2 MiB folio split into 512 4 KiB requests can charge up to 512x its real size, pushing global dirty+writeback accounting past the system's dirty threshold and forcing every buffered writer on the host into the hard-throttle path, including unrelated in-kernel NFS server threads sharing the box.
Charge each request only for the pages it actually covers.
--- fs/nfs/internal.h | 12 +++++++----- fs/nfs/pnfs_nfs.c | 2 +- fs/nfs/write.c | 14 ++++++++------ 3 files changed, 16 insertions(+), 12 deletions(-)
fs/nfs/internal.h +7 -5
@@ -847,17 +847,19 @@ void nfs_super_set_maxbytes(struct super }/* -* Record the page as unstable (an extra writeback period) and mark its -* inode as dirty. +* Record the request's range as unstable (an extra writeback period) and +* mark its inode as dirty. */ -static inline void nfs_folio_mark_unstable (struct folio *folio, +static inline void nfs_folio_mark_unstable (struct nfs_page *req,struct nfs_commit_info *cinfo) { +struct folio *folio =nfs_page_to_folio (req); +if (folio && !cinfo->dreq) {struct inode *inode = folio->mapping->host; -long nr =folio_nr_pages (folio); +long nr =DIV_ROUND_UP (req->wb_bytes,PAGE_SIZE ); -/* This page is really still in write-back - just that the +/* This range is really still in write-back - just that the * writeback is happening on the server now. */ node_stat_mod_folio (folio,NR_WRITEBACK , nr);
fs/nfs/pnfs_nfs.c +1 -1
@@ -1199,7 +1199,7 @@ pnfs_layout_mark_request_commit(struct nnfs_request_add_commit_list_locked (req, list, cinfo);mutex_unlock (&NFS_I (cinfo->inode)->commit_mutex); -nfs_folio_mark_unstable (nfs_page_to_folio (req), cinfo); +nfs_folio_mark_unstable (req, cinfo);return ; out_resched:mutex_unlock (&NFS_I (cinfo->inode)->commit_mutex);
fs/nfs/write.c +8 -6
@@ -807,7 +807,7 @@ nfs_request_add_commit_list(struct nfs_pmutex_lock (&NFS_I (cinfo->inode)->commit_mutex);nfs_request_add_commit_list_locked (req, &cinfo->mds->list, cinfo);mutex_unlock (&NFS_I (cinfo->inode)->commit_mutex); -nfs_folio_mark_unstable (nfs_page_to_folio (req), cinfo); +nfs_folio_mark_unstable (req, cinfo); }EXPORT_SYMBOL_GPL (nfs_request_add_commit_list);
@@ -866,10 +866,12 @@ nfs_mark_request_commit(struct nfs_pagenfs_request_add_commit_list (req, cinfo); } -static void nfs_folio_clear_commit (struct folio *folio) +static void nfs_folio_clear_commit (struct nfs_page *req) { +struct folio *folio =nfs_page_to_folio (req); +if (folio) { -long nr =folio_nr_pages (folio); +long nr =DIV_ROUND_UP (req->wb_bytes,PAGE_SIZE );node_stat_mod_folio (folio,NR_WRITEBACK , -nr);bdi_wb_stat_mod (folio->mapping->host,WB_WRITEBACK , -nr);
@@ -889,7 +891,7 @@ static void nfs_clear_request_commit(strnfs_request_remove_commit_list (req, cinfo); }mutex_unlock (&NFS_I (inode)->commit_mutex); -nfs_folio_clear_commit (nfs_page_to_folio (req)); +nfs_folio_clear_commit (req); } }
@@ -1741,7 +1743,7 @@ void nfs_retry_commit(struct list_head * req =nfs_list_entry (page_list->next);nfs_list_remove_request (req);nfs_mark_request_commit (req, lseg, cinfo, ds_commit_idx); -nfs_folio_clear_commit (nfs_page_to_folio (req)); +nfs_folio_clear_commit (req);nfs_unlock_and_release_request (req); } }
@@ -1813,7 +1815,7 @@ static void nfs_commit_release_pages(str req =nfs_list_entry (data->pages.next);nfs_list_remove_request (req); folio =nfs_page_to_folio (req); -nfs_folio_clear_commit (folio); +nfs_folio_clear_commit (req); dprintk("NFS: commit (%s/%llu %d@%lld)", nfs_req_openctx(req)->dentry->d_sb->s_id,
Greg Kroah-Hartman
maintainerPATCH 1813/2077 nvme-apple: Prevent shared tags across queues on Apple A11
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Nick Chan <[email protected]>
commit 6fe0687245e8406bf26143bd45eb16441bbe5280 upstream.
On Apple A11, tags of pending commands must be unique across the admin and IO queues, else the firmware crashes with
"duplicate tag error for tag N", with N being the tag.
Apply the existing workaround for M1 of reserving two tags for the admin queue to A11.
--- drivers/nvme/host/apple.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/nvme/host/apple.c +4 -8
@@ -225,7 +225,7 @@ static unsigned int apple_nvme_queue_dep {struct apple_nvme *anv =queue_to_apple_nvme (q); -if (q->is_adminq && anv->hw->has_lsq_nvmmu) +if (q->is_adminq)return APPLE_NVME_AQ_DEPTH ;return anv->hw->max_queue_depth;
@@ -303,7 +303,7 @@ static void apple_nvme_submit_cmd_t8015(memcpy ((void *)q->sqes + (q->sq_tail <<APPLE_NVME_IOSQES ), cmd,sizeof (*cmd)); -if (++q->sq_tail == anv->hw->max_queue_depth) +if (++q->sq_tail ==apple_nvme_queue_depth (q)) q->sq_tail =0 ;writel (q->sq_tail, q->sq_db);
@@ -1139,10 +1139,7 @@ static void apple_nvme_reset_work(struct }/* Setup the admin queue */ -if (anv->hw->has_lsq_nvmmu) - aqa =APPLE_NVME_AQ_DEPTH -1 ; -else - aqa = anv->hw->max_queue_depth -1 ; + aqa =APPLE_NVME_AQ_DEPTH -1 ; aqa |= aqa <<16 ;writel (aqa, anv->mmio_nvme +NVME_REG_AQA );writeq (anv->adminq.sq_dma_addr, anv->mmio_nvme +NVME_REG_ASQ );
@@ -1325,8 +1322,7 @@ static int apple_nvme_alloc_tagsets(stru* both queues. The admin queue gets the first APPLE_NVME_AQ_DEPTH which * must be marked as reserved in the IO queue. */ -if (anv->hw->has_lsq_nvmmu) - anv->tagset.reserved_tags =APPLE_NVME_AQ_DEPTH ; + anv->tagset.reserved_tags =APPLE_NVME_AQ_DEPTH ; anv->tagset.queue_depth = anv->hw->max_queue_depth -1 ; anv->tagset.timeout =NVME_IO_TIMEOUT ; anv->tagset.numa_node =NUMA_NO_NODE ;
Greg Kroah-Hartman
maintainerPATCH 1814/2077 nvmet-auth: reject short AUTH_RECEIVE buffers
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Michael Bommarito <[email protected]>
commit 779575bc35c687697ba69e904f2cd22e60112534 upstream.
nvmet_execute_auth_receive() trusts the AUTH_RECEIVE allocation length after checking only that it is nonzero and matches the transfer length. In the SUCCESS1 and FAILURE1/default states, that lets a remote NVMe-oF initiator reach the fixed-size DH-HMAC-CHAP response builders with a kmalloc() buffer shorter than the response, so nvmet_auth_success1() and nvmet_auth_failure1() write past the allocation; both only WARN_ON the short length and then format the message anyway.
Impact: A remote NVMe-oF initiator with access to an auth-enabled target can trigger a 16-byte heap out-of-bounds write via a one-byte AUTH_RECEIVE allocation length.
Compute the minimum response length for the current DH-HMAC-CHAP step in nvmet_auth_receive_data_len() and report a zero data length when the host-supplied allocation length is shorter, so the existing zero-length check in nvmet_execute_auth_receive() rejects the command before any builder runs. The SUCCESS1 minimum is sizeof(struct nvmf_auth_dhchap_success1_data) plus the HMAC hash length, because the response hash is written into the rval[] flexible-array tail, so the minimum is state dependent rather than a flat sizeof. CHALLENGE keeps its existing variable-length guard in nvmet_auth_challenge().
This is reachable only when in-band DH-HMAC-CHAP authentication is configured on the target.
--- drivers/nvme/target/fabrics-cmd-auth.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/nvme/target/fabrics-cmd-auth.c +25 -1
@@ -493,7 +493,31 @@ static void nvmet_auth_failure1(struct nu32 nvmet_auth_receive_data_len (struct nvmet_req *req) { -return le32_to_cpu (req->cmd->auth_receive.al); +struct nvmet_ctrl *ctrl = req->sq->ctrl; +u32 al =le32_to_cpu (req->cmd->auth_receive.al); +u32 min_len; + +/* +* Reject too-short al before kmalloc(al), since the SUCCESS1 and +* FAILURE1/default builders write fixed response headers into it. +*/ +switch (req->sq->dhchap_step) { +case NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE : +return al; +case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1 : + min_len =sizeof (struct nvmf_auth_dhchap_success1_data ); +if (req->sq->dhchap_c2) + min_len +=nvme_auth_hmac_hash_len (ctrl->shash_id); +break ; +default : + min_len =sizeof (struct nvmf_auth_dhchap_failure_data ); +break ; + } + +if (al < min_len) +return 0 ; + +return al; }void nvmet_execute_auth_receive (struct nvmet_req *req)
Greg Kroah-Hartman
maintainerPATCH 1815/2077 nvmet-rdma: handle inline data with a nonzero offset
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Bryam Vargas <[email protected]>
commit 48c0162f647bb47e6084ffbc71b8f213f5e2f4f8 upstream.
nvmet_rdma_use_inline_sg() maps the host-controlled inline data offset into the per-command inline scatterlist. The bounds check admits any offset with off + len <= inline_data_size, but the mapping still assumes the data begins in the first inline page:
sg->offset = off; sg->length = min_t(int, len, PAGE_SIZE - off);
When a port is configured with inline_data_size > PAGE_SIZE (settable up to max(SZ_16K, PAGE_SIZE)), an offset in (PAGE_SIZE, inline_data_size] makes "PAGE_SIZE - off" underflow, so sg->length is set to ~4 GiB and the block backend reads far past the first inline page. num_pages(len) also ignores the offset, so an in-bounds offset whose [off, off+len) span crosses a page boundary under-counts the scatterlist.
Map the offset properly: split it into a page index and an in-page offset, start the scatterlist at that page, and size the page count from page_off + len. Because the request scatterlist may now start at inline_sg[page_idx] rather than inline_sg[0], generalize the inline-SGL identity test in nvmet_rdma_release_rsp() to a range test; otherwise the persistent inline scatterlist is mistaken for an allocated one and nvmet_req_free_sgls() frees an inline page (and warns in free_large_kmalloc()).
--- drivers/nvme/target/rdma.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/nvme/target/rdma.c +10 -8
@@ -666,7 +666,8 @@ static void nvmet_rdma_release_rsp(strucif (rsp->n_rdma)nvmet_rdma_rw_ctx_destroy (rsp); -if (rsp->req.sg != rsp->cmd->inline_sg) +if (rsp->req.sg < rsp->cmd->inline_sg || + rsp->req.sg >= rsp->cmd->inline_sg + queue->dev->inline_page_count)nvmet_req_free_sgls (&rsp->req);if (unlikely (!list_empty_careful (&queue->rsp_wr_wait_list)))
@@ -821,24 +822,25 @@ static void nvmet_rdma_write_data_done(sstatic void nvmet_rdma_use_inline_sg (struct nvmet_rdma_rsp *rsp,u32 len,u64 off) { -int sg_count =num_pages (len); +u64 page_off = off %PAGE_SIZE ; +u64 page_idx = off /PAGE_SIZE ; +int sg_count =num_pages (page_off + len);struct scatterlist *sg;int i; - sg = rsp->cmd->inline_sg; + sg = &rsp->cmd->inline_sg[page_idx];for (i =0 ; i < sg_count; i++, sg++) {if (i < sg_count -1 )sg_unmark_end (sg);else sg_mark_end (sg); - sg->offset = off; - sg->length =min_t (int, len,PAGE_SIZE - off); + sg->offset = page_off; + sg->length =min_t (u64, len,PAGE_SIZE - page_off); len -= sg->length; -if (!i) - off =0 ; + page_off =0 ; } - rsp->req.sg = rsp->cmd->inline_sg; + rsp->req.sg = &rsp->cmd->inline_sg[page_idx]; rsp->req.sg_cnt = sg_count; }
Greg Kroah-Hartman
maintainerPATCH 1816/2077 nvmet: fix refcount leak in nvmet_sq_create()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Wentao Liang <[email protected]>
commit 34b9a83c50660148bde01cde16451dbe78369749 upstream.
In nvmet_sq_create(), a reference on the ctrl is taken via kref_get_unless_zero() before calling nvmet_check_sqid(). If nvmet_check_sqid() fails, the function returns the error directly without releasing the reference, leading to a leak.
Fix this by jumping to the "ctrl_put" label, which already performs the necessary nvmet_ctrl_put(ctrl). This ensures the reference is properly released on this error path.
--- drivers/nvme/target/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/target/core.c +1 -1
@@ -944,7 +944,7 @@ u16 nvmet_sq_create(struct nvmet_ctrl *c status =nvmet_check_sqid (ctrl, sqid, true);if (status !=NVME_SC_SUCCESS ) -return status; + goto ctrl_put; ret =nvmet_sq_init (sq, cq);if (ret) {
Greg Kroah-Hartman
maintainerPATCH 1817/2077 netdev-genl: report NAPI thread PID in the callers pid namespace
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Maoyi Xie <[email protected]>
commit 1f24c0d01db214c9e661915e9972404c96ca73c0 upstream.
netdev_nl_napi_fill_one() reports the NAPI kthread PID in NETDEV_A_NAPI_PID using task_pid_nr(), which returns the PID in the initial pid namespace.
NETDEV_CMD_NAPI_GET does not have GENL_ADMIN_PERM and the netdev genl family is netnsok, so a caller in a child pid namespace can issue it. That caller then sees the kthread's global PID, even though the kthread is not visible in its pid namespace, where the value should be 0.
Translate the PID through the caller's pid namespace, the same way commit 3799c2570982 ("io_uring/fdinfo: translate SqThread PID through caller's pid_ns") did for the io_uring SQPOLL thread. The doit and dumpit paths both run synchronously in the caller's context, so task_active_pid_ns(current) is the caller's pid namespace.
--- net/core/netdev-genl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/netdev-genl.c +3 -1
@@ -2,6 +2,7 @@#include <linux/netdevice.h> #include <linux/notifier.h> +#include <linux/pid_namespace.h> #include <linux/rtnetlink.h> #include <net/busy_poll.h> #include <net/net_namespace.h>
@@ -189,7 +190,8 @@ netdev_nl_napi_fill_one(struct sk_buff * goto nla_put_failure;if (napi->thread) { - pid =task_pid_nr (napi->thread); + pid =task_pid_nr_ns (napi->thread, +task_active_pid_ns (current));if (nla_put_u32 (rsp,NETDEV_A_NAPI_PID , pid)) goto nla_put_failure; }
Greg Kroah-Hartman
maintainerPATCH 1818/2077 ovl: use linked upper dentry in copy-up tmpfile
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Souvik Banerjee <[email protected]>
commit e348eecd4d8fa8d18a5157ff59f7be1dc59c5928 upstream.
ovl_copy_up_tmpfile() stores the disconnected O_TMPFILE dentry as the overlay's upper dentry reference via ovl_inode_update(). vfs_tmpfile() allocated this dentry via d_alloc(parentpath->dentry, &slash_name), so d_name is "/" and d_parent is c->workdir. Local upper filesystems (ext4, btrfs, xfs, ...) immediately rename it to "#<inum>" via d_mark_tmpfile() inside their ->tmpfile() op; FUSE and virtiofs do not, so both fields stay that way. Neither identifies the destination directory and filename where ovl_do_link() actually linked the file.
When the upper filesystem implements ->d_revalidate() (e.g. FUSE or virtiofs), ovl_revalidate_real() calls it with the dentry's parent inode and a snapshot of d_name. The server tries to look up "/" inside c->workdir, fails, and overlayfs reports -ESTALE.
This causes persistent ESTALE errors for any file that was copied up via the tmpfile path, breaking dpkg, apt, and other tools that do rename-over-existing on overlayfs with a FUSE/virtiofs upper.
Before commit 6b52243f633e ("ovl: fold copy-up helpers into callers"), the tmpfile copy-up path used a dedicated helper ovl_link_tmpfile() that captured the linked destination dentry returned by ovl_do_link():
err = ovl_do_link(temp, udir, upper);
...
if (!err)
*newdentry = dget(upper);and published it via ovl_inode_update(d_inode(c->dentry), newdentry). The fold inlined ovl_do_link() into ovl_copy_up_tmpfile() but dropped the dget(upper) capture, and rewrote the publish line as ovl_inode_update(d_inode(c->dentry), dget(temp)) — where temp is the disconnected O_TMPFILE dentry.
Fix by keeping a reference to the linked destination dentry after ovl_do_link() succeeds, and publishing that dentry at the existing ovl_inode_update() call site. The non-tmpfile/workdir path continues to publish the renamed temporary dentry.
Reproducer:
- Mount overlayfs with virtiofs (or a FUSE fs whose server advertises FUSE_TMPFILE) as upper
- Run: dpkg -i <any .deb>
- Observe: "error installing new file '...': Stale file handle"
--- fs/overlayfs/copy_up.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
fs/overlayfs/copy_up.c +10 -2
@@ -853,7 +853,7 @@ static int ovl_copy_up_tmpfile(struct ov {struct ovl_fs *ofs =OVL_FS (c->dentry->d_sb);struct inode *udir =d_inode (c->destdir); -struct dentry *temp, *upper; +struct dentry *temp, *upper, *newdentry =NULL ;struct file *tmpfile;int err;
@@ -889,6 +889,14 @@ static int ovl_copy_up_tmpfile(struct ov err =PTR_ERR (upper);if (!IS_ERR (upper)) { err =ovl_do_link (ofs, temp, udir, upper); +if (!err) { +/* +* Record the linked dentry -- not the disconnected +* O_TMPFILE dentry -- so that ->d_revalidate() on +* the upper fs sees the real parent/name. +*/ + newdentry =dget (upper); + }end_creating (upper); }
@@ -903,7 +911,7 @@ static int ovl_copy_up_tmpfile(struct ovif (!c->metacopy)ovl_set_upperdata (d_inode (c->dentry)); -ovl_inode_update (d_inode (c->dentry),dget (temp)); +ovl_inode_update (d_inode (c->dentry), newdentry); out:ovl_end_write (c->dentry);
Greg Kroah-Hartman
maintainerPATCH 1819/2077 can: esd_usb: kill anchored URBs before freeing netdevs
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Fan Wu <[email protected]>
commit c43122fef328a70045fe7621c06de6b2b8e19264 upstream.
esd_usb_disconnect() frees each CAN netdev with free_candev() inside its per-netdev loop and only calls unlink_all_urbs(dev) afterwards. The per-netdev private data (struct esd_usb_net_priv) is embedded in the net_device allocation returned by alloc_candev(), so once free_candev() has run, dev->nets[i] points to freed memory. unlink_all_urbs() then dereferences the freed dev->nets[i] to kill the per-netdev TX anchor (usb_kill_anchored_urbs(&priv->tx_submitted)), clear active_tx_jobs, and reset priv->tx_contexts[].
Reorder the teardown so the anchored URBs are killed before the netdevs are freed, matching other CAN/USB drivers in the same directory such as ems_usb, usb_8dev and mcba_usb, which unregister, then unlink, then
free: unregister the netdevs first (which stops their TX queues), call unlink_all_urbs(dev) once, then free the netdevs.
This issue was found by an in-house static analysis tool.
--- drivers/net/can/usb/esd_usb.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/can/usb/esd_usb.c +4 -1
@@ -1390,10 +1390,13 @@ static void esd_usb_disconnect(struct us netdev = dev->nets[i]->netdev;netdev_info (netdev,"unregister\n" );unregister_netdev (netdev); -free_candev (netdev); } }unlink_all_urbs (dev); +for (i =0 ; i < dev->net_count; i++) { +if (dev->nets[i]) +free_candev (dev->nets[i]->netdev); + }kfree (dev); } }
Greg Kroah-Hartman
maintainerPATCH 1820/2077 can: isotp: use unconditional synchronize_rcu() in isotp_release()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 9b1a02e0d980ac6b0e36a90378f847062f81d7e4 upstream.
isotp_notify() unregisters the (RCU) CAN filters via can_rx_unregister() and clears so->bound without waiting for a grace period. isotp_release() uses so->bound to decide whether it needs to call synchronize_rcu() before cancelling so->rxtimer, so when NETDEV_UNREGISTER runs first it skips that synchronize_rcu() and can cancel the timer while an in-flight isotp_rcv() is still executing and about to re-arm it via isotp_send_fc(), leading to a use-after-free timer callback on the freed socket.
sakisho-bot remarked a problem with rtnl_lock held in isotp_notify(), therefore make isotp_release() always call synchronize_rcu() before cancelling the timers, regardless of so->bound. This still closes the original race (isotp_notify() clearing so->bound without waiting for in-flight isotp_rcv() callers before isotp_release() cancels the RX timer) without adding any RCU wait to the netdevice notifier path.
--- net/can/isotp.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
net/can/isotp.c +8 -1
@@ -1237,11 +1237,18 @@ static int isotp_release(struct socket *SINGLE_MASK (so->txid), isotp_rcv_echo, sk);dev_put (dev); -synchronize_rcu (); } } } +/* Always wait for a grace period before touching the timers below. +* A concurrent NETDEV_UNREGISTER may have already unregistered our +* filters and cleared so->bound in isotp_notify() without waiting +* for in-flight isotp_rcv() callers to finish, so this call must not +* be skipped just because so->bound is already 0 here. +*/ +synchronize_rcu (); +hrtimer_cancel (&so->txfrtimer);hrtimer_cancel (&so->txtimer);hrtimer_cancel (&so->rxtimer);
Greg Kroah-Hartman
maintainerPATCH 1821/2077 can: isotp: fix use-after-free race with concurrent NETDEV_UNREGISTER
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 20bab8b88baac140ca3701116e1d486c7f51e311 upstream.
isotp_release() looked up the bound network device via dev_get_by_index() using the stored ifindex. During device unregistration the device is unlisted from the ifindex hash before the NETDEV_UNREGISTER notifier chain runs, so a concurrent isotp_release() could find no device, skip can_rx_unregister() entirely, and still proceed to free the socket. Since isotp_release() had already removed itself from the isotp notifier list at that point, isotp_notify() would never get a chance to clean up either, leaving a stale CAN filter that keeps pointing at the freed socket.
Fix this the same way raw.c already does: hold a tracked reference to the bound net_device in the socket (so->dev/so->dev_tracker) from bind() onward instead of re-resolving it from the ifindex, and serialize bind()/release() with rtnl_lock() so that so->dev is always consistent with what the NETDEV_UNREGISTER notifier sees. so->dev stays valid regardless of ifindex-hash unlisting, and is only ever cleared by whichever of isotp_release()/isotp_notify() gets there first, so the filter is always removed exactly once.
isotp_bind() now rejects a (re)bind with -EAGAIN while so->[tx|rx].state isn't ISOTP_IDLE yet, so a timer left running by a prior NETDEV_UNREGISTER can't act on a newly bound so->ifindex. Both checks share the same lock_sock() section, so there is no window in which a concurrent isotp_notify() clearing so->bound could be missed.
--- net/can/isotp.c | 87 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 28 deletions(-)
net/can/isotp.c +59 -28
@@ -152,6 +152,8 @@ struct isotp_sock {struct sock sk;int bound;int ifindex; +struct net_device *dev; +netdevice_tracker dev_tracker;canid_t txid;canid_t rxid;ktime_t tx_gap;
@@ -978,6 +980,14 @@ static int isotp_sendmsg(struct socket * goto err_event_drop; } +/* so->bound is only checked once above - a wakeup may have +* unbound/rebound the socket meanwhile, so re-validate it +*/ +if (!so->bound) { + err = -EADDRNOTAVAIL ; + goto err_out_drop; + } +/* PDU size > default => try max_pdu_size */ if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {u8 *newbuf =kmalloc (max_pdu_size,GFP_KERNEL );
@@ -1219,28 +1229,30 @@ static int isotp_release(struct socket *list_del (&so->notifier);spin_unlock (&isotp_notifier_lock); +rtnl_lock ();lock_sock (sk); -/* remove current filters & unregister */ -if (so->bound) { -if (so->ifindex) { -struct net_device *dev; - - dev =dev_get_by_index (net, so->ifindex); -if (dev) { -if (isotp_register_rxid (so)) -can_rx_unregister (net, dev, so->rxid, -SINGLE_MASK (so->rxid), - isotp_rcv, sk); - -can_rx_unregister (net, dev, so->txid, -SINGLE_MASK (so->txid), - isotp_rcv_echo, sk); -dev_put (dev); - } - } +/* remove current filters & unregister +* tracked reference so->dev is taken at bind() time with rtnl_lock +*/ +if (so->bound && so->dev) { +if (isotp_register_rxid (so)) +can_rx_unregister (net, so->dev, so->rxid, +SINGLE_MASK (so->rxid), + isotp_rcv, sk); + +can_rx_unregister (net, so->dev, so->txid, +SINGLE_MASK (so->txid), + isotp_rcv_echo, sk); +netdev_put (so->dev, &so->dev_tracker); } + so->ifindex =0 ; + so->bound =0 ; + so->dev =NULL ; + +rtnl_unlock (); +/* Always wait for a grace period before touching the timers below. * A concurrent NETDEV_UNREGISTER may have already unregistered our * filters and cleared so->bound in isotp_notify() without waiting
@@ -1253,9 +1265,6 @@ static int isotp_release(struct socket *hrtimer_cancel (&so->txtimer);hrtimer_cancel (&so->rxtimer); - so->ifindex =0 ; - so->bound =0 ; -sock_orphan (sk); sock->sk =NULL ;
@@ -1310,6 +1319,7 @@ static int isotp_bind(struct socket *socif (!addr->can_ifindex)return -ENODEV ; +rtnl_lock ();lock_sock (sk);if (so->bound) {
@@ -1317,6 +1327,17 @@ static int isotp_bind(struct socket *soc goto out; } +/* A transmission or reception that outlived a previous binding +* (unbound by NETDEV_UNREGISTER) may still be draining; the FC/echo +* and RX watchdog timers bound how long this takes. Checked together +* with so->bound in the same lock_sock() section above, so there is +* no window in which a concurrent isotp_notify() could be missed. +*/ +if (so->tx.state !=ISOTP_IDLE || so->rx.state !=ISOTP_IDLE ) { + err = -EAGAIN ; + goto out; + } +/* ensure different CAN IDs when the rx_id is to be registered */ if (isotp_register_rxid (so) && rx_id == tx_id) { err = -EADDRNOTAVAIL ;
@@ -1329,14 +1350,12 @@ static int isotp_bind(struct socket *soc goto out; }if (dev->type !=ARPHRD_CAN ) { -dev_put (dev); err = -ENODEV ; - goto out; + goto out_put_dev; }if (READ_ONCE (dev->mtu) < so->ll.mtu) { -dev_put (dev); err = -EINVAL ; - goto out; + goto out_put_dev; }if (!(dev->flags &IFF_UP )) notify_enetdown =1 ;
@@ -1354,16 +1373,25 @@ static int isotp_bind(struct socket *soccan_rx_register (net, dev, tx_id,SINGLE_MASK (tx_id), isotp_rcv_echo, sk,"isotpe" , sk); -dev_put (dev); -/* switch to new settings */ so->ifindex = ifindex; so->rxid = rx_id; so->txid = tx_id; so->bound =1 ; +/* bind() ok -> hold a reference for so->dev so that isotp_release() +* can safely reach the device later, even if a concurrent +* NETDEV_UNREGISTER has already unlisted it by ifindex. +*/ + so->dev = dev; +netdev_hold (so->dev, &so->dev_tracker,GFP_KERNEL ); + +out_put_dev: +/* remove potential reference from dev_get_by_index() */ +dev_put (dev); out:release_sock (sk); +rtnl_unlock ();if (notify_enetdown) { sk->sk_err =ENETDOWN ;
@@ -1566,7 +1594,7 @@ static void isotp_notify(struct isotp_soif (!net_eq (dev_net (dev),sock_net (sk)))return ; -if (so->ifindex != dev->ifindex) +if (so->dev != dev)return ;switch (msg) {
@@ -1582,10 +1610,12 @@ static void isotp_notify(struct isotp_socan_rx_unregister (dev_net (dev), dev, so->txid,SINGLE_MASK (so->txid), isotp_rcv_echo, sk); +netdev_put (so->dev, &so->dev_tracker); } so->ifindex =0 ; so->bound =0 ; + so->dev =NULL ;release_sock (sk); sk->sk_err =ENODEV ;
@@ -1645,6 +1675,7 @@ static int isotp_init(struct sock *sk) so->ifindex =0 ; so->bound =0 ; + so->dev =NULL ; so->opt.flags =CAN_ISOTP_DEFAULT_FLAGS ; so->opt.ext_address =CAN_ISOTP_DEFAULT_EXT_ADDRESS ;
Greg Kroah-Hartman
maintainerPATCH 1822/2077 can: isotp: serialize TX state transitions under so->rx_lock
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit cf070fe33bfbd1a4c21236078fadb35dd223a157 upstream.
The TX state machine (so->tx.state) is driven from three contexts:sendmsg() claiming and progressing a transfer, the RX path consuming Flow Control/echo frames, and two hrtimers timing out a stalled transfer. Mixing a lock-free cmpxchg() claim in sendmsg() with hrtimer_cancel() calls made under so->rx_lock elsewhere left windows where a frame or timer callback could act on a state that had already moved on, corrupting an unrelated transfer.
so->rx_lock now covers the full lifecycle of a TX claim: sendmsg() takes it to check so->tx.state is ISOTP_IDLE, switch it to ISOTP_SENDING, bump so->tx_gen and drain the previous transfer's timers - all as one critical section. isotp_rcv_fc()/isotp_rcv_cf() already run under this lock via isotp_rcv(), and isotp_rcv_echo() now takes it itself, so none of them can ever observe a transfer mid-claim. This also means a transfer can no longer be handed to sendmsg()'s cleanup paths (signal or send error) while another thread is concurrently claiming or finishing it, so those paths can cancel timers and reset the state unconditionally.
isotp_release() claims the socket the same way, so a racing sendmsg() sees a consistent ISOTP_SHUTDOWN and skips arming its timer or sending.
Only the hrtimer callbacks stay outside so->rx_lock, since they run under so->rx_lock's cancellation elsewhere and taking it themselves would deadlock. so->tx_gen lets them recognize whether the transfer they timed out is still the one currently active, so they don't report an error against a transfer that has since completed or been superseded.
--- net/can/isotp.c | 202 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 160 insertions(+), 42 deletions(-)
net/can/isotp.c +160 -42
@@ -158,7 +158,7 @@ struct isotp_sock {canid_t rxid;ktime_t tx_gap;ktime_t lastrxcf_tstamp; -struct hrtimer rxtimer, txtimer, txfrtimer; +struct hrtimer rxtimer, txtimer, txfrtimer, echotimer;struct can_isotp_options opt;struct can_isotp_fc_options rxfc, txfc;struct can_isotp_ll_options ll;
@@ -166,6 +166,7 @@ struct isotp_sock {u32 force_tx_stmin;u32 force_rx_stmin;u32 cfecho;/* consecutive frame echo tag */ +u32 tx_gen;/* generation, bumped per new tx transfer */ struct tpcon rx, tx;struct list_head notifier;wait_queue_head_t wait;
@@ -378,6 +379,15 @@ static int isotp_rcv_fc(struct isotp_sochrtimer_cancel (&so->txtimer); +/* isotp_tx_timeout() may have given up on this job while +* hrtimer_cancel() above waited for it to finish; so->rx_lock +* (held by our caller isotp_rcv()) rules out a concurrent claim, +* so a plain recheck is enough here. +*/ +if (so->tx.state !=ISOTP_WAIT_FC && + so->tx.state !=ISOTP_WAIT_FIRST_FC ) +return 1 ; +if ((cf->len < ae +FC_CONTENT_SZ ) || ((so->opt.flags &ISOTP_CHECK_PADDING ) &&check_pad (so, cf, ae +FC_CONTENT_SZ , so->opt.rxpad_content))) {
@@ -424,7 +434,7 @@ static int isotp_rcv_fc(struct isotp_soc so->tx.bs =0 ; so->tx.state =ISOTP_SENDING ;/* send CF frame and enable echo timeout handling */ -hrtimer_start (&so->txtimer,ktime_set (ISOTP_ECHO_TIMEOUT ,0 ), +hrtimer_start (&so->echotimer,ktime_set (ISOTP_ECHO_TIMEOUT ,0 ),HRTIMER_MODE_REL_SOFT );isotp_send_cframe (so);break ;
@@ -577,6 +587,14 @@ static int isotp_rcv_cf(struct sock *sk,hrtimer_cancel (&so->rxtimer); +/* isotp_rx_timer_handler() may have raced us for so->rx.state +* while hrtimer_cancel() above waited for it to finish, already +* reporting ETIMEDOUT and resetting the reception; don't process +* this CF into a reassembly that has already been given up on. +*/ +if (so->rx.state !=ISOTP_WAIT_DATA ) +return 1 ; +/* CFs are never longer than the FF */ if (cf->len > so->rx.ll_dl)return 1 ;
@@ -872,20 +890,36 @@ static void isotp_rcv_echo(struct sk_bufstruct canfd_frame *cf = (struct canfd_frame *)skb->data;/* only handle my own local echo CF/SF skb's (no FF!) */ -if (skb->sk != sk || so->cfecho != *(u32 *)cf->data) +if (skb->sk != sk)return ; +/* unlike isotp_rcv_fc()/isotp_rcv_cf(), not already under so->rx_lock +* (no isotp_rcv() caller here), so take it ourselves +*/ +spin_lock (&so->rx_lock); + +/* so->cfecho may since belong to a new transfer; recheck under lock */ +if (so->cfecho != *(u32 *)cf->data) + goto out_unlock; +/* cancel local echo timeout */ -hrtimer_cancel (&so->txtimer); +hrtimer_cancel (&so->echotimer);/* local echo skb with consecutive frame has been consumed */ so->cfecho =0 ; +/* claiming a transfer also takes so->rx_lock, so a plain recheck +* is enough: so->tx.state can't have flipped to ISOTP_SENDING for +* a new claim while we're still in here +*/ +if (so->tx.state !=ISOTP_SENDING ) + goto out_unlock; +if (so->tx.idx >= so->tx.len) {/* we are done */ so->tx.state =ISOTP_IDLE ;wake_up_interruptible (&so->wait); -return ; + goto out_unlock; }if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
@@ -893,53 +927,83 @@ static void isotp_rcv_echo(struct sk_buf so->tx.state =ISOTP_WAIT_FC ;hrtimer_start (&so->txtimer,ktime_set (ISOTP_FC_TIMEOUT ,0 ),HRTIMER_MODE_REL_SOFT ); -return ; + goto out_unlock; }/* no gap between data frames needed => use burst mode */ if (!so->tx_gap) {/* enable echo timeout handling */ -hrtimer_start (&so->txtimer,ktime_set (ISOTP_ECHO_TIMEOUT ,0 ), +hrtimer_start (&so->echotimer,ktime_set (ISOTP_ECHO_TIMEOUT ,0 ),HRTIMER_MODE_REL_SOFT );isotp_send_cframe (so); -return ; + goto out_unlock; }/* start timer to send next consecutive frame with correct delay */ hrtimer_start (&so->txfrtimer, so->tx_gap,HRTIMER_MODE_REL_SOFT ); + +out_unlock: +spin_unlock (&so->rx_lock); } -static enum hrtimer_restart isotp_tx_timer_handler (struct hrtimer *hrtimer) +/* shared by so->txtimer's and so->echotimer's callbacks. Both timers get +* cancelled under so->rx_lock elsewhere, so this must stay lock-free to +* avoid deadlocking with that; uses so->tx_gen instead to avoid tainting +* a new transfer with an error from the one that just timed out. +*/ +static enum hrtimer_restart isotp_tx_timeout (struct isotp_sock *so) { - struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, - txtimer);struct sock *sk = &so->sk; +u32 gen =READ_ONCE (so->tx_gen); +u32 old_state =READ_ONCE (so->tx.state);/* don't handle timeouts in IDLE or SHUTDOWN state */ -if (so->tx.state ==ISOTP_IDLE || so->tx.state ==ISOTP_SHUTDOWN ) +if (old_state ==ISOTP_IDLE || old_state ==ISOTP_SHUTDOWN ) +return HRTIMER_NORESTART ; + +/* only claim the timeout if the state is still unchanged */ +if (cmpxchg (&so->tx.state, old_state,ISOTP_IDLE ) != old_state)return HRTIMER_NORESTART ;/* we did not get any flow control or echo frame in time */ -/* report 'communication error on send' */ - sk->sk_err =ECOMM ; -if (!sock_flag (sk,SOCK_DEAD )) -sk_error_report (sk); +if (READ_ONCE (so->tx_gen) == gen) { +/* report 'communication error on send' */ + sk->sk_err =ECOMM ; +if (!sock_flag (sk,SOCK_DEAD )) +sk_error_report (sk); + } -/* reset tx state */ - so->tx.state =ISOTP_IDLE ;wake_up_interruptible (&so->wait);return HRTIMER_NORESTART ; } +/* so->txtimer: fires when a Flow Control frame does not arrive in time */ +static enum hrtimer_restart isotp_tx_timer_handler (struct hrtimer *hrtimer) +{ + struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, + txtimer); + +return isotp_tx_timeout (so); +} + +/* so->echotimer: fires when a sent CF/SF's local echo does not arrive */ +static enum hrtimer_restart isotp_echo_timer_handler (struct hrtimer *hrtimer) +{ + struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, + echotimer); + +return isotp_tx_timeout (so); +} +static enum hrtimer_restart isotp_txfr_timer_handler (struct hrtimer *hrtimer) { struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, txfrtimer);/* start echo timeout handling and cover below protocol error */ -hrtimer_start (&so->txtimer,ktime_set (ISOTP_ECHO_TIMEOUT ,0 ), +hrtimer_start (&so->echotimer,ktime_set (ISOTP_ECHO_TIMEOUT ,0 ),HRTIMER_MODE_REL_SOFT );/* cfecho should be consumed by isotp_rcv_echo() here */
@@ -960,13 +1024,24 @@ static int isotp_sendmsg(struct socket *int ae = (so->opt.flags &CAN_ISOTP_EXTEND_ADDR ) ?1 :0 ;int wait_tx_done = (so->opt.flags &CAN_ISOTP_WAIT_TX_DONE ) ?1 :0 ;s64 hrtimer_sec =ISOTP_ECHO_TIMEOUT ; +struct hrtimer *tx_hrt = &so->echotimer; +u32 new_state =ISOTP_SENDING ;int off;int err;if (!so->bound || so->tx.state ==ISOTP_SHUTDOWN )return -EADDRNOTAVAIL ; -while (cmpxchg (&so->tx.state,ISOTP_IDLE ,ISOTP_SENDING ) !=ISOTP_IDLE ) { +/* claim the socket under so->rx_lock: this serializes the claim +* with the RX path and with sendmsg()'s own error paths below, so +* none of them can ever see a transfer mid-claim +*/ +for (;;) { +spin_lock_bh (&so->rx_lock); +if (READ_ONCE (so->tx.state) ==ISOTP_IDLE ) +break ; +spin_unlock_bh (&so->rx_lock); +/* we do not support multiple buffers - for now */ if (msg->msg_flags &MSG_DONTWAIT )return -EAGAIN ;
@@ -975,11 +1050,23 @@ static int isotp_sendmsg(struct socket *return -EADDRNOTAVAIL ;/* wait for complete transmission of current pdu */ - err =wait_event_interruptible (so->wait, so->tx.state ==ISOTP_IDLE ); + err =wait_event_interruptible (so->wait, + so->tx.state ==ISOTP_IDLE );if (err) - goto err_event_drop; +return err; } +/* new transfer: bump so->tx_gen and drain the old one's timers, +* still under the so->rx_lock we just claimed the socket with +*/ +WRITE_ONCE (so->tx.state,ISOTP_SENDING ); +WRITE_ONCE (so->tx_gen,READ_ONCE (so->tx_gen) +1 ); +hrtimer_cancel (&so->txtimer); +hrtimer_cancel (&so->echotimer); +hrtimer_cancel (&so->txfrtimer); + so->cfecho =0 ; +spin_unlock_bh (&so->rx_lock); +/* so->bound is only checked once above - a wakeup may have * unbound/rebound the socket meanwhile, so re-validate it */
@@ -1096,18 +1183,33 @@ static int isotp_sendmsg(struct socket * so->cfecho = *(u32 *)cf->data; }else {/* standard flow control check */ - so->tx.state =ISOTP_WAIT_FIRST_FC ; + new_state =ISOTP_WAIT_FIRST_FC ;/* start timeout for FC */ hrtimer_sec =ISOTP_FC_TIMEOUT ; + tx_hrt = &so->txtimer;/* no CF echo tag for isotp_rcv_echo() (FF-mode) */ so->cfecho =0 ; } } -hrtimer_start (&so->txtimer,ktime_set (hrtimer_sec,0 ), +spin_lock_bh (&so->rx_lock); +if (so->tx.state ==ISOTP_SHUTDOWN ) { +/* isotp_release() has since taken over and already drained +* our timers - don't send into a socket that's going away +*/ +spin_unlock_bh (&so->rx_lock); +kfree_skb (skb); +dev_put (dev); +wake_up_interruptible (&so->wait); +return -EADDRNOTAVAIL ; + } +/* WAIT_FIRST_FC for standard FF, else stays ISOTP_SENDING */ + so->tx.state = new_state; +hrtimer_start (tx_hrt,ktime_set (hrtimer_sec,0 ),HRTIMER_MODE_REL_SOFT ); +spin_unlock_bh (&so->rx_lock);/* send the first or only CAN frame */ cf->flags = so->ll.tx_flags;
@@ -1120,13 +1222,10 @@ static int isotp_sendmsg(struct socket *pr_notice_once ("can-isotp: %s: can_send_ret %pe\n" , __func__,ERR_PTR (err)); +spin_lock_bh (&so->rx_lock);/* no transmission -> no timeout monitoring */ -hrtimer_cancel (&so->txtimer); - -/* reset consecutive frame echo tag */ - so->cfecho =0 ; - - goto err_out_drop; +hrtimer_cancel (tx_hrt); + goto err_out_drop_locked; }if (wait_tx_done) {
@@ -1142,14 +1241,21 @@ static int isotp_sendmsg(struct socket *return size; +err_out_drop: +/* claimed but nothing sent yet - no timer to cancel */ +spin_lock_bh (&so->rx_lock); + goto err_out_drop_locked; err_event_drop: -/* got signal: force tx state machine to be idle */ - so->tx.state =ISOTP_IDLE ; +/* interrupted waiting on our own transfer - drain its timers */ +spin_lock_bh (&so->rx_lock);hrtimer_cancel (&so->txfrtimer);hrtimer_cancel (&so->txtimer); -err_out_drop: -/* drop this PDU and unlock a potential wait queue */ +hrtimer_cancel (&so->echotimer); +err_out_drop_locked: +/* release the claim; so->rx_lock still held from above */ + so->cfecho =0 ; so->tx.state =ISOTP_IDLE ; +spin_unlock_bh (&so->rx_lock);wake_up_interruptible (&so->wait);return err;
@@ -1211,13 +1317,20 @@ static int isotp_release(struct socket * so =isotp_sk (sk); net =sock_net (sk); -/* wait for complete transmission of current pdu */ -while (wait_event_interruptible (so->wait, so->tx.state ==ISOTP_IDLE ) ==0 && -cmpxchg (&so->tx.state,ISOTP_IDLE ,ISOTP_SHUTDOWN ) !=ISOTP_IDLE ) +/* best-effort: wait for a running pdu to finish, but don't block on +* it forever - give up after the first signal +*/ +while (so->tx.state !=ISOTP_IDLE && +wait_event_interruptible (so->wait, so->tx.state ==ISOTP_IDLE ) ==0 ) ; -/* force state machines to be idle also when a signal occurred */ +/* claim the socket under so->rx_lock like sendmsg() does, so its +* claim can't race the forced ISOTP_SHUTDOWN below; force it +* unconditionally, even when a signal cut the wait above short +*/ +spin_lock_bh (&so->rx_lock); so->tx.state =ISOTP_SHUTDOWN ; +spin_unlock_bh (&so->rx_lock); so->rx.state =ISOTP_IDLE ;spin_lock (&isotp_notifier_lock);
@@ -1263,6 +1376,7 @@ static int isotp_release(struct socket *hrtimer_cancel (&so->txfrtimer);hrtimer_cancel (&so->txtimer); +hrtimer_cancel (&so->echotimer);hrtimer_cancel (&so->rxtimer);sock_orphan (sk);
@@ -1702,10 +1816,14 @@ static int isotp_init(struct sock *sk) so->rx.buflen =ARRAY_SIZE (so->rx.sbuf); so->tx.buflen =ARRAY_SIZE (so->tx.sbuf); -hrtimer_setup (&so->rxtimer, isotp_rx_timer_handler,CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); -hrtimer_setup (&so->txtimer, isotp_tx_timer_handler,CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); -hrtimer_setup (&so->txfrtimer, isotp_txfr_timer_handler,CLOCK_MONOTONIC , -HRTIMER_MODE_REL_SOFT ); +hrtimer_setup (&so->rxtimer, isotp_rx_timer_handler, +CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); +hrtimer_setup (&so->txtimer, isotp_tx_timer_handler, +CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); +hrtimer_setup (&so->echotimer, isotp_echo_timer_handler, +CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); +hrtimer_setup (&so->txfrtimer, isotp_txfr_timer_handler, +CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT );init_waitqueue_head (&so->wait);spin_lock_init (&so->rx_lock);
Greg Kroah-Hartman
maintainerPATCH 1823/2077 can: bcm: defer rx_op deallocation to workqueue to fix thrtimer UAF
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Lee Jones <[email protected]>
commit 68973f9db76144825e4f35dfdc80fb8279eb2d57 upstream.
Commit f1b4e32aca08 ("can: bcm: use call_rcu() instead of costly synchronize_rcu()") replaced synchronize_rcu() in bcm_delete_rx_op() with call_rcu() and introduced the RX_NO_AUTOTIMER flag.
However, this flag check was omitted for thrtimer in the packet rx fast-path. During BCM RX operation teardown, a concurrent RCU reader (bcm_rx_handler) can race and re-arm thrtimer via bcm_rx_update_and_send() after call_rcu() has been scheduled. Once the RCU grace period elapses, bcm_op is freed. The subsequently firing thrtimer then dereferences the deallocated op, causing a UAF.
Adding flag checks to the rx fast-path (bcm_rx_update_and_send) does not fully close the TOCTOU race and introduces latency for every CAN frame. Conversely, calling hrtimer_cancel() directly inside the RCU callback (softirq context) is fatal as hrtimer_cancel() can sleep, triggering a "scheduling while atomic" panic.
Resolve this by deferring the timer cancellation and memory free to a dedicated unbound workqueue (bcm_wq). The RCU callback now queues a work item to bcm_wq, which safely cancels both timers and deallocates memory in sleepable process context. A dedicated workqueue is used to prevent system-wide WQ saturation and is cleanly flushed/destroyed on module unload to avoid rmmod page faults.
Since the deferred work can now outlive the calling context by an unbounded amount, also take a reference on op->sk when it is assigned and drop it only once the deferred work has cancelled both timers, so a socket can no longer be freed out from under a still-armed timer whose callback (bcm_send_to_user()) dereferences op->sk.
--- net/can/bcm.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-)
net/can/bcm.c +34 -3
@@ -58,6 +58,7 @@#include <linux/can/skb.h> #include <linux/can/bcm.h> #include <linux/slab.h> +#include <linux/workqueue.h> #include <linux/spinlock.h> #include <net/can.h> #include <net/sock.h>
@@ -92,6 +93,8 @@ MODULE_ALIAS("can-proto-2");#define BCM_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex) +static struct workqueue_struct *bcm_wq; +/* * easy access to the first 64 bit of can(fd)_frame payload. cp->data is * 64 bit aligned so the offset has to be multiples of 8 which is ensured
@@ -105,6 +108,7 @@ static inline u64 get_u64(const struct cstruct bcm_op {struct list_head list;struct rcu_head rcu; +struct work_struct work;int ifindex;canid_t can_id;u32 flags;
@@ -793,9 +797,12 @@ static struct bcm_op *bcm_find_op(structreturn NULL ; } -static void bcm_free_op_rcu (struct rcu_head *rcu_head) +static void bcm_free_op_work (struct work_struct *work) { - struct bcm_op *op = container_of(rcu_head, struct bcm_op, rcu); + struct bcm_op *op = container_of(work, struct bcm_op, work); + +hrtimer_cancel (&op->timer); +hrtimer_cancel (&op->thrtimer);if ((op->frames) && (op->frames != &op->sframe))kfree (op->frames);
@@ -803,9 +810,23 @@ static void bcm_free_op_rcu(struct rcu_hif ((op->last_frames) && (op->last_frames != &op->last_sframe))kfree (op->last_frames); +/* the last possible access to op->timer/op->thrtimer has now +* happened above via hrtimer_cancel() - op->sk is no longer +* needed by any pending timer callback, so drop our reference +*/ +sock_put (op->sk); +kfree (op); } +static void bcm_free_op_rcu (struct rcu_head *rcu_head) +{ + struct bcm_op *op = container_of(rcu_head, struct bcm_op, rcu); + +INIT_WORK (&op->work, bcm_free_op_work); +queue_work (bcm_wq, &op->work); +} +static void bcm_remove_op (struct bcm_op *op) {hrtimer_cancel (&op->timer);
@@ -1060,6 +1081,7 @@ static int bcm_tx_setup(struct bcm_msg_h/* bcm_can_tx / bcm_tx_timeout_handler needs this */ op->sk = sk; +sock_hold (sk); op->ifindex = ifindex;/* initialize uninitialized (kzalloc) structure */
@@ -1221,6 +1243,7 @@ static int bcm_rx_setup(struct bcm_msg_h/* bcm_can_tx / bcm_tx_timeout_handler needs this */ op->sk = sk; +sock_hold (sk); op->ifindex = ifindex;/* ifindex for timeout events w/o previous frame reception */
@@ -1839,11 +1862,15 @@ static int __init bcm_module_init(void) {int err; + bcm_wq =alloc_workqueue ("can-bcm-wq" ,WQ_UNBOUND ,0 ); +if (!bcm_wq) +return -ENOMEM ; +pr_info ("can: broadcast manager protocol\n" ); err =register_pernet_subsys (&canbcm_pernet_ops);if (err) -return err; + goto register_pernet_failed; err =register_netdevice_notifier (&canbcm_notifier);if (err)
@@ -1861,6 +1888,8 @@ register_proto_failed:unregister_netdevice_notifier (&canbcm_notifier); register_notifier_failed:unregister_pernet_subsys (&canbcm_pernet_ops); +register_pernet_failed: +destroy_workqueue (bcm_wq);return err; }
@@ -1869,6 +1898,8 @@ static void __exit bcm_module_exit(void)can_proto_unregister (&bcm_can_proto);unregister_netdevice_notifier (&canbcm_notifier);unregister_pernet_subsys (&canbcm_pernet_ops); +rcu_barrier (); +destroy_workqueue (bcm_wq); }module_init (bcm_module_init);
Greg Kroah-Hartman
maintainerPATCH 1824/2077 can: bcm: fix lockless bound/ifindex race and silent RX_SETUP failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit d9b091d9d22fee81ec53fb55d2032951993ceadb upstream.
bcm_sendmsg() reads bo->ifindex and checks bo->bound before taking lock_sock(), while bcm_notify(), bcm_connect() and bcm_release() all mutate both fields under that same lock. Because the lockless reads and the locked writes are unordered with respect to each other, a racing bcm_notify() (device unregister) or bcm_connect() (concurrent bind on another thread sharing the socket) can make bcm_sendmsg() observe an inconsistent combination, e.g. a stale bound=1 together with the now-cleared ifindex=0, silently turning a socket bound to a specific CAN interface into one that also matches "any" interface.
Keep the lockless bo->bound check purely as a fast-path reject, and move the ifindex read (and a bo->bound re-check) into the locked section, where every writer already serializes. This removes the possibility of observing the two fields torn against each other, rather than trying to fix it with more READ_ONCE()/WRITE_ONCE() pairs on two independently updated fields. Annotate the now-purely-lockless bo->bound accesses consistently across all its write sites.
Also fix bcm_rx_setup() silently returning success when the target device disappears concurrently instead of reporting -ENODEV, so a broken RX op is no longer left registered as if it had succeeded.
--- net/can/bcm.c | 65 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 14 deletions(-)
net/can/bcm.c +51 -14
@@ -1323,6 +1323,11 @@ static int bcm_rx_setup(struct bcm_msg_h op->rx_reg_dev = dev;dev_put (dev); + }else { +/* the requested device is gone - do not +* silently succeed without registering +*/ + err = -ENODEV ; } }else
@@ -1396,12 +1401,13 @@ static int bcm_sendmsg(struct socket *so {struct sock *sk = sock->sk;struct bcm_sock *bo =bcm_sk (sk); -int ifindex = bo->ifindex;/* default ifindex for this bcm_op */ +int ifindex;struct bcm_msg_head msg_head;int cfsiz;int ret;/* read bytes or error codes as return value */ -if (!bo->bound) +/* Lockless fast-path check for bound socket */ +if (!READ_ONCE (bo->bound))return -ENOTCONN ;/* check for valid message length from userspace */
@@ -1417,17 +1423,38 @@ static int bcm_sendmsg(struct socket *soif ((size -MHSIZ ) % cfsiz)return -EINVAL ; +lock_sock (sk); + +/* Re-validate under the socket lock: a concurrent bcm_notify() +* may have unbound this socket (device removal) after the +* lockless fast-path check above. bo->ifindex is only ever +* mutated under lock_sock(), so reading it here - instead of +* before taking the lock - guarantees it can't be observed +* torn against bo->bound. +*/ +if (!bo->bound) { + ret = -ENOTCONN ; + goto out_release; + } + +/* default ifindex for this bcm_op */ + ifindex = bo->ifindex; +/* check for alternative ifindex for this bcm_op */ if (!ifindex && msg->msg_name) {/* no bound device as default => check msg_name */ DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); -if (msg->msg_namelen <BCM_MIN_NAMELEN ) -return -EINVAL ; +if (msg->msg_namelen <BCM_MIN_NAMELEN ) { + ret = -EINVAL ; + goto out_release; + } -if (addr->can_family !=AF_CAN ) -return -EINVAL ; +if (addr->can_family !=AF_CAN ) { + ret = -EINVAL ; + goto out_release; + }/* ifindex from sendto() */ ifindex = addr->can_ifindex;
@@ -1436,20 +1463,21 @@ static int bcm_sendmsg(struct socket *sostruct net_device *dev; dev =dev_get_by_index (sock_net (sk), ifindex); -if (!dev) -return -ENODEV ; +if (!dev) { + ret = -ENODEV ; + goto out_release; + }if (dev->type !=ARPHRD_CAN ) {dev_put (dev); -return -ENODEV ; + ret = -ENODEV ; + goto out_release; }dev_put (dev); } } -lock_sock (sk); -switch (msg_head.opcode) {case TX_SETUP :
@@ -1499,6 +1527,7 @@ static int bcm_sendmsg(struct socket *sobreak ; } +out_release:release_sock (sk);return ret;
@@ -1535,7 +1564,12 @@ static void bcm_notify(struct bcm_sock * bo->bcm_proc_read = NULL; }#endif - bo->bound =0 ; +/* Paired with the lockless fast-path check in +* bcm_sendmsg(); bo->ifindex itself is only ever +* accessed under lock_sock() so it needs no +* annotation. +*/ +WRITE_ONCE (bo->bound,0 ); bo->ifindex =0 ; notify_enodev =1 ; }
@@ -1676,7 +1710,7 @@ static int bcm_release(struct socket *so/* remove device reference */ if (bo->bound) { - bo->bound =0 ; +WRITE_ONCE (bo->bound,0 ); bo->ifindex =0 ; }
@@ -1746,7 +1780,10 @@ static int bcm_connect(struct socket *so }#endif /* CONFIG_PROC_FS */ - bo->bound =1 ; +/* bo->ifindex above is fully assigned before this point; pairs +* with the lockless fast-path check in bcm_sendmsg() +*/ +WRITE_ONCE (bo->bound,1 ); fail:release_sock (sk);
Greg Kroah-Hartman
maintainerPATCH 1825/2077 can: bcm: add locking when updating filter and timer values
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 749179c2e25b95d22499ed29096b3e02d6dfd2b4 upstream.
KCSAN detected a simultaneous access to timer values that can be overwritten in bcm_rx_setup() when updating timer and filter content while bcm_rx_handler(), bcm_rx_timeout_handler() or bcm_rx_thr_handler() run concurrently on incoming CAN traffic.
Protect the timer (ival1/ival2/kt_ival1/kt_ival2/kt_lastmsg) and filter (nframes/flags/frames/last_frames) updates in bcm_rx_setup() with a new per-op bcm_rx_update_lock, taken with the matching scope in the RX handlers. memcpy_from_msg() is staged into a temporary buffer before the lock is taken, since it can sleep and must not run under a spinlock.
hrtimer_cancel() is always called without bcm_rx_update_lock held, since bcm_rx_timeout_handler()/bcm_rx_thr_handler() take the same lock and a running callback would otherwise deadlock against the canceller.
Also close a related race: bcm_rx_setup() cleared the RTR flag in the stored reply frame's can_id as a separate, unprotected step after the frame content was already installed, so a concurrent bcm_rx_handler() could transmit a stale reply with CAN_RTR_FLAG still set. Fold that normalization into the initial frame preparation instead (on the staged buffer for updates, directly on op->frames pre-registration for new ops), so the installed frame is always atomically self-consistent.
bcm_rx_handler()'s RX_RTR_FRAME check now takes a lock-protected snapshot of op->flags before deciding whether to call bcm_can_tx(), but does not hold the lock across that call.
Also take a lock-protected snapshot of the currframe in bcm_can_tx() to avoid partly overwrites by content updates in bcm_tx_setup(). Finally check if a TX_RESET_MULTI_IDX/SETTIMER might have reset op->currframe between the two locked sections in bcm_can_tx().
Omit calling hrtimer_forward() with zero interval in bcm_rx_thr_handler(). kt_ival2 may have been concurrently cleared by bcm_rx_setup() before it cancels this timer, so check kt_ival2 inside the bcm_rx_update_lock.
--- net/can/bcm.c | 176 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 133 insertions(+), 43 deletions(-)
net/can/bcm.c +133 -43
@@ -129,6 +129,7 @@ struct bcm_op {struct sock *sk;struct net_device *rx_reg_dev;spinlock_t bcm_tx_lock;/* protect currframe/count in runtime updates */ +spinlock_t bcm_rx_update_lock;/* protect filter/timer data updates */ };struct bcm_sock {
@@ -293,22 +294,28 @@ static int bcm_proc_show(struct seq_file* bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface * of the given bcm tx op */ -static void bcm_can_tx (struct bcm_op *op) +static void bcm_can_tx (struct bcm_op *op,struct canfd_frame *cf) {struct sk_buff *skb;struct can_skb_ext *csx;struct net_device *dev; -struct canfd_frame *cf; +struct canfd_frame cframe; +bool cyclic = !cf; +unsigned int idx =0 ;int err;/* no target device? => exit */ if (!op->ifindex)return ; -/* read currframe under lock protection */ -spin_lock_bh (&op->bcm_tx_lock); - cf = op->frames + op->cfsiz * op->currframe; -spin_unlock_bh (&op->bcm_tx_lock); +if (cyclic) { +/* read currframe under lock protection */ +spin_lock_bh (&op->bcm_tx_lock); + idx = op->currframe; +memcpy (&cframe, op->frames + op->cfsiz * idx, op->cfsiz); + cf = &cframe; +spin_unlock_bh (&op->bcm_tx_lock); + } dev =dev_get_by_index (sock_net (op->sk), op->ifindex);if (!dev) {
@@ -341,14 +348,20 @@ static void bcm_can_tx(struct bcm_op *opif (!err) op->frames_abs++; - op->currframe++; +/* only advance the cyclic sequence if nothing reset currframe while +* we were sending - a concurrent TX_RESET_MULTI_IDX means this +* frame's bookkeeping belongs to a sequence that no longer exists +*/ +if (!cyclic || op->currframe == idx) { + op->currframe++; -/* reached last frame? */ -if (op->currframe >= op->nframes) - op->currframe =0 ; +/* reached last frame? */ +if (op->currframe >= op->nframes) + op->currframe =0 ; -if (op->count >0 ) - op->count--; +if (op->count >0 ) + op->count--; + }spin_unlock_bh (&op->bcm_tx_lock); out:
@@ -461,7 +474,7 @@ static enum hrtimer_restart bcm_tx_timeostruct bcm_msg_head msg_head;if (op->kt_ival1 && (op->count >0 )) { -bcm_can_tx (op); +bcm_can_tx (op,NULL );if (!op->count && (op->flags &TX_COUNTEVT )) {/* create notification to user */
@@ -478,7 +491,7 @@ static enum hrtimer_restart bcm_tx_timeo } }else if (op->kt_ival2) { -bcm_can_tx (op); +bcm_can_tx (op,NULL ); } return bcm_tx_set_expiry(op, &op->timer) ?
@@ -622,6 +635,8 @@ static enum hrtimer_restart bcm_rx_timeo struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);struct bcm_msg_head msg_head; +spin_lock_bh (&op->bcm_rx_update_lock); +/* if user wants to be informed, when cyclic CAN-Messages come back */ if ((op->flags &RX_ANNOUNCE_RESUME ) && op->last_frames) {/* clear received CAN frames to indicate 'nothing received' */
@@ -638,6 +653,8 @@ static enum hrtimer_restart bcm_rx_timeo msg_head.can_id = op->can_id; msg_head.nframes =0 ; +spin_unlock_bh (&op->bcm_rx_update_lock); +bcm_send_to_user (op, &msg_head,NULL ,0 );return HRTIMER_NORESTART ;
@@ -686,15 +703,26 @@ static int bcm_rx_thr_flush(struct bcm_ostatic enum hrtimer_restart bcm_rx_thr_handler (struct hrtimer *hrtimer) { struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer); +enum hrtimer_restart ret; -if (bcm_rx_thr_flush (op)) { +spin_lock_bh (&op->bcm_rx_update_lock); + +/* kt_ival2 may have been concurrently cleared by bcm_rx_setup() +* before it cancels this timer - never forward with a zero +* interval in that case. +*/ +if (bcm_rx_thr_flush (op) && op->kt_ival2) {hrtimer_forward_now (hrtimer, op->kt_ival2); -return HRTIMER_RESTART ; + ret =HRTIMER_RESTART ; }else {/* rearm throttle handling */ op->kt_lastmsg =0 ; -return HRTIMER_NORESTART ; + ret =HRTIMER_NORESTART ; } + +spin_unlock_bh (&op->bcm_rx_update_lock); + +return ret; }/*
@@ -704,8 +732,10 @@ static void bcm_rx_handler(struct sk_buf {struct bcm_op *op = (struct bcm_op *)data;const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data; +struct canfd_frame rtrframe;unsigned int i;unsigned char traffic_flags; +bool rtr_frame;if (op->can_id != rxframe->can_id)return ;
@@ -729,9 +759,18 @@ static void bcm_rx_handler(struct sk_buf/* update statistics */ op->frames_abs++; -if (op->flags &RX_RTR_FRAME ) { +/* snapshot the flag under lock: op->flags/op->frames may be updated +* concurrently by bcm_rx_setup(). +*/ +spin_lock_bh (&op->bcm_rx_update_lock); + rtr_frame = op->flags &RX_RTR_FRAME ; +if (rtr_frame) +memcpy (&rtrframe, op->frames, op->cfsiz); +spin_unlock_bh (&op->bcm_rx_update_lock); + +if (rtr_frame) {/* send reply for RTR-request (placed in op->frames[0]) */ -bcm_can_tx (op); +bcm_can_tx (op, &rtrframe);return ; }
@@ -743,6 +782,8 @@ static void bcm_rx_handler(struct sk_buf traffic_flags |=RX_OWN ; } +spin_lock_bh (&op->bcm_rx_update_lock); +if (op->flags &RX_FILTER_ID ) {/* the easiest case */ bcm_rx_update_and_send(op, op->last_frames, rxframe,
@@ -778,6 +819,8 @@ static void bcm_rx_handler(struct sk_buf rx_starttimer:bcm_rx_starttimer (op); + +spin_unlock_bh (&op->bcm_rx_update_lock); }/*
@@ -1116,7 +1159,7 @@ static int bcm_tx_setup(struct bcm_msg_h }if (op->flags &TX_ANNOUNCE ) -bcm_can_tx (op); +bcm_can_tx (op,NULL );if (op->flags &STARTTIMER )bcm_tx_start_timer (op);
@@ -1130,6 +1173,24 @@ free_op:return err; } +static void bcm_rx_setup_rtr_check (struct bcm_msg_head *msg_head, +struct bcm_op *op,void *new_frames) +{ +/* funny feature in RX(!)_SETUP only for RTR-mode: +* copy can_id into frame BUT without RTR-flag to +* prevent a full-load-loopback-test ... ;-] +* normalize this on the staged buffer, before it is +* ever installed into op->frames. +*/ +if (msg_head->flags &RX_RTR_FRAME ) { +struct canfd_frame *frame0 = new_frames; + +if ((msg_head->flags &TX_CP_CAN_ID ) || + frame0->can_id == op->can_id) + frame0->can_id = op->can_id & ~CAN_RTR_FLAG ; + } +} +/* * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg) */
@@ -1164,6 +1225,8 @@ static int bcm_rx_setup(struct bcm_msg_h/* check the given can_id */ op =bcm_find_op (&bo->rx_ops, msg_head, ifindex);if (op) { +void *new_frames =NULL ; +/* update existing BCM operation */ /*
@@ -1175,19 +1238,48 @@ static int bcm_rx_setup(struct bcm_msg_hreturn -E2BIG ;if (msg_head->nframes) { -/* update CAN frames content */ - err =memcpy_from_msg (op->frames, msg, +/* get new CAN frames content before locking */ + new_frames =kmalloc (msg_head->nframes * op->cfsiz, +GFP_KERNEL ); +if (!new_frames) +return -ENOMEM ; + + err =memcpy_from_msg (new_frames, msg, msg_head->nframes * op->cfsiz); -if (err <0 ) +if (err <0 ) { +kfree (new_frames);return err; + } -/* clear last_frames to indicate 'nothing received' */ -memset (op->last_frames,0 , msg_head->nframes * op->cfsiz); +bcm_rx_setup_rtr_check (msg_head, op, new_frames); } +spin_lock_bh (&op->bcm_rx_update_lock); op->nframes = msg_head->nframes; op->flags = msg_head->flags; +if (msg_head->nframes) { +/* update CAN frames content */ +memcpy (op->frames, new_frames, + msg_head->nframes * op->cfsiz); + +/* clear last_frames to indicate 'nothing received' */ +memset (op->last_frames,0 , + msg_head->nframes * op->cfsiz); + } + +if (msg_head->flags &SETTIMER ) { + op->ival1 = msg_head->ival1; + op->ival2 = msg_head->ival2; + op->kt_ival1 =bcm_timeval_to_ktime (msg_head->ival1); + op->kt_ival2 =bcm_timeval_to_ktime (msg_head->ival2); + op->kt_lastmsg =0 ; + } +spin_unlock_bh (&op->bcm_rx_update_lock); + +/* free temporary frames / kfree(NULL) is safe */ +kfree (new_frames); +/* Only an update -> do not call can_rx_register() */ do_rx_register =0 ;
@@ -1198,6 +1290,7 @@ static int bcm_rx_setup(struct bcm_msg_hreturn -ENOMEM ;spin_lock_init (&op->bcm_tx_lock); +spin_lock_init (&op->bcm_rx_update_lock); op->can_id = msg_head->can_id; op->nframes = msg_head->nframes; op->cfsiz =CFSIZ (msg_head->flags);
@@ -1239,6 +1332,8 @@ static int bcm_rx_setup(struct bcm_msg_hkfree (op);return err; } + +bcm_rx_setup_rtr_check (msg_head, op, op->frames); }/* bcm_can_tx / bcm_tx_timeout_handler needs this */
@@ -1266,29 +1361,22 @@ static int bcm_rx_setup(struct bcm_msg_h/* check flags */ if (op->flags &RX_RTR_FRAME ) { -struct canfd_frame *frame0 = op->frames; -/* no timers in RTR-mode */ hrtimer_cancel (&op->thrtimer);hrtimer_cancel (&op->timer); - -/* -* funny feature in RX(!)_SETUP only for RTR-mode: -* copy can_id into frame BUT without RTR-flag to -* prevent a full-load-loopback-test ... ;-] -*/ -if ((op->flags &TX_CP_CAN_ID ) || - (frame0->can_id == op->can_id)) - frame0->can_id = op->can_id & ~CAN_RTR_FLAG ; - }else {if (op->flags &SETTIMER ) { -/* set timer value */ - op->ival1 = msg_head->ival1; - op->ival2 = msg_head->ival2; - op->kt_ival1 =bcm_timeval_to_ktime (msg_head->ival1); - op->kt_ival2 =bcm_timeval_to_ktime (msg_head->ival2); +/* set timers (locked) for newly created op */ +if (do_rx_register) { +spin_lock_bh (&op->bcm_rx_update_lock); + op->ival1 = msg_head->ival1; + op->ival2 = msg_head->ival2; + op->kt_ival1 =bcm_timeval_to_ktime (msg_head->ival1); + op->kt_ival2 =bcm_timeval_to_ktime (msg_head->ival2); + op->kt_lastmsg =0 ; +spin_unlock_bh (&op->bcm_rx_update_lock); + }/* disable an active timer due to zero value? */ if (!op->kt_ival1)
@@ -1298,9 +1386,11 @@ static int bcm_rx_setup(struct bcm_msg_h* In any case cancel the throttle timer, flush * potentially blocked msgs and reset throttle handling */ - op->kt_lastmsg =0 ;hrtimer_cancel (&op->thrtimer); + +spin_lock_bh (&op->bcm_rx_update_lock);bcm_rx_thr_flush (op); +spin_unlock_bh (&op->bcm_rx_update_lock); }if ((op->flags &STARTTIMER ) && op->kt_ival1)
Greg Kroah-Hartman
maintainerPATCH 1826/2077 can: bcm: add missing rcu list annotations and operations
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 7b2c3eabc4dafc062a25e10711154f2107526a78 upstream.
sashiko-bot remarked the missing use of list_add_rcu() in bcm_[rx|tx]_setup() to have a proper initialized bcm_op structure when bcm_proc_show() traverses the bcm_op's under rcu_read_lock().
To cover all initial settings of the bcm_op's the list_add_rcu() calls are moved to the end of the setup code.
While at it, also fix the mirroring removal side: bcm_release() called bcm_remove_op() - which frees the op via call_rcu() - on ops that were still linked in bo->tx_ops/bo->rx_ops, without list_del_rcu() first. Unlink each op with list_del_rcu() before handing it to bcm_remove_op(), matching the existing pattern in bcm_delete_tx_op()/bcm_delete_rx_op().
--- net/can/bcm.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
net/can/bcm.c +16 -9
@@ -262,7 +262,7 @@ static int bcm_proc_show(struct seq_file (reduction == 100) ? "near " : "", reduction); } -list_for_each_entry (op, &bo->tx_ops, list) { +list_for_each_entry_rcu (op, &bo->tx_ops, list) {seq_printf (m,"tx_op: %03X %s " , op->can_id,bcm_proc_getifname (net, ifname, op->ifindex));
@@ -994,6 +994,7 @@ static int bcm_tx_setup(struct bcm_msg_hstruct bcm_sock *bo =bcm_sk (sk);struct bcm_op *op;struct canfd_frame *cf; +bool add_op_to_list = false;unsigned int i;int err;
@@ -1135,8 +1136,7 @@ static int bcm_tx_setup(struct bcm_msg_hhrtimer_setup (&op->thrtimer, hrtimer_dummy_timeout,CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); -/* add this bcm_op to the list of the tx_ops */ -list_add (&op->list, &bo->tx_ops); + add_op_to_list = true; }/* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
@@ -1158,6 +1158,10 @@ static int bcm_tx_setup(struct bcm_msg_h op->flags |=TX_ANNOUNCE ; } +/* add this bcm_op to the list of the tx_ops? */ +if (add_op_to_list) +list_add_rcu (&op->list, &bo->tx_ops); +if (op->flags &TX_ANNOUNCE )bcm_can_tx (op,NULL );
@@ -1350,9 +1354,6 @@ static int bcm_rx_setup(struct bcm_msg_hhrtimer_setup (&op->thrtimer, bcm_rx_thr_handler,CLOCK_MONOTONIC ,HRTIMER_MODE_REL_SOFT ); -/* add this bcm_op to the list of the rx_ops */ -list_add (&op->list, &bo->rx_ops); -/* call can_rx_register() */ do_rx_register =1 ;
@@ -1426,10 +1427,12 @@ static int bcm_rx_setup(struct bcm_msg_h bcm_rx_handler, op, "bcm", sk);if (err) {/* this bcm rx op is broken -> remove it */ -list_del_rcu (&op->list);bcm_remove_op (op);return err; } + +/* add this bcm_op to the list of the rx_ops */ +list_add_rcu (&op->list, &bo->rx_ops); }return msg_head->nframes * op->cfsiz +MHSIZ ;
@@ -1763,8 +1766,10 @@ static int bcm_release(struct socket *soremove_proc_entry (bo->procname, net->can.bcmproc_dir);#endif /* CONFIG_PROC_FS */ -list_for_each_entry_safe (op, next, &bo->tx_ops, list) +list_for_each_entry_safe (op, next, &bo->tx_ops, list) { +list_del_rcu (&op->list);bcm_remove_op (op); + }list_for_each_entry_safe (op, next, &bo->rx_ops, list) {/*
@@ -1795,8 +1800,10 @@ static int bcm_release(struct socket *sosynchronize_rcu (); -list_for_each_entry_safe (op, next, &bo->rx_ops, list) +list_for_each_entry_safe (op, next, &bo->rx_ops, list) { +list_del_rcu (&op->list);bcm_remove_op (op); + }/* remove device reference */ if (bo->bound) {
Greg Kroah-Hartman
maintainerPATCH 1827/2077 can: bcm: extend bcm_tx_lock usage for data and timer updates
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 12ce799f7ab1e05bd8fbf79e46f403bfe5597ebc upstream.
Stage new CAN frame content for an existing tx op into a kmalloc()'d buffer and validate it there, mirroring the approach already used in bcm_rx_setup(). Only copy the validated data into op->frames while holding op->bcm_tx_lock, so bcm_can_tx() and bcm_tx_timeout_handler() can no longer observe a partially updated or unvalidated frame.
Add a missing error path for memcpy_from_msg() when copying CAN frame data from userspace.
Also move the kt_ival1/kt_ival2/ival1/ival2 updates in bcm_tx_setup() under op->bcm_tx_lock, and read kt_ival1/kt_ival2/count under the same lock in bcm_tx_set_expiry() and bcm_tx_timeout_handler(), closing the torn 64-bit ktime_t read on 32-bit platforms.
--- net/can/bcm.c | 104 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 29 deletions(-)
net/can/bcm.c +75 -29
@@ -128,7 +128,7 @@ struct bcm_op {struct canfd_frame last_sframe;struct sock *sk;struct net_device *rx_reg_dev; -spinlock_t bcm_tx_lock;/* protect currframe/count in runtime updates */ +spinlock_t bcm_tx_lock;/* protect tx data and timer updates */ spinlock_t bcm_rx_update_lock;/* protect filter/timer data updates */ };
@@ -450,12 +450,18 @@ static bool bcm_tx_set_expiry(struct bcm {ktime_t ival; +spin_lock_bh (&op->bcm_tx_lock); +if (op->kt_ival1 && op->count) ival = op->kt_ival1; -else if (op->kt_ival2) +else if (op->kt_ival2) { ival = op->kt_ival2; -else + }else { +spin_unlock_bh (&op->bcm_tx_lock);return false; + } + +spin_unlock_bh (&op->bcm_tx_lock);hrtimer_set_expires (hrt,ktime_add (ktime_get (), ival));return true;
@@ -472,25 +478,47 @@ static enum hrtimer_restart bcm_tx_timeo { struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);struct bcm_msg_head msg_head; +bool tx_ival1, tx_ival2; + +/* snapshot kt_ival1/kt_ival2/count under lock to avoid torn +* ktime_t reads racing with concurrent bcm_tx_setup() updates +*/ +spin_lock_bh (&op->bcm_tx_lock); + tx_ival1 = op->kt_ival1 && (op->count >0 ); + tx_ival2 = !!op->kt_ival2; +spin_unlock_bh (&op->bcm_tx_lock); + +if (tx_ival1) { +u32 flags, count; +struct bcm_timeval ival1, ival2; -if (op->kt_ival1 && (op->count >0 )) {bcm_can_tx (op,NULL ); -if (!op->count && (op->flags &TX_COUNTEVT )) { +/* snapshot variables under lock to avoid torn reads racing +* with concurrent bcm_tx_setup() updates +*/ +spin_lock_bh (&op->bcm_tx_lock); + flags = op->flags; + count = op->count; + ival1 = op->ival1; + ival2 = op->ival2; +spin_unlock_bh (&op->bcm_tx_lock); + +if (!count && (flags &TX_COUNTEVT )) {/* create notification to user */ memset (&msg_head,0 ,sizeof (msg_head)); msg_head.opcode =TX_EXPIRED ; - msg_head.flags = op->flags; - msg_head.count = op->count; - msg_head.ival1 = op->ival1; - msg_head.ival2 = op->ival2; + msg_head.flags = flags; + msg_head.count = count; + msg_head.ival1 = ival1; + msg_head.ival2 = ival2; msg_head.can_id = op->can_id; msg_head.nframes =0 ;bcm_send_to_user (op, &msg_head,NULL ,0 ); } - }else if (op->kt_ival2) { + }else if (tx_ival2) {bcm_can_tx (op,NULL ); }
@@ -1013,6 +1041,8 @@ static int bcm_tx_setup(struct bcm_msg_h/* check the given can_id */ op =bcm_find_op (&bo->tx_ops, msg_head, ifindex);if (op) { +void *new_frames; +/* update existing BCM operation */ /*
@@ -1023,11 +1053,23 @@ static int bcm_tx_setup(struct bcm_msg_hif (msg_head->nframes > op->nframes)return -E2BIG ; -/* update CAN frames content */ +/* get new CAN frames content into a staging buffer before +* locking: validate and normalize the frames there so that +* bcm_can_tx() / bcm_tx_timeout_handler() never observe a +* partially updated or unvalidated frame in op->frames +*/ + new_frames =kmalloc (msg_head->nframes * op->cfsiz,GFP_KERNEL ); +if (!new_frames) +return -ENOMEM ; +for (i =0 ; i < msg_head->nframes; i++) { - cf = op->frames + op->cfsiz * i; + cf = new_frames + op->cfsiz * i; err =memcpy_from_msg ((u8 *)cf, msg, op->cfsiz); +if (err <0 ) { +kfree (new_frames); +return err; + }if (op->flags &CAN_FD_FRAME ) {if (cf->len >64 )
@@ -1037,36 +1079,38 @@ static int bcm_tx_setup(struct bcm_msg_h err = -EINVAL ; } -if (err <0 ) +if (err <0 ) { +kfree (new_frames);return err; + }if (msg_head->flags &TX_CP_CAN_ID ) {/* copy can_id into frame */ cf->can_id = msg_head->can_id; } } + +spin_lock_bh (&op->bcm_tx_lock); + +/* update CAN frames content */ +memcpy (op->frames, new_frames, msg_head->nframes * op->cfsiz); + op->flags = msg_head->flags; -/* only lock for unlikely count/nframes/currframe changes */ if (op->nframes != msg_head->nframes || - op->flags &TX_RESET_MULTI_IDX || - op->flags &SETTIMER ) { - -spin_lock_bh (&op->bcm_tx_lock); + op->flags &TX_RESET_MULTI_IDX ) { +/* potentially update changed nframes */ + op->nframes = msg_head->nframes; +/* restart multiple frame transmission */ + op->currframe =0 ; + } -if (op->nframes != msg_head->nframes || - op->flags &TX_RESET_MULTI_IDX ) { -/* potentially update changed nframes */ - op->nframes = msg_head->nframes; -/* restart multiple frame transmission */ - op->currframe =0 ; - } +if (op->flags &SETTIMER ) + op->count = msg_head->count; -if (op->flags &SETTIMER ) - op->count = msg_head->count; +spin_unlock_bh (&op->bcm_tx_lock); -spin_unlock_bh (&op->bcm_tx_lock); - } +kfree (new_frames); }else {/* insert new BCM operation for the given can_id */
@@ -1142,10 +1186,12 @@ static int bcm_tx_setup(struct bcm_msg_hif (op->flags &SETTIMER ) {/* set timer values */ +spin_lock_bh (&op->bcm_tx_lock); op->ival1 = msg_head->ival1; op->ival2 = msg_head->ival2; op->kt_ival1 =bcm_timeval_to_ktime (msg_head->ival1); op->kt_ival2 =bcm_timeval_to_ktime (msg_head->ival2); +spin_unlock_bh (&op->bcm_tx_lock);/* disable an active timer due to zero values? */ if (!op->kt_ival1 && !op->kt_ival2)
Greg Kroah-Hartman
maintainerPATCH 1828/2077 can: bcm: fix CAN frame rx/tx statistics
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit e6c24ba95fc3f1b5e1dcd28b1c6e59ef61a9daa5 upstream.
KCSAN detected a data race within the bcm_rx_handler() when two CAN frames have been simultaneously received and processed in a single rx op by two different CPUs.
Use atomic operations with (signed) long data types to access the statistics in the hot path to fix the KCSAN complaint.
Additionally simplify the update and check of statistics overflow by using the atomic operations in separate bcm_update_[rx|tx]_stats() functions. The rx variant runs under bcm_rx_update_lock to prevent races when resetting the two rx counters; the tx variant runs under bcm_tx_lock and only needs to guard its own counter's overflow.
As the rx path resets its values already at LONG_MAX / 100, there is no conflict between the two locking domains (bcm_rx_update_lock vs. bcm_tx_lock) even for ops that use both paths.
The rx statistics update and the frames_filtered update in bcm_rx_changed() were previously performed in two separate bcm_rx_update_lock sections. For an rx op subscribed on all interfaces (ifindex == 0), bcm_rx_handler() can run concurrently on different CPUs, so a counter reset by one CPU between these two sections could leave frames_filtered larger than frames_abs on another CPU, producing a bogus (even negative) reduction percentage in procfs. Update the statistics in the same critical section as bcm_rx_changed() to close this gap, which also removes the now unneeded extra lock/unlock pair around the traffic_flags calculation.
--- net/can/bcm.c | 69 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 23 deletions(-)
net/can/bcm.c +46 -23
@@ -112,7 +112,7 @@ struct bcm_op {int ifindex;canid_t can_id;u32 flags; -unsigned long frames_abs, frames_filtered; +atomic_long_t frames_abs, frames_filtered;struct bcm_timeval ival1, ival2;struct hrtimer timer, thrtimer;ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
@@ -229,10 +229,13 @@ static int bcm_proc_show(struct seq_filelist_for_each_entry_rcu (op, &bo->rx_ops, list) { -unsigned long reduction; +long reduction, frames_filtered, frames_abs; + + frames_filtered =atomic_long_read (&op->frames_filtered); + frames_abs =atomic_long_read (&op->frames_abs);/* print only active entries & prevent division by zero */ -if (!op->frames_abs) +if (!frames_abs)continue ; seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
@@ -254,9 +257,9 @@ static int bcm_proc_show(struct seq_file (long long)ktime_to_us(op->kt_ival2));seq_printf (m,"# recv %ld (%ld) => reduction: " , - op->frames_filtered, op->frames_abs); + frames_filtered, frames_abs); - reduction =100 - (op->frames_filtered *100 ) / op->frames_abs; + reduction =100 - (frames_filtered *100 ) / frames_abs;seq_printf (m,"%s%ld%%\n" , (reduction ==100 ) ?"near " :"" , reduction);
@@ -280,7 +283,8 @@ static int bcm_proc_show(struct seq_fileseq_printf (m,"t2=%lld " , (long long )ktime_to_us (op->kt_ival2)); -seq_printf (m,"# sent %ld\n" , op->frames_abs); +seq_printf (m,"# sent %ld\n" , +atomic_long_read (&op->frames_abs)); }seq_putc (m,'\n' );
@@ -290,6 +294,24 @@ static int bcm_proc_show(struct seq_file }#endif /* CONFIG_PROC_FS */ +static void bcm_update_rx_stats (struct bcm_op *op) +{ +/* prevent overflow of the reduction% calculation in bcm_proc_show() */ +if (atomic_long_inc_return (&op->frames_abs) >LONG_MAX /100 ) { +atomic_long_set (&op->frames_filtered,0 ); +atomic_long_set (&op->frames_abs,0 ); + } +} + +static void bcm_update_tx_stats (struct bcm_op *op) +{ +/* tx_op has no reduction% calculation - use the full range and +* just keep the displayed counter non-negative on overflow +*/ +if (atomic_long_inc_return (&op->frames_abs) ==LONG_MAX ) +atomic_long_set (&op->frames_abs,0 ); +} +/* * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface * of the given bcm tx op
@@ -346,7 +368,7 @@ static void bcm_can_tx(struct bcm_op *opspin_lock_bh (&op->bcm_tx_lock);if (!err) - op->frames_abs++; +bcm_update_tx_stats (op);/* only advance the cyclic sequence if nothing reset currframe while * we were sending - a concurrent TX_RESET_MULTI_IDX means this
@@ -533,12 +555,9 @@ static void bcm_rx_changed(struct bcm_op {struct bcm_msg_head head; -/* update statistics */ - op->frames_filtered++; - -/* prevent statistics overflow */ -if (op->frames_filtered >ULONG_MAX /100 ) - op->frames_filtered = op->frames_abs =0 ; +/* update statistics (frames_filtered <= frames_abs) */ +if (atomic_long_read (&op->frames_abs)) +atomic_long_inc (&op->frames_filtered);/* this element is not throttled anymore */ data->flags &= ~RX_THR ;
@@ -784,24 +803,30 @@ static void bcm_rx_handler(struct sk_buf op->rx_stamp = skb->tstamp;/* save originator for recvfrom() */ op->rx_ifindex = skb->dev->ifindex; -/* update statistics */ - op->frames_abs++; -/* snapshot the flag under lock: op->flags/op->frames may be updated -* concurrently by bcm_rx_setup(). -*/ +/* op->flags/op->frames may be updated concurrently by bcm_rx_setup() */ spin_lock_bh (&op->bcm_rx_update_lock); + rtr_frame = op->flags &RX_RTR_FRAME ; -if (rtr_frame) +if (rtr_frame) { +bcm_update_rx_stats (op); +/* snapshot RTR content under lock */ memcpy (&rtrframe, op->frames, op->cfsiz); -spin_unlock_bh (&op->bcm_rx_update_lock); +spin_unlock_bh (&op->bcm_rx_update_lock); -if (rtr_frame) {/* send reply for RTR-request (placed in op->frames[0]) */ bcm_can_tx (op, &rtrframe);return ; } +/* update statistics in the same critical section as bcm_rx_changed() +* below: frames_filtered must never be checked/incremented against a +* frames_abs snapshot from a concurrent bcm_rx_handler() call on +* another CPU for the same (wildcard) op, or frames_filtered can end +* up larger than frames_abs. +*/ +bcm_update_rx_stats (op); +/* compute flags to distinguish between own/local/remote CAN traffic */ traffic_flags =0 ;if (skb->sk) {
@@ -810,8 +835,6 @@ static void bcm_rx_handler(struct sk_buf traffic_flags |=RX_OWN ; } -spin_lock_bh (&op->bcm_rx_update_lock); -if (op->flags &RX_FILTER_ID ) {/* the easiest case */ bcm_rx_update_and_send(op, op->last_frames, rxframe,
Greg Kroah-Hartman
maintainerPATCH 1829/2077 can: bcm: fix data race on rx_stamp/rx_ifindex in bcm_rx_handler()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 58fd6cbc8541216af1d7ed272ea7ac2b66d50fd8 upstream.
For an rx op subscribed on all interfaces (ifindex == 0), the same op is registered once in the shared per-netns wildcard filter list, so bcm_rx_handler() can run concurrently on different CPUs for frames arriving on different net devices.
op->rx_stamp and op->rx_ifindex were written before bcm_rx_update_lock was taken, allowing concurrent writers to race each other - including a torn store of the 64-bit rx_stamp on 32-bit platforms.
Beyond a torn store bcm_send_to_user() must report the timestamp/ifindex of the very same frame whose content it is delivering. So the assignment is placed in the same unbroken bcm_rx_update_lock section as the content comparison.
As a side effect, the RTR-request frame feature (which never reach bcm_send_to_user()) no longer updates rx_stamp/rx_ifindex, since only the notification path needs them.
--- net/can/bcm.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
net/can/bcm.c +8 -5
@@ -799,11 +799,6 @@ static void bcm_rx_handler(struct sk_buf/* disable timeout */ hrtimer_cancel (&op->timer); -/* save rx timestamp */ - op->rx_stamp = skb->tstamp; -/* save originator for recvfrom() */ - op->rx_ifindex = skb->dev->ifindex; -/* op->flags/op->frames may be updated concurrently by bcm_rx_setup() */ spin_lock_bh (&op->bcm_rx_update_lock);
@@ -835,6 +830,14 @@ static void bcm_rx_handler(struct sk_buf traffic_flags |=RX_OWN ; } +/* save rx timestamp and originator for recvfrom() under lock. +* For an op subscribed on all interfaces (ifindex == 0) +* bcm_rx_handler() can run concurrently on different CPUs so +* the CAN content and the meta data must be bundled correctly. +*/ + op->rx_stamp = skb->tstamp; + op->rx_ifindex = skb->dev->ifindex; +if (op->flags &RX_FILTER_ID ) {/* the easiest case */ bcm_rx_update_and_send(op, op->last_frames, rxframe,
Greg Kroah-Hartman
maintainerPATCH 1830/2077 can: bcm: fix stale rx/tx ops after device removal
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 3b762c0d950383ab7a002686c9136b9aa55d2d70 upstream.
RX: an RX_SETUP update(!) for an existing op skipped can_rx_register() unconditionally, even when a concurrent NETDEV_UNREGISTER had already torn down its registration (op->rx_reg_dev == NULL). This silently did not re-enable frame delivery for that updated filter. bcm_rx_setup() now re-registers in that case, while leaving rx_ops with ifindex = 0 (all CAN devices) which never carry a tracked rx_reg_dev registered as-is.
TX: bcm_notify() only handled bo->rx_ops on NETDEV_UNREGISTER, leaving tx_ops with an active cyclic transmission re-arming its hrtimer indefinitely to execute bcm_tx_timeout_handler(). Cancelling the hrtimer prevents the runaway timer and any injection into a later reused ifindex, since nothing else calls bcm_can_tx() for the op until an explicit TX_SETUP update re-arms it.
Unlike bcm_rx_unreg(), which clears the tracked rx_reg_dev for rx_ops, the ifindex is intentionally left unchanged for tx_ops. bcm_tx_setup() always rejects ifindex 0, so clearing it would strand the op: neither a later TX_SETUP (bcm_find_op()) nor TX_DELETE (bcm_delete_tx_op()) could ever find it again, since both require an exact ifindex match.
--- net/can/bcm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-)
net/can/bcm.c +44 -10
@@ -1276,6 +1276,7 @@ static int bcm_rx_setup(struct bcm_msg_hstruct bcm_sock *bo =bcm_sk (sk);struct bcm_op *op;int do_rx_register; +int new_op =0 ;int err =0 ;if ((msg_head->flags &RX_FILTER_ID ) || (!(msg_head->nframes))) {
@@ -1356,8 +1357,15 @@ static int bcm_rx_setup(struct bcm_msg_h/* free temporary frames / kfree(NULL) is safe */ kfree (new_frames); -/* Only an update -> do not call can_rx_register() */ - do_rx_register =0 ; +/* Don't register a new CAN filter for the rx_op update unless +* a concurrent NETDEV_UNREGISTER notifier already tore down +* the previous registration. In this case the receiver needs +* to be re-registered here so that this update doesn't +* silently stop delivering frames for the given ifindex. +* Ops with ifindex = 0 (all CAN interfaces) never carry a +* tracked rx_reg_dev and stay registered as-is. +*/ + do_rx_register = (ifindex && !op->rx_reg_dev) ?1 :0 ; }else {/* insert new BCM operation for the given can_id */
@@ -1428,6 +1436,7 @@ static int bcm_rx_setup(struct bcm_msg_h/* call can_rx_register() */ do_rx_register =1 ; + new_op =1 ; }/* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
@@ -1441,7 +1450,7 @@ static int bcm_rx_setup(struct bcm_msg_hif (op->flags &SETTIMER ) {/* set timers (locked) for newly created op */ -if (do_rx_register) { +if (new_op) {spin_lock_bh (&op->bcm_rx_update_lock); op->ival1 = msg_head->ival1; op->ival2 = msg_head->ival2;
@@ -1471,7 +1480,10 @@ static int bcm_rx_setup(struct bcm_msg_h HRTIMER_MODE_REL_SOFT); } -/* now we can register for can_ids, if we added a new bcm_op */ +/* now we can register for can_ids, if we added a new bcm_op +* or need to re-register after a NETDEV_UNREGISTER tore down +* the previous registration of an existing op +*/ if (do_rx_register) {if (ifindex) {struct net_device *dev;
@@ -1493,18 +1505,32 @@ static int bcm_rx_setup(struct bcm_msg_h err = -ENODEV ; } - }else + }else { err =can_rx_register (sock_net (sk),NULL , op->can_id,REGMASK (op->can_id), bcm_rx_handler, op,"bcm" , sk); + } +if (err) { -/* this bcm rx op is broken -> remove it */ -bcm_remove_op (op); +/* newly created bcm rx op is broken -> remove it */ +if (new_op) { +bcm_remove_op (op); +return err; + } + +/* an existing op just stays unregistered. +* Cancel op->timer and (defensively) op->thrtimer. +* Other settings can't be reached until the next +* successful RX_SETUP. +*/ +hrtimer_cancel (&op->timer); +hrtimer_cancel (&op->thrtimer);return err; } -/* add this bcm_op to the list of the rx_ops */ -list_add_rcu (&op->list, &bo->rx_ops); +/* add a new bcm_op to the list of the rx_ops */ +if (new_op) +list_add_rcu (&op->list, &bo->rx_ops); }return msg_head->nframes * op->cfsiz +MHSIZ ;
@@ -1716,11 +1742,19 @@ static void bcm_notify(struct bcm_sock *case NETDEV_UNREGISTER :lock_sock (sk); -/* remove device specific receive entries */ +/* rx_ops: remove device specific receive entries */ list_for_each_entry (op, &bo->rx_ops, list)if (op->rx_reg_dev == dev)bcm_rx_unreg (dev, op); +/* tx_ops: stop device specific cyclic transmissions on the +* vanishing ifindex. Cancelling the timer is enough to stop +* cyclic bcm_can_tx() calls as there is no re-arming. +*/ +list_for_each_entry (op, &bo->tx_ops, list) +if (op->ifindex == dev->ifindex) +hrtimer_cancel (&op->timer); +/* remove device reference, if this is our bound device */ if (bo->bound && bo->ifindex == dev->ifindex) {#if IS_ENABLED (CONFIG_PROC_FS )
Greg Kroah-Hartman
maintainerPATCH 1831/2077 can: bcm: track a single source interface for ANYDEV timeout/throttle ops
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 2f5976f54a04e9f18b25283036ac3136be453b17 upstream.
An ANYDEV rx op (ifindex == 0) with an active RX timeout and/or throttle timer has no defined semantics when matching frames arrive from several interfaces: bcm_rx_handler() can run concurrently for the same op on different CPUs, racing hrtimer_cancel()/ bcm_rx_starttimer() against bcm_rx_timeout_handler() and causing spurious RX_TIMEOUT notifications and last_frames corruption. The same concurrency lets throttled multiplex frames from different interfaces clobber the single rx_ifindex/rx_stamp fields shared by the op.
Add op->if_detected to track the first interface that delivers a matching frame while a timeout/throttle timer is configured, and reject frames from any other interface for that op. The claim is decided in bcm_rx_handler() before hrtimer_cancel() touches op->timer, so a rejected frame can never disturb the claimed interface's watchdog. RTR-mode ops are excluded via RX_RTR_FRAME, independent of kt_ival1/kt_ival2, since those may briefly hold a stale value from an earlier non-RTR configuration.
The claim is released in bcm_notify() on NETDEV_UNREGISTER and in bcm_rx_setup() when SETTIMER reconfigures the timer values.
A (re-)claim is only possible on CAN devices in NETREG_REGISTERED dev->reg_state to cover the release in bcm_notify() where reg_state becomes NETREG_UNREGISTERING until synchronize_net().
--- net/can/bcm.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-)
net/can/bcm.c +44 -5
@@ -117,6 +117,7 @@ struct bcm_op {struct hrtimer timer, thrtimer;ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;int rx_ifindex; +int if_detected;/* first received ifindex in ANYDEV rx_op mode */ int cfsiz;u32 count;u32 nframes;
@@ -796,6 +797,33 @@ static void bcm_rx_handler(struct sk_bufreturn ; } +/* An ANYDEV op with an active RX timeout and/or throttle timer +* tracks a single source interface: claim the first interface that +* delivers a matching frame and reject frames from any other one, +* before hrtimer_cancel() below can touch op->timer - this avoids +* racing bcm_rx_timeout_handler() across concurrent interfaces. +* RX_RTR_FRAME ops are excluded, as kt_ival1/kt_ival2 may briefly +* hold a stale value from an earlier non-RTR configuration. +*/ +if (!op->ifindex) { +spin_lock_bh (&op->bcm_rx_update_lock); + +if (!(op->flags &RX_RTR_FRAME ) && + (op->kt_ival1 || op->kt_ival2)) { +/* don't claim to vanishing interface */ +if (!op->if_detected && +READ_ONCE (skb->dev->reg_state) ==NETREG_REGISTERED ) + op->if_detected = skb->dev->ifindex; + +if (op->if_detected != skb->dev->ifindex) { +spin_unlock_bh (&op->bcm_rx_update_lock); +return ; + } + } + +spin_unlock_bh (&op->bcm_rx_update_lock); + } +/* disable timeout */ hrtimer_cancel (&op->timer);
@@ -830,10 +858,9 @@ static void bcm_rx_handler(struct sk_buf traffic_flags |=RX_OWN ; } -/* save rx timestamp and originator for recvfrom() under lock. -* For an op subscribed on all interfaces (ifindex == 0) -* bcm_rx_handler() can run concurrently on different CPUs so -* the CAN content and the meta data must be bundled correctly. +/* save rx timestamp and originator for recvfrom() under lock: an +* ANYDEV op without an active timer can still run concurrently on +* different CPUs, so content and meta data must be bundled here. */ op->rx_stamp = skb->tstamp; op->rx_ifindex = skb->dev->ifindex;
@@ -1351,6 +1378,7 @@ static int bcm_rx_setup(struct bcm_msg_h op->kt_ival1 =bcm_timeval_to_ktime (msg_head->ival1); op->kt_ival2 =bcm_timeval_to_ktime (msg_head->ival2); op->kt_lastmsg =0 ; + op->if_detected =0 ;/* reclaim ifindex in ANYDEV mode */ }spin_unlock_bh (&op->bcm_rx_update_lock);
@@ -1743,10 +1771,21 @@ static void bcm_notify(struct bcm_sock *lock_sock (sk);/* rx_ops: remove device specific receive entries */ -list_for_each_entry (op, &bo->rx_ops, list) +list_for_each_entry (op, &bo->rx_ops, list) {if (op->rx_reg_dev == dev)bcm_rx_unreg (dev, op); +/* release an ANYDEV op's claim (see bcm_rx_handler()) +* on this now confirmed-gone interface. +*/ +if (!op->ifindex) { +spin_lock_bh (&op->bcm_rx_update_lock); +if (op->if_detected == dev->ifindex) + op->if_detected =0 ; +spin_unlock_bh (&op->bcm_rx_update_lock); + } + } +/* tx_ops: stop device specific cyclic transmissions on the * vanishing ifindex. Cancelling the timer is enough to stop * cyclic bcm_can_tx() calls as there is no re-arming.
Greg Kroah-Hartman
maintainerPATCH 1832/2077 can: bcm: validate frame length in bcm_rx_setup() for RTR replies
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit 62ec41f364648be79d54d94d0d240ee326948afd upstream.
bcm_tx_setup() validates cf->len against the CAN/CAN FD DLC limits before installing frames for TX_SETUP, but bcm_rx_setup() never did the same for the RTR-reply frame configured via RX_SETUP with RX_RTR_FRAME.
--- net/can/bcm.c | 59 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 18 deletions(-)
net/can/bcm.c +41 -18
@@ -1276,22 +1276,37 @@ free_op:return err; } -static void bcm_rx_setup_rtr_check (struct bcm_msg_head *msg_head, -struct bcm_op *op,void *new_frames) +static int bcm_rx_setup_rtr_check (struct bcm_msg_head *msg_head, +struct bcm_op *op,void *new_frames) { +struct canfd_frame *frame0 = new_frames; + +if (!(msg_head->flags &RX_RTR_FRAME )) +return 0 ; + +/* this frame is sent out as-is by bcm_can_tx() whenever a matching +* remote request is received, so validate its length the same way +* bcm_tx_setup() validates TX_SETUP frames before installing it +*/ +if (msg_head->flags &CAN_FD_FRAME ) { +if (frame0->len >64 ) +return -EINVAL ; + }else { +if (frame0->len >8 ) +return -EINVAL ; + } +/* funny feature in RX(!)_SETUP only for RTR-mode: * copy can_id into frame BUT without RTR-flag to * prevent a full-load-loopback-test ... ;-] * normalize this on the staged buffer, before it is * ever installed into op->frames. */ -if (msg_head->flags &RX_RTR_FRAME ) { -struct canfd_frame *frame0 = new_frames; +if ((msg_head->flags &TX_CP_CAN_ID ) || + frame0->can_id == op->can_id) + frame0->can_id = op->can_id & ~CAN_RTR_FLAG ; -if ((msg_head->flags &TX_CP_CAN_ID ) || - frame0->can_id == op->can_id) - frame0->can_id = op->can_id & ~CAN_RTR_FLAG ; - } +return 0 ; }/*
@@ -1355,7 +1370,11 @@ static int bcm_rx_setup(struct bcm_msg_hreturn err; } -bcm_rx_setup_rtr_check (msg_head, op, new_frames); + err =bcm_rx_setup_rtr_check (msg_head, op, new_frames); +if (err <0 ) { +kfree (new_frames); +return err; + } }spin_lock_bh (&op->bcm_rx_update_lock);
@@ -1436,16 +1455,12 @@ static int bcm_rx_setup(struct bcm_msg_hif (msg_head->nframes) { err =memcpy_from_msg (op->frames, msg, msg_head->nframes * op->cfsiz); -if (err <0 ) { -if (op->frames != &op->sframe) -kfree (op->frames); -if (op->last_frames != &op->last_sframe) -kfree (op->last_frames); -kfree (op); -return err; - } +if (err <0 ) + goto free_op; -bcm_rx_setup_rtr_check (msg_head, op, op->frames); + err =bcm_rx_setup_rtr_check (msg_head, op, op->frames); +if (err <0 ) + goto free_op; }/* bcm_can_tx / bcm_tx_timeout_handler needs this */
@@ -1562,6 +1577,14 @@ static int bcm_rx_setup(struct bcm_msg_h }return msg_head->nframes * op->cfsiz +MHSIZ ; + +free_op: +if (op->frames != &op->sframe) +kfree (op->frames); +if (op->last_frames != &op->last_sframe) +kfree (op->last_frames); +kfree (op); +return err; }/*
Greg Kroah-Hartman
maintainerPATCH 1833/2077 can: bcm: add missing device refcount for CAN filter removal
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Oliver Hartkopp <[email protected]>
commit d59948293ea34b6337ce2b5febab8510de70048c upstream.
sashiko-bot remarked a problem with a concurrent device unregistration in isotp.c which also is present in the bcm.c code. A former fix for raw.c commit c275a176e4b6 ("can: raw: add missing refcount for memory leak fix") introduced a netdevice_tracker which solves the issue for bcm.c too.
bcm_release(), bcm_delete_rx_op() and bcm_notifier() relied on dev_get_by_index(ifindex) to re-find the device for an rx_op before unregistering its filter. If a concurrent NETDEV_UNREGISTER has already unlisted the device from the ifindex table, that lookup fails and can_rx_unregister() is silently skipped, leaving a stale CAN filter pointing at the soon-to-be-freed bcm_op/socket.
Hold a netdev_hold()/netdev_put() tracked reference on op->rx_reg_dev from the moment the rx filter is registered in bcm_rx_setup() until it is unregistered in bcm_rx_unreg(), and use that reference directly in bcm_release() and bcm_delete_rx_op() instead of re-looking the device up by ifindex.
--- net/can/bcm.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-)
net/can/bcm.c +27 -20
@@ -129,6 +129,7 @@ struct bcm_op {struct canfd_frame last_sframe;struct sock *sk;struct net_device *rx_reg_dev; +netdevice_tracker rx_reg_dev_tracker;spinlock_t bcm_tx_lock;/* protect tx data and timer updates */ spinlock_t bcm_rx_update_lock;/* protect filter/timer data updates */ };
@@ -967,6 +968,7 @@ static void bcm_rx_unreg(struct net_devi/* mark as removed subscription */ op->rx_reg_dev =NULL ; +netdev_put (dev, &op->rx_reg_dev_tracker); }else printk (KERN_ERR "can-bcm: bcm_rx_unreg: registered device " "mismatch %p %p\n" , op->rx_reg_dev, dev);
@@ -997,17 +999,14 @@ static int bcm_delete_rx_op(struct list_* Only remove subscriptions that had not * been removed due to NETDEV_UNREGISTER * in bcm_notifier() +* +* op->rx_reg_dev is a tracked reference taken +* when the subscription was registered, so it +* stays valid here even if a concurrent +* NETDEV_UNREGISTER already unlisted the dev. */ -if (op->rx_reg_dev) { -struct net_device *dev; - - dev =dev_get_by_index (sock_net (op->sk), - op->ifindex); -if (dev) { -bcm_rx_unreg (dev, op); -dev_put (dev); - } - } +if (op->rx_reg_dev) +bcm_rx_unreg (op->rx_reg_dev, op); }else can_rx_unregister(sock_net(op->sk), NULL, op->can_id,
@@ -1539,7 +1538,17 @@ static int bcm_rx_setup(struct bcm_msg_h bcm_rx_handler, op, "bcm", sk); - op->rx_reg_dev = dev; +/* keep a tracked reference so that a later +* unregister can safely reach the device even +* if a concurrent NETDEV_UNREGISTER has +* already unlisted it by ifindex +*/ +if (!err) { + op->rx_reg_dev = dev; +netdev_hold (dev, + &op->rx_reg_dev_tracker, +GFP_KERNEL ); + }dev_put (dev); }else {/* the requested device is gone - do not
@@ -1949,16 +1958,14 @@ static int bcm_release(struct socket *so* Only remove subscriptions that had not * been removed due to NETDEV_UNREGISTER * in bcm_notifier() +* +* op->rx_reg_dev is a tracked reference taken +* when the subscription was registered, so it +* stays valid here even if a concurrent +* NETDEV_UNREGISTER already unlisted the device. */ -if (op->rx_reg_dev) { -struct net_device *dev; - - dev =dev_get_by_index (net, op->ifindex); -if (dev) { -bcm_rx_unreg (dev, op); -dev_put (dev); - } - } +if (op->rx_reg_dev) +bcm_rx_unreg (op->rx_reg_dev, op); }else can_rx_unregister(net, NULL, op->can_id, REGMASK(op->can_id),
Greg Kroah-Hartman
maintainerPATCH 1834/2077 io_uring/bpf-ops: reject re-registration of an already-bound ops
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Woraphat Khiaodaeng <[email protected]>
commit 3afc64c61ce906a04f073ca350b46de10e8302f9 upstream.
io_install_bpf() only rejects a second registration on the ctx side (ctx->bpf_ops) and sets the per-map back-pointer ops->priv unconditionally. The struct_ops link path never advances a map past BPF_STRUCT_OPS_STATE_READY, so the same io_uring_bpf_ops map can be registered more than once, and bpf_io_reg() re-resolves the target ring via fget(ops->ring_fd) on every call. A caller can therefore point the same ring_fd at a different io_ring_ctx between two BPF_LINK_CREATE calls.
The second registration passes the ctx->bpf_ops check (the new ctx has none) and overwrites ops->priv, orphaning the first ctx. Teardown (io_eject_bpf()/bpf_io_unreg()) only reaches a ctx through ops->priv, so the orphaned ctx is never torn down: its ctx->loop_step keeps pointing into the struct_ops trampoline, which is freed once the map is gone. A later io_uring_enter() on the orphaned ring then calls the dangling ctx->loop_step from io_run_loop() -- a use-after-free of freed executable memory, reachable by a task with CAP_BPF + CAP_PERFMON.
Reject registration when ops->priv is already set, as hid_bpf_reg() does for its struct_ops.
--- io_uring/bpf-ops.c | 2 ++ 1 file changed, 2 insertions(+)
io_uring/bpf-ops.c +2 -0
@@ -165,6 +165,8 @@ static int io_install_bpf(struct io_ringif (ctx->bpf_ops)return -EBUSY ; +if (ops->priv) +return -EBUSY ;if (WARN_ON_ONCE (!ops->loop_step))return -EINVAL ;
Greg Kroah-Hartman
maintainerPATCH 1835/2077 bpf: Reset register bounds before narrowing retval range in check_mem_access()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Tristan Madani <[email protected]>
commit 5e0b273e0a62cc04ec338c7b502797c66c2ed42a upstream.
When the BPF verifier processes a context load of an LSM hook return value, it calls __mark_reg_s32_range() to narrow the register to the hook's valid range. However, __mark_reg_s32_range() intersects the new range with the register's existing bounds using max_t()/min_t() rather than replacing them.
If the destination register carries stale bounds from a prior instruction (e.g. BPF_MOV64_IMM), the intersection can produce a range narrower than reality. The verifier then believes it knows the register's exact value, while at runtime the actual hook return value is loaded, creating a verifier/runtime mismatch that can be used to bypass BPF memory safety checks.
The else branch already calls mark_reg_unknown() to reset register state before any narrowing. Apply the same reset in the is_retval path so stale bounds are cleared before __mark_reg_s32_range() intersects.
--- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
@@ -6454,6 +6454,7 @@ static int check_mem_access(struct bpf_v*/ if (info.reg_type ==SCALAR_VALUE ) {if (info.is_retval &&get_func_retval_range (env->prog, &range)) { +mark_reg_unknown (env, regs, value_regno); err =__mark_reg_s32_range (env, regs, value_regno, range.minval, range.maxval);if (err)
Greg Kroah-Hartman
maintainerPATCH 1836/2077 bpf,fork: wipe ->bpf_storage before bailouts that access it
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Jann Horn <[email protected]>
commit 9b51a6155d14389876916726430da30eabb1d4ed upstream.
Currently, copy_process() can bail out to free_task() before p->bpf_storage has been initialized, with this call graph (shown here for the !CONFIG_MEMCG case):
copy_process
dup_task_struct
arch_dup_task_struct
[copies the entire task_struct, including ->bpf_storage member]
[RLIMIT_NPROC check fails]
delayed_free_task
free_task
bpf_task_storage_free
rcu_dereference(task->bpf_storage)
bpf_local_storage_destroyIn this case, the nascent task's ->bpf_storage member that bpf_local_storage_destroy() operates on is a plain copy of the parent's ->bpf_storage pointer, not a real initialized pointer.
This leads to badness (kernel hangs, UAF).
This is reachable as long as the process calling fork() has been inserted into a task storage map.
--- kernel/fork.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/fork.c +5 -4
@@ -1004,6 +1004,11 @@ static struct task_struct *dup_task_stru tsk->mm_cid.active =0 ;INIT_HLIST_NODE (&tsk->mm_cid.node);#endif + +#ifdef CONFIG_BPF_SYSCALL +RCU_INIT_POINTER (tsk->bpf_storage,NULL ); + tsk->bpf_ctx =NULL ; +#endif return tsk; free_stack:
@@ -2223,10 +2228,6 @@ __latent_entropy struct task_struct *cop p->sequential_io =0 ; p->sequential_io_avg =0 ;#endif -#ifdef CONFIG_BPF_SYSCALL -RCU_INIT_POINTER (p->bpf_storage,NULL ); - p->bpf_ctx =NULL ; -#endif unwind_task_init (p);
Greg Kroah-Hartman
maintainerPATCH 1837/2077 bpf: Add missing access_ok call to copy_user_syms
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Jiri Olsa <[email protected]>
commit d5dc200c3a3f217de072af269dd90adddf90e48d upstream.
As reported by sashiko we use __get_user without prior access_ok call on the user space pointer. Adding the missing call for the whole pointer array.
Plus removing the err check in the error path, because it's not needed and also we can return -ENOMEM directly from the first kvmalloc_array fail path.
Cc: [email protected]
[1] https://lore.kernel.org/bpf/[email protected]/
--- kernel/trace/bpf_trace.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/trace/bpf_trace.c +6 -5
@@ -2331,9 +2331,12 @@ static int copy_user_syms(struct user_syint err = -ENOMEM ;unsigned int i; +if (!access_ok (usyms, cnt *sizeof (*usyms))) +return -EFAULT ; + syms =kvmalloc_array (cnt,sizeof (*syms),GFP_KERNEL );if (!syms) - goto error; +return -ENOMEM ; buf =kvmalloc_array (cnt,KSYM_NAME_LEN ,GFP_KERNEL );if (!buf)
@@ -2358,10 +2361,8 @@ static int copy_user_syms(struct user_syreturn 0 ; error: -if (err) { -kvfree (syms); -kvfree (buf); - } +kvfree (syms); +kvfree (buf);return err; }
Greg Kroah-Hartman
maintainerPATCH 1838/2077 selftests/bpf: Cover negative buffer pointer offsets
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Sun Jian <[email protected]>
commit 6f59deb32efa4673fc3b1fef9f3a0da48e9d8494 upstream.
Add verifier coverage for constant negative offsets on PTR_TO_TP_BUFFER and PTR_TO_BUF pointers. Both programs adjust the buffer pointer by -8 and access it at offset zero, so the negative effective start must be rejected at load time.
Switch the raw tracepoint writable attach checks from nbd_send_request to bpf_testmod_test_writable_bare_tp, avoiding a dependency on the NBD tracepoint. Keep the existing past-end case and add a case with a negative var_off compensated by a positive instruction offset. The effective start remains non-negative, so the program loads, but its access end exceeds the writable context size and bpf_raw_tracepoint_open() must return -EINVAL.
--- tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_bad_access.c | 57 ++++++++++ tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_nbd_invalid.c | 43 ------- tools/testing/selftests/bpf/prog_tests/verifier.c | 2 tools/testing/selftests/bpf/progs/verifier_ptr_to_buf.c | 27 ++++ tools/testing/selftests/bpf/progs/verifier_raw_tp_writable.c | 16 ++ 5 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_bad_access.c delete mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_nbd_invalid.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_ptr_to_buf.c
tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_bad_access.c new file +57 -0
@@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "test_kmods/bpf_testmod.h" +#include "bpf_util.h" + +static void check_attach_reject (const struct bpf_insn *program,size_t prog_len) +{ +LIBBPF_OPTS (bpf_prog_load_opts, opts); +char error[4096 ]; +int bpf_fd, tp_fd; + + opts.log_level =2 ; + opts.log_buf = error; + opts.log_size =sizeof (error); + + bpf_fd =bpf_prog_load (BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ,NULL ,"GPL v2" , + program, prog_len, &opts); +if (!ASSERT_GE (bpf_fd,0 ,"prog_load" )) +return ; + + tp_fd =bpf_raw_tracepoint_open ("bpf_testmod_test_writable_bare_tp" , bpf_fd); +ASSERT_EQ (tp_fd, -EINVAL ,"bpf_raw_tracepoint_open" ); +if (tp_fd >=0 ) +close (tp_fd); + +close (bpf_fd); +} + +void test_raw_tp_writable_reject_bad_access (void ) +{ +const struct bpf_insn program[] = { +/* r6 is our tp buffer */ +BPF_LDX_MEM (BPF_DW ,BPF_REG_6 ,BPF_REG_1 ,0 ), +/* one byte beyond the end of the writable context */ +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_6 , +sizeof (struct bpf_testmod_test_writable_ctx )), +BPF_EXIT_INSN (), + }; + +const struct bpf_insn negative_var_off_program[] = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_6 ,BPF_REG_1 ,0 ), +/* make var_off negative, but keep the effective access offset non-negative */ +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_6 ,-8 ), +/* one byte beyond the end of the writable context */ +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_6 , +sizeof (struct bpf_testmod_test_writable_ctx ) +8 ), +BPF_EXIT_INSN (), + }; + +if (test__start_subtest ("past_end" )) +check_attach_reject (program,ARRAY_SIZE (program)); + +if (test__start_subtest ("negative_var_off_past_end" )) +check_attach_reject (negative_var_off_program, +ARRAY_SIZE (negative_var_off_program)); +}
tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_nbd_invalid.c deleted +0 -43
43 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -77,6 +77,7 @@#include "verifier_precision.skel.h" #include "verifier_prevent_map_lookup.skel.h" #include "verifier_private_stack.skel.h" +#include "verifier_ptr_to_buf.skel.h" #include "verifier_raw_stack.skel.h" #include "verifier_raw_tp_writable.skel.h" #include "verifier_reg_equal.skel.h"
@@ -225,6 +226,7 @@ void test_verifier_or_jmp32_k(void)void test_verifier_precision (void ) {RUN (verifier_precision); }void test_verifier_prevent_map_lookup (void ) {RUN (verifier_prevent_map_lookup); }void test_verifier_private_stack (void ) {RUN (verifier_private_stack); } +void test_verifier_ptr_to_buf (void ) {RUN (verifier_ptr_to_buf); }void test_verifier_raw_stack (void ) {RUN (verifier_raw_stack); }void test_verifier_raw_tp_writable (void ) {RUN (verifier_raw_tp_writable); }void test_verifier_reg_equal (void ) {RUN (verifier_reg_equal); }
tools/testing/selftests/bpf/progs/verifier_ptr_to_buf.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" + +SEC("iter/bpf_map_elem") +__description("PTR_TO_BUF: reject negative const offset") +__failure +__msg("invalid negative rdwr buffer offset") +__naked void ptr_to_buf_reject_negative_const_offset(void) +{ + asmvolatile ("r0 = 0; \ +r2 = *(u64 *)(r1 + %[value_off]); \ +if r2 == 0 goto l0_%=; \ +r2 += -8; \ +r0 = *(u64 *)(r2 + 0); \ +l0_%=: \ +exit; \ +" + : + : __imm_const(value_off, + offsetof(struct bpf_iter__bpf_map_elem, value)) + : __clobber_all); +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_raw_tp_writable.c +16 -0
@@ -47,4 +47,20 @@ l0_%=: /* shift the buffer pointer to a : __clobber_all); } +SEC("raw_tracepoint.w") +__description("raw_tracepoint_writable: reject negative const offset") +__failure +__msg("invalid negative tracepoint buffer offset") +__naked void tracepoint_writable_reject_negative_const_offset(void) +{ + asmvolatile (" \ +r6 = *(u64 *)(r1 + 0); \ +r6 += -8; \ +r0 = *(u64 *)(r6 + 0); \ +exit; \ +" : + : + : __clobber_all); +} + char _license[] SEC("license") = "GPL";
Greg Kroah-Hartman
maintainerPATCH 1839/2077 block: remove redundant GD_NEED_PART_SCAN in add_disk_final()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Connor Williamson <[email protected]>
commit 181bb9c9eae4f69fe510a62a42c2932d0314a800 upstream.
add_disk_final() sets GD_NEED_PART_SCAN before calling bdev_add(), then calls disk_scan_partitions() which sets the flag itself. The early set is redundant and introduces a race.
Between bdev_add() and disk_scan_partitions(), concurrent openers (multipathd, blkid, LVM) see the flag in blkdev_get_whole() and trigger bdev_disk_changed(). When disk_scan_partitions() then runs, it calls bdev_disk_changed() again, dropping the partitions the concurrent opener already created before re-adding them, which can result in transient partition disappearances.
The race is observable by inserting an msleep() between bdev_add() and disk_scan_partitions() while running concurrent open() calls during device bind. Without artificial delay, it manifests under scheduling pressure during boot on systems with aggressive device scanners (multipathd, systemd-udevd).
Therefore, do not set GD_NEED_PART_SCAN in add_disk_final(). Other GD_NEED_PART_SCAN consumers (blkdev_get_whole(), sd_need_revalidate()) should not be affected as the flag is set internally by disk_scan_partitions().
The retry-on-next-open intention from commit e5cfefa97bcc ("block: fix scan partition for exclusively open device again") should also not be affected as the early return paths in disk_scan_partitions() should be unreachable at device registration time (bd_holder is NULL and open_partitions is zero).
--- block/genhd.c | 4 ---- 1 file changed, 4 deletions(-)
block/genhd.c +0 -4
@@ -407,10 +407,6 @@ static void add_disk_final(struct gendisstruct device *ddev =disk_to_dev (disk);if (!(disk->flags &GENHD_FL_HIDDEN )) { -/* Make sure the first partition scan will be proceed */ -if (get_capacity (disk) &&disk_has_partscan (disk)) -set_bit (GD_NEED_PART_SCAN , &disk->state); -bdev_add (disk->part0, ddev->devt);if (get_capacity (disk))disk_scan_partitions (disk,BLK_OPEN_READ );
Greg Kroah-Hartman
maintainerPATCH 1840/2077 block: fix race in blk_time_get_ns() returning 0
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mike Waychison <[email protected]>
commit 691b052139c94ee6640ac39e0b764dd3867897c0 upstream.
blk_time_get_ns() populates the per-plug cached timestamp and then returns it by re-reading the field:
if (!plug->cur_ktime) {
plug->cur_ktime = ktime_get_ns();
current->flags |= PF_BLOCK_TS;
}
return plug->cur_ktime;This is problematic when the compiler emits the final "return plug->cur_ktime" as a reload from memory, after PF_BLOCK_TS has already been set.
Since the cached timestamp is now invalidated from finish_task_switch() (fad156c2af22 "block: invalidate cached plug timestamp after task switch"), a task preempted between setting PF_BLOCK_TS and that reload has plug->cur_ktime zeroed by blk_plug_invalidate_ts() when it is scheduled back in. The reload then returns 0.
A 0 handed back here is stored as a start timestamp -- e.g. blk_account_io_start() writes it to rq->start_time_ns -- and later subtracted from "now". blk_account_io_done() then adds (now - 0), i.e. roughly the system uptime, to the per-group nsecs[] counters. On an otherwise idle, healthy device this appears as sudden ~uptime-sized jumps in the diskstats time fields (write_ticks/discard_ticks/time_in_queue).
The solution is to be explicit in our reads and writes to this field that is preemption volatile. We also add a barrier() to ensure that any setting of PF_BLOCK_TS is ordered to happen after the cur_ktime update.
This issue was discovered using AI-assisted kprobes looking for paths that were leaking zeroed timestamps in a live system, based on the observation that we were sometimes seeing uptime-sized jumps in kernel exported counters. This was flagged by NodeDiskIOSaturation prometheus alerts that started firing on all hosts post 7.1.3 kernel upgrade, due to node-exporter now exporting a nonsensical node_disk_io_time_weighted_seconds_total.
--- block/blk.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
block/blk.h +10 -3
@@ -689,6 +689,7 @@ static inline int req_ref_read(struct restatic inline u64 blk_time_get_ns (void ) {struct blk_plug *plug = current->plug; +u64 now;if (!plug || !in_task ())return ktime_get_ns ();
@@ -697,12 +698,18 @@ static inline u64 blk_time_get_ns(void)* 0 could very well be a valid time, but rather than flag "this is * a valid timestamp" separately, just accept that we'll do an extra * ktime_get_ns() if we just happen to get 0 as the current time. +* +* cur_ktime can be zeroed by pre-emption the moment PF_BLOCK_TS is set. */ -if (!plug->cur_ktime) { - plug->cur_ktime =ktime_get_ns (); + now =READ_ONCE (plug->cur_ktime); +if (!now) { + now =ktime_get_ns (); +WRITE_ONCE (plug->cur_ktime, now); +/* Ensure PF_BLOCK_TS is set after cur_ktime. */ +barrier (); current->flags |=PF_BLOCK_TS ; } -return plug->cur_ktime; +return now; }static inline ktime_t blk_time_get (void )
Greg Kroah-Hartman
maintainerPATCH 1841/2077 block: fix IORING_URING_CMD_REISSUE flags check in blkdev_uring_cmd
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Yitang Yang <[email protected]>
commit 4f919141be38ea2b1314e3a531b7b998eb64e8bc upstream.
blkdev_uring_cmd() checks IORING_URING_CMD_REISSUE to determine whether this is the first issue. However, this flag lives in cmd->flags instead of issue_flags.
Coincidentally, IO_URING_F_NONBLOCK shares bit 31 with IORING_URING_CMD_REISSUE. As a result, the SQE read was never performed, bic->len remained zero, and every BLOCK_URING_CMD_DISCARD failed with -EINVAL.
Fix it by checking cmd->flags as intended.
--- block/ioctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/ioctl.c +1 -1
@@ -951,7 +951,7 @@ int blkdev_uring_cmd(struct io_uring_cmdu32 cmd_op = cmd->cmd_op;/* Read what we need from the SQE on the first issue */ -if (!(issue_flags &IORING_URING_CMD_REISSUE )) { +if (!(cmd->flags &IORING_URING_CMD_REISSUE )) {const struct io_uring_sqe *sqe = cmd->sqe; if (unlikely(sqe->ioprio || sqe->__pad1 || sqe->len ||
Greg Kroah-Hartman
maintainerPATCH 1842/2077 net: sparx5: unregister blocking notifier on init failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Haoxiang Li <[email protected]>
commit 483be61b4a9a6df3b7cb277e8f189e082dee4cb8 upstream.
sparx5_register_notifier_blocks() registers the switchdev blocking notifier before allocating the ordered workqueue. If the workqueue allocation fails, the error path unregisters the switchdev and netdevice notifiers, but leaves the blocking notifier registered.
Add a separate error label for the workqueue allocation failure path and unregister the switchdev blocking notifier there.
--- drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c +3 -1
@@ -765,11 +765,13 @@ int sparx5_register_notifier_blocks(stru sparx5_owq =alloc_ordered_workqueue ("sparx5_order" ,0 );if (!sparx5_owq) { err = -ENOMEM ; - goto err_switchdev_blocking_nb; + goto err_alloc_workqueue; }return 0 ; +err_alloc_workqueue: +unregister_switchdev_blocking_notifier (&s5->switchdev_blocking_nb); err_switchdev_blocking_nb:unregister_switchdev_notifier (&s5->switchdev_nb); err_switchdev_nb:
Greg Kroah-Hartman
maintainerPATCH 1843/2077 dm thin metadata: fix superblock refcount leak on snapshot shadow failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Genjian Zhang <[email protected]>
commit 4b22d0801fadfcae2e106e6ba32e49439c7c7ebf upstream.
__reserve_metadata_snap() increments THIN_SUPERBLOCK_LOCATION in the metadata space map before shadowing it. When dm_tm_shadow_block() fails, a reference is leaked in the metadata space map.
Fix by adding the missing dm_sm_dec_block().
--- drivers/md/dm-thin-metadata.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm-thin-metadata.c +3 -1
@@ -1353,8 +1353,10 @@ static int __reserve_metadata_snap(strucdm_sm_inc_block (pmd->metadata_sm,THIN_SUPERBLOCK_LOCATION ); r =dm_tm_shadow_block (pmd->tm,THIN_SUPERBLOCK_LOCATION , &sb_validator, ©, &inc); -if (r) +if (r) { +dm_sm_dec_block (pmd->metadata_sm,THIN_SUPERBLOCK_LOCATION );return r; + }BUG_ON (!inc);
Greg Kroah-Hartman
maintainerPATCH 1844/2077 dm thin metadata: fix metadata snapshot consistency on commit failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Ming-Hung Tsai <[email protected]>
commit 5bcd4d3058ebaf46ad2e163829d87dd4870c7a45 upstream.
__reserve_metadata_snap() and __release_metadata_snap() modify the superblock's held_root directly in the block_manager's buffer. If the subsequent metadata commit fails, the held_root gets flushed to disk through the abort_transaction path, resulting in inconsistent metadata.
Reproducer 1: __reserve_metadata_snap()
1. Create a 2 MiB metadata device and make the region after the 14th block inaccessible, to trigger metadata commit failure in the subsequent reserve_metadata_snap operation. The 14th block will be the shadow destination for the index block.
dmsetup create tmeta --table "0 112 linear /dev/sdc 0 112 3984 error"
2. Create a 16 MiB thin-pool
dmsetup create tdata --table "0 32768 zero"
dd if=/dev/zero of=/dev/mapper/tmeta bs=4k count=1 dmsetup create tpool --table "0 32768 thin-pool /dev/mapper/tmeta \/dev/mapper/tdata 128 0 1 skip_block_zeroing"
3. Take a metadata snapshot to trigger metadata commit failure and transaction abort. However, the held_root is written to disk, breaking metadata consistency.
dmsetup message tpool 0 "reserve_metadata_snap"
thin_check v1.2.2 result:
Bad reference count for metadata block 6. Expected 2, but space map contains 1. Bad reference count for metadata block 7. Expected 2, but space map contains 1. Bad reference count for metadata block 13. Expected 1, but space map contains 0.
Reproducer 2: __release_metadata_snap()
1. Create a 2 MiB metadata device and make the region after the 16th block inaccessible, to trigger metadata commit failure in the subsequent release_metadata_snap operation. The 16th block will be the shadow destination for the index block.
dmsetup create tmeta --table "0 128 linear /dev/sdc 0 128 3968 error"
2. Create a 16 MiB thin-pool
dmsetup create tdata --table "0 32768 zero"
dd if=/dev/zero of=/dev/mapper/tmeta bs=4k count=1 dmsetup create tpool --table "0 32768 thin-pool /dev/mapper/tmeta \/dev/mapper/tdata 128 0 1 skip_block_zeroing"
3. Reserve then release the metadata snapshot, to trigger metadata commit failure and transaction abort. The held_root gets removed from the on-disk superblock, causing inconsistent metadata.
dmsetup message tpool 0 "reserve_metadata_snap"
dmsetup message tpool 0 "release_metadata_snap"
thin_check v1.2.2 result:
Bad reference count for metadata block 6. Expected 1, but space map contains 2. Bad reference count for metadata block 7. Expected 1, but space map contains 2. 1 metadata blocks have leaked.
Fix by deferring the held_root update to commit time.
Additionally, move the existing-snapshot check in __reserve_metadata_snap before the shadow operation to avoid unnecessary work. In __release_metadata_snap, clear pmd->held_root before btree deletion so partial failure leaks blocks rather than leaving a stale reference, and unlock the snapshot block before decrementing its refcount.
--- drivers/md/dm-thin-metadata.c | 63 ++++++++++++------------------------------ 1 file changed, 18 insertions(+), 45 deletions(-)
drivers/md/dm-thin-metadata.c +18 -45
@@ -186,6 +186,7 @@ struct dm_pool_metadata {uint32_t time;dm_block_t root;dm_block_t details_root; +dm_block_t held_root;struct list_head thin_devices;uint64_t trans_id;unsigned long flags;
@@ -748,6 +749,7 @@ static int __open_metadata(struct dm_poo*/ pmd->root =le64_to_cpu (disk_super->data_mapping_root); pmd->details_root =le64_to_cpu (disk_super->device_details_root); + pmd->held_root =le64_to_cpu (disk_super->held_root);__setup_btree_details (pmd);dm_bm_unlock (sblock);
@@ -838,6 +840,7 @@ static int __begin_transaction(struct dm pmd->time =le32_to_cpu (disk_super->time); pmd->root =le64_to_cpu (disk_super->data_mapping_root); pmd->details_root =le64_to_cpu (disk_super->device_details_root); + pmd->held_root =le64_to_cpu (disk_super->held_root); pmd->trans_id =le64_to_cpu (disk_super->trans_id); pmd->flags =le32_to_cpu (disk_super->flags); pmd->data_block_size =le32_to_cpu (disk_super->data_block_size);
@@ -928,6 +931,7 @@ static int __commit_transaction(struct d disk_super->time =cpu_to_le32 (pmd->time); disk_super->data_mapping_root =cpu_to_le64 (pmd->root); disk_super->device_details_root =cpu_to_le64 (pmd->details_root); + disk_super->held_root =cpu_to_le64 (pmd->held_root); disk_super->trans_id =cpu_to_le64 (pmd->trans_id); disk_super->flags =cpu_to_le32 (pmd->flags);
@@ -1333,9 +1337,14 @@ static int __reserve_metadata_snap(struc {int r, inc;struct thin_disk_superblock *disk_super; -struct dm_block *copy, *sblock; +struct dm_block *copy;dm_block_t held_root; +if (pmd->held_root) { +DMWARN ("Pool metadata snapshot already exists: release this before taking another." ); +return -EBUSY ; + } +/* * We commit to ensure the btree roots which we increment in a * moment are up to date.
@@ -1363,14 +1372,6 @@ static int __reserve_metadata_snap(struc held_root =dm_block_location (copy); disk_super =dm_block_data (copy); -if (le64_to_cpu (disk_super->held_root)) { -DMWARN ("Pool metadata snapshot already exists: release this before taking another." ); - -dm_tm_dec (pmd->tm, held_root); -dm_tm_unlock (pmd->tm, copy); -return -EBUSY ; - } -/* * Wipe the spacemap since we're not publishing this. */
@@ -1386,18 +1387,8 @@ static int __reserve_metadata_snap(strucdm_tm_inc (pmd->tm,le64_to_cpu (disk_super->device_details_root));dm_tm_unlock (pmd->tm, copy); -/* -* Write the held root into the superblock. -*/ - r =superblock_lock (pmd, &sblock); -if (r) { -dm_tm_dec (pmd->tm, held_root); -return r; - } + pmd->held_root = held_root; - disk_super =dm_block_data (sblock); - disk_super->held_root =cpu_to_le64 (held_root); -dm_bm_unlock (sblock);return 0 ; }
@@ -1417,18 +1408,10 @@ static int __release_metadata_snap(struc {int r;struct thin_disk_superblock *disk_super; -struct dm_block *sblock, *copy; +struct dm_block *copy;dm_block_t held_root; - r =superblock_lock (pmd, &sblock); -if (r) -return r; - - disk_super =dm_block_data (sblock); - held_root =le64_to_cpu (disk_super->held_root); - disk_super->held_root =cpu_to_le64 (0 ); - -dm_bm_unlock (sblock); + held_root = pmd->held_root;if (!held_root) {DMWARN ("No pool metadata snapshot found: nothing to release." );
@@ -1439,13 +1422,15 @@ static int __release_metadata_snap(strucif (r)return r; + pmd->held_root =0 ; + disk_super =dm_block_data (copy);dm_btree_del (&pmd->info,le64_to_cpu (disk_super->data_mapping_root));dm_btree_del (&pmd->details_info,le64_to_cpu (disk_super->device_details_root)); -dm_sm_dec_block (pmd->metadata_sm, held_root); -dm_tm_unlock (pmd->tm, copy); +dm_sm_dec_block (pmd->metadata_sm, held_root); +return 0 ; }
@@ -1464,19 +1449,7 @@ int dm_pool_release_metadata_snap(structstatic int __get_metadata_snap (struct dm_pool_metadata *pmd,dm_block_t *result) { -int r; -struct thin_disk_superblock *disk_super; -struct dm_block *sblock; - - r =dm_bm_read_lock (pmd->bm,THIN_SUPERBLOCK_LOCATION , - &sb_validator, &sblock); -if (r) -return r; - - disk_super =dm_block_data (sblock); - *result =le64_to_cpu (disk_super->held_root); - -dm_bm_unlock (sblock); + *result = pmd->held_root;return 0 ; }
Greg Kroah-Hartman
maintainerPATCH 1845/2077 dm era: fix out-of-bounds memory access for non-zero start sector
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Samuel Moelius <[email protected]>
commit a868196f03c2b19418ae3d2b69e195d668a271e5 upstream.
dm-era tracks writes in target-relative blocks, but era_map() calculates the writeset block before applying the target offset. Tables with a non-zero start sector can therefore pass an absolute mapped-device block to metadata_current_marked().
If the absolute block is beyond the current writeset size, writeset_marked() tests past the end of the in-core bitset. KASAN reports this as a vmalloc-out-of-bounds access.
Apply the target offset before calculating the era block so writeset lookups use the target-relative block number.
--- drivers/md/dm-era-target.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm-era-target.c +3 -1
@@ -1231,6 +1231,7 @@ static dm_block_t get_block(struct era *static void remap_to_origin (struct era *era,struct bio *bio) {bio_set_dev (bio, era->origin_dev->bdev); + bio->bi_iter.bi_sector =dm_target_offset (era->ti, bio->bi_iter.bi_sector); }/*
@@ -1562,7 +1563,7 @@ static void era_dtr(struct dm_target *tistatic int era_map (struct dm_target *ti,struct bio *bio) {struct era *era = ti->private; -dm_block_t block =get_block (era, bio); +dm_block_t block;/* * All bios get remapped to the origin device. We do this now, but
@@ -1570,6 +1571,7 @@ static int era_map(struct dm_target *ti,* block is marked in this era. */ remap_to_origin (era, bio); + block =get_block (era, bio);/* * REQ_PREFLUSH bios carry no data, so we're not interested in them.
Greg Kroah-Hartman
maintainerPATCH 1846/2077 dm-bufio: fix wrong count calculation in dm_bufio_issue_discard
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 422f1d4f141eaa3a6e4199ceec86cc6b9bf26570 upstream.
block_to_sector converts a block number to a sector number and adds c->start to the result. It is inappropriate to use this function for converting the number of blocks to a number to sectors because c->start would be incorrectly added to the result.
Luckily, the only target that uses dm_bufio_issue_discard is dm-ebs, which sets c->start to 0, so this bug is latent.
--- drivers/md/dm-bufio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm-bufio.c +3 -1
@@ -2238,7 +2238,9 @@ int dm_bufio_issue_discard(struct dm_bufstruct dm_io_region io_reg = { .bdev = c->bdev, .sector =block_to_sector (c, block), - .count =block_to_sector (c, count), + .count =likely (c->sectors_per_block_bits >=0 ) ? + count << c->sectors_per_block_bits : + count * (c->block_size >>SECTOR_SHIFT ), };if (WARN_ON_ONCE (dm_bufio_in_request ()))
Greg Kroah-Hartman
maintainerPATCH 1847/2077 dm-ioctl: fix a possible overflow in list_version_get_info
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 76c6f845dc0c614304a6e6ee619b552f97cf24b3 upstream.
sizeof(tt->version) is 12 bytes, but the code writes 16 bytes into the output buffer - info->vers->version[0], info->vers->version[1], info->vers->version[2] and info->vers->next. This can cause buffer overflow.
Fix this buffer overflow by replacing "sizeof(tt->version)" with "sizeof(struct dm_target_versions)".
--- drivers/md/dm-ioctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-ioctl.c +1 -1
@@ -698,7 +698,7 @@ static void list_version_get_info(structstruct vers_iter *info = param;/* Check space - it might have changed since the first iteration */ -if ((char *)info->vers +sizeof (tt->version) +strlen (tt->name) +1 > info->end) { +if ((char *)info->vers +sizeof (struct dm_target_versions ) +strlen (tt->name) +1 > info->end) { info->flags =DM_BUFFER_FULL_FLAG ;return ; }
Greg Kroah-Hartman
maintainerPATCH 1848/2077 dm-log: fix a bitset_size overflow on 32bit machines
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Benjamin Marzinski <[email protected]>
commit 9743132a41f4d9d0e54c5f2adcb821b04796bab1 upstream.
Commit c20e36b7631d ("dm log: fix out-of-bounds write due to region_count overflow") made sure that region_count could fit in an unsigned int. But the bitmap memory isn't allocated based on region_count. It uses bitset_size (a size_t variable). The first step of calculating bitset_size is to set it to region_count, rounded up to a multiple of BITS_PER_LONG. If region_size is less than BITS_PER_LONG smaller than UINT_MAX, it will get rounded up to 2^32. On a 32bit architecture, this will make bitset_size wrap around to 0 and fail, despite region_count being valid.
Since bitset_size gets divided by 8, it can hold any valid region_count. It just needs a special case to handle the rollover. If it is 0, the value rolled over, and bitset size should be set to the number of bytes needed to hold 2^32 bits.
--- drivers/md/dm-log.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/md/dm-log.c +3 -0
@@ -425,6 +425,9 @@ static int create_log_context(struct dm_*/ bitset_size =dm_round_up (region_count,BITS_PER_LONG ); bitset_size >>=BYTE_SHIFT ; +/* Handle dm_round_up rollover on 32-bit systems */ +if (!bitset_size) + bitset_size =1UL << (BITS_PER_LONG -BYTE_SHIFT ); lc->bitset_uint32_count = bitset_size /sizeof (*lc->clean_bits);
Greg Kroah-Hartman
maintainerPATCH 1849/2077 dm-pcache: reject option groups without values
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Samuel Moelius <[email protected]>
commit d9c631e3fbd44246a2be781d26cfacbb9b8ec127 upstream.
The pcache target parses optional arguments as name/value pairs. A table that advertises one optional argument and supplies only a recognized option name, for example "cache_mode", reaches parse_cache_opts() with argc == 1. The parser consumes the name, decrements argc to zero, then calls dm_shift_arg() again for the value. dm_shift_arg() returns NULL when no arguments remain, and the following strcmp() dereferences that NULL pointer.
Check that each recognized option has a value before consuming it. This keeps valid "cache_mode writeback" and "data_crc true/false" tables unchanged while making malformed tables fail during target construction with a precise missing-value error.
--- drivers/md/dm-pcache/dm_pcache.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/md/dm-pcache/dm_pcache.c +8 -0
@@ -168,6 +168,10 @@ static int parse_cache_opts(struct dm_pc argc--;if (!strcmp (arg,"cache_mode" )) { +if (!argc) { + *error ="Missing value for cache_mode" ; +return -EINVAL ; + } arg =dm_shift_arg (as);if (!strcmp (arg,"writeback" )) { opts->cache_mode =PCACHE_CACHE_MODE_WRITEBACK ;
@@ -177,6 +181,10 @@ static int parse_cache_opts(struct dm_pc } argc--; }else if (!strcmp (arg,"data_crc" )) { +if (!argc) { + *error ="Missing value for data_crc" ; +return -EINVAL ; + } arg =dm_shift_arg (as);if (!strcmp (arg,"true" )) { opts->data_crc = true;
Greg Kroah-Hartman
maintainerPATCH 1850/2077 dm-stats: fix dm_jiffies_to_msec64
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 386df1a57b631c456d14f857cb0c0c2e11c16bef upstream.
There were wrong calculations in dm_jiffies_to_msec64 that produced incorrect output when HZ was different from 1000. This commit fixes them.
--- drivers/md/dm-stats.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/md/dm-stats.c +2 -2
@@ -842,10 +842,10 @@ static unsigned long long dm_jiffies_to_ result =jiffies_to_msecs (j &0x3fffff );if (j >=1 <<22 ) { mult =jiffies_to_msecs (1 <<22 ); - result += (unsigned long long )mult * (unsigned long long )jiffies_to_msecs ((j >>22 ) &0x3fffff ); + result += (unsigned long long )mult * ((j >>22 ) &0x3fffff ); }if (j >=1ULL <<44 ) - result += (unsigned long long )mult * (unsigned long long )mult * (unsigned long long )jiffies_to_msecs (j >>44 ); + result += (unsigned long long )mult * (unsigned long long )(1 <<22 ) * (j >>44 );return result; }
Greg Kroah-Hartman
maintainerPATCH 1851/2077 dm-stats: fix merge accounting
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 1917eb2db750ecbdf710f79a8042eaa545a063c7 upstream.
There were wrong parentheses when setting stats_aux->merged, so that merging was never properly accounted. This commit fixes it.
--- drivers/md/dm-stats.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/md/dm-stats.c +2 -4
@@ -692,10 +692,8 @@ void dm_stats_account_io(struct dm_stats*/ last =raw_cpu_ptr (stats->last); stats_aux->merged = - (bi_sector == (READ_ONCE (last->last_sector) && - ((bi_rw ==WRITE ) == - (READ_ONCE (last->last_rw) ==WRITE )) - )); + bi_sector ==READ_ONCE (last->last_sector) && + (bi_rw ==WRITE ) == (READ_ONCE (last->last_rw) ==WRITE );WRITE_ONCE (last->last_sector, end_sector);WRITE_ONCE (last->last_rw, bi_rw); }else
Greg Kroah-Hartman
maintainerPATCH 1852/2077 dm: avoid leaking the callers thread keyring via the table device file
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Ingo Blechschmidt <[email protected]>
commit 981ccd97f7153d310dfa92a534525bbaf46752c2 upstream.
The refactoring in commit a28d893eb327 ("md: port block device access to file") accidentally causes the caller's thread keyring to be kept alive long beyond the caller's lifetime.
As a result, "cryptsetup luksSuspend" silently fails to wipe the LUKS volume key from memory.
In detail: "cryptsetup luksOpen" uses its supposedly ephemeral thread keyring to pass the volume key to the kernel. dm-crypt's crypt_set_keyring_key() copies the key material into its own crypt_config structure and then drops its own reference to the key in the keyring with key_put().
With this fix, restoring pre-v6.9 behavior, the copy in the thread keyring is then promptly garbage collected, such that exactly one copy of the volume key remains. This single copy is correctly wiped from memory on "cryptsetup luksSuspend".
Without this fix, the thread keyring and the volume key in it remains. This second copy is only freed on "luksClose". "luksSuspend" neither knows about this copy nor has any way to remove it, so the key remains recoverable from RAM after a suspend that is documented to have wiped it.
This fix should not introduce new security problems, as the code is anyway gated by CAP_SYS_ADMIN. The device-mapper core, not the calling task, is the legitimate owner of this long-lived file.
--- drivers/md/dm.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/md/dm.c +10 -1
@@ -735,7 +735,16 @@ static struct table_device *open_table_dreturn ERR_PTR (-ENOMEM );refcount_set (&td->count,1 ); - bdev_file =bdev_file_open_by_dev (dev, mode, _dm_claim_ptr,NULL ); +/* +* Open the backing device with kernel rather than caller +* credentials. Otherwise the caller's credentials would be +* pinned in bdev_file->f_cred until the table device is closed. +* That would keep the caller's thread keyring alive long beyond the +* lifetime of the caller, breaking userspace expectation (e.g. +* cryptsetup(8) leaking the LUKS volume key). +*/ +scoped_with_kernel_creds () + bdev_file = bdev_file_open_by_dev(dev, mode, _dm_claim_ptr, NULL);if (IS_ERR (bdev_file)) { r =PTR_ERR (bdev_file); goto out_free_td;
Greg Kroah-Hartman
maintainerPATCH 1853/2077 dm_early_create: fix freeing used table on dm_resume failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 366665416f20527ff7cad548a32d1ddf23195740 upstream.
If dm_resume fails, the kernel attempts to free table with dm_table_destroy, but the table was already instantiated with dm_swap_table. This commit skips the call to dm_table_destroy in this case.
--- drivers/md/dm-ioctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-ioctl.c +1 -1
@@ -2351,7 +2351,7 @@ int __init dm_early_create(struct dm_ioc/* resume device */ r =dm_resume (md);if (r) - goto err_destroy_table; + goto err_hash_remove;DMINFO ("%s (%s) is ready" , md->disk->disk_name, dmi->name);dm_put (md);
Greg Kroah-Hartman
maintainerPATCH 1854/2077 dm-integrity: fix leaking uninitialized kernel memory
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 7bb03b2b01b814a9fc14afbfc2cbb2cca5b34750 upstream.
If hash size is less than device's tuple size, dm-integrity is supposed to zero the remaining space. There was a bug in the code that zeroing didn't work. This commit fixes it.
--- drivers/md/dm-integrity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-integrity.c +1 -1
@@ -2606,7 +2606,7 @@ skip_spinlock:struct bio_vec bv =bio_iter_iovec (bio, dio->bio_details.bi_iter);const char *mem =integrity_kmap (ic, bv.bv_page);if (ic->tag_size < ic->tuple_size) -memset (dio->integrity_payload + pos + ic->tag_size,0 , ic->tuple_size - ic->tuple_size); +memset (dio->integrity_payload + pos + ic->tag_size,0 , ic->tuple_size - ic->tag_size);integrity_sector_checksum (ic, &dio->ahash_req, dio->bio_details.bi_iter.bi_sector, mem, bv.bv_offset, dio->integrity_payload + pos);integrity_kunmap (ic, mem); pos += ic->tuple_size;
Greg Kroah-Hartman
maintainerPATCH 1855/2077 dm-integrity: fix a bug if the bio is out of limits
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 5a266764fadaff8b5c1fe37a186ebf9b09cb953e upstream.
If dm_integrity_check_limits fails, the code would exit with DM_MAPIO_KILL. However, the range would be already locked at this point, and it wouldn't be unlocked, resulting in a deadlock. Let's move the limit check up, so that when it exits, no resources are leaked.
--- drivers/md/dm-integrity.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/md/dm-integrity.c +3 -4
@@ -2523,6 +2523,9 @@ static int dm_integrity_map_inline(strucif (unlikely ((bio->bi_opf &REQ_PREFLUSH ) !=0 ))return DM_MAPIO_REMAPPED ; +if (unlikely (!dm_integrity_check_limits (ic, bio->bi_iter.bi_sector, bio))) +return DM_MAPIO_KILL ; + retry:if (!dio->integrity_payload) {unsigned digest_size, extra_size;
@@ -2587,10 +2590,6 @@ skip_spinlock: dio->bio_details.bi_iter = bio->bi_iter; -if (unlikely (!dm_integrity_check_limits (ic, bio->bi_iter.bi_sector, bio))) { -return DM_MAPIO_KILL ; - } - bio->bi_iter.bi_sector += ic->start +SB_SECTORS ; bip =bio_integrity_alloc (bio,GFP_NOIO ,1 );
Greg Kroah-Hartman
maintainerPATCH 1856/2077 dm-integrity: dont increment hash_offset twice
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit edf025f083854f80032b73a1aad69a3c90db236f upstream.
hash_offset is already incremented in the loop "for (i = 0; i < to_copy; i++, ts--)". Do not increment it again.
--- drivers/md/dm-integrity.c | 3 --- 1 file changed, 3 deletions(-)
drivers/md/dm-integrity.c +0 -3
@@ -1480,9 +1480,6 @@ thorough_test: *metadata_offset =0 ; } -if (unlikely (!is_power_of_2 (ic->tag_size))) - hash_offset = (hash_offset + to_copy) % ic->tag_size; - total_size -= to_copy; }while (unlikely (total_size));
Greg Kroah-Hartman
maintainerPATCH 1857/2077 dm-verity: fix buffer overflow in FEC calculation
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 31d6e6c0ba8d5a7bd59660035a089307100c5e8e upstream.
There's a buffer overflow in dm-verity-fec:
if (neras && *neras <= v->fec->roots)
fio->erasures[(*neras)++] = i;
This allows *neras to reach roots + 1 (the post-increment pushes it past roots). This value is then passed as no_eras to decode_rs8(). Inside the RS decoder (lib/reed_solomon/decode_rs.c:113-121), the erasure locator polynomial loop writes lambda[j] where j can reach nroots + 1 — one element past the end of lambda[] (which is sized nroots + 1, valid indices 0..nroots). The out-of-bounds write lands on syn[0], corrupting the syndrome buffer.
--- drivers/md/dm-verity-fec.c | 4 ++-- drivers/md/dm-verity-fec.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/md/dm-verity-fec.c +2 -2
@@ -220,7 +220,7 @@ static int fec_read_bufs(struct dm_verit PTR_ERR(bbuf));/* assume the block is corrupted */ -if (neras && *neras <= v->fec->roots) +if (neras && *neras < v->fec->roots) fio->erasures[(*neras)++] = i;continue ;
@@ -238,7 +238,7 @@ static int fec_read_bufs(struct dm_verit* skip if we have already found the theoretical * maximum number (i.e. fec->roots) of erasures */ -if (neras && *neras <= v->fec->roots && +if (neras && *neras < v->fec->roots &&fec_is_erasure (v, io, want_digest, bbuf)) fio->erasures[(*neras)++] = i; }
drivers/md/dm-verity-fec.h +1 -1
@@ -47,7 +47,7 @@ struct dm_verity_fec {/* per-bio data */ struct dm_verity_fec_io {struct rs_control *rs;/* Reed-Solomon state */ -int erasures[DM_VERITY_FEC_MAX_ROOTS +1 ];/* erasures for decode_rs8 */ +int erasures[DM_VERITY_FEC_MAX_ROOTS ];/* erasures for decode_rs8 */ u8 *output;/* buffer for corrected output */ unsigned int level;/* recursion level */ unsigned int nbufs;/* number of buffers allocated */
Greg Kroah-Hartman
maintainerPATCH 1858/2077 dm-verity: avoid double increment of &use_bh_wq_enabled
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 72e9ec2fe32b00994f41719cf77423fca67d48b2 upstream.
verity_parse_opt_args is called twice, first with the only_modifier_opts, first with only_modifier_opts == true and then with only_modifier_opts == false. Thus, the static branch &use_bh_wq_enabled was incremented twice and the destructor verity_dtr would only decrement it once.
Fix tihs bug by only incrementing it on the first call, on the second call, when v->use_bh_wq is true, do nothing.
--- drivers/md/dm-verity-target.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/md/dm-verity-target.c +2 -0
@@ -1262,6 +1262,8 @@ static int verity_parse_opt_args(structcontinue ; }else if (!strcasecmp (arg_name,DM_VERITY_OPT_TASKLET_VERIFY )) { +if (v->use_bh_wq) +continue ; v->use_bh_wq = true;static_branch_inc (&use_bh_wq_enabled);continue ;
Greg Kroah-Hartman
maintainerPATCH 1859/2077 dm-verity: fix a possible NULL pointer dereference
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit e72b793ae440f6900fb17a4b8518c707b5cd3e17 upstream.
Fix a possible NULL pointer dereference dm_verity_loadpin_is_bdev_trusted if the device has no table.
--- drivers/md/dm-verity-loadpin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-verity-loadpin.c +1 -1
@@ -70,7 +70,7 @@ bool dm_verity_loadpin_is_bdev_trusted(s table =dm_get_live_table (md, &srcu_idx); -if (table->num_targets !=1 ) +if (!table || table->num_targets !=1 ) goto out; ti =dm_table_get_target (table,0 );
Greg Kroah-Hartman
maintainerPATCH 1860/2077 dm-verity: increase sprintf buffer size
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 88dd117c92a142253fb7a17e791773902b3babc6 upstream.
The prefix "DM_VERITY_ERR_BLOCK_NR" is 22 chars. Add '=', one digit for type, ',', up to 20 digits for a u64 block number, and a NUL terminator:
that's 46 bytes. The buffer is 42 bytes. For block numbers >= 16 decimal digits (devices larger than ~16 EB with 4K blocks), snprintf silently truncates the uevent environment variable.
--- drivers/md/dm-verity-target.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-verity-target.c +1 -1
@@ -26,7 +26,7 @@#define DM_MSG_PREFIX "verity" -#define DM_VERITY_ENV_LENGTH 42 +#define DM_VERITY_ENV_LENGTH 46#define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"#define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
Greg Kroah-Hartman
maintainerPATCH 1861/2077 dm-verity: make error counter atomic
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Mikulas Patocka <[email protected]>
commit 8ec4d9c5a5cf4b61fc087f871465b1f79b393325 upstream.
The error counter "v->corrupted_errs" was not atomic, thus it could be subject to race conditions. The call to dm_audit_log_target("max-corrupted-errors") may be skipped due to the races.
--- drivers/md/dm-verity-target.c | 12 +++++++----- drivers/md/dm-verity.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)
drivers/md/dm-verity-target.c +7 -5
@@ -180,14 +180,16 @@ static int verity_handle_err(struct dm_vchar *envp[] = { verity_env,NULL };const char *type_str ="" ;struct mapped_device *md =dm_table_get_md (v->ti->table); +int ce;/* Corruption should be visible in device status in all modes */ v->hash_failed = true; -if (v->corrupted_errs >=DM_VERITY_MAX_CORRUPTED_ERRS ) - goto out; - - v->corrupted_errs++; + ce =atomic_read (&v->corrupted_errs); +do { +if (ce >=DM_VERITY_MAX_CORRUPTED_ERRS ) + goto out; + }while (!atomic_try_cmpxchg (&v->corrupted_errs, &ce, ce +1 ));switch (type) {case DM_VERITY_BLOCK_TYPE_DATA :
@@ -203,7 +205,7 @@ static int verity_handle_err(struct dm_vDMERR_LIMIT ("%s: %s block %llu is corrupted" , v->data_dev->name, type_str, block); -if (v->corrupted_errs ==DM_VERITY_MAX_CORRUPTED_ERRS ) { +if (ce +1 ==DM_VERITY_MAX_CORRUPTED_ERRS ) {DMERR ("%s: reached maximum errors" , v->data_dev->name);dm_audit_log_target (DM_MSG_PREFIX ,"max-corrupted-errors" , v->ti,0 ); }
drivers/md/dm-verity.h +1 -1
@@ -68,7 +68,7 @@ struct dm_verity {unsigned int digest_size;/* digest size for the current hash algorithm */ enum verity_mode mode;/* mode for handling verification errors */ enum verity_mode error_mode;/* mode for handling I/O errors */ -unsigned int corrupted_errs;/* Number of errors for corrupted blocks */ +atomic_t corrupted_errs;/* Number of errors for corrupted blocks */ struct workqueue_struct *verify_wq;
Greg Kroah-Hartman
maintainerPATCH 1862/2077 dma-fence: Make dma_fence_dedup_array() robust against 0-count input
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Baineng Shou <[email protected]>
commit 77a9298741f8f9e8b963c977f5582ab21c6d3427 upstream.
dma_fence_dedup_array() returns 1 when called with num_fences == 0:
the for-loop body never executes, j stays at 0, and the final return ++j yields 1. This contradicts both the kernel-doc ("Return:
Number of unique fences remaining in the array") and the natural expectation that 0 input gives 0 output.
The caller __dma_fence_unwrap_merge() bails out via the if (count == 0 || count == 1) fast path and so is save.
But amdgpu_userq_wait_*() could reach the dedup call with a zero local count and dereference an uninitialized fence slot in the array.
Make the contract match the documentation by returning 0 early. This also skips an unnecessary sort() call on an empty array.
--- drivers/dma-buf/dma-fence-unwrap.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/dma-buf/dma-fence-unwrap.c +3 -0
@@ -97,6 +97,9 @@ int dma_fence_dedup_array(struct dma_fen {int i, j; +if (!num_fences) +return 0 ; +sort (fences, num_fences,sizeof (*fences), fence_cmp,NULL );/*
Greg Kroah-Hartman
maintainerPATCH 1863/2077 dma-fence: use correct callback in dma_fence_timeline_name()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: André Draszik <[email protected]>
commit 032a6f6ce21fc701468fd15403d2f53a30107f5b upstream.
dma_fence_timeline_name() is a wrapper around dma_fence_ops::get_timeline_name(). Since the blamed commit below, it calls an incorrect callback.
Update it to restore functionality by calling the intended callback.
--- drivers/dma-buf/dma-fence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-fence.c +1 -1
@@ -1202,7 +1202,7 @@ const char __rcu *dma_fence_timeline_nam/* RCU protection is required for safe access to returned string */ ops =rcu_dereference (fence->ops);if (!dma_fence_test_signaled_flag (fence)) -return (const char __rcu *)ops->get_driver_name (fence); +return (const char __rcu *)ops->get_timeline_name (fence);else return (const char __rcu *)"signaled-timeline" ; }
Greg Kroah-Hartman
maintainerPATCH 1864/2077 dma-buf: dma-fence: Fix potential NULL pointer dereference
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Philipp Stanner <[email protected]>
commit 035219a760edb35ae9a9e96beba7f122e26a997b upstream.
The commit mentioned in the fixes tag below introduced a mechanism through which fence producers can fully decouple from fence consumers. This, desirable, mechanism is based on the fence's signaled-bit as the "decoupling point".
A sophisticated interaction between RCU and atomic instructions attempts to ensure that fence consumers can still interact with fence producers through the dma_fence_ops (callback pointers into the producer).
This is the desired behavior: to check for decoupling, the signaled-bit is first checked. If it's not yet signaled, RCU ensures that the ops pointer cannot yet be NULL.
Hereby, dma_fence_signal_timestamp_locked() first sets the signaled-bit, and then sets the ops pointer to NULL. Readers first load the ops pointer, and then check through the signaled-bit whether the pointer can legally be accessed.
These set and load operations could occur out of order on weakly ordered platforms. This problem can be solved very elegantly by using the ops pointer itself as the synchronization point. The pointer is either NULL, or cannot become NULL while it is being used thanks to RCU.
Replace the signaled-bit check in dma_fence_timeline_name() and dma_fence_driver_name().
--- drivers/dma-buf/dma-fence.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +2 -2
@@ -1168,7 +1168,7 @@ const char __rcu *dma_fence_driver_name(/* RCU protection is required for safe access to returned string */ ops =rcu_dereference (fence->ops); -if (!dma_fence_test_signaled_flag (fence)) +if (ops)return (const char __rcu *)ops->get_driver_name (fence);else return (const char __rcu *)"detached-driver" ;
@@ -1201,7 +1201,7 @@ const char __rcu *dma_fence_timeline_nam/* RCU protection is required for safe access to returned string */ ops =rcu_dereference (fence->ops); -if (!dma_fence_test_signaled_flag (fence)) +if (ops)return (const char __rcu *)ops->get_timeline_name (fence);else return (const char __rcu *)"signaled-timeline" ;
Greg Kroah-Hartman
maintainerPATCH 1865/2077 accel/amdxdna: Fix use-after-free in amdxdna_gem_dmabuf_mmap()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Wentao Liang <[email protected]>
commit 63bbf9ac5dde2ba85e7b39d0a0b7d540e6252ba4 upstream.
When vm_insert_pages() fails, the error path calls vma->vm_ops->close(vma) which internally calls drm_gem_vm_close() → drm_gem_object_put(), releasing the GEM object reference acquired at the start of the function. However, the close_vma label then falls through to put_obj, which calls drm_gem_object_put() a second time on the same object.
If the first put releases the last reference, the object is freed and the second put accesses freed memory, causing a use-after-free.
Fix by returning directly from close_vma instead of falling through to put_obj, since the close handler already performs all necessary cleanup including the object put.
--- drivers/accel/amdxdna/amdxdna_gem.c | 1 + 1 file changed, 1 insertion(+)
drivers/accel/amdxdna/amdxdna_gem.c +1 -0
@@ -480,6 +480,7 @@ static int amdxdna_gem_dmabuf_mmap(struc close_vma: vma->vm_ops->close (vma); +return ret; put_obj:drm_gem_object_put (gobj);return ret;
Greg Kroah-Hartman
maintainerPATCH 1866/2077 accel/amdxdna: reject command submission on devices without a submit op
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Doruk Tan Ozturk <[email protected]>
commit 38953513d7313992676d4136cd425cdb70c6278e upstream.
amdxdna_cmd_submit() calls xdna->dev_info->ops->cmd_submit() unconditionally, but only aie2_dev_ops defines that callback. aie4_vf_ops (the AIE4 SR-IOV virtual function) does not, so a user AMDXDNA_EXEC_CMD ioctl on an AIE4 device reaches a NULL function-pointer call and oopses the kernel. AIE4 submits work through a mapped user queue and doorbell, not this ioctl path.
Reject the submission early with -EOPNOTSUPP when the device provides no cmd_submit op, so the shared EXEC ioctl is a clean no-op on such devices.
Found by 0sec automated security-research tooling (https://0sec.ai).
--- drivers/accel/amdxdna/amdxdna_ctx.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/accel/amdxdna/amdxdna_ctx.c +4 -0
@@ -496,6 +496,10 @@ int amdxdna_cmd_submit(struct amdxdna_clint ret, idx;XDNA_DBG (xdna,"Command BO hdl %d, Arg BO count %d" , cmd_bo_hdl, arg_bo_cnt); + +if (!xdna->dev_info->ops->cmd_submit) +return -EOPNOTSUPP ; + job =kzalloc_flex (*job, bos, arg_bo_cnt);if (!job)return -ENOMEM ;
Greg Kroah-Hartman
maintainerPATCH 1867/2077 accel/amdxdna: reject user command submission without a command BO
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Doruk Tan Ozturk <[email protected]>
commit 261c1fe3327ad24508f54552c6366e3e4db82c15 upstream.
amdxdna_drm_submit_execbuf() passes the user-supplied command BO handle straight into amdxdna_cmd_submit() with drv_cmd == NULL. When the handle is AMDXDNA_INVALID_BO_HANDLE (0), the block that fetches job->cmd_bo is skipped, leaving it NULL, and no check rejects it on the user path (the !job->cmd_bo guard lives inside the != INVALID branch).
The job is then armed and pushed to the DRM scheduler. aie2_sched_job_run() takes the drv_cmd == NULL path and calls amdxdna_cmd_set_state(job->cmd_bo) -> amdxdna_gem_vmap(NULL) -> to_gobj(NULL)->dev, a NULL pointer dereference in the drm_sched worker. A process with access to the accel node on a system with a probed AMD NPU can trigger a kernel oops with a single AMDXDNA_EXEC_CMD ioctl (cmd_handles = 0).
Only internal driver commands (SYNC_DEBUG_BO / ATTACH_DEBUG_BO) legitimately pass AMDXDNA_INVALID_BO_HANDLE, and they always set drv_cmd. Reject the invalid handle for user submissions (drv_cmd == NULL) at the submit choke point so every user path is covered.
Found by 0sec automated security-research tooling (https://0sec.ai).
--- drivers/accel/amdxdna/amdxdna_ctx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/accel/amdxdna/amdxdna_ctx.c +10 -0
@@ -513,6 +513,16 @@ int amdxdna_cmd_submit(struct amdxdna_cl ret = -EINVAL ; goto free_job; } + }else if (!drv_cmd) { +/* +* Only internal driver commands (drv_cmd != NULL) may omit a +* command BO. A user command submission with the invalid handle +* would leave job->cmd_bo NULL and later fault when the scheduler +* dereferences it in amdxdna_cmd_set_state(). +*/ +XDNA_DBG (xdna,"Command BO handle required for user submission" ); + ret = -EINVAL ; + goto free_job; } ret =amdxdna_arg_bos_lookup (client, job, arg_bo_hdls, arg_bo_cnt);
Greg Kroah-Hartman
maintainerPATCH 1868/2077 accel/amdxdna: Use caller client for debug BO sync
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Shuvam Pandey <[email protected]>
commit 7caf2a2351d4053075670ff3e26a6815da0a9e1e upstream.
amdxdna_drm_sync_bo_ioctl() looks up args->handle in the ioctl caller's drm_file. For SYNC_DIRECT_FROM_DEVICE, it then calls amdxdna_hwctx_sync_debug_bo(), but passes abo->client.
amdxdna_hwctx_sync_debug_bo() uses the passed client both as the handle namespace for debug_bo_hdl and as the owner of the hardware context xarray. Those must match the file that supplied args->handle. The BO's stored client pointer is object state, not the ioctl context.
Pass filp->driver_priv instead, matching the original handle lookup.
--- drivers/accel/amdxdna/amdxdna_gem.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_gem.c +2 -1
@@ -1039,6 +1039,7 @@ int amdxdna_drm_get_bo_info_ioctl(structint amdxdna_drm_sync_bo_ioctl (struct drm_device *dev,void *data,struct drm_file *filp) { +struct amdxdna_client *client = filp->driver_priv;struct amdxdna_dev *xdna =to_xdna_dev (dev);struct amdxdna_drm_sync_bo *args = data;struct amdxdna_gem_obj *abo;
@@ -1073,7 +1074,7 @@ int amdxdna_drm_sync_bo_ioctl(struct drm args->handle, args->offset, args->size);if (args->direction ==SYNC_DIRECT_FROM_DEVICE ) - ret =amdxdna_hwctx_sync_debug_bo (abo->client, args->handle); + ret =amdxdna_hwctx_sync_debug_bo (client, args->handle); put_obj:drm_gem_object_put (gobj);
Greg Kroah-Hartman
maintainerPATCH 1869/2077 accel/ivpu: Reject firmware log with size smaller than header
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Jhonraushan <[email protected]>
commit ddb44baed257560f192b145ed36cf8c0a412de47 upstream.
fw_log_from_bo() validates the tracing buffer header_size and that the log fits within the BO, but never checks that log->size is at least log->header_size. fw_log_print_buffer() then computes:
u32 data_size = log->size - log->header_size;
which underflows to a near-U32_MAX value when firmware reports a log whose size is smaller than its header. That huge data_size defeats the log_start/log_end bounds clamps added by commit dd1311bcf0e6 ("accel/ivpu:
Add bounds checks for firmware log indices"), so fw_log_print_lines() reads far past the small real data region of the BO. A size of 0 also makes fw_log_from_bo() advance the offset by 0, causing the callers to loop forever on the same header.
Reject logs whose size is smaller than the header (which also rejects size == 0).
--- drivers/accel/ivpu/ivpu_fw_log.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/accel/ivpu/ivpu_fw_log.c +4 -0
@@ -43,6 +43,10 @@ static int fw_log_from_bo(struct ivpu_deivpu_dbg (vdev,FW_BOOT ,"Invalid header size 0x%x\n" , log->header_size);return -EINVAL ; } +if (log->size < log->header_size) { +ivpu_dbg (vdev,FW_BOOT ,"Invalid log size 0x%x\n" , log->size); +return -EINVAL ; + }if ((char *)log + log->size > (char *)ivpu_bo_vaddr (bo) +ivpu_bo_size (bo)) {ivpu_dbg (vdev,FW_BOOT ,"Invalid log size 0x%x\n" , log->size);return -EINVAL ;
Greg Kroah-Hartman
maintainerPATCH 1870/2077 scsi: hpsa: Fix DMA mapping leak on IOACCEL2 reset path
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Haoxiang Li <[email protected]>
commit e166bafc483e927150cb9b5f286c9191ea0df84e upstream.
If phys_disk->in_reset is set, the function returns directly without undoing the resources acquired for the command. Add the missing error cleanup by unmapping the IOACCEL2 SG chain block when needed, unmapping the SCSI command, and dropping the outstanding IOACCEL command count before returning.
--- drivers/scsi/hpsa.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/scsi/hpsa.c +4 -0
@@ -5017,6 +5017,10 @@ static int hpsa_scsi_ioaccel2_queue_commif (phys_disk->in_reset) { cmd->result =DID_RESET <<16 ; +atomic_dec (&phys_disk->ioaccel_cmds_out); +scsi_dma_unmap (cmd); +if (use_sg > h->ioaccel_maxsg) +hpsa_unmap_ioaccel2_sg_chain_block (h, cp);return -1 ; }
Greg Kroah-Hartman
maintainerPATCH 1871/2077 scsi: lpfc: Fix memory leak in lpfc_sli4_driver_resource_setup()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Abdun Nihaal <[email protected]>
commit 1bd28625e25be549ee7c47532e7c3ef91c682410 upstream.
The memory allocated for mboxq using mempool_alloc() is not freed in some of the early exit error paths. Fix that by moving the mempool_free() call to an earlier point after last use.
--- drivers/scsi/lpfc/lpfc_init.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/scsi/lpfc/lpfc_init.c +1 -2
@@ -8189,6 +8189,7 @@ lpfc_sli4_driver_resource_setup(struct lmempool_free (mboxq, phba->mbox_mem_pool); goto out_free_bsmbx; } +mempool_free (mboxq, phba->mbox_mem_pool);/* * 1 for cmd, 1 for rsp, NVME adds an extra one
@@ -8311,8 +8312,6 @@ lpfc_sli4_driver_resource_setup(struct l goto out_free_sg_dma_buf; } -mempool_free (mboxq, phba->mbox_mem_pool); -/* Verify OAS is supported */ lpfc_sli4_oas_verify (phba);
Greg Kroah-Hartman
maintainerPATCH 1872/2077 scsi: sg: Report request-table problems when any status is set
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Xu Rao <[email protected]>
commit 1d3a742afeb761eaead774691bde1ced699e9a5d upstream.
SG_GET_REQUEST_TABLE reports per-request diagnostic state through sg_req_info::problem. The field is meant to indicate whether there is an error to report for a completed request.
sg_fill_request_table() currently combines masked_status, host_status and driver_status with bitwise AND. This only reports a problem when all three status fields are non-zero at the same time. A normal target check condition, for example, has masked_status set while host_status and driver_status may both be zero, so the request is incorrectly reported as clean.
Use the same condition as sg_new_read(), which sets SG_INFO_CHECK when any of the three status fields is non-zero.
--- drivers/scsi/sg.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/scsi/sg.c +3 -4
@@ -863,10 +863,9 @@ sg_fill_request_table(Sg_fd *sfp, sg_reqif (val >=SG_MAX_QUEUE )break ; rinfo[val].req_state = srp->done +1 ; - rinfo[val].problem = - srp->header.masked_status & - srp->header.host_status & - srp->header.driver_status; + rinfo[val].problem = srp->header.masked_status || + srp->header.host_status || + srp->header.driver_status;if (srp->done) rinfo[val].duration = srp->header.duration;
Greg Kroah-Hartman
maintainerPATCH 1873/2077 scsi: xen: scsiback: Free the command tag on the TMR submit-failure path
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Michael Bommarito <[email protected]>
commit 66aefc277ebb796ec285d550305535dc3fc0179f upstream.
scsiback_device_action() obtains a command tag in scsiback_get_pend_req() and submits a task-management request with target_submit_tmr(). When target_submit_tmr() fails it returns < 0 and scsiback jumps to the err: label, which sends a response but frees nothing, leaking the tag.
Impact: a pvSCSI guest can leak the command tags of a LUN's session, stopping the LUN, by issuing VSCSIIF_ACT_SCSI_ABORT or RESET requests whenever target_submit_tmr() fails.
transport_generic_free_cmd() cannot be used here. By the time target_submit_tmr() returns an error it has already run __target_init_cmd() (so se_cmd->cmd_kref is one, not zero), and on its target_get_sess_cmd() error path it has freed se_cmd->se_tmr_req via core_tmr_release_req() while leaving SCF_SCSI_TMR_CDB set and the pointer dangling. Letting the command release run target_free_cmd_mem() would then double-free se_tmr_req.
Use the same helper, which returns just the tag, on this path too.
--- drivers/xen/xen-scsiback.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/xen/xen-scsiback.c +1 -1
@@ -639,7 +639,7 @@ static void scsiback_device_action(strucreturn ; err: -scsiback_do_resp_with_sense (NULL , err,0 , pending_req); +scsiback_resp_and_free (pending_req, err); }/*
Greg Kroah-Hartman
maintainerPATCH 1874/2077 scsi: xen: scsiback: Free unsubmitted command instead of double-putting it
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Michael Bommarito <[email protected]>
commit ca978f8a93d4d36841839bf2847d29b88c2591d6 upstream.
scsiback_get_pend_req() obtains a command tag and returns a vscsibk_pend whose embedded se_cmd has only been memset to 0, so its cmd_kref is 0; the se_cmd is initialised (kref_init() via target_init_cmd()) only later, in scsiback_cmd_exec(), on the successful VSCSIIF_ACT_SCSI_CDB path. The two error paths in scsiback_do_cmd_fn() taken before the command is submitted -- a failed scsiback_gnttab_data_map() and an unknown ring_req.act -- call transport_generic_free_cmd(&pending_req->se_cmd, 0), which kref_put()s a refcount of 0. That underflows it ("refcount_t: underflow; use-after-free") and, as the release function is not run, leaks the command tag.
Impact: a pvSCSI guest can leak every command tag of a LUN's session, stopping the LUN, by submitting requests with a bad grant reference or an unknown request type; under panic_on_warn the refcount underflow panics the host.
Add a helper that just returns the tag with target_free_tag() and sends the error response. It frees the tag while the v2p reference still pins the session, and snapshots the response fields beforehand because freeing the tag can let another ring reuse the pending_req slot.
--- drivers/xen/xen-scsiback.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/xen/xen-scsiback.c +22 -6
@@ -611,6 +611,25 @@ static void scsiback_disconnect(struct vxenbus_unmap_ring_vfree (info->dev, info->ring.sring); } +/* +* Send the error response for a request that did not reach the target core +* and return its tag. Free the tag before the response drops the v2p +* reference that keeps the session alive, and snapshot what the response +* needs since returning the tag can let the slot be reused. +*/ +static void scsiback_resp_and_free (struct vscsibk_pend *pending_req, +int32_t result) +{ +struct vscsibk_info *info = pending_req->info; +struct v2p_entry *v2p = pending_req->v2p; +struct se_session *se_sess = v2p->tpg->tpg_nexus->tvn_se_sess; +u16 rqid = pending_req->rqid; + +target_free_tag (se_sess, &pending_req->se_cmd); +scsiback_send_response (info,NULL , result,0 , rqid); +kref_put (&v2p->kref, scsiback_free_translation_entry); +} +static void scsiback_device_action (struct vscsibk_pend *pending_req,enum tcm_tmreq_table act,int tag) {
@@ -792,9 +811,8 @@ static int scsiback_do_cmd_fn(struct vsccase VSCSIIF_ACT_SCSI_CDB :if (scsiback_gnttab_data_map (&ring_req, pending_req)) {scsiback_fast_flush_area (pending_req); -scsiback_do_resp_with_sense (NULL , -DID_ERROR <<16 ,0 , pending_req); -transport_generic_free_cmd (&pending_req->se_cmd,0 ); +scsiback_resp_and_free (pending_req, +DID_ERROR <<16 ); }else {scsiback_cmd_exec (pending_req); }
@@ -808,9 +826,7 @@ static int scsiback_do_cmd_fn(struct vscbreak ;default :pr_err_ratelimited ("invalid request\n" ); -scsiback_do_resp_with_sense (NULL ,DID_ERROR <<16 ,0 , - pending_req); -transport_generic_free_cmd (&pending_req->se_cmd,0 ); +scsiback_resp_and_free (pending_req,DID_ERROR <<16 );break ; }
Greg Kroah-Hartman
maintainerPATCH 1875/2077 scsi: target: Bound PR-OUT TransportID parsing to the received buffer
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Bryam Vargas <[email protected]>
commit d04a179085c262c9ed577d0a4cbc6482ff1fd9a3 upstream.
core_scsi3_decode_spec_i_port() and core_scsi3_emulate_register_and_move() hand the raw PERSISTENT RESERVE OUT parameter buffer to target_parse_pr_out_transport_id() without telling it how many bytes are valid. For an iSCSI TransportID (FORMAT CODE 01b), iscsi_parse_pr_out_transport_id() locates the ",i,0x" ISID separator with an unbounded strstr() (and on the error path prints the name with a further unbounded "%s"). An initiator can submit a TransportID whose iSCSI name contains neither a ",i,0x" substring nor a NUL terminator, filling the parameter list to its end, so the scan runs off the end of the buffer.
When the parameter list spans more than one page the buffer is a multi-page vmap (transport_kmap_data_sg()), so the over-read walks into the trailing vmalloc guard page and oopses (KASAN: vmalloc-out-of-bounds in strstr). It is reachable by any fabric that delivers a PR OUT to a device exported through an iSCSI TPG, including a guest via vhost-scsi.
Pass the number of received bytes down to the parser and validate the iSCSI TransportID's own self-described length (ADDITIONAL LENGTH + 4) once, up
front: reject it if it is below the spc4r17 minimum or larger than the received buffer, then bound the separator search, the ISID walk and the name copy by that length. This is the length check the callers already perform after the parse (core_scsi3_decode_spec_i_port() compares tid_len against tpdl, core_scsi3_emulate_register_and_move() validates it against data_length), moved ahead of the scan. Also drop the unbounded "%s" of the unterminated name.
Add per-format explicit name-length checks before copying into i_str, rather than silently truncating with min_t: for FORMAT CODE 00b reject if the descriptor body (tid_len - 4 bytes) cannot fit in i_str[TRANSPORT_IQN_LEN]; for FORMAT CODE 01b reject if the name portion (from &buf[4] up to the separator) cannot fit. Both checks make the bounds intent explicit at each format branch.
While here, also reject a FORMAT CODE 01b TransportID whose ",i,0x" separator sits at the very end of the descriptor: that leaves an empty ISID and points the returned port nexus pointer at buf + tid_len, one past the descriptor, which the registration code (__core_scsi3_locate_pr_reg(), __core_scsi3_alloc_registration()) then dereferences as the ISID string -- the same over-read of the parameter buffer for a malformed descriptor.
--- drivers/target/target_core_fabric_lib.c | 89 ++++++++++++++++++++++++-------- drivers/target/target_core_internal.h | 3 - drivers/target/target_core_pr.c | 4 - 3 files changed, 73 insertions(+), 23 deletions(-)
drivers/target/target_core_fabric_lib.c +69 -20
@@ -290,13 +290,24 @@ static void sbp_parse_pr_out_transport_istatic bool iscsi_parse_pr_out_transport_id (struct se_portal_group *se_tpg,char *buf, +u32 buf_len,u32 *out_tid_len,char **port_nexus_ptr,char *i_str) {char *p; +u32 tid_len;int i; -u8 format_code = (buf[0 ] &0xc0 ); +u8 format_code; + +/* +* The 4-byte iSCSI TransportID header (FORMAT CODE + 2-byte ADDITIONAL +* LENGTH) must be present before any of it can be parsed. +*/ +if (buf_len <4 ) +return false; + + format_code = buf[0 ] &0xc0 ;/* * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6: *
@@ -316,15 +327,17 @@ static bool iscsi_parse_pr_out_transportreturn false; }/* -* If the caller wants the TransportID Length, we set that value for the -* entire iSCSI Tarnsport ID now. +* Reconstruct the self-described TransportID length from the ADDITIONAL +* LENGTH field plus the 4-byte header. Reject it if it is below the +* spc4r17 section 7.5.4.6 minimum (ADDITIONAL LENGTH shall be at least +* 20) or if it runs past the bytes actually received, so that every +* access below stays inside the TransportID. */ -if (out_tid_len) { -/* The shift works thanks to integer promotion rules */ - *out_tid_len =get_unaligned_be16 (&buf[2 ]); -/* Add four bytes for iSCSI Transport ID header */ - *out_tid_len +=4 ; - } + tid_len =get_unaligned_be16 (&buf[2 ]) +4 ; +if (tid_len <24 || tid_len > buf_len) +return false; +if (out_tid_len) + *out_tid_len = tid_len;/* * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
@@ -332,16 +345,32 @@ static bool iscsi_parse_pr_out_transport* format. */ if (format_code ==0x40 ) { - p =strstr (&buf[4 ],",i,0x" ); + p =strnstr (&buf[4 ],",i,0x" , tid_len -4 );if (!p) { -pr_err ("Unable to locate \",i,0x\" separator" -" for Initiator port identifier: %s\n" , - &buf[4 ]); +pr_err ("Unable to locate \",i,0x\" separator in iSCSI TransportID\n" ); +return false; + } +/* +* The iSCSI name runs from &buf[4] up to the separator; reject it +* if it cannot fit in i_str[TRANSPORT_IQN_LEN]. +*/ +if (p - &buf[4 ] >=TRANSPORT_IQN_LEN ) { +pr_err ("iSCSI Initiator port name too long in TransportID\n" );return false; } *p ='\0' ;/* Terminate iSCSI Name */ p +=5 ;/* Skip over ",i,0x" separator */ +/* +* The ISID must follow the separator. A ",i,0x" sitting at the +* very end of the TransportID leaves no ISID and would point the +* port nexus at buf + tid_len, i.e. past the descriptor, which +* the registration code then reads as the ISID string. +*/ +if (p >= buf + tid_len) { +pr_err ("Missing ISID in iSCSI Initiator port TransportID\n" ); +return false; + } *port_nexus_ptr = p;/* * Go ahead and do the lower case conversion of the received
@@ -349,7 +378,7 @@ static bool iscsi_parse_pr_out_transport* for comparison against the running iSCSI session's ISID from * iscsi_target.c:lio_sess_get_initiator_sid() */ -for (i =0 ; i <12 ; i++) { +for (i =0 ; i <12 && p < buf + tid_len; i++) {/* * The first ISCSI INITIATOR SESSION ID field byte * containing an ASCII null character terminates the
@@ -367,10 +396,22 @@ static bool iscsi_parse_pr_out_transport *p =tolower (*p); p++; } - }else +strscpy (i_str, &buf[4 ],TRANSPORT_IQN_LEN ); + }else { *port_nexus_ptr =NULL ; - -strscpy (i_str, &buf[4 ],TRANSPORT_IQN_LEN ); +/* +* FORMAT CODE 00b: the name occupies buf[4..tid_len-1]. The +* declared length tid_len - 4 must fit in i_str[TRANSPORT_IQN_LEN]. +* (For 01b the same tid_len bound would be over-restrictive: the +* descriptor also carries the separator and ISID, so a legal +* <=223-byte name gives tid_len up to 244.) +*/ +if (tid_len -4 >=TRANSPORT_IQN_LEN ) { +pr_err ("iSCSI Initiator port name too long in TransportID\n" ); +return false; + } +strscpy (i_str, &buf[4 ], tid_len -4 ); + }return true; }
@@ -420,8 +461,16 @@ int target_get_pr_transport_id(struct se }bool target_parse_pr_out_transport_id (struct se_portal_group *tpg, -char *buf,u32 *out_tid_len,char **port_nexus_ptr,char *i_str) +char *buf,u32 buf_len,u32 *out_tid_len, +char **port_nexus_ptr,char *i_str) { +/* +* The fixed-length SAS/SRP/FCP/SBP TransportIDs are 24 bytes; the iSCSI +* format is variable and bounds itself against buf_len below. +*/ +if (tpg->proto_id !=SCSI_PROTOCOL_ISCSI && buf_len <24 ) +return false; +switch (tpg->proto_id) {case SCSI_PROTOCOL_SAS :/*
@@ -440,8 +489,8 @@ bool target_parse_pr_out_transport_id(stsbp_parse_pr_out_transport_id (buf, i_str);break ;case SCSI_PROTOCOL_ISCSI : -return iscsi_parse_pr_out_transport_id (tpg, buf, out_tid_len, - port_nexus_ptr, i_str); +return iscsi_parse_pr_out_transport_id (tpg, buf, buf_len, + out_tid_len, port_nexus_ptr, i_str);default :pr_err ("Unknown proto_id: 0x%02x\n" , tpg->proto_id);return false;
drivers/target/target_core_internal.h +2 -1
@@ -104,7 +104,8 @@ int target_get_pr_transport_id(struct se struct t10_pr_registration *pr_reg, int *format_code, unsigned char *buf);bool target_parse_pr_out_transport_id (struct se_portal_group *tpg, -char *buf,u32 *out_tid_len,char **port_nexus_ptr,char *i_str); +char *buf,u32 buf_len,u32 *out_tid_len, +char **port_nexus_ptr,char *i_str);/* target_core_hba.c */ struct se_hba *core_alloc_hba (const char *,u32 ,u32 );
drivers/target/target_core_pr.c +2 -2
@@ -1573,7 +1573,7 @@ core_scsi3_decode_spec_i_port( iport_ptr =NULL ; tid_found =target_parse_pr_out_transport_id (tmp_tpg, - ptr, &tid_len, &iport_ptr, i_str); + ptr, tpdl, &tid_len, &iport_ptr, i_str);if (!tid_found)continue ;/*
@@ -3285,7 +3285,7 @@ core_scsi3_emulate_pro_register_and_move goto out; } tid_found =target_parse_pr_out_transport_id (dest_se_tpg, - &buf[24 ], &tmp_tid_len, &iport_ptr, initiator_str); + &buf[24 ], tid_len, &tmp_tid_len, &iport_ptr, initiator_str);if (!tid_found) {pr_err ("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" " initiator_str from Transport ID\n" );
Greg Kroah-Hartman
maintainerPATCH 1876/2077 scsi: target: core: Fix iSCSI ISID use-after-free in REGISTER AND MOVE
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Bryam Vargas <[email protected]>
commit fda6a1f3c3d7047b5ce5654487649c2daa738bfc upstream.
core_scsi3_emulate_pro_register_and_move() maps the PERSISTENT RESERVE OUT parameter list with transport_kmap_data_sg() and parses the destination TransportID with target_parse_pr_out_transport_id(). For an iSCSI TransportID (FORMAT CODE 01b), iscsi_parse_pr_out_transport_id() returns the ISID in iport_ptr as a raw pointer into that mapped buffer.
The function then unmaps the buffer with transport_kunmap_data_sg() before dereferencing iport_ptr in strcmp(), __core_scsi3_locate_pr_reg() and core_scsi3_alloc_registration(). When the parameter list spans more than one page (PARAMETER LIST LENGTH > 4096), transport_kmap_data_sg() uses vmap() and transport_kunmap_data_sg() does vunmap(), so the kernel virtual address backing iport_ptr is torn down and every subsequent dereference is a use-after-free read of the unmapped region.
Keep the parameter list mapped until iport_ptr is no longer needed: drop the early transport_kunmap_data_sg() and unmap once on the success path, right before returning. The error paths already unmap through the existing "if (buf) transport_kunmap_data_sg(cmd)" at the out: label, which now runs on every post-map error exit because buf is no longer cleared early. Only reads of the mapping happen while spinlocks are held; the map and unmap calls remain outside any lock. The sibling caller core_scsi3_decode_spec_i_port() already uses the buffer before unmapping it and is left unchanged.
--- drivers/target/target_core_pr.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/target/target_core_pr.c +5 -3
@@ -3293,9 +3293,6 @@ core_scsi3_emulate_pro_register_and_move goto out; } -transport_kunmap_data_sg (cmd); - buf =NULL ; - pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s" " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ? "port" : "device", initiator_str, (iport_ptr != NULL) ?
@@ -3532,6 +3529,11 @@ after_iport_check:core_scsi3_update_and_write_aptpl (cmd->se_dev, aptpl);core_scsi3_put_pr_reg (dest_pr_reg); +/* +* iport_ptr aliases the PR-OUT parameter list mapped above, so the +* buffer is unmapped only here on success (and at out: on error). +*/ +transport_kunmap_data_sg (cmd);return 0 ; out:if (buf)
Greg Kroah-Hartman
maintainerPATCH 1877/2077 scsi: elx: efct: Fix refcount leak in efct_hw_io_abort()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: WenTao Liang <[email protected]>
commit 2c007acf7b31c39c08ce4959451ad00b19be4c1f upstream.
When efct_hw_reqtag_alloc() fails in efct_hw_io_abort(), the error path returns -ENOSPC without releasing the reference obtained via kref_get_unless_zero() earlier in the function. All other error paths correctly drop the reference. This causes a permanent reference leak on the io_to_abort object.
Additionally, the abort_in_progress flag is left set to true on this path, which means future abort attempts for the same I/O will immediately return -EINPROGRESS even though the abort was never submitted, effectively blocking recovery.
Fix this by adding the missing kref_put() call and reset abort_in_progress to false, matching the cleanup done in the efct_hw_wq_write() failure path below.
--- drivers/scsi/elx/efct/efct_hw.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/scsi/elx/efct/efct_hw.c +2 -0
@@ -1997,6 +1997,8 @@ efct_hw_io_abort(struct efct_hw *hw, str wqcb =efct_hw_reqtag_alloc (hw, efct_hw_wq_process_abort, io_to_abort);if (!wqcb) {efc_log_err (hw->os,"can't allocate request tag\n" ); + io_to_abort->abort_in_progress = false; +kref_put (&io_to_abort->ref, io_to_abort->release);return -ENOSPC ; }
Greg Kroah-Hartman
maintainerPATCH 1878/2077 scsi: elx: efct: Fix I/O leak on unsupported additional CDB
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Haoxiang Li <[email protected]>
commit 9cb2d5291dbfe7bed565ead3337047dee9ed1064 upstream.
efct_dispatch_fcp_cmd() allocates an efct_io before dispatching an unsolicited FCP command. If the command has an unsupported additional CDB, the function returns -EIO before handing the IO to the SCSI layer.
Free the allocated IO before returning from this error path.
--- drivers/scsi/elx/efct/efct_unsol.c | 1 + 1 file changed, 1 insertion(+)
drivers/scsi/elx/efct/efct_unsol.c +1 -0
@@ -385,6 +385,7 @@ efct_dispatch_fcp_cmd(struct efct_node *if (cmnd->fc_flags &FCP_CFL_LEN_MASK ) {efc_log_err (efct,"Additional CDB not supported\n" ); +efct_scsi_io_free (io);return -EIO ; }/*
Greg Kroah-Hartman
maintainerPATCH 1879/2077 fs/resctrl: Fix use-after-free during unmount
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Tony Luck <[email protected]>
commit 52fce648607e0d6a76eeb443d78708c49df1c554 upstream.
During unmount or failure teardown all mon_data structures that contain monitoring event file private data are freed after which kernfs nodes are removed. However, the RDT_DELETED flag is never set for the statically allocated default resource group.
A concurrent reader of an event file associated with the default resource group may, after dropping kernfs active protection, block on rdtgroup_mutex while unmount proceeds to free the file private data and destroy the kernfs node without waiting for the reader.
When the mutex is released, the reader wakes up, observes that RDT_DELETED is not set for the default group, and dereferences the already-freed file private data.
The scenario can be depicted as follows:
CPU0 CPU1
/*
* Default resource group's
* monitoring data accessible via
* kernfs file with kernfs_node::priv
* pointing to a struct mon_data.
* User opens the file for reading.
*/
rdtgroup_mondata_show() /* arch encounters fatal error */
rdtgroup_kn_lock_live() resctrl_exit()
atomic_inc(&rdtgroup_default.waitcount) cpus_read_lock()
kernfs_break_active_protection(kn) mutex_lock(&rdtgroup_mutex)
cpus_read_lock() resctrl_fs_teardown()
mutex_lock(&rdtgroup_mutex) rmdir_all_sub()
mon_put_kn_priv()
/* Delete all mon_data structures */
rdtgroup_destroy_root()
kernfs_destroy_root()
rdtgroup_default.kn = NULL
mutex_unlock(&rdtgroup_mutex)
/*
* rdtgroup_default.flags is empty so
* rdtgroup_kn_lock_live() returns
* &rdtgroup_default
*/
md = of->kn->priv;
/* md points to freed mon_data */Set RDT_DELETED for the default group unconditionally since the flag does not lead to the freeing of this statically allocated group.
Do not allow a new resctrl mount if there are any waiters on default group of previous mount. A new mount will re-initialize the default group that would appear to waiters from previous mount as though the default group is accessible causing them to access the mon_data structures from the previous mount that have been removed.
--- fs/resctrl/rdtgroup.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
fs/resctrl/rdtgroup.c +16 -2
@@ -585,14 +585,20 @@ unlock:* * On resource group creation via a mkdir, an extra kernfs_node reference is * taken to ensure that the rdtgroup structure remains accessible for the -* rdtgroup_kn_unlock() calls where it is removed. +* rdtgroup_kn_unlock() calls where it is removed. The default group is +* statically allocated: it does not have an extra reference but will have +* RDT_DELETED set on unmount to support safe access to its associated files +* via rdtgroup_kn_lock_live/rdtgroup_kn_unlock(). * -* Drop the extra reference here, then free the rdtgroup structure. +* For all but the default group: drop the extra reference, then free the +* rdtgroup structure. * * Return: void */ static void rdtgroup_remove (struct rdtgroup *rdtgrp) { +if (rdtgrp == &rdtgroup_default) +return ;kernfs_put (rdtgrp->kn);kfree (rdtgrp); }
@@ -2802,6 +2808,12 @@ static int rdt_get_tree(struct fs_contex goto out; } +/* Avoid races from pending operations from a previous mount */ +if (atomic_read (&rdtgroup_default.waitcount) !=0 ) { + ret = -EBUSY ; + goto out; + } + ret =setup_rmid_lru_list ();if (ret) goto out;
@@ -3164,6 +3176,7 @@ static void resctrl_fs_teardown(void)mon_put_kn_priv ();rdt_pseudo_lock_release (); rdtgroup_default.mode =RDT_MODE_SHAREABLE ; + rdtgroup_default.flags =RDT_DELETED ;closid_exit ();schemata_list_destroy ();rdtgroup_destroy_root ();
@@ -4264,6 +4277,7 @@ static int rdtgroup_setup_root(struct rd ctx->kfc.root = rdt_root; rdtgroup_default.kn =kernfs_root_to_node (rdt_root); + rdtgroup_default.flags =0 ;return 0 ; }
Greg Kroah-Hartman
maintainerPATCH 1880/2077 Input: ims-pcu - fix use-after-free and double-free in disconnect
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 462a999917755a3bf77448dfd64307963cf0a9f0 upstream.
ims_pcu_disconnect() only intended to perform cleanup when the primary (control) interface is unbound. However, it currently relies on the interface class to distinguish between control and data interfaces. A malicious device could present a data interface with the same class as the control interface, leading to premature cleanup and potential use-after-free or double-free.
Switch to verifying that the interface being disconnected is indeed the control interface.
--- drivers/input/misc/ims-pcu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/input/misc/ims-pcu.c +1 -2
@@ -2071,7 +2071,6 @@ err_free_mem:static void ims_pcu_disconnect (struct usb_interface *intf) {struct ims_pcu *pcu =usb_get_intfdata (intf); -struct usb_host_interface *alt = intf->cur_altsetting;usb_set_intfdata (intf,NULL );
@@ -2079,7 +2078,7 @@ static void ims_pcu_disconnect(struct us* See if we are dealing with control or data interface. The cleanup * happens when we unbind primary (control) interface. */ -if (alt->desc.bInterfaceClass !=USB_CLASS_COMM ) +if (intf != pcu->ctrl_intf)return ;ims_pcu_stop_io (pcu);
Greg Kroah-Hartman
maintainerPATCH 1881/2077 Input: ims-pcu - only expose sysfs attributes on control interface
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 001428ea4d2c371107cb984108e266adf99f1f1e upstream.
When the driver was converted to use the driver core to instantiate device attributes (via .dev_groups in the usb_driver structure), the attributes started appearing on all interfaces bound to the driver. Since the ims-pcu driver manually claims the secondary data interface during probe, the driver core automatically creates the sysfs attributes for that interface as well.
However, the driver only supports these attributes on the primary control interface. Data interfaces lack the necessary descriptors and internal state to handle these requests, and accessing them can lead to unexpected behavior or crashes.
Fix this by updating the is_visible() callbacks for both the main and OFN attribute groups to verify that the interface being accessed is indeed the control interface.
--- drivers/input/misc/ims-pcu.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/input/misc/ims-pcu.c +7 -0
@@ -14,6 +14,7 @@#include <linux/leds.h> #include <linux/module.h> #include <linux/slab.h> +#include <linux/sysfs.h> #include <linux/types.h> #include <linux/usb/input.h> #include <linux/usb/cdc.h>
@@ -1249,6 +1250,9 @@ static umode_t ims_pcu_is_attr_visible(sstruct ims_pcu *pcu =usb_get_intfdata (intf);umode_t mode = attr->mode; +if (intf != pcu->ctrl_intf) +return 0 ; +if (pcu->bootloader_mode) { if (attr != &dev_attr_update_firmware_status.attr && attr != &dev_attr_update_firmware.attr &&
@@ -1488,6 +1492,9 @@ static umode_t ims_pcu_ofn_is_attr_visibstruct ims_pcu *pcu =usb_get_intfdata (intf);umode_t mode = attr->mode; +if (intf != pcu->ctrl_intf) +return SYSFS_GROUP_INVISIBLE ; +/* * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor. */
Greg Kroah-Hartman
maintainerPATCH 1882/2077 Input: ims-pcu - release data interface on disconnect
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 441c510a649c8ddce38aa0311334ed8bb546b36c upstream.
During probe the driver claims the data interface, but it never releases it. Release it in disconnect to avoid leaving it permanently claimed.
--- drivers/input/misc/ims-pcu.c | 1 + 1 file changed, 1 insertion(+)
drivers/input/misc/ims-pcu.c +1 -0
@@ -2096,6 +2096,7 @@ static void ims_pcu_disconnect(struct usims_pcu_destroy_application_mode (pcu);ims_pcu_buffers_free (pcu); +usb_driver_release_interface (&ims_pcu_driver, pcu->data_intf);kfree (pcu); }
Greg Kroah-Hartman
maintainerPATCH 1883/2077 Input: ims-pcu - validate control endpoint type
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit baf56975806534268e24acf9a8abb1c447ce11e9 upstream.
The driver currently assumes that the first endpoint of the control interface is an interrupt IN endpoint without verifying it. A malicious device could provide a different endpoint type, which would then be passed to usb_fill_int_urb(), potentially leading to kernel warnings or undefined behavior.
Verify that the control endpoint is an interrupt IN endpoint.
--- drivers/input/misc/ims-pcu.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/input/misc/ims-pcu.c +6 -0
@@ -1709,6 +1709,12 @@ static int ims_pcu_parse_cdc_data(structreturn -ENODEV ; pcu->ep_ctrl = &alt->endpoint[0 ].desc; +if (!usb_endpoint_is_int_in (pcu->ep_ctrl)) { +dev_err (pcu->dev, +"Control endpoint is not INTERRUPT IN\n" ); +return -EINVAL ; + } + pcu->max_ctrl_size =usb_endpoint_maxp (pcu->ep_ctrl); pcu->data_intf = usb_ifnum_to_if(pcu->udev,
Greg Kroah-Hartman
maintainerPATCH 1884/2077 Input: ims-pcu - add response length checks
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 48c9d92fd4ee3a8f5d2cb46c802a0eff8e67c79c upstream.
The driver processes response data from device buffers without verifying that the device actually sent enough data. This can lead to out-of-bounds reads or processing stale data.
Add checks for the expected response length before accessing the buffers.
--- drivers/input/misc/ims-pcu.c | 87 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 6 deletions(-)
drivers/input/misc/ims-pcu.c +81 -6
@@ -407,7 +407,16 @@ static void ims_pcu_destroy_gamepad(strustatic void ims_pcu_report_events (struct ims_pcu *pcu) { -u32 data =get_unaligned_be32 (&pcu->read_buf[3 ]); +u32 data; + +/* 6-axis setting (1 byte) + button data + checksum */ +if (pcu->read_pos <IMS_PCU_DATA_OFFSET +1 +sizeof (data) +1 ) { +dev_warn (pcu->dev,"Short buttons report: %d bytes\n" , + pcu->read_pos); +return ; + } + + data =get_unaligned_be32 (&pcu->read_buf[IMS_PCU_DATA_OFFSET +1 ]);ims_pcu_buttons_report (pcu, data & ~IMS_PCU_GAMEPAD_MASK );if (pcu->gamepad)
@@ -687,11 +696,19 @@ static int __ims_pcu_execute_bl_command(return error; } -if (expected_response && pcu->cmd_buf[2 ] != expected_response) { -dev_err (pcu->dev, -"Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n" , - pcu->cmd_buf[2 ], expected_response); -return -EINVAL ; +if (expected_response) { +if (pcu->cmd_buf_len <3 ) { +dev_err (pcu->dev,"Short response from bootloader: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + +if (pcu->cmd_buf[2 ] != expected_response) { +dev_err (pcu->dev, +"Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n" , + pcu->cmd_buf[2 ], expected_response); +return -EINVAL ; + } }return 0 ;
@@ -719,6 +736,12 @@ static int ims_pcu_get_info(struct ims_preturn error; } +if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +IMS_PCU_SET_INFO_SIZE +1 ) { +dev_err (pcu->dev,"Short GET_INFO response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } +memcpy (pcu->part_number, &pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET ],sizeof (pcu->part_number));
@@ -816,6 +839,12 @@ static int ims_pcu_verify_block(struct ireturn error; } +if (pcu->cmd_buf_len <IMS_PCU_BL_DATA_OFFSET +sizeof (*fragment) + len +1 ) { +dev_err (pcu->dev,"Short READ_APP response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + fragment = (void *)&pcu->cmd_buf[IMS_PCU_BL_DATA_OFFSET ];if (get_unaligned_le32 (&fragment->addr) != addr || fragment->len != len) {
@@ -1010,6 +1039,10 @@ ims_pcu_backlight_get_brightness(struct error);/* Assume the LED is OFF */ brightness =LED_OFF ; + }else if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +2 +1 ) { +dev_err (pcu->dev,"Short GET_BRIGHTNESS response: %d bytes\n" , + pcu->cmd_buf_len); + brightness =LED_OFF ; }else { brightness =get_unaligned_le16 (&pcu->cmd_buf[IMS_PCU_DATA_OFFSET ]);
@@ -1286,6 +1319,12 @@ static int ims_pcu_read_ofn_config(strucif (error)return error; +if (pcu->cmd_buf_len <OFN_REG_RESULT_OFFSET +2 +1 ) { +dev_err (pcu->dev,"Short OFN_GET_CONFIG response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + result = (s16 )get_unaligned_le16 (pcu->cmd_buf +OFN_REG_RESULT_OFFSET );if (result <0 )return -EIO ;
@@ -1306,6 +1345,12 @@ static int ims_pcu_write_ofn_config(struif (error)return error; +if (pcu->cmd_buf_len <OFN_REG_RESULT_OFFSET +2 +1 ) { +dev_err (pcu->dev,"Short OFN_SET_CONFIG response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + result = (s16 )get_unaligned_le16 (pcu->cmd_buf +OFN_REG_RESULT_OFFSET );if (result <0 )return -EIO ;
@@ -1844,6 +1889,12 @@ static int ims_pcu_get_device_info(strucreturn error; } +if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +6 +1 ) { +dev_err (pcu->dev,"Short GET_FW_VERSION response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + snprintf(pcu->fw_version, sizeof(pcu->fw_version), "%02d%02d%02d%02d.%c%c", pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
@@ -1856,6 +1907,12 @@ static int ims_pcu_get_device_info(strucreturn error; } +if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +6 +1 ) { +dev_err (pcu->dev,"Short GET_BL_VERSION response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + snprintf(pcu->bl_version, sizeof(pcu->bl_version), "%02d%02d%02d%02d.%c%c", pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
@@ -1868,6 +1925,12 @@ static int ims_pcu_get_device_info(strucreturn error; } +if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +1 +1 ) { +dev_err (pcu->dev,"Short RESET_REASON response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } +snprintf (pcu->reset_reason,sizeof (pcu->reset_reason),"%02x" , pcu->cmd_buf[IMS_PCU_DATA_OFFSET ]);
@@ -1894,6 +1957,12 @@ static int ims_pcu_identify_type(structreturn error; } +if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +1 +1 ) { +dev_err (pcu->dev,"Short GET_DEVICE_ID response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + *device_id = pcu->cmd_buf[IMS_PCU_DATA_OFFSET ];dev_dbg (pcu->dev,"Detected device ID: %d\n" , *device_id);
@@ -1985,6 +2054,12 @@ static int ims_pcu_init_bootloader_mode(return error; } +if (pcu->cmd_buf_len <IMS_PCU_DATA_OFFSET +15 +4 +1 ) { +dev_err (pcu->dev,"Short QUERY_DEVICE response: %d bytes\n" , + pcu->cmd_buf_len); +return -EIO ; + } + pcu->fw_start_addr =get_unaligned_le32 (&pcu->cmd_buf[IMS_PCU_DATA_OFFSET +11 ]); pcu->fw_end_addr =
Greg Kroah-Hartman
maintainerPATCH 1885/2077 Input: ims-pcu - fix DMA mapping violation in line setup
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 8adf4289d945e8990e4336436a97da71d21d2cae upstream.
In ims_pcu_line_setup(), the driver uses pcu->cmd_buf as a transfer buffer for usb_control_msg(). However, pcu->cmd_buf is embedded in the struct ims_pcu allocation, which violates DMA mapping rules regarding cacheline alignment.
Use a heap-allocated buffer for the line coding data instead.
--- drivers/input/misc/ims-pcu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/input/misc/ims-pcu.c +7 -2
@@ -1841,11 +1841,16 @@ static void ims_pcu_stop_io(struct ims_pstatic int ims_pcu_line_setup (struct ims_pcu *pcu) {struct usb_host_interface *interface = pcu->ctrl_intf->cur_altsetting; -struct usb_cdc_line_coding *line = (void *)pcu->cmd_buf; + struct usb_cdc_line_coding *line __free(kfree) = +kmalloc (sizeof (*line),GFP_KERNEL );int error; -memset (line,0 ,sizeof (*line)); +if (!line) +return -ENOMEM ; + line->dwDTERate =cpu_to_le32 (57600 ); + line->bCharFormat =USB_CDC_1_STOP_BITS ; + line->bParityType =USB_CDC_NO_PARITY ; line->bDataBits =8 ; error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
Greg Kroah-Hartman
maintainerPATCH 1886/2077 Input: ims-pcu - fix firmware leak in async update
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit d48795b5cd6828d36b707e8d62fc9e5c90e004ab upstream.
The firmware object was not being released if validation failed. Use __free(firmware) to ensure the firmware is always released.
--- drivers/input/misc/ims-pcu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/input/misc/ims-pcu.c +2 -3
@@ -964,9 +964,10 @@ out:return retval; } -static void ims_pcu_process_async_firmware (const struct firmware *fw, +static void ims_pcu_process_async_firmware (const struct firmware *_fw,void *context) { + const struct firmware *fw __free(firmware) = _fw;struct ims_pcu *pcu = context;int error;
@@ -986,8 +987,6 @@ static void ims_pcu_process_async_firmwa scoped_guard(mutex, &pcu->cmd_mutex)ims_pcu_handle_firmware_update (pcu, fw); -release_firmware (fw); - out:complete (&pcu->async_firmware_done); }
Greg Kroah-Hartman
maintainerPATCH 1887/2077 Input: ims-pcu - fix logic error in packet reset
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 2c9b85a14abb4811e8d4773ccd13559e59792efb upstream.
ims_pcu_reset_packet() incorrectly sets have_stx to true, which implies that the start-of-packet delimiter has already been received. This causes the protocol parser to skip waiting for the next STX byte and potentially process garbage data.
Correctly set have_stx to false when resetting the packet state.
--- drivers/input/misc/ims-pcu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/ims-pcu.c +1 -1
@@ -450,7 +450,7 @@ static void ims_pcu_handle_response(strustatic void ims_pcu_reset_packet (struct ims_pcu *pcu) { - pcu->have_stx = true; + pcu->have_stx = false; pcu->have_dle = false; pcu->read_pos =0 ; pcu->check_sum =0 ;
Greg Kroah-Hartman
maintainerPATCH 1888/2077 Input: ims-pcu - fix out-of-bounds read in ims_pcu_irq() debug logging
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 403b0a6970b1084bb27907c0f8225801fdd0fe1d upstream.
The debug logging in ims_pcu_irq() unconditionally prints data from pcu->urb_in_buf. However, if the interrupt fired for pcu->urb_ctrl, the actual data resides in pcu->urb_ctrl_buf. If urb->actual_length for the control URB exceeds pcu->max_in_size, this leads to an out-of-bounds read.
Fix this by printing from the correct buffer associated with the URB.
--- drivers/input/misc/ims-pcu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/ims-pcu.c +1 -1
@@ -1579,7 +1579,7 @@ static void ims_pcu_irq(struct urb *urb) }dev_dbg (pcu->dev,"%s: received %d: %*ph\n" , __func__, - urb->actual_length, urb->actual_length, pcu->urb_in_buf); + urb->actual_length, urb->actual_length, urb->transfer_buffer);if (urb == pcu->urb_in)ims_pcu_process_data (pcu, urb);
Greg Kroah-Hartman
maintainerPATCH 1889/2077 Input: ims-pcu - fix potential infinite loop in CDC union descriptor parsing
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit d4579af29e67ca8722db0a1194227f8015c8981d upstream.
The driver parses CDC union descriptors in ims_pcu_get_cdc_union_desc() by iterating through the extra descriptor data. However, it does not verify that the bLength of each descriptor is at least 2. A malicious device could provide a descriptor with bLength = 0, leading to an infinite loop in the driver.
Add a check to ensure bLength is at least 2 before proceeding with parsing.
--- drivers/input/misc/ims-pcu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/input/misc/ims-pcu.c +3 -2
@@ -1707,8 +1707,9 @@ ims_pcu_get_cdc_union_desc(struct usb_inwhile (buflen >=sizeof (*union_desc)) { union_desc = (struct usb_cdc_union_desc *)buf; -if (union_desc->bLength > buflen) { -dev_err (&intf->dev,"Too large descriptor\n" ); +if (union_desc->bLength <2 || union_desc->bLength > buflen) { +dev_err (&intf->dev,"Invalid descriptor length: %d\n" , + union_desc->bLength);return NULL ; }
Greg Kroah-Hartman
maintainerPATCH 1890/2077 Input: ims-pcu - fix race condition in reset_device sysfs callback
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit 411b8c4b274737c3bf08e1e025801161603cfffc upstream.
The ims_pcu_reset_device() sysfs callback calls ims_pcu_execute_command() without acquiring pcu->cmd_mutex. This can lead to data races and corruption of the shared command buffer if triggered concurrently with other commands.
Acquire pcu->cmd_mutex before calling ims_pcu_execute_command().
--- drivers/input/misc/ims-pcu.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/input/misc/ims-pcu.c +2 -0
@@ -1186,6 +1186,8 @@ static ssize_t ims_pcu_reset_device(strudev_info (pcu->dev,"Attempting to reset device\n" ); +guard (mutex)(&pcu->cmd_mutex); + error =ims_pcu_execute_command (pcu,PCU_RESET , &reset_byte,1 );if (error) { dev_info(pcu->dev,
Greg Kroah-Hartman
maintainerPATCH 1891/2077 Input: ims-pcu - fix type confusion in CDC union descriptor parsing
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dmitry Torokhov <[email protected]>
commit ca459e237bc49567649c56bc72e4c602fb92fd67 upstream.
The driver currently trusts the bMasterInterface0 from the CDC union descriptor without verifying that it matches the interface being probed. This could lead to the driver overwriting the private data of another interface.
Validate that the control interface found in the descriptor is indeed the one we are probing.
--- drivers/input/misc/ims-pcu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/ims-pcu.c +1 -1
@@ -1747,7 +1747,7 @@ static int ims_pcu_parse_cdc_data(struct pcu->ctrl_intf =usb_ifnum_to_if (pcu->udev, union_desc->bMasterInterface0); -if (!pcu->ctrl_intf) +if (pcu->ctrl_intf != intf)return -EINVAL ; alt = pcu->ctrl_intf->cur_altsetting;
Greg Kroah-Hartman
maintainerPATCH 1892/2077 mmc: vub300: fix use-after-free on probe failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Guangshuo Li <[email protected]>
commit a3b5f242997a3be7404112fd48784881560aea57 upstream.
The vub300 driver lifetime-manages its controller state using vub300->kref, with vub300_delete() freeing the mmc host when the last reference is dropped. The probe error path after the inactivity timer has been armed still bypasses that lifetime rule, however, and falls through to mmc_free_host() directly if mmc_add_host() fails.
The race window is between arming the inactivity timer and reaching the probe error unwind after mmc_add_host() fails:
probe thread timer/workqueue
------------ ---------------
kref_init(&vub300->kref) ref = 1
kref_get(&vub300->kref) ref = 2, timer ref
add_timer(inactivity_timer) fires after one second
|
| race window
|<---------------------------------------------------->
| mmc_add_host(mmc)
inactivity timer fires vub300_queue_dead_work()
kref_get() ref = 3
queue_work(deadwork)
mmc_add_host() fails
timer_delete_sync()
mmc_free_host(mmc)
frees vub300
deadwork runs
use-after-free
The inactivity timeout is one second, so this would require mmc_add_host() to both fail and take more than one second to do so. This is unlikely to happen in practice, but the error path is still wrong.
timer_delete_sync() only waits for the timer callback itself. It does not flush deadwork that the callback may already have queued. As a result, queued deadwork can still hold a kref while the probe error path directly frees the backing mmc host, including the vub300 storage.
Fix this by using the same lifetime mechanism as disconnect. Clear vub300->interface so that the timer callback and any queued deadwork return early and drop their references, then drop the initial probe reference and return without falling through to err_free_host.
--- drivers/mmc/host/vub300.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/mmc/host/vub300.c +7 -3
@@ -2336,12 +2336,16 @@ static int vub300_probe(struct usb_inter interface_to_InterfaceNumber(interface)); retval =mmc_add_host (mmc);if (retval) - goto err_delete_timer; + goto err_stop_io;return 0 ; -err_delete_timer: -timer_delete_sync (&vub300->inactivity_timer); +err_stop_io: + vub300->interface =NULL ; +kref_put (&vub300->kref, vub300_delete); + +return retval; + err_free_host:mmc_free_host (mmc);/*
Greg Kroah-Hartman
maintainerPATCH 1893/2077 net/mlx5e: macsec: fix use-after-free of metadata_dst on RX SC delete
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Doruk Tan Ozturk <[email protected]>
commit de74d8fd10291763d97b218f09adcc7513c975e4 upstream.
When an offloaded MACsec RX SC is deleted, macsec_del_rxsc_ctx() freed the per-SC metadata_dst with metadata_dst_free(), which kfree()s the object unconditionally and ignores the dst reference count. The RX datapath in mlx5e_macsec_offload_handle_rx_skb() looks up the SC under rcu_read_lock() via xa_load(), takes a reference with dst_hold() and attaches the dst to the skb with skb_dst_set(). A reader that already obtained the rx_sc pointer can race with the delete path and operate on freed memory.
Fix the owner side by dropping the reference with dst_release() instead of freeing unconditionally, and convert the RX datapath to dst_hold_safe() so a reader racing the SC delete cannot attach a dst whose last reference was just dropped; only attach it when a reference was actually taken.
mlx5e_macsec_add_rxsc() also published sc_xarray_element via xa_alloc() before rx_sc->md_dst was allocated and initialised, so a datapath reader that looked the SC up by fs_id could observe rx_sc with md_dst still NULL or, on weakly-ordered architectures, a non-NULL md_dst pointer whose contents were not yet visible. NULL-check the xa_load() result and md_dst on the datapath, and reorder add_rxsc() so the xa_alloc() publish happens only after md_dst is fully initialised; the xarray RCU publish then pairs with the rcu_read_lock()/xa_load() in the datapath.
Note: macsec_del_rxsc_ctx() also kfree()s rx_sc->sc_xarray_element without an RCU grace period while the same datapath reads it under rcu_read_lock(); that is a separate pre-existing issue left to a follow-up patch.
Found by 0sec automated security-research tooling (https://0sec.ai).
--- drivers/net/ethernet/mellanox/mlx5/core/en_accel/macsec.c | 47 ++++++++------ 1 file changed, 28 insertions(+), 19 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/macsec.c +28 -19
@@ -714,34 +714,43 @@ static int mlx5e_macsec_add_rxsc(struct } sc_xarray_element->rx_sc = rx_sc; - err =xa_alloc (&macsec->sc_xarray, &sc_xarray_element->fs_id, sc_xarray_element, -XA_LIMIT (1 ,MLX5_MACEC_RX_FS_ID_MAX ),GFP_KERNEL ); -if (err) { -if (err == -EBUSY ) -netdev_err (ctx->netdev, -"MACsec offload: unable to create entry for RX SC (%d Rx SCs already allocated)\n" , -MLX5_MACEC_RX_FS_ID_MAX ); - goto destroy_sc_xarray_elemenet; - } rx_sc->md_dst =metadata_dst_alloc (0 ,METADATA_MACSEC ,GFP_KERNEL );if (!rx_sc->md_dst) { err = -ENOMEM ; - goto erase_xa_alloc; + goto destroy_sc_xarray_elemenet; } rx_sc->sci = ctx_rx_sc->sci; rx_sc->active = ctx_rx_sc->active; -list_add_rcu (&rx_sc->rx_sc_list_element, rx_sc_list); - rx_sc->sc_xarray_element = sc_xarray_element; rx_sc->md_dst->u.macsec_info.sci = rx_sc->sci; + +/* +* Publish the fully-initialised SC last: xa_alloc() makes +* sc_xarray_element->rx_sc (and rx_sc->md_dst) reachable from the RX +* datapath via xa_load(). Doing it only after md_dst is allocated and +* initialised pairs with the rcu_read_lock()/xa_load() in +* mlx5e_macsec_offload_handle_rx_skb(), so a reader can never observe +* a non-NULL md_dst with uninitialised contents. +*/ + err =xa_alloc (&macsec->sc_xarray, &sc_xarray_element->fs_id, sc_xarray_element, +XA_LIMIT (1 ,MLX5_MACEC_RX_FS_ID_MAX ),GFP_KERNEL ); +if (err) { +if (err == -EBUSY ) +netdev_err (ctx->netdev, +"MACsec offload: unable to create entry for RX SC (%d Rx SCs already allocated)\n" , +MLX5_MACEC_RX_FS_ID_MAX ); + goto destroy_md_dst; + } + +list_add_rcu (&rx_sc->rx_sc_list_element, rx_sc_list);mutex_unlock (&macsec->lock);return 0 ; -erase_xa_alloc: -xa_erase (&macsec->sc_xarray, sc_xarray_element->fs_id); +destroy_md_dst: +dst_release (&rx_sc->md_dst->dst); destroy_sc_xarray_elemenet:kfree (sc_xarray_element); destroy_rx_sc:
@@ -829,7 +838,7 @@ static void macsec_del_rxsc_ctx(struct m*/ list_del_rcu (&rx_sc->rx_sc_list_element);xa_erase (&macsec->sc_xarray, rx_sc->sc_xarray_element->fs_id); -metadata_dst_free (rx_sc->md_dst); +dst_release (&rx_sc->md_dst->dst);kfree (rx_sc->sc_xarray_element);kfree_rcu_mightsleep (rx_sc); }
@@ -1695,10 +1704,10 @@ void mlx5e_macsec_offload_handle_rx_skb(rcu_read_lock (); sc_xarray_element =xa_load (&macsec->sc_xarray, fs_id); - rx_sc = sc_xarray_element->rx_sc; -if (rx_sc) { -dst_hold (&rx_sc->md_dst->dst); -skb_dst_set (skb, &rx_sc->md_dst->dst); + rx_sc = sc_xarray_element ? sc_xarray_element->rx_sc :NULL ; +if (rx_sc && rx_sc->md_dst) { +if (dst_hold_safe (&rx_sc->md_dst->dst)) +skb_dst_set (skb, &rx_sc->md_dst->dst); }rcu_read_unlock ();
Greg Kroah-Hartman
maintainerPATCH 1894/2077 tracing/user_events: Fix use-after-free in user_event_mm_dup()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Michael Bommarito <[email protected]>
commit 50fd6dd755c6e48a38af2fa4621167eea56829c2 upstream.
user_event_mm_dup() walks the parent mm's enabler list locklessly under rcu_read_lock() during fork() (from copy_process()); it does not take event_mutex:
rcu_read_lock(); list_for_each_entry_rcu(enabler, &old_mm->enablers, mm_enablers_link) enabler->event = user_event_get(orig->event);
user_event_enabler_destroy() removes an enabler from that list with list_del_rcu() and then, without waiting for a grace period, drops the enabler's user_event reference with user_event_put() and frees the enabler with kfree(). A reader that loaded the enabler before the list_del_rcu() can still be walking it, which leads to two use-after-frees:
- kfree(enabler) frees the enabler while that reader dereferences enabler->event.
- user_event_put() may drop the last reference to the user_event, which is then freed (via delayed_destroy_user_event() on a work queue), while the same reader does user_event_get(orig->event) on it.
Both are reachable by an unprivileged task that can open user_events_data:
one multithreaded process that registers an enabler and then concurrently unregisters it and calls fork() triggers the race. KASAN reports a slab-use-after-free in user_event_mm_dup() during clone(), with a "refcount_t: addition on 0" warning when the user_event is freed.
The enabler use-after-free was found first; the user_event one was reported by XIAO WU, and the earlier enabler-only fix did not address it.
Defer both the user_event_put() and the kfree(enabler) to a work item queued with queue_rcu_work(), so they run only after an RCU grace period, once all readers walking the enabler list have finished. The put must run in process context because user_event_put() takes event_mutex on the last reference, so a work queue is used rather than call_rcu(). The now-unlocked put lets the locked argument of user_event_enabler_destroy() be removed; all callers are updated.
--- kernel/trace/trace_events_user.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-)
kernel/trace/trace_events_user.c +32 -7
@@ -109,6 +109,9 @@ struct user_event_enabler {/* Track enable bit, flags, etc. Aligned for bitops. */ unsigned long values; + +/* Defer the event put and enabler free past an RCU grace period. */ +struct rcu_work put_rwork; };/* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */
@@ -396,17 +399,39 @@ error:return NULL ; }; -static void user_event_enabler_destroy (struct user_event_enabler *enabler, -bool locked) +static void delayed_user_event_enabler_put (struct work_struct *work) { -list_del_rcu (&enabler->mm_enablers_link); +struct user_event_enabler *enabler =container_of (to_rcu_work (work), + struct user_event_enabler, put_rwork);/* No longer tracking the event via the enabler */ -user_event_put (enabler->event, locked); +user_event_put (enabler->event, false); +/* Run from queue_rcu_work(), the RCU grace period has elapsed */ kfree (enabler); } +static void user_event_enabler_destroy (struct user_event_enabler *enabler) +{ +list_del_rcu (&enabler->mm_enablers_link); + +/* +* The enabler is removed from an RCU-traversed list +* (user_event_mm_dup() walks mm->enablers under rcu_read_lock() only), +* and readers there dereference enabler->event and take a new ref on +* it. Both the put of that event reference and the free of the enabler +* therefore have to wait for a grace period so no reader can be looking +* at the enabler or racing the last put of its event. +* +* The put itself must not run in RCU context: when it drops the last +* reference user_event_put() takes event_mutex, which cannot be taken +* from a softirq/RCU callback. Defer both to a work item scheduled +* after a grace period via queue_rcu_work(). +*/ +INIT_RCU_WORK (&enabler->put_rwork, delayed_user_event_enabler_put); +queue_rcu_work (system_percpu_wq, &enabler->put_rwork); +} +static int user_event_mm_fault_in (struct user_event_mm *mm,unsigned long uaddr,int attempt) {
@@ -464,7 +489,7 @@ static void user_event_enabler_fault_fix/* User asked for enabler to be removed during fault */ if (test_bit (ENABLE_VAL_FREEING_BIT ,ENABLE_BITOPS (enabler))) { -user_event_enabler_destroy (enabler, true); +user_event_enabler_destroy (enabler); goto out; }
@@ -764,7 +789,7 @@ static void user_event_mm_destroy(structstruct user_event_enabler *enabler, *next;list_for_each_entry_safe (enabler, next, &mm->enablers, mm_enablers_link) -user_event_enabler_destroy (enabler, false); +user_event_enabler_destroy (enabler);mmdrop (mm->mm);kfree (mm);
@@ -2645,7 +2670,7 @@ static long user_events_ioctl_unreg(unsi flags |= enabler->values &ENABLE_VAL_COMPAT_MASK ;if (!test_bit (ENABLE_VAL_FAULTING_BIT ,ENABLE_BITOPS (enabler))) -user_event_enabler_destroy (enabler, true); +user_event_enabler_destroy (enabler);/* Removed at least one */ ret =0 ;
Greg Kroah-Hartman
maintainerPATCH 1895/2077 wifi: libertas_tf: fix use-after-free in lbtf_free_adapter()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Maoyi Xie <[email protected]>
commit aa6dcd5c8dd9ba1d7d0f60093bcda41c0d6d438d upstream.
lbtf_free_adapter() calls timer_delete(&priv->command_timer), which does not wait for a running command_timer_fn() callback. lbtf_free_adapter() runs on the teardown path right before ieee80211_free_hw() frees priv, both in lbtf_remove_card() and in the probe error path. command_timer is armed by mod_timer() in lbtf_cmd() whenever a firmware command is sent. command_timer_fn() dereferences priv. If a command times out as the device is removed, command_timer_fn() runs concurrently with teardown and dereferences priv after it has been freed.
This is the same use-after-free that commit 03cc8f90d053 ("wifi: libertas:
fix use-after-free in lbs_free_adapter()") fixed in the sibling libertas driver. The libertas_tf variant has the identical pattern and was left unchanged. Use timer_delete_sync() so any in-flight callback completes before priv is freed.
--- drivers/net/wireless/marvell/libertas_tf/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/marvell/libertas_tf/main.c +1 -1
@@ -174,7 +174,7 @@ static void lbtf_free_adapter(struct lbt {lbtf_deb_enter (LBTF_DEB_MAIN );lbtf_free_cmd_buffer (priv); -timer_delete (&priv->command_timer); +timer_delete_sync (&priv->command_timer);lbtf_deb_leave (LBTF_DEB_MAIN ); }
Greg Kroah-Hartman
maintainerPATCH 1896/2077 locking/rt: Fix the incorrect RCU protection in rt_spin_unlock()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Thomas Gleixner <[email protected]>
commit 89038cc87d80c77e7aa6f42a64b2573b74af339f upstream.
rt_spin_unlock() releases the RCU protection before unlocking the lock. That opens the door for the following UAF scenario:
T1 T2
spin_lock(&p->lock); rcu_read_lock();
invalidate(p); p = rcu_dereference(ptr);
rcu_assign_pointer(ptr, NULL); if (!p) return;
spin_unlock(&p->lock); spin_lock(&p->lock)
lock(&lock->lock); rcu_read_lock(); kfree_rcu(p); rcu_read_unlock(); .... spin_unlock(&p->lock) rcu_read_unlock(); // Ends grace period rcu_do_batch() kfree(p); UAF -> rt_mutex_cmpxchg_release(&lock->lock...)
Regular spinlocks keep preemption disabled accross the unlock operation, which provides full RCU protection, but the RT substitution fails to resemble that. Same applies for the rwlock substitution.
Move the rcu_read_unlock() invocation past the unlock operations to match the non-RT semantics. This makes it asymmetric vs. rt_xxx_lock(), but that's harmless as the caller needs to hold RCU read lock across the lock operation. The migrate_enable() call stays before the unlock operation because there is no per CPU operation in the unlock path which would require migration to be kept disabled.
--- kernel/locking/spinlock_rt.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/locking/spinlock_rt.c +24 -3
@@ -79,10 +79,27 @@ void __sched rt_spin_unlock(spinlock_t * {spin_release (&lock->dep_map, _RET_IP_);migrate_enable (); -rcu_read_unlock ();if (unlikely (!rt_mutex_cmpxchg_release (&lock->lock, current,NULL )))rt_mutex_slowunlock (&lock->lock); + +/* +* This must be last to prevent the following UAF: +* +* T1 T2 +* spin_lock(&p->lock); rcu_read_lock(); +* invalidate(p); p = rcu_dereference(ptr); +* rcu_assign_pointer(ptr, NULL); if (!p) return; +* spin_unlock(&p->lock); spin_lock(&p->lock); +* kfree_rcu(p); rcu_read_unlock(); +* .... +* spin_unlock(&p->lock) +* rcu_read_unlock(); // Ends grace period +* rcu_do_batch() +* kfree(p); +* UAF -> rt_mutex_cmpxchg_release(&p->lock.lock...) +*/ +rcu_read_unlock (); }EXPORT_SYMBOL (rt_spin_unlock);
@@ -262,17 +279,21 @@ void __sched rt_read_unlock(rwlock_t *rw {rwlock_release (&rwlock->dep_map, _RET_IP_);migrate_enable (); -rcu_read_unlock ();rwbase_read_unlock (&rwlock->rwbase,TASK_RTLOCK_WAIT ); + +/* This must be last. See comment in rt_spin_unlock() */ +rcu_read_unlock (); }EXPORT_SYMBOL (rt_read_unlock); void __sched rt_write_unlock(rwlock_t *rwlock) __releases(RCU) {rwlock_release (&rwlock->dep_map, _RET_IP_); -rcu_read_unlock ();migrate_enable ();rwbase_write_unlock (&rwlock->rwbase); + +/* This must be last. See comment in rt_spin_unlock() */ +rcu_read_unlock (); }EXPORT_SYMBOL (rt_write_unlock);
Greg Kroah-Hartman
maintainerPATCH 1897/2077 posix-cpu-timers: Use u64 multiplication in update_rlimit_cpu()
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Zhan Xusheng <[email protected]>
commit 26aff38fefb1d6cd87e22525f41cc8f1aa61b24f upstream.
update_rlimit_cpu() converts the RLIMIT_CPU value to nanoseconds with
u64 nsecs = rlim_new * NSEC_PER_SEC;
On 32-bit kernels both rlim_new (unsigned long) and NSEC_PER_SEC (1000000000L) are 32-bit, so the multiplication is performed in unsigned long and truncated for rlim_new > 4 seconds before being widened to u64.
The same file already casts to u64 for the matching computation in check_process_timers():
u64 softns = (u64)soft * NSEC_PER_SEC;
As a result, the truncated value is installed into the CPUCLOCK_PROF expiry cache (nextevt), causing the process CPU timer to be programmed to fire prematurely for any RLIMIT_CPU soft limit >= 5 seconds. The actual SIGXCPU/SIGKILL decision in check_process_timers() already casts to u64 and is therefore correct, so limit enforcement is not broken; only the expiry-cache programming is wrong. Apply the same cast here so both paths convert rlim_cur identically.
64-bit kernels are unaffected.
--- kernel/time/posix-cpu-timers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +1 -1
@@ -41,7 +41,7 @@ void posix_cputimers_group_init(struct p*/ int update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new) { -u64 nsecs = rlim_new *NSEC_PER_SEC ; +u64 nsecs = (u64 )rlim_new *NSEC_PER_SEC ;unsigned long irq_fl;if (!lock_task_sighand (task, &irq_fl))
Greg Kroah-Hartman
maintainerPATCH 1898/2077 selftests/ftrace: Drop invalid top-level local in test_ownership
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Cao Ruichuang <[email protected]>
commit 808c447df2fe234eb7d9e08ecf53159d291c104c upstream.
test_ownership.tc is sourced by ftracetest under /bin/sh.
The script currently declares mount_point with local at file scope, which makes /bin/sh abort with "local: not in a function" before the test can reach the eventfs ownership checks.
Replace the top-level local declaration with a normal shell variable so kernels that support the gid= tracefs mount option can run the test at all.
--- tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc +1 -1
@@ -6,7 +6,7 @@ original_group=`stat -c "%g" .` original_owner=`stat -c "%u" .` -local mount_point=$(get_mount_point) +mount_point=$(get_mount_point) mount_options=$(get_mnt_options "$mount_point")
Greg Kroah-Hartman
maintainerPATCH 1899/2077 cpu: hotplug: Preserve per instance callback errors
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Bradley Morgan <[email protected]>
commit 673db10729fb121ea1b16fe57791a0cb9eac1eb5 upstream.
cpuhp_invoke_callback() unwinds earlier callbacks for the same hotplug state when one instance fails. The rollback path currently reuses ret, so a successful rollback can hide the original error and make the failed transition look successful.
Keep the rollback result separate from the original error.
--- kernel/cpu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/cpu.c +4 -4
@@ -174,7 +174,7 @@ static int cpuhp_invoke_callback(unsignestruct cpuhp_step *step =cpuhp_get_step (state);int (*cbm)(unsigned int cpu,struct hlist_node *node);int (*cb)(unsigned int cpu); -int ret, cnt; +int ret, cnt, rollback_ret;if (st->fail == state) { st->fail =CPUHP_INVALID ;
@@ -238,12 +238,12 @@ err:break ;trace_cpuhp_multi_enter (cpu, st->target, state, cbm, node); - ret =cbm (cpu, node); -trace_cpuhp_exit (cpu, st->state, state, ret); + rollback_ret =cbm (cpu, node); +trace_cpuhp_exit (cpu, st->state, state, rollback_ret);/* * Rollback must not fail, */ -WARN_ON_ONCE (ret); +WARN_ON_ONCE (rollback_ret); }return ret; }
Greg Kroah-Hartman
maintainerPATCH 1900/2077 cpu: hotplug: Bound hotplug states sysfs output
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Bradley Morgan <[email protected]>
commit 86f436567f2516a0083b210bedc933544826a2c3 upstream.
states_show() adds CPU hotplug state names into a single sysfs buffer using sprintf(). With enough registered states, this can write past the end of the PAGE_SIZE buffer.
Use sysfs_emit_at() so output is bounded.
--- kernel/cpu.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
kernel/cpu.c +4 -8
@@ -2854,21 +2854,17 @@ static const struct attribute_group cpuh .name = "hotplug", }; -static ssize_t states_show (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t states_show (struct device *dev,struct device_attribute *attr,char *buf) { -ssize_t cur, res =0 ; +ssize_t res =0 ;int i;mutex_lock (&cpuhp_state_mutex);for (i =CPUHP_OFFLINE ; i <=CPUHP_ONLINE ; i++) {struct cpuhp_step *sp =cpuhp_get_step (i); -if (sp->name) { - cur =sprintf (buf,"%3d: %s\n" , i, sp->name); - buf += cur; - res += cur; - } +if (sp->name) + res +=sysfs_emit_at (buf, res,"%3d: %s\n" , i, sp->name); }mutex_unlock (&cpuhp_state_mutex);return res;
Greg Kroah-Hartman
maintainerPATCH 1901/2077 net: mana: Validate the packet length reported by the NIC
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dexuan Cui <[email protected]>
commit 2e2a83b4998af4384e677d3b2ac08565274279bf upstream.
Validate the packet length reported in the RX CQE before passing it to skb processing. The CQE is supplied by the NIC device and should not be blindly trusted.
--- drivers/net/ethernet/microsoft/mana/mana_en.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_en.c +18 -5
@@ -2182,12 +2182,25 @@ static void mana_process_rx_cqe(struct m rxbuf_oob = &rxq->rx_oobs[curr];WARN_ON_ONCE (rxbuf_oob->wqe_inf.wqe_size_in_bu !=1 ); -mana_refill_rx_oob (dev, rxq, rxbuf_oob, &old_buf, &old_fp); +if (unlikely (pktlen > rxq->datasize)) { +/* Increase it even if mana_rx_skb() isn't called. */ + rxq->rx_cq.work_done++; -/* Unsuccessful refill will have old_buf == NULL. -* In this case, mana_rx_skb() will drop the packet. -*/ -mana_rx_skb (old_buf, old_fp, oob, rxq, i); + ++ndev->stats.rx_dropped; +netdev_warn_once (ndev, +"Dropped oversized RX packet: len=%u, datasize=%u\n" , + pktlen, rxq->datasize); + +/* Reuse the RX buffer since rxbuf_oob is unchanged. */ + }else { + +mana_refill_rx_oob (dev, rxq, rxbuf_oob, &old_buf, &old_fp); + +/* Unsuccessful refill will have old_buf == NULL. +* In this case, mana_rx_skb() will drop the packet. +*/ +mana_rx_skb (old_buf, old_fp, oob, rxq, i); + }mana_move_wq_tail (rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
Greg Kroah-Hartman
maintainerPATCH 1902/2077 net: mana: Sync page pool RX frags for CPU
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Dexuan Cui <[email protected]>
commit c72a0f09c57f92113df69f9b902d11c9e4b132f5 upstream.
MANA allocates RX buffers from page pool fragments when frag_count is greater than 1. In that case the buffers remain DMA mapped by page pool and the RX completion path does not call dma_unmap_single(). As a result, the implicit sync-for-CPU normally performed by dma_unmap_single() is missing before the packet data is passed to the networking stack.
This breaks RX on configurations which require explicit DMA syncing, for example when booted with swiotlb=force.
Fix this by recording the page pool page and DMA sync offset when the RX buffer is allocated, and syncing the received packet range for CPU access before handing the RX buffer to the stack.
--- drivers/net/ethernet/microsoft/mana/mana_en.c | 40 +++++++++++++++++++++----- include/net/mana/mana.h | 8 +++++ 2 files changed, 41 insertions(+), 7 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_en.c +33 -7
@@ -2056,12 +2056,16 @@ drop: }static void *mana_get_rxfrag (struct mana_rxq *rxq,struct device *dev, -dma_addr_t *da,bool *from_pool) +dma_addr_t *da,bool *from_pool, +struct page **pp_page,u32 *dma_sync_offset) {struct page *page;u32 offset;void *va; + *from_pool = false; + *pp_page =NULL ; + *dma_sync_offset =0 ;/* Don't use fragments for jumbo frames or XDP where it's 1 fragment * per page.
@@ -2099,31 +2103,47 @@ static void *mana_get_rxfrag(struct mana va =page_to_virt (page) + offset; *da =page_pool_get_dma_addr (page) + offset + rxq->headroom; *from_pool = true; + *pp_page = page; + *dma_sync_offset = offset + rxq->headroom;return va; }/* Allocate frag for rx buffer, and save the old buf */ static void mana_refill_rx_oob (struct device *dev,struct mana_rxq *rxq, -struct mana_recv_buf_oob *rxoob,void **old_buf, -bool *old_fp) +struct mana_recv_buf_oob *rxoob,u32 pktlen, +void **old_buf,bool *old_fp) { +struct page *pp_page; +u32 dma_sync_offset;bool from_pool;dma_addr_t da;void *va; - va =mana_get_rxfrag (rxq, dev, &da, &from_pool); + va =mana_get_rxfrag (rxq, dev, &da, &from_pool, &pp_page, + &dma_sync_offset);if (!va)return ; -if (!rxoob->from_pool || rxq->frag_count ==1 ) +if (!rxoob->from_pool || rxq->frag_count ==1 ) {dma_unmap_single (dev, rxoob->sgl[0 ].address, rxq->datasize,DMA_FROM_DEVICE ); + }else { +/* The page pool maps the whole page and only syncs for device +* automatically (PP_FLAG_DMA_SYNC_DEV). Sync the received bytes +* for the CPU before they are read: this is required if DMA +* is incoherent or bounce buffers are used. +*/ +page_pool_dma_sync_for_cpu (rxq->page_pool, rxoob->pp_page, + rxoob->dma_sync_offset, pktlen); + } *old_buf = rxoob->buf_va; *old_fp = rxoob->from_pool; rxoob->buf_va = va; rxoob->sgl[0 ].address = da; rxoob->from_pool = from_pool; + rxoob->pp_page = pp_page; + rxoob->dma_sync_offset = dma_sync_offset; } static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
@@ -2194,7 +2214,8 @@ static void mana_process_rx_cqe(struct m/* Reuse the RX buffer since rxbuf_oob is unchanged. */ }else { -mana_refill_rx_oob (dev, rxq, rxbuf_oob, &old_buf, &old_fp); +mana_refill_rx_oob (dev, rxq, rxbuf_oob, pktlen, + &old_buf, &old_fp);/* Unsuccessful refill will have old_buf == NULL. * In this case, mana_rx_skb() will drop the packet.
@@ -2592,6 +2613,8 @@ static int mana_fill_rx_oob(struct mana_ struct mana_rxq *rxq, struct device *dev) { struct mana_port_context *mpc = netdev_priv(rxq->ndev); +struct page *pp_page =NULL ; +u32 dma_sync_offset =0 ;bool from_pool = false;dma_addr_t da;void *va;
@@ -2599,13 +2622,16 @@ static int mana_fill_rx_oob(struct mana_if (mpc->rxbufs_pre) va =mana_get_rxbuf_pre (rxq, &da);else - va =mana_get_rxfrag (rxq, dev, &da, &from_pool); + va =mana_get_rxfrag (rxq, dev, &da, &from_pool, &pp_page, + &dma_sync_offset);if (!va)return -ENOMEM ; rx_oob->buf_va = va; rx_oob->from_pool = from_pool; + rx_oob->pp_page = pp_page; + rx_oob->dma_sync_offset = dma_sync_offset; rx_oob->sgl[0 ].address = da; rx_oob->sgl[0 ].size = rxq->datasize;
include/net/mana/mana.h +8 -0
@@ -305,6 +305,14 @@ struct mana_recv_buf_oob {void *buf_va;bool from_pool;/* allocated from a page pool */ +/* head page of the page_pool fragment; valid only when +* from_pool && frag_count > 1. +*/ +struct page *pp_page; +/* Fragment offset plus rxq->headroom, passed to +* page_pool_dma_sync_for_cpu(). +*/ +u32 dma_sync_offset;/* SGL of the buffer going to be sent as part of the work request. */ u32 num_sge;
Greg Kroah-Hartman
maintainerPATCH 1903/2077 gpio: mt7621: more robust management of IRQ domain teardown
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Sergio Paracuellos <[email protected]>
commit 839738536adabae1a7e98ed3fc332ce9cc991d27 upstream.
The driver uses devm_gpiochip_add_data() to register the GPIO chips which means the devres subsystem will unregister them only after the function 'mt7621_gpio_remove()' returns. During the window between domain destruction and devres unregistering the GPIO chips, the chips are still fully active. If a consumer or userspace invokes gpiod_to_irq() during this window, 'mt7621_gpio_to_irq()' can dereference the already-freed irq domain pointer. Thus, manage the IRQ domain teardown using 'devm_add_action_or_reset()' to guarantee it is destroyed strictly after the GPIO chips are removed.
--- drivers/gpio/gpio-mt7621.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/gpio/gpio-mt7621.c +7 -8
@@ -270,9 +270,9 @@ static const struct irq_chip mt7621_irq_ };static void -mt7621_gpio_remove (struct platform_device *pdev) +mt7621_gpio_remove (void *data) { -struct mtk *priv =platform_get_drvdata (pdev); +struct mtk *priv = data;int offset, virq;if (priv->gpio_irq >0 )
@@ -473,14 +473,14 @@ mediatek_gpio_probe(struct platform_deviif (mtk->gpio_irq >0 ) { ret =mt7621_gpio_irq_setup (pdev, mtk);if (ret) - goto fail; +return ret; } -return 0 ; + ret =devm_add_action_or_reset (dev, mt7621_gpio_remove, mtk); +if (ret) +return ret; -fail: -mt7621_gpio_remove (pdev); -return ret; +return 0 ; }static const struct of_device_id mediatek_gpio_match[] = {
@@ -491,7 +491,6 @@ MODULE_DEVICE_TABLE(of, mediatek_gpio_mastatic struct platform_driver mediatek_gpio_driver = { .probe = mediatek_gpio_probe, - .remove = mt7621_gpio_remove, .driver = { .name ="mt7621_gpio" , .of_match_table = mediatek_gpio_match,
Greg Kroah-Hartman
maintainerPATCH 1904/2077 gpio: tegra: do not call pinctrl for GPIO direction
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Runyu Xiao <[email protected]>
commit d3e91a95b2b0fc6336dbf3ec90d831a1654d2720 upstream.
tegra_gpio_direction_input() and tegra_gpio_direction_output() already program the GPIO controller direction registers directly. The additional pinctrl_gpio_direction_input/output() calls do not add a Tegra pinctrl operation, because the Tegra pinmux ops provide GPIO request/free handling but no gpio_set_direction hook.
The extra call still enters the pinctrl core and takes pctldev->mutex. Shared GPIO users can call the direction path while holding their per-line spinlock, so this otherwise redundant pinctrl direction call can sleep in an atomic context.
This was found by our static analysis tool and then confirmed by manual review of tegra_gpio_probe(), the Tegra GPIO direction callbacks and the Tegra pinctrl ops. The reviewed path has a default non-sleeping struct gpio_chip while the direction callback still enters the pinctrl mutex path.
A directed runtime validation kept the same non-sleeping chip registration and drove:
gpio_shared_proxy_direction_output()
gpiod_direction_output_raw_commit()
tegra_gpio_direction_output()
pinctrl_gpio_direction_output()
Lockdep reported a sleep-in-atomic warning with the shared GPIO spinlock held and pinctrl_get_device_gpio_range() plus tegra_gpio_direction_output() on the stack.
Do not mark the whole chip as can_sleep to paper over this: can_sleep describes whether get()/set() may sleep, and Tegra value access is MMIO. Remove the redundant pinctrl direction calls and keep pinctrl involvement in the existing request/free path.
--- drivers/gpio/gpio-tegra.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
drivers/gpio/gpio-tegra.c +2 -16
@@ -172,18 +172,11 @@ static int tegra_gpio_direction_input(st unsigned int offset) {struct tegra_gpio_info *tgi =gpiochip_get_data (chip); -int ret;tegra_gpio_mask_write (tgi,GPIO_MSK_OE (tgi, offset), offset,0 );tegra_gpio_enable (tgi, offset); - ret =pinctrl_gpio_direction_input (chip, offset); -if (ret <0 ) -dev_err (tgi->dev, -"Failed to set pinctrl input direction of GPIO %d: %d" , - chip->base + offset, ret); - -return ret; +return 0 ; } static int tegra_gpio_direction_output(struct gpio_chip *chip,
@@ -191,19 +184,12 @@ static int tegra_gpio_direction_output(s int value) {struct tegra_gpio_info *tgi =gpiochip_get_data (chip); -int ret;tegra_gpio_set (chip, offset, value);tegra_gpio_mask_write (tgi,GPIO_MSK_OE (tgi, offset), offset,1 );tegra_gpio_enable (tgi, offset); - ret =pinctrl_gpio_direction_output (chip, offset); -if (ret <0 ) -dev_err (tgi->dev, -"Failed to set pinctrl output direction of GPIO %d: %d" , - chip->base + offset, ret); - -return ret; +return 0 ; } static int tegra_gpio_get_direction(struct gpio_chip *chip,
Greg Kroah-Hartman
maintainerPATCH 1905/2077 gpio: mt7621: be sure IRQ domain is created before exposing GPIO chips
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Sergio Paracuellos <[email protected]>
commit 0e024f58291dfcb28d98c512002e1a80fad69798 upstream.
Function 'mediatek_gpio_bank_probe()' registers three GPIO chips using 'devm_gpiochip_add_data()'. At this point, the chips become live and visible to consumers. However, the IRQ domain isn't allocated and set up until 'mt7621_gpio_irq_setup()' is called after the GPIO chips setup finishes. If a consumer requests a GPIO IRQ concurrently 'mt7621_gpio_to_irq()' can be called and pass a NULL irq domain pointer irq_create_mapping(), that can corrupt the mappings or cause a crash. Fix this possible problem seting up irq domain before GPIO chips setup is performed.
--- drivers/gpio/gpio-mt7621.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/gpio/gpio-mt7621.c +6 -6
@@ -464,12 +464,6 @@ mediatek_gpio_probe(struct platform_devi mtk->num_gpios =MTK_BANK_WIDTH *MTK_BANK_CNT ;platform_set_drvdata (pdev, mtk); -for (i =0 ; i <MTK_BANK_CNT ; i++) { - ret =mediatek_gpio_bank_probe (dev, i); -if (ret) -return ret; - } -if (mtk->gpio_irq >0 ) { ret =mt7621_gpio_irq_setup (pdev, mtk);if (ret)
@@ -480,6 +474,12 @@ mediatek_gpio_probe(struct platform_deviif (ret)return ret; +for (i =0 ; i <MTK_BANK_CNT ; i++) { + ret =mediatek_gpio_bank_probe (dev, i); +if (ret) +return ret; + } +return 0 ; }
Greg Kroah-Hartman
maintainerPATCH 1906/2077 gpio-f7188x: Add support for NCT6126D version B
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Paul Louvel <[email protected]>
commit 9a6c0b6ea12746d50cf53d59a7e05fd83f974bda upstream.
The Nuvoton NCT6126D Super-I/O is available in two hardware revisions. According to the manufacturer datasheet revision 2.4, version A reports chip ID 0xD283, while version B reports chip ID 0xD284.
The driver currently only recognizes only the version A ID. Version B only contains hardware fixes unrelated to the GPIO functionality, so it can be supported by simply adding its chip ID without any other driver changes.
--- drivers/gpio/gpio-f7188x.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpio/gpio-f7188x.c +4 -2
@@ -48,7 +48,8 @@/* * Nuvoton devices. */ -#define SIO_NCT6126D_ID 0xD283/* NCT6126D chipset ID */ +#define SIO_NCT6126D_VER_A_ID 0xD283/* NCT6126D version A chipset ID */ +#define SIO_NCT6126D_VER_B_ID 0xD284/* NCT6126D version B chipset ID */ #define SIO_LD_GPIO_NUVOTON 0x07/* GPIO logical device */
@@ -564,7 +565,8 @@ static int __init f7188x_find(int addr,case SIO_F81865_ID : sio->type = f81865;break ; -case SIO_NCT6126D_ID : +case SIO_NCT6126D_VER_A_ID : +case SIO_NCT6126D_VER_B_ID : sio->device =SIO_LD_GPIO_NUVOTON ; sio->type = nct6126d;break ;
Greg Kroah-Hartman
maintainerPATCH 1907/2077 gpio: mt7621: avoid corruption of shared interrupt trigger state
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Sergio Paracuellos <[email protected]>
commit 1781172526d1092323af443fa03f00e6de560401 upstream.
The bank-shared fields like 'rising' and 'falling' are modified using non-atomic read-modify-write operations. Since every gpio chip instance represents an entire bank of 32 pins, if 'mediatek_gpio_irq_type()' is called concurrently for different IRQs on the same bank a possible overwrite of each other's configuration is possible. Thus, protect this state with 'gpio_generic_lock_irqsave' lock in the same way it is handled in irp_chip 'mediatek_gpio_irq_mask()' and 'mediatek_gpio_irq_unmask()' callbacks.
--- drivers/gpio/gpio-mt7621.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpio/gpio-mt7621.c +2 -0
@@ -187,6 +187,8 @@ mediatek_gpio_irq_type(struct irq_data *struct mtk_gc *rg =gpiochip_get_data (gc);u32 mask =BIT (mt7621_gpio_hwirq_to_offset (d->hwirq, rg)); +guard (gpio_generic_lock_irqsave)(&rg->chip); +if (type ==IRQ_TYPE_PROBE ) {if ((rg->rising | rg->falling | rg->hlevel | rg->llevel) & mask)
Greg Kroah-Hartman
maintainerPATCH 1908/2077 gpios: palmas: add .get_direction() op
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Andreas Kemnade <[email protected]>
commit db4a79713ed8e252d5e4edf6eaaa80948b6855a2 upstream.
Accessing debug/gpio is quite noisy without a get_direction() implementation. To calm that down add an implementation.
--- drivers/gpio/gpio-palmas.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/gpio/gpio-palmas.c +19 -0
@@ -116,6 +116,24 @@ static int palmas_gpio_input(struct gpioreturn ret; } +static int palmas_gpio_get_direction (struct gpio_chip *gc,unsigned int offset) +{ +struct palmas_gpio *pg =gpiochip_get_data (gc); +struct palmas *palmas = pg->palmas; +unsigned int val; +unsigned int reg; +int ret; +int gpio16 = (offset/8 ); + + offset %=8 ; + reg = (gpio16) ?PALMAS_GPIO_DATA_DIR2 :PALMAS_GPIO_DATA_DIR ; + ret =palmas_read (palmas,PALMAS_GPIO_BASE , reg, &val); +if (ret) +return ret; + +return (val &BIT (offset)) ?GPIO_LINE_DIRECTION_OUT :GPIO_LINE_DIRECTION_IN ; +} +static int palmas_gpio_to_irq (struct gpio_chip *gc,unsigned offset) {struct palmas_gpio *pg =gpiochip_get_data (gc);
@@ -165,6 +183,7 @@ static int palmas_gpio_probe(struct plat palmas_gpio->gpio_chip.can_sleep = true; palmas_gpio->gpio_chip.direction_input = palmas_gpio_input; palmas_gpio->gpio_chip.direction_output = palmas_gpio_output; + palmas_gpio->gpio_chip.get_direction = palmas_gpio_get_direction; palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq; palmas_gpio->gpio_chip.set = palmas_gpio_set; palmas_gpio->gpio_chip.get = palmas_gpio_get;
Greg Kroah-Hartman
maintainerPATCH 1909/2077 net: sit: require CAP_NET_ADMIN in the device netns for changelink
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Maoyi Xie <[email protected]>
commit 27ccb68e7cccead5d8c611665a45d23032d468b3 upstream.
ipip6_changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Gate ipip6_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed. sit was the one tunnel type not covered by the recent series that added this check to the other changelink() handlers.
--- net/ipv6/sit.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv6/sit.c +3 -0
@@ -1611,6 +1611,9 @@ static int ipip6_changelink(struct net_d__u32 fwmark = t->fwmark;int err; +if (!rtnl_dev_link_net_capable (dev, net)) +return -EPERM ; +if (dev == sitn->fb_tunnel_dev)return -EINVAL ;
Greg Kroah-Hartman
maintainerPATCH 1910/2077 net: ethernet: ti: icssg: guard PA stat lookups
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Philippe Schenker <[email protected]>
commit 27b9daba50609335db6ca81e4cccf50ded21ec76 upstream.
icssg_ndo_get_stats64() unconditionally calls emac_get_stat_by_name() with FW PA stat names regardless of whether the PA stats block is present on the hardware. emac_get_stat_by_name() already guards the PA stats lookup with if (emac->prueth->pa_stats); when that pointer is NULL the lookup falls through to netdev_err() and returns -EINVAL. Because ndo_get_stats64 is polled regularly by the networking stack this produces thousands of log entries of the form:
icssg-prueth icssg1-eth end0: Invalid stats FW_RX_ERROR
A secondary consequence is that the int(-EINVAL) return value is implicitly widened to a near-ULLONG_MAX unsigned value when accumulated into the __u64 fields of rtnl_link_stats64, silently corrupting the rx_errors, rx_dropped and tx_dropped counters reported by ip -s link.
Every other PA-aware code path in the driver is already guarded with the same if (emac->prueth->pa_stats) check. Apply the same guard here.
--- drivers/net/ethernet/ti/icssg/icssg_common.c | 49 +++++++++++++++------------ 1 file changed, 28 insertions(+), 21 deletions(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +28 -21
@@ -1651,28 +1651,35 @@ void icssg_ndo_get_stats64(struct net_de stats->rx_over_errors =emac_get_stat_by_name (emac,"rx_over_errors" ); stats->multicast =emac_get_stat_by_name (emac,"rx_multicast_frames" ); - stats->rx_errors = ndev->stats.rx_errors + -emac_get_stat_by_name (emac,"FW_RX_ERROR" ) + -emac_get_stat_by_name (emac,"FW_RX_EOF_SHORT_FRMERR" ) + -emac_get_stat_by_name (emac,"FW_RX_B0_DROP_EARLY_EOF" ) + -emac_get_stat_by_name (emac,"FW_RX_EXP_FRAG_Q_DROP" ) + -emac_get_stat_by_name (emac,"FW_RX_FIFO_OVERRUN" ); - stats->rx_dropped = ndev->stats.rx_dropped + -emac_get_stat_by_name (emac,"FW_DROPPED_PKT" ) + -emac_get_stat_by_name (emac,"FW_INF_PORT_DISABLED" ) + -emac_get_stat_by_name (emac,"FW_INF_SAV" ) + -emac_get_stat_by_name (emac,"FW_INF_SA_DL" ) + -emac_get_stat_by_name (emac,"FW_INF_PORT_BLOCKED" ) + -emac_get_stat_by_name (emac,"FW_INF_DROP_TAGGED" ) + -emac_get_stat_by_name (emac,"FW_INF_DROP_PRIOTAGGED" ) + -emac_get_stat_by_name (emac,"FW_INF_DROP_NOTAG" ) + -emac_get_stat_by_name (emac,"FW_INF_DROP_NOTMEMBER" ); + stats->rx_errors = ndev->stats.rx_errors; + stats->rx_dropped = ndev->stats.rx_dropped; stats->tx_errors = ndev->stats.tx_errors; - stats->tx_dropped = ndev->stats.tx_dropped + -emac_get_stat_by_name (emac,"FW_RTU_PKT_DROP" ) + -emac_get_stat_by_name (emac,"FW_TX_DROPPED_PACKET" ) + -emac_get_stat_by_name (emac,"FW_TX_TS_DROPPED_PACKET" ) + -emac_get_stat_by_name (emac,"FW_TX_JUMBO_FRM_CUTOFF" ); + stats->tx_dropped = ndev->stats.tx_dropped; + +if (!emac->prueth->pa_stats) +return ; + + stats->rx_errors += +emac_get_stat_by_name (emac,"FW_RX_ERROR" ) + +emac_get_stat_by_name (emac,"FW_RX_EOF_SHORT_FRMERR" ) + +emac_get_stat_by_name (emac,"FW_RX_B0_DROP_EARLY_EOF" ) + +emac_get_stat_by_name (emac,"FW_RX_EXP_FRAG_Q_DROP" ) + +emac_get_stat_by_name (emac,"FW_RX_FIFO_OVERRUN" ); + stats->rx_dropped += +emac_get_stat_by_name (emac,"FW_DROPPED_PKT" ) + +emac_get_stat_by_name (emac,"FW_INF_PORT_DISABLED" ) + +emac_get_stat_by_name (emac,"FW_INF_SAV" ) + +emac_get_stat_by_name (emac,"FW_INF_SA_DL" ) + +emac_get_stat_by_name (emac,"FW_INF_PORT_BLOCKED" ) + +emac_get_stat_by_name (emac,"FW_INF_DROP_TAGGED" ) + +emac_get_stat_by_name (emac,"FW_INF_DROP_PRIOTAGGED" ) + +emac_get_stat_by_name (emac,"FW_INF_DROP_NOTAG" ) + +emac_get_stat_by_name (emac,"FW_INF_DROP_NOTMEMBER" ); + stats->tx_dropped += +emac_get_stat_by_name (emac,"FW_RTU_PKT_DROP" ) + +emac_get_stat_by_name (emac,"FW_TX_DROPPED_PACKET" ) + +emac_get_stat_by_name (emac,"FW_TX_TS_DROPPED_PACKET" ) + +emac_get_stat_by_name (emac,"FW_TX_JUMBO_FRM_CUTOFF" ); }EXPORT_SYMBOL_GPL (icssg_ndo_get_stats64);
Greg Kroah-Hartman
maintainerPATCH 1911/2077 net: wwan: t7xx: destroy DMA pool on CLDMA late init failure
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Haoxiang Li <[email protected]>
commit 2bd6f26d4ce1e87de4d736b1e8896daf3acf1c0e upstream.
t7xx_cldma_late_init() creates md_ctrl->gpd_dmapool before initializing the TX and RX rings. If any ring initialization fails, the error path frees the already initialized rings but leaves the DMA pool allocated.
Destroy md_ctrl->gpd_dmapool on the late-init failure path to avoid leaking the DMA pool.
--- drivers/net/wwan/t7xx/t7xx_hif_cldma.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/net/wwan/t7xx/t7xx_hif_cldma.c +3 -0
@@ -1063,6 +1063,9 @@ err_free_tx_ring:while (i--)t7xx_cldma_ring_free (md_ctrl, &md_ctrl->tx_ring[i],DMA_TO_DEVICE ); +dma_pool_destroy (md_ctrl->gpd_dmapool); + md_ctrl->gpd_dmapool =NULL ; +return ret; }
Greg Kroah-Hartman
maintainerPATCH 1912/2077 net: ixp4xx_hss: fix duplicate HDLC netdev allocation
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Haoxiang Li <[email protected]>
commit db818b0e8af7bac16860116a19c341a63d6677b4 upstream.
ixp4xx_hss_probe() allocates two HDLC netdevs. The first one is stored in ndev, initialized, and registered with register_hdlc_device(). The second one is stored in port->netdev and later used by the remove path for unregister_hdlc_device() and free_netdev().
This means that the registered netdev is not the same object that is unregistered and freed on remove. It also leaks the first allocation if the second alloc_hdlcdev() call fails, and the first allocation is not checked before ndev is used.
Older code allocated the HDLC netdev only once and stored the same object in both the local variable and port->netdev. The buggy conversion split this into two alloc_hdlcdev() calls. A later rename changed the local variable name to ndev, but the underlying mismatch remained.
Fix this by allocating the HDLC netdev only once and assigning the same object to port->netdev.
--- drivers/net/wan/ixp4xx_hss.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/wan/ixp4xx_hss.c +2 -2
@@ -1487,11 +1487,11 @@ static int ixp4xx_hss_probe(struct platf "unable to get CLK internal GPIO\n"); ndev =alloc_hdlcdev (port); - port->netdev =alloc_hdlcdev (port); -if (!port->netdev) { +if (!ndev) { err = -ENOMEM ; goto err_plat; } + port->netdev = ndev;SET_NETDEV_DEV (ndev, &pdev->dev); hdlc =dev_to_hdlc (ndev);
Greg Kroah-Hartman
maintainerPATCH 1913/2077 net/sched: act_ct: preserve tc_skb_cb across defragmentation
In reply to Greg Kroah-Hartman7.1-stable review patch. If anyone has any objections, please let me know.
------------------
From: Zihan Xi <[email protected]>
commit 9092e15defbe6c7bc241c306093ca9d358a578e7 upstream.
tcf_ct_handle_fragments() calls nf_ct_handle_fragments() without saving and restoring skb->cb. The defrag helper clears IPCB/IP6CB, which aliases the tc_skb_cb/qdisc_skb_cb control buffer. Fragmented traffic through act_ct therefore loses qdisc metadata such as pkt_segs and can trigger WARN_ON_ONCE() in qdisc_pkt_segs() when panic_on_warn is enabled.
Save and restore the full tc_skb_cb around nf_ct_handle_fragments(), matching the pattern used by ovs_ct_handle_fragments().
--- net/sched/act_ct.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
net/sched/act_ct.c +4 -3
@@ -844,11 +844,11 @@ static int tcf_ct_handle_fragments(struc u8 family, u16 zone, bool *defrag) { enum ip_conntrack_info ctinfo; +struct tc_skb_cb cb;struct nf_conn *ct;int err =0 ;bool frag;u8 proto; -u16 mru;/* Previously seen (loopback)? Ignore. */ ct =nf_ct_get (skb, &ctinfo);
@@ -862,12 +862,13 @@ static int tcf_ct_handle_fragments(strucif (err || !frag)return err; - err =nf_ct_handle_fragments (net, skb, zone, family, &proto, &mru); + cb = *tc_skb_cb (skb); + err =nf_ct_handle_fragments (net, skb, zone, family, &proto, &cb.mru);if (err)return err; *defrag = true; -tc_skb_cb (skb)->mru = mru; + *tc_skb_cb (skb) = cb;return 0 ; }