All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Chao Yu <yuchao0@huawei.com>,
	Gao Xiang <gaoxiang25@huawei.com>
Subject: [PATCH 4.20 10/76] staging: erofs: fix race of initializing xattrs of a inode at the same time
Date: Fri,  8 Mar 2019 13:49:22 +0100	[thread overview]
Message-ID: <20190308124915.149162065@linuxfoundation.org> (raw)
In-Reply-To: <20190308124914.789210760@linuxfoundation.org>

4.20-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Gao Xiang <gaoxiang25@huawei.com>

commit 62dc45979f3f8cb0ea67302a93bff686f0c46c5a upstream.

In real scenario, there could be several threads accessing xattrs
of the same xattr-uninitialized inode, and init_inode_xattrs()
almost at the same time.

That's actually an unexpected behavior, this patch closes the race.

Fixes: b17500a0fdba ("staging: erofs: introduce xattr & acl support")
Cc: <stable@vger.kernel.org> # 4.19+
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/staging/erofs/internal.h |   11 +++++++---
 drivers/staging/erofs/xattr.c    |   41 +++++++++++++++++++++++++++------------
 2 files changed, 37 insertions(+), 15 deletions(-)

--- a/drivers/staging/erofs/internal.h
+++ b/drivers/staging/erofs/internal.h
@@ -352,12 +352,17 @@ static inline erofs_off_t iloc(struct er
 	return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
 }
 
-#define inode_set_inited_xattr(inode)   (EROFS_V(inode)->flags |= 1)
-#define inode_has_inited_xattr(inode)   (EROFS_V(inode)->flags & 1)
+/* atomic flag definitions */
+#define EROFS_V_EA_INITED_BIT	0
+
+/* bitlock definitions (arranged in reverse order) */
+#define EROFS_V_BL_XATTR_BIT	(BITS_PER_LONG - 1)
 
 struct erofs_vnode {
 	erofs_nid_t nid;
-	unsigned int flags;
+
+	/* atomic flags (including bitlocks) */
+	unsigned long flags;
 
 	unsigned char data_mapping_mode;
 	/* inline size in bytes */
--- a/drivers/staging/erofs/xattr.c
+++ b/drivers/staging/erofs/xattr.c
@@ -44,18 +44,25 @@ static inline void xattr_iter_end_final(
 
 static int init_inode_xattrs(struct inode *inode)
 {
+	struct erofs_vnode *const vi = EROFS_V(inode);
 	struct xattr_iter it;
 	unsigned int i;
 	struct erofs_xattr_ibody_header *ih;
 	struct super_block *sb;
 	struct erofs_sb_info *sbi;
-	struct erofs_vnode *vi;
 	bool atomic_map;
+	int ret = 0;
 
-	if (likely(inode_has_inited_xattr(inode)))
+	/* the most case is that xattrs of this inode are initialized. */
+	if (test_bit(EROFS_V_EA_INITED_BIT, &vi->flags))
 		return 0;
 
-	vi = EROFS_V(inode);
+	if (wait_on_bit_lock(&vi->flags, EROFS_V_BL_XATTR_BIT, TASK_KILLABLE))
+		return -ERESTARTSYS;
+
+	/* someone has initialized xattrs for us? */
+	if (test_bit(EROFS_V_EA_INITED_BIT, &vi->flags))
+		goto out_unlock;
 
 	/*
 	 * bypass all xattr operations if ->xattr_isize is not greater than
@@ -68,13 +75,16 @@ static int init_inode_xattrs(struct inod
 	if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
 		errln("xattr_isize %d of nid %llu is not supported yet",
 		      vi->xattr_isize, vi->nid);
-		return -ENOTSUPP;
+		ret = -ENOTSUPP;
+		goto out_unlock;
 	} else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
 		if (unlikely(vi->xattr_isize)) {
 			DBG_BUGON(1);
-			return -EIO;	/* xattr ondisk layout error */
+			ret = -EIO;
+			goto out_unlock;	/* xattr ondisk layout error */
 		}
-		return -ENOATTR;
+		ret = -ENOATTR;
+		goto out_unlock;
 	}
 
 	sb = inode->i_sb;
@@ -83,8 +93,10 @@ static int init_inode_xattrs(struct inod
 	it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
 
 	it.page = erofs_get_inline_page(inode, it.blkaddr);
-	if (IS_ERR(it.page))
-		return PTR_ERR(it.page);
+	if (IS_ERR(it.page)) {
+		ret = PTR_ERR(it.page);
+		goto out_unlock;
+	}
 
 	/* read in shared xattr array (non-atomic, see kmalloc below) */
 	it.kaddr = kmap(it.page);
@@ -97,7 +109,8 @@ static int init_inode_xattrs(struct inod
 						sizeof(uint), GFP_KERNEL);
 	if (vi->xattr_shared_xattrs == NULL) {
 		xattr_iter_end(&it, atomic_map);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto out_unlock;
 	}
 
 	/* let's skip ibody header */
@@ -114,7 +127,8 @@ static int init_inode_xattrs(struct inod
 			if (IS_ERR(it.page)) {
 				kfree(vi->xattr_shared_xattrs);
 				vi->xattr_shared_xattrs = NULL;
-				return PTR_ERR(it.page);
+				ret = PTR_ERR(it.page);
+				goto out_unlock;
 			}
 
 			it.kaddr = kmap_atomic(it.page);
@@ -127,8 +141,11 @@ static int init_inode_xattrs(struct inod
 	}
 	xattr_iter_end(&it, atomic_map);
 
-	inode_set_inited_xattr(inode);
-	return 0;
+	set_bit(EROFS_V_EA_INITED_BIT, &vi->flags);
+
+out_unlock:
+	clear_and_wake_up_bit(EROFS_V_BL_XATTR_BIT, &vi->flags);
+	return ret;
 }
 
 /*



  parent reply	other threads:[~2019-03-08 13:07 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-08 12:49 [PATCH 4.20 00/76] 4.20.15-stable review Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 01/76] cpufreq: Use struct kobj_attribute instead of struct global_attr Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 02/76] staging: erofs: fix mis-acted TAIL merging behavior Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 03/76] binder: create node flag to request senders security context Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 04/76] USB: serial: option: add Telit ME910 ECM composition Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 05/76] USB: serial: cp210x: add ID for Ingenico 3070 Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 06/76] USB: serial: ftdi_sio: add ID for Hjelmslund Electronics USB485 Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 07/76] driver core: Postpone DMA tear-down until after devres release Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 08/76] staging: erofs: fix fast symlink w/o xattr when fs xattr is on Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 09/76] staging: erofs: fix memleak of inodes shared xattr array Greg Kroah-Hartman
2019-03-08 12:49 ` Greg Kroah-Hartman [this message]
2019-03-08 12:49 ` [PATCH 4.20 11/76] staging: erofs: fix illegal address access under memory pressure Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 12/76] staging: erofs: compressed_pages should not be accessed again after freed Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 13/76] staging: comedi: ni_660x: fix missing break in switch statement Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 14/76] staging: wilc1000: fix to set correct value for vif_num Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 15/76] staging: android: ion: fix sys heap pools gfp_flags Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 16/76] staging: android: ashmem: Dont call fallocate() with ashmem_mutex held Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 17/76] staging: android: ashmem: Avoid range_alloc() allocation " Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 18/76] ip6mr: Do not call __IP6_INC_STATS() from preemptible context Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 19/76] net: dsa: mv88e6xxx: add call to mv88e6xxx_ports_cmode_init to probe for new DSA framework Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 20/76] net: dsa: mv88e6xxx: handle unknown duplex modes gracefully in mv88e6xxx_port_set_duplex Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 21/76] net: dsa: mv8e6xxx: fix number of internal PHYs for 88E6x90 family Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 22/76] net: mscc: Enable all ports in QSGMII Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 23/76] net: sched: put back q.qlen into a single location Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 24/76] net-sysfs: Fix mem leak in netdev_register_kobject Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 25/76] qmi_wwan: Add support for Quectel EG12/EM12 Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 26/76] sctp: call iov_iter_revert() after sending ABORT Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 27/76] sky2: Disable MSI on Dell Inspiron 1545 and Gateway P-79 Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 28/76] team: Free BPF filter when unregistering netdev Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 29/76] tipc: fix RDM/DGRAM connect() regression Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 30/76] bnxt_en: Drop oversize TX packets to prevent errors Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 31/76] geneve: correctly handle ipv6.disable module parameter Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 32/76] hv_netvsc: Fix IP header checksum for coalesced packets Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 33/76] ipv4: Add ICMPv6 support when parse route ipproto Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 34/76] lan743x: Fix TX Stall Issue Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 35/76] net: dsa: mv88e6xxx: Fix statistics on mv88e6161 Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 36/76] net: dsa: mv88e6xxx: Fix u64 statistics Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 37/76] netlabel: fix out-of-bounds memory accesses Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 38/76] net: netem: fix skb length BUG_ON in __skb_to_sgvec Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 39/76] net: nfc: Fix NULL dereference on nfc_llcp_build_tlv fails Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 40/76] net: phy: Micrel KSZ8061: link failure after cable connect Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 41/76] net: phy: phylink: fix uninitialized variable in phylink_get_mac_state Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 42/76] net: sit: fix memory leak in sit_init_net() Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 43/76] net: socket: set sock->sk to NULL after calling proto_ops::release() Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 44/76] tipc: fix race condition causing hung sendto Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 45/76] tun: fix blocking read Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 46/76] xen-netback: dont populate the hash cache on XenBus disconnect Greg Kroah-Hartman
2019-03-08 12:49 ` [PATCH 4.20 47/76] xen-netback: fix occasional leak of grant ref mappings under memory pressure Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 48/76] tun: remove unnecessary memory barrier Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 49/76] net: Add __icmp_send helper Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 50/76] net: avoid use IPCB in cipso_v4_error Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 51/76] ipv4: Return error for RTA_VIA attribute Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 52/76] ipv6: " Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 53/76] mpls: Return error for RTA_GATEWAY attribute Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 54/76] ipv4: Pass original device to ip_rcv_finish_core Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 55/76] net: dsa: mv88e6xxx: power serdes on/off for 10G interfaces on 6390X Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 56/76] net: dsa: mv88e6xxx: prevent interrupt storm caused by mv88e6390x_port_set_cmode Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 57/76] net/sched: act_ipt: fix refcount leak when replace fails Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 58/76] net/sched: act_skbedit: " Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 59/76] net: sched: act_tunnel_key: fix NULL pointer dereference during init Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 60/76] x86/CPU/AMD: Set the CPB bit unconditionally on F17h Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 61/76] x86/boot/compressed/64: Do not read legacy ROM on EFI system Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 62/76] tracing: Fix event filters and triggers to handle negative numbers Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 63/76] xhci: tegra: Prevent error pointer dereference Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 64/76] usb: xhci: Fix for Enabling USB ROLE SWITCH QUIRK on INTEL_SUNRISEPOINT_LP_XHCI Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 65/76] applicom: Fix potential Spectre v1 vulnerabilities Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 66/76] MIPS: irq: Allocate accurate order pages for irq stack Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 67/76] aio: Fix locking in aio_poll() Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 68/76] xtensa: fix get_wchan Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 69/76] gnss: sirf: fix premature wakeup interrupt enable Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 70/76] USB: serial: cp210x: fix GPIO in autosuspend Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 71/76] selftests: firmware: fix verify_reqs() return value Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 72/76] Bluetooth: btrtl: Restore old logic to assume firmware is already loaded Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 73/76] Bluetooth: Fix locking in bt_accept_enqueue() for BH context Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 74/76] exec: Fix mem leak in kernel_read_file Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 75/76] scsi: core: reset host byte in DID_NEXUS_FAILURE case Greg Kroah-Hartman
2019-03-08 12:50 ` [PATCH 4.20 76/76] bpf: fix sanitation rewrite in case of non-pointers Greg Kroah-Hartman
2019-03-08 16:04 ` [PATCH 4.20 00/76] 4.20.15-stable review Jon Hunter
2019-03-08 16:04   ` Jon Hunter
2019-03-08 20:57 ` shuah
2019-03-09  6:44 ` Naresh Kamboju
2019-03-09 22:35 ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190308124915.149162065@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=gaoxiang25@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=yuchao0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.