From: Greg Kroah-Hartman Date: Tue, 17 Sep 2019 12:05:25 +0000 (+0200) Subject: 4.14-stable patches X-Git-Tag: v4.14.145~29 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8e57b9655d4b727aef0d4c6d20ab021aa49981cc;p=thirdparty%2Fkernel%2Fstable-queue.git 4.14-stable patches added patches: clk-rockchip-don-t-yell-about-bad-mmc-phases-when-getting.patch driver-core-fix-use-after-free-and-double-free-on-glue-directory.patch drm-meson-add-support-for-xbgr8888-abgr8888-formats.patch mtd-rawnand-mtk-fix-wrongly-assigned-oob-buffer-pointer-issue.patch pci-always-allow-probing-with-driver_override.patch powerpc-add-barrier_nospec-to-raw_copy_in_user.patch ubifs-correctly-use-tnc_next-in-search_dh_cookie.patch --- diff --git a/queue-4.14/clk-rockchip-don-t-yell-about-bad-mmc-phases-when-getting.patch b/queue-4.14/clk-rockchip-don-t-yell-about-bad-mmc-phases-when-getting.patch new file mode 100644 index 00000000000..56892c1e2fd --- /dev/null +++ b/queue-4.14/clk-rockchip-don-t-yell-about-bad-mmc-phases-when-getting.patch @@ -0,0 +1,48 @@ +From 6943b839721ad4a31ad2bacf6e71b21f2dfe3134 Mon Sep 17 00:00:00 2001 +From: Douglas Anderson +Date: Fri, 3 May 2019 14:22:08 -0700 +Subject: clk: rockchip: Don't yell about bad mmc phases when getting + +From: Douglas Anderson + +commit 6943b839721ad4a31ad2bacf6e71b21f2dfe3134 upstream. + +At boot time, my rk3288-veyron devices yell with 8 lines that look +like this: + [ 0.000000] rockchip_mmc_get_phase: invalid clk rate + +This is because the clock framework at clk_register() time tries to +get the phase but we don't have a parent yet. + +While the errors appear to be harmless they are still ugly and, in +general, we don't want yells like this in the log unless they are +important. + +There's no real reason to be yelling here. We can still return +-EINVAL to indicate that the phase makes no sense without a parent. +If someone really tries to do tuning and the clock is reported as 0 +then we'll see the yells in rockchip_mmc_set_phase(). + +Fixes: 4bf59902b500 ("clk: rockchip: Prevent calculating mmc phase if clock rate is zero") +Signed-off-by: Douglas Anderson +Signed-off-by: Heiko Stuebner +Signed-off-by: Greg Kroah-Hartman + +--- + drivers/clk/rockchip/clk-mmc-phase.c | 4 +--- + 1 file changed, 1 insertion(+), 3 deletions(-) + +--- a/drivers/clk/rockchip/clk-mmc-phase.c ++++ b/drivers/clk/rockchip/clk-mmc-phase.c +@@ -59,10 +59,8 @@ static int rockchip_mmc_get_phase(struct + u32 delay_num = 0; + + /* See the comment for rockchip_mmc_set_phase below */ +- if (!rate) { +- pr_err("%s: invalid clk rate\n", __func__); ++ if (!rate) + return -EINVAL; +- } + + raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift); + diff --git a/queue-4.14/driver-core-fix-use-after-free-and-double-free-on-glue-directory.patch b/queue-4.14/driver-core-fix-use-after-free-and-double-free-on-glue-directory.patch new file mode 100644 index 00000000000..7ad4c596ab0 --- /dev/null +++ b/queue-4.14/driver-core-fix-use-after-free-and-double-free-on-glue-directory.patch @@ -0,0 +1,171 @@ +From ac43432cb1f5c2950408534987e57c2071e24d8f Mon Sep 17 00:00:00 2001 +From: Muchun Song +Date: Sat, 27 Jul 2019 11:21:22 +0800 +Subject: driver core: Fix use-after-free and double free on glue directory + +From: Muchun Song + +commit ac43432cb1f5c2950408534987e57c2071e24d8f upstream. + +There is a race condition between removing glue directory and adding a new +device under the glue dir. It can be reproduced in following test: + +CPU1: CPU2: + +device_add() + get_device_parent() + class_dir_create_and_add() + kobject_add_internal() + create_dir() // create glue_dir + + device_add() + get_device_parent() + kobject_get() // get glue_dir + +device_del() + cleanup_glue_dir() + kobject_del(glue_dir) + + kobject_add() + kobject_add_internal() + create_dir() // in glue_dir + sysfs_create_dir_ns() + kernfs_create_dir_ns(sd) + + sysfs_remove_dir() // glue_dir->sd=NULL + sysfs_put() // free glue_dir->sd + + // sd is freed + kernfs_new_node(sd) + kernfs_get(glue_dir) + kernfs_add_one() + kernfs_put() + +Before CPU1 remove last child device under glue dir, if CPU2 add a new +device under glue dir, the glue_dir kobject reference count will be +increase to 2 via kobject_get() in get_device_parent(). And CPU2 has +been called kernfs_create_dir_ns(), but not call kernfs_new_node(). +Meanwhile, CPU1 call sysfs_remove_dir() and sysfs_put(). This result in +glue_dir->sd is freed and it's reference count will be 0. Then CPU2 call +kernfs_get(glue_dir) will trigger a warning in kernfs_get() and increase +it's reference count to 1. Because glue_dir->sd is freed by CPU1, the next +call kernfs_add_one() by CPU2 will fail(This is also use-after-free) +and call kernfs_put() to decrease reference count. Because the reference +count is decremented to 0, it will also call kmem_cache_free() to free +the glue_dir->sd again. This will result in double free. + +In order to avoid this happening, we also should make sure that kernfs_node +for glue_dir is released in CPU1 only when refcount for glue_dir kobj is +1 to fix this race. + +The following calltrace is captured in kernel 4.14 with the following patch +applied: + +commit 726e41097920 ("drivers: core: Remove glue dirs from sysfs earlier") + +-------------------------------------------------------------------------- +[ 3.633703] WARNING: CPU: 4 PID: 513 at .../fs/kernfs/dir.c:494 + Here is WARN_ON(!atomic_read(&kn->count) in kernfs_get(). +.... +[ 3.633986] Call trace: +[ 3.633991] kernfs_create_dir_ns+0xa8/0xb0 +[ 3.633994] sysfs_create_dir_ns+0x54/0xe8 +[ 3.634001] kobject_add_internal+0x22c/0x3f0 +[ 3.634005] kobject_add+0xe4/0x118 +[ 3.634011] device_add+0x200/0x870 +[ 3.634017] _request_firmware+0x958/0xc38 +[ 3.634020] request_firmware_into_buf+0x4c/0x70 +.... +[ 3.634064] kernel BUG at .../mm/slub.c:294! + Here is BUG_ON(object == fp) in set_freepointer(). +.... +[ 3.634346] Call trace: +[ 3.634351] kmem_cache_free+0x504/0x6b8 +[ 3.634355] kernfs_put+0x14c/0x1d8 +[ 3.634359] kernfs_create_dir_ns+0x88/0xb0 +[ 3.634362] sysfs_create_dir_ns+0x54/0xe8 +[ 3.634366] kobject_add_internal+0x22c/0x3f0 +[ 3.634370] kobject_add+0xe4/0x118 +[ 3.634374] device_add+0x200/0x870 +[ 3.634378] _request_firmware+0x958/0xc38 +[ 3.634381] request_firmware_into_buf+0x4c/0x70 +-------------------------------------------------------------------------- + +Fixes: 726e41097920 ("drivers: core: Remove glue dirs from sysfs earlier") +Signed-off-by: Muchun Song +Reviewed-by: Mukesh Ojha +Signed-off-by: Prateek Sood +Link: https://lore.kernel.org/r/20190727032122.24639-1-smuchun@gmail.com +Signed-off-by: Greg Kroah-Hartman + +--- + drivers/base/core.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++- + 1 file changed, 52 insertions(+), 1 deletion(-) + +--- a/drivers/base/core.c ++++ b/drivers/base/core.c +@@ -1572,12 +1572,63 @@ static inline struct kobject *get_glue_d + */ + static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) + { ++ unsigned int ref; ++ + /* see if we live in a "glue" directory */ + if (!live_in_glue_dir(glue_dir, dev)) + return; + + mutex_lock(&gdp_mutex); +- if (!kobject_has_children(glue_dir)) ++ /** ++ * There is a race condition between removing glue directory ++ * and adding a new device under the glue directory. ++ * ++ * CPU1: CPU2: ++ * ++ * device_add() ++ * get_device_parent() ++ * class_dir_create_and_add() ++ * kobject_add_internal() ++ * create_dir() // create glue_dir ++ * ++ * device_add() ++ * get_device_parent() ++ * kobject_get() // get glue_dir ++ * ++ * device_del() ++ * cleanup_glue_dir() ++ * kobject_del(glue_dir) ++ * ++ * kobject_add() ++ * kobject_add_internal() ++ * create_dir() // in glue_dir ++ * sysfs_create_dir_ns() ++ * kernfs_create_dir_ns(sd) ++ * ++ * sysfs_remove_dir() // glue_dir->sd=NULL ++ * sysfs_put() // free glue_dir->sd ++ * ++ * // sd is freed ++ * kernfs_new_node(sd) ++ * kernfs_get(glue_dir) ++ * kernfs_add_one() ++ * kernfs_put() ++ * ++ * Before CPU1 remove last child device under glue dir, if CPU2 add ++ * a new device under glue dir, the glue_dir kobject reference count ++ * will be increase to 2 in kobject_get(k). And CPU2 has been called ++ * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir() ++ * and sysfs_put(). This result in glue_dir->sd is freed. ++ * ++ * Then the CPU2 will see a stale "empty" but still potentially used ++ * glue dir around in kernfs_new_node(). ++ * ++ * In order to avoid this happening, we also should make sure that ++ * kernfs_node for glue_dir is released in CPU1 only when refcount ++ * for glue_dir kobj is 1. ++ */ ++ ref = kref_read(&glue_dir->kref); ++ if (!kobject_has_children(glue_dir) && !--ref) + kobject_del(glue_dir); + kobject_put(glue_dir); + mutex_unlock(&gdp_mutex); diff --git a/queue-4.14/drm-meson-add-support-for-xbgr8888-abgr8888-formats.patch b/queue-4.14/drm-meson-add-support-for-xbgr8888-abgr8888-formats.patch new file mode 100644 index 00000000000..74770e5e2ed --- /dev/null +++ b/queue-4.14/drm-meson-add-support-for-xbgr8888-abgr8888-formats.patch @@ -0,0 +1,61 @@ +From 5ffff4415f9eeae834960226770963e2947e17eb Mon Sep 17 00:00:00 2001 +From: Neil Armstrong +Date: Mon, 29 Apr 2019 09:52:38 +0200 +Subject: drm/meson: Add support for XBGR8888 & ABGR8888 formats + +From: Neil Armstrong + +commit 5ffff4415f9eeae834960226770963e2947e17eb upstream. + +Add missing XBGR8888 & ABGR8888 formats variants from the primary plane. + +Fixes: bbbe775ec5b5 ("drm: Add support for Amlogic Meson Graphic Controller") +Signed-off-by: Neil Armstrong +Reviewed-by: Kevin Hilman +Link: https://patchwork.freedesktop.org/patch/msgid/20190429075238.7884-1-narmstrong@baylibre.com +Signed-off-by: Greg Kroah-Hartman + +--- + drivers/gpu/drm/meson/meson_plane.c | 16 ++++++++++++++++ + 1 file changed, 16 insertions(+) + +--- a/drivers/gpu/drm/meson/meson_plane.c ++++ b/drivers/gpu/drm/meson/meson_plane.c +@@ -124,6 +124,13 @@ static void meson_plane_atomic_update(st + priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 | + OSD_COLOR_MATRIX_32_ARGB; + break; ++ case DRM_FORMAT_XBGR8888: ++ /* For XRGB, replace the pixel's alpha by 0xFF */ ++ writel_bits_relaxed(OSD_REPLACE_EN, OSD_REPLACE_EN, ++ priv->io_base + _REG(VIU_OSD1_CTRL_STAT2)); ++ priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 | ++ OSD_COLOR_MATRIX_32_ABGR; ++ break; + case DRM_FORMAT_ARGB8888: + /* For ARGB, use the pixel's alpha */ + writel_bits_relaxed(OSD_REPLACE_EN, 0, +@@ -131,6 +138,13 @@ static void meson_plane_atomic_update(st + priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 | + OSD_COLOR_MATRIX_32_ARGB; + break; ++ case DRM_FORMAT_ABGR8888: ++ /* For ARGB, use the pixel's alpha */ ++ writel_bits_relaxed(OSD_REPLACE_EN, 0, ++ priv->io_base + _REG(VIU_OSD1_CTRL_STAT2)); ++ priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 | ++ OSD_COLOR_MATRIX_32_ABGR; ++ break; + case DRM_FORMAT_RGB888: + priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_24 | + OSD_COLOR_MATRIX_24_RGB; +@@ -200,7 +214,9 @@ static const struct drm_plane_funcs meso + + static const uint32_t supported_drm_formats[] = { + DRM_FORMAT_ARGB8888, ++ DRM_FORMAT_ABGR8888, + DRM_FORMAT_XRGB8888, ++ DRM_FORMAT_XBGR8888, + DRM_FORMAT_RGB888, + DRM_FORMAT_RGB565, + }; diff --git a/queue-4.14/mtd-rawnand-mtk-fix-wrongly-assigned-oob-buffer-pointer-issue.patch b/queue-4.14/mtd-rawnand-mtk-fix-wrongly-assigned-oob-buffer-pointer-issue.patch new file mode 100644 index 00000000000..1cd6200f717 --- /dev/null +++ b/queue-4.14/mtd-rawnand-mtk-fix-wrongly-assigned-oob-buffer-pointer-issue.patch @@ -0,0 +1,84 @@ +From 336d4b138be2dad372b67a2388e42805c48aaa38 Mon Sep 17 00:00:00 2001 +From: Xiaolei Li +Date: Tue, 7 May 2019 18:25:41 +0800 +Subject: mtd: rawnand: mtk: Fix wrongly assigned OOB buffer pointer issue + +From: Xiaolei Li + +commit 336d4b138be2dad372b67a2388e42805c48aaa38 upstream. + +One main goal of the function mtk_nfc_update_ecc_stats is to check +whether sectors are all empty. If they are empty, set these sectors's +data buffer and OOB buffer as 0xff. + +But now, the sector OOB buffer pointer is wrongly assigned. We always +do memset from sector 0. + +To fix this issue, pass start sector number to make OOB buffer pointer +be properly assigned. + +Fixes: 1d6b1e464950 ("mtd: mediatek: driver for MTK Smart Device") +Signed-off-by: Xiaolei Li +Reviewed-by: Miquel Raynal +Signed-off-by: Miquel Raynal +Signed-off-by: Greg Kroah-Hartman + +--- + drivers/mtd/nand/mtk_nand.c | 21 ++++++++++----------- + 1 file changed, 10 insertions(+), 11 deletions(-) + +--- a/drivers/mtd/nand/mtk_nand.c ++++ b/drivers/mtd/nand/mtk_nand.c +@@ -846,19 +846,21 @@ static int mtk_nfc_write_oob_std(struct + return ret & NAND_STATUS_FAIL ? -EIO : 0; + } + +-static int mtk_nfc_update_ecc_stats(struct mtd_info *mtd, u8 *buf, u32 sectors) ++static int mtk_nfc_update_ecc_stats(struct mtd_info *mtd, u8 *buf, u32 start, ++ u32 sectors) + { + struct nand_chip *chip = mtd_to_nand(mtd); + struct mtk_nfc *nfc = nand_get_controller_data(chip); + struct mtk_nfc_nand_chip *mtk_nand = to_mtk_nand(chip); + struct mtk_ecc_stats stats; ++ u32 reg_size = mtk_nand->fdm.reg_size; + int rc, i; + + rc = nfi_readl(nfc, NFI_STA) & STA_EMP_PAGE; + if (rc) { + memset(buf, 0xff, sectors * chip->ecc.size); + for (i = 0; i < sectors; i++) +- memset(oob_ptr(chip, i), 0xff, mtk_nand->fdm.reg_size); ++ memset(oob_ptr(chip, start + i), 0xff, reg_size); + return 0; + } + +@@ -878,7 +880,7 @@ static int mtk_nfc_read_subpage(struct m + u32 spare = mtk_nand->spare_per_sector; + u32 column, sectors, start, end, reg; + dma_addr_t addr; +- int bitflips; ++ int bitflips = 0; + size_t len; + u8 *buf; + int rc; +@@ -946,14 +948,11 @@ static int mtk_nfc_read_subpage(struct m + if (rc < 0) { + dev_err(nfc->dev, "subpage done timeout\n"); + bitflips = -EIO; +- } else { +- bitflips = 0; +- if (!raw) { +- rc = mtk_ecc_wait_done(nfc->ecc, ECC_DECODE); +- bitflips = rc < 0 ? -ETIMEDOUT : +- mtk_nfc_update_ecc_stats(mtd, buf, sectors); +- mtk_nfc_read_fdm(chip, start, sectors); +- } ++ } else if (!raw) { ++ rc = mtk_ecc_wait_done(nfc->ecc, ECC_DECODE); ++ bitflips = rc < 0 ? -ETIMEDOUT : ++ mtk_nfc_update_ecc_stats(mtd, buf, start, sectors); ++ mtk_nfc_read_fdm(chip, start, sectors); + } + + dma_unmap_single(nfc->dev, addr, len, DMA_FROM_DEVICE); diff --git a/queue-4.14/pci-always-allow-probing-with-driver_override.patch b/queue-4.14/pci-always-allow-probing-with-driver_override.patch new file mode 100644 index 00000000000..21817e67155 --- /dev/null +++ b/queue-4.14/pci-always-allow-probing-with-driver_override.patch @@ -0,0 +1,51 @@ +From 2d2f4273cbe9058d1f5a518e5e880d27d7b3b30f Mon Sep 17 00:00:00 2001 +From: Alex Williamson +Date: Thu, 9 May 2019 13:27:22 -0600 +Subject: PCI: Always allow probing with driver_override + +From: Alex Williamson + +commit 2d2f4273cbe9058d1f5a518e5e880d27d7b3b30f upstream. + +Commit 0e7df22401a3 ("PCI: Add sysfs sriov_drivers_autoprobe to control +VF driver binding") introduced the sriov_drivers_autoprobe attribute +which allows users to prevent the kernel from automatically probing a +driver for new VFs as they are created. This allows VFs to be spawned +without automatically binding the new device to a host driver, such as +in cases where the user intends to use the device only with a meta +driver like vfio-pci. However, the current implementation prevents any +use of drivers_probe with the VF while sriov_drivers_autoprobe=0. This +blocks the now current general practice of setting driver_override +followed by using drivers_probe to bind a device to a specified driver. + +The kernel never automatically sets a driver_override therefore it seems +we can assume a driver_override reflects the intent of the user. Also, +probing a device using a driver_override match seems outside the scope +of the 'auto' part of sriov_drivers_autoprobe. Therefore, let's allow +driver_override matches regardless of sriov_drivers_autoprobe, which we +can do by simply testing if a driver_override is set for a device as a +'can probe' condition. + +Fixes: 0e7df22401a3 ("PCI: Add sysfs sriov_drivers_autoprobe to control VF driver binding") +Link: https://lore.kernel.org/lkml/155742996741.21878.569845487290798703.stgit@gimli.home +Link: https://lore.kernel.org/linux-pci/155672991496.20698.4279330795743262888.stgit@gimli.home/T/#u +Signed-off-by: Alex Williamson +Signed-off-by: Bjorn Helgaas +Signed-off-by: Greg Kroah-Hartman + +--- + drivers/pci/pci-driver.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +--- a/drivers/pci/pci-driver.c ++++ b/drivers/pci/pci-driver.c +@@ -400,7 +400,8 @@ void __weak pcibios_free_irq(struct pci_ + #ifdef CONFIG_PCI_IOV + static inline bool pci_device_can_probe(struct pci_dev *pdev) + { +- return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe); ++ return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe || ++ pdev->driver_override); + } + #else + static inline bool pci_device_can_probe(struct pci_dev *pdev) diff --git a/queue-4.14/powerpc-add-barrier_nospec-to-raw_copy_in_user.patch b/queue-4.14/powerpc-add-barrier_nospec-to-raw_copy_in_user.patch new file mode 100644 index 00000000000..10e407f400b --- /dev/null +++ b/queue-4.14/powerpc-add-barrier_nospec-to-raw_copy_in_user.patch @@ -0,0 +1,37 @@ +From 6fbcdd59094ade30db63f32316e9502425d7b256 Mon Sep 17 00:00:00 2001 +From: Suraj Jitindar Singh +Date: Wed, 6 Mar 2019 12:10:38 +1100 +Subject: powerpc: Add barrier_nospec to raw_copy_in_user() + +From: Suraj Jitindar Singh + +commit 6fbcdd59094ade30db63f32316e9502425d7b256 upstream. + +Commit ddf35cf3764b ("powerpc: Use barrier_nospec in copy_from_user()") +Added barrier_nospec before loading from user-controlled pointers. The +intention was to order the load from the potentially user-controlled +pointer vs a previous branch based on an access_ok() check or similar. + +In order to achieve the same result, add a barrier_nospec to the +raw_copy_in_user() function before loading from such a user-controlled +pointer. + +Fixes: ddf35cf3764b ("powerpc: Use barrier_nospec in copy_from_user()") +Signed-off-by: Suraj Jitindar Singh +Signed-off-by: Michael Ellerman +Signed-off-by: Greg Kroah-Hartman + +--- + arch/powerpc/include/asm/uaccess.h | 1 + + 1 file changed, 1 insertion(+) + +--- a/arch/powerpc/include/asm/uaccess.h ++++ b/arch/powerpc/include/asm/uaccess.h +@@ -280,6 +280,7 @@ extern unsigned long __copy_tofrom_user( + static inline unsigned long + raw_copy_in_user(void __user *to, const void __user *from, unsigned long n) + { ++ barrier_nospec(); + return __copy_tofrom_user(to, from, n); + } + #endif /* __powerpc64__ */ diff --git a/queue-4.14/series b/queue-4.14/series index 795187a6411..b3a2ea22d3f 100644 --- a/queue-4.14/series +++ b/queue-4.14/series @@ -24,3 +24,10 @@ kvm-x86-work-around-leak-of-uninitialized-stack-contents.patch kvm-nvmx-handle-page-fault-in-vmread.patch mips-vdso-prevent-use-of-smp_processor_id.patch mips-vdso-use-same-m-float-cflag-as-the-kernel-proper.patch +powerpc-add-barrier_nospec-to-raw_copy_in_user.patch +drm-meson-add-support-for-xbgr8888-abgr8888-formats.patch +clk-rockchip-don-t-yell-about-bad-mmc-phases-when-getting.patch +mtd-rawnand-mtk-fix-wrongly-assigned-oob-buffer-pointer-issue.patch +pci-always-allow-probing-with-driver_override.patch +ubifs-correctly-use-tnc_next-in-search_dh_cookie.patch +driver-core-fix-use-after-free-and-double-free-on-glue-directory.patch diff --git a/queue-4.14/ubifs-correctly-use-tnc_next-in-search_dh_cookie.patch b/queue-4.14/ubifs-correctly-use-tnc_next-in-search_dh_cookie.patch new file mode 100644 index 00000000000..4806f58bb98 --- /dev/null +++ b/queue-4.14/ubifs-correctly-use-tnc_next-in-search_dh_cookie.patch @@ -0,0 +1,85 @@ +From bacfa94b08027b9f66ede7044972e3b066766b3e Mon Sep 17 00:00:00 2001 +From: Richard Weinberger +Date: Tue, 14 May 2019 22:31:08 +0200 +Subject: ubifs: Correctly use tnc_next() in search_dh_cookie() + +From: Richard Weinberger + +commit bacfa94b08027b9f66ede7044972e3b066766b3e upstream. + +Commit c877154d307f fixed an uninitialized variable and optimized +the function to not call tnc_next() in the first iteration of the +loop. While this seemed perfectly legit and wise, it turned out to +be illegal. +If the lookup function does not find an exact match it will rewind +the cursor by 1. +The rewinded cursor will not match the name hash we are looking for +and this results in a spurious -ENOENT. +So we need to move to the next entry in case of an non-exact match, +but not if the match was exact. + +While we are here, update the documentation to avoid further confusion. + +Cc: Hyunchul Lee +Cc: Geert Uytterhoeven +Fixes: c877154d307f ("ubifs: Fix uninitialized variable in search_dh_cookie()") +Fixes: 781f675e2d7e ("ubifs: Fix unlink code wrt. double hash lookups") +Signed-off-by: Richard Weinberger +Signed-off-by: Greg Kroah-Hartman + +--- + fs/ubifs/tnc.c | 16 +++++++++++----- + 1 file changed, 11 insertions(+), 5 deletions(-) + +--- a/fs/ubifs/tnc.c ++++ b/fs/ubifs/tnc.c +@@ -1164,8 +1164,8 @@ static struct ubifs_znode *dirty_cow_bot + * o exact match, i.e. the found zero-level znode contains key @key, then %1 + * is returned and slot number of the matched branch is stored in @n; + * o not exact match, which means that zero-level znode does not contain +- * @key, then %0 is returned and slot number of the closest branch is stored +- * in @n; ++ * @key, then %0 is returned and slot number of the closest branch or %-1 ++ * is stored in @n; In this case calling tnc_next() is mandatory. + * o @key is so small that it is even less than the lowest key of the + * leftmost zero-level node, then %0 is returned and %0 is stored in @n. + * +@@ -1882,13 +1882,19 @@ int ubifs_tnc_lookup_nm(struct ubifs_inf + + static int search_dh_cookie(struct ubifs_info *c, const union ubifs_key *key, + struct ubifs_dent_node *dent, uint32_t cookie, +- struct ubifs_znode **zn, int *n) ++ struct ubifs_znode **zn, int *n, int exact) + { + int err; + struct ubifs_znode *znode = *zn; + struct ubifs_zbranch *zbr; + union ubifs_key *dkey; + ++ if (!exact) { ++ err = tnc_next(c, &znode, n); ++ if (err) ++ return err; ++ } ++ + for (;;) { + zbr = &znode->zbranch[*n]; + dkey = &zbr->key; +@@ -1930,7 +1936,7 @@ static int do_lookup_dh(struct ubifs_inf + if (unlikely(err < 0)) + goto out_unlock; + +- err = search_dh_cookie(c, key, dent, cookie, &znode, &n); ++ err = search_dh_cookie(c, key, dent, cookie, &znode, &n, err); + + out_unlock: + mutex_unlock(&c->tnc_mutex); +@@ -2716,7 +2722,7 @@ int ubifs_tnc_remove_dh(struct ubifs_inf + if (unlikely(err < 0)) + goto out_free; + +- err = search_dh_cookie(c, key, dent, cookie, &znode, &n); ++ err = search_dh_cookie(c, key, dent, cookie, &znode, &n, err); + if (err) + goto out_free; + }