NFSv4: Fix memory leak in nfs4_set_security_label
Subsystems (6)
Sasha Levin
maintainerPATCH 1/12 NFSv4: Fix memory leak in nfs4_set_security_label
From: Dmitry Mastykin <[email protected]>
[ Upstream commit aad11473f8f4be3df86461081ce35ec5b145ba68 ]
We leak nfs_fattr and nfs4_label every time we set a security xattr.
--- fs/nfs/nfs4proc.c | 1 + 1 file changed, 1 insertion(+)
fs/nfs/nfs4proc.c +1 -0
@@ -6274,6 +6274,7 @@ nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)if (status ==0 )nfs_setsecurity (inode, fattr); +nfs_free_fattr (fattr); return status; }#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 2/12 nfs: propagate readlink errors in nfs_symlink_filler
From: Sagi Grimberg <[email protected]>
[ Upstream commit 134d0b3f2440cdddd12fc3444c9c0f62331ce6fc ]
There is an inherent race where a symlink file may have been overriden (by a different client) between lookup and readlink, resulting in a spurious EIO error returned to userspace. Fix this by propagating back ESTALE errors such that the vfs will retry the lookup/get_link (similar to nfs4_file_open) at least once.
--- fs/nfs/symlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfs/symlink.c +1 -1
@@ -41,7 +41,7 @@ static int nfs_symlink_filler(struct file *file, struct folio *folio) error:folio_set_error (folio);folio_unlock (folio); -return -EIO ; +return error; } static const char *nfs_get_link(struct dentry *dentry,
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 3/12 nfs: don't invalidate dentries on transient errors
In reply to Sasha LevinFrom: Scott Mayhew <[email protected]>
[ Upstream commit 0c8c7c559740d2d8b66048162af6c4dba8f0c88c ]
This is a slight variation on a patch previously proposed by Neil Brown that never got merged.
Prior to commit 5ceb9d7fdaaf ("NFS: Refactor nfs_lookup_revalidate()"), any error from nfs_lookup_verify_inode() other than -ESTALE would result in nfs_lookup_revalidate() returning that error (-ESTALE is mapped to zero).
Since that commit, all errors result in nfs_lookup_revalidate() returning zero, resulting in dentries being invalidated where they previously were not (particularly in the case of -ERESTARTSYS).
Fix it by passing the actual error code to nfs_lookup_revalidate_done(), and leaving the decision on whether to map the error code to zero or one to nfs_lookup_revalidate_done().
A simple reproducer is to run the following python code in a subdirectory of an NFS mount (not in the root of the NFS mount):
---8<---
import os
import multiprocessing
import time
if __name__=="__main__":
multiprocessing.set_start_method("spawn")
count = 0
while True:
try:
os.getcwd()
pool = multiprocessing.Pool(10) pool.close()
pool.terminate()
count += 1
except Exception as e:
print(f"Failed after {count} iterations") print(e)
break
---8<---
Prior to commit 5ceb9d7fdaaf, the above code would run indefinitely. After commit 5ceb9d7fdaaf, it fails almost immediately with -ENOENT.
--- fs/nfs/dir.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
fs/nfs/dir.c +13 -14
@@ -1615,7 +1615,16 @@ nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,switch (error) {case 1 :break ; -case 0 : +case -ETIMEDOUT : +if (inode && (IS_ROOT (dentry) || +NFS_SERVER (inode)->flags &NFS_MOUNT_SOFTREVAL )) + error =1 ; +break ; +case -ESTALE : +case -ENOENT : + error =0 ; + fallthrough; +default :/* * We can't d_drop the root of a disconnected tree: * its d_hash is on the s_anon list and d_drop() would hide
@@ -1670,18 +1679,8 @@ static int nfs_lookup_revalidate_dentry(struct inode *dir, dir_verifier =nfs_save_change_attribute (dir); ret =NFS_PROTO (dir)->lookup (dir, dentry, fhandle, fattr); -if (ret <0 ) { -switch (ret) { -case -ESTALE : -case -ENOENT : - ret =0 ; -break ; -case -ETIMEDOUT : -if (NFS_SERVER (inode)->flags &NFS_MOUNT_SOFTREVAL ) - ret =1 ; - } +if (ret <0 ) goto out; - }/* Request help from readdirplus */ nfs_lookup_advise_force_readdirplus (dir, flags);
@@ -1725,7 +1724,7 @@ nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, unsigned int flags) {struct inode *inode; -int error; +int error =0 ;nfs_inc_stats (dir,NFSIOS_DENTRYREVALIDATE ); inode =d_inode (dentry);
@@ -1770,7 +1769,7 @@ nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, out_bad:if (flags &LOOKUP_RCU )return -ECHILD ; -return nfs_lookup_revalidate_done (dir, dentry, inode,0 ); +return nfs_lookup_revalidate_done (dir, dentry, inode, error); } static int
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 4/12 cachefiles: add consistency check for copen/cread
In reply to Sasha LevinFrom: Baokun Li <[email protected]>
[ Upstream commit a26dc49df37e996876f50a0210039b2d211fdd6f ]
This prevents malicious processes from completing random copen/cread requests and crashing the system. Added checks are listed below:
* Generic, copen can only complete open requests, and cread can only complete read requests.
* For copen, ondemand_id must not be 0, because this indicates that the request has not been read by the daemon.
* For cread, the object corresponding to fd and req should be the same.
--- fs/cachefiles/ondemand.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-)
fs/cachefiles/ondemand.c +20 -7
@@ -97,12 +97,12 @@ static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos, }static long cachefiles_ondemand_fd_ioctl (struct file *filp,unsigned int ioctl, -unsigned long arg) +unsigned long id) {struct cachefiles_object *object = filp->private_data;struct cachefiles_cache *cache = object->volume->cache;struct cachefiles_req *req; -unsigned long id; +XA_STATE (xas, &cache->reqs, id);if (ioctl !=CACHEFILES_IOC_READ_COMPLETE )return -EINVAL ;
@@ -110,10 +110,15 @@ static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,if (!test_bit (CACHEFILES_ONDEMAND_MODE , &cache->flags))return -EOPNOTSUPP ; - id = arg; - req =xa_erase (&cache->reqs, id); -if (!req) +xa_lock (&cache->reqs); + req =xas_load (&xas); +if (!req || req->msg.opcode !=CACHEFILES_OP_READ || + req->object != object) { +xa_unlock (&cache->reqs);return -EINVAL ; + } +xas_store (&xas,NULL ); +xa_unlock (&cache->reqs);trace_cachefiles_ondemand_cread (object, id);complete (&req->done);
@@ -142,6 +147,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)unsigned long id;long size;int ret; +XA_STATE (xas, &cache->reqs,0 );if (!test_bit (CACHEFILES_ONDEMAND_MODE , &cache->flags))return -EOPNOTSUPP ;
@@ -165,9 +171,16 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)if (ret)return ret; - req =xa_erase (&cache->reqs, id); -if (!req) +xa_lock (&cache->reqs); + xas.xa_index = id; + req =xas_load (&xas); +if (!req || req->msg.opcode !=CACHEFILES_OP_OPEN || + !req->object->ondemand->ondemand_id) { +xa_unlock (&cache->reqs);return -EINVAL ; + } +xas_store (&xas,NULL ); +xa_unlock (&cache->reqs);/* fail OPEN request if copen format is invalid */ ret =kstrtol (psize,0 , &size);
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 5/12 cachefiles: Set object to close if ondemand_id < 0 in copen
In reply to Sasha LevinFrom: Zizhi Wo <[email protected]>
[ Upstream commit 4f8703fb3482f92edcfd31661857b16fec89c2c0 ]
If copen is maliciously called in the user mode, it may delete the request corresponding to the random id. And the request may have not been read yet.
Note that when the object is set to reopen, the open request will be done with the still reopen state in above case. As a result, the request corresponding to this object is always skipped in select_req function, so the read request is never completed and blocks other process.
Fix this issue by simply set object to close if its id < 0 in copen.
--- fs/cachefiles/ondemand.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/cachefiles/ondemand.c +6 -1
@@ -182,6 +182,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)xas_store (&xas,NULL );xa_unlock (&cache->reqs); + info = req->object->ondemand;/* fail OPEN request if copen format is invalid */ ret =kstrtol (psize,0 , &size);if (ret) {
@@ -201,7 +202,6 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) goto out; } - info = req->object->ondemand;spin_lock (&info->lock);/* * The anonymous fd was closed before copen ? Fail the request.
@@ -241,6 +241,11 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)wake_up_all (&cache->daemon_pollwq); out: +spin_lock (&info->lock); +/* Need to set object close to avoid reopen status continuing */ +if (info->ondemand_id ==CACHEFILES_ONDEMAND_ID_CLOSED ) +cachefiles_ondemand_set_object_close (req->object); +spin_unlock (&info->lock);complete (&req->done);return ret; }
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 6/12 cachefiles: make on-demand read killable
In reply to Sasha LevinFrom: Baokun Li <[email protected]>
[ Upstream commit bc9dde6155464e906e630a0a5c17a4cab241ffbb ]
Replacing wait_for_completion() with wait_for_completion_killable() in cachefiles_ondemand_send_req() allows us to kill processes that might trigger a hunk_task if the daemon is abnormal.
But now only CACHEFILES_OP_READ is killable, because OP_CLOSE and OP_OPEN is initiated from kworker context and the signal is prohibited in these kworker.
Note that when the req in xas changes, i.e. xas_load(&xas) != req, it means that a process will complete the current request soon, so wait again for the request to be completed.
In addition, add the cachefiles_ondemand_finish_req() helper function to simplify the code.
--- fs/cachefiles/ondemand.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)
fs/cachefiles/ondemand.c +28 -12
@@ -380,6 +380,20 @@ static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xareturn NULL ; } +static inline bool cachefiles_ondemand_finish_req (struct cachefiles_req *req, +struct xa_state *xas,int err) +{ +if (unlikely (!xas || !req)) +return false; + +if (xa_cmpxchg (xas->xa, xas->xa_index, req,NULL ,0 ) != req) +return false; + + req->error = err; +complete (&req->done); +return true; +} +ssize_t cachefiles_ondemand_daemon_read (struct cachefiles_cache *cache,char __user *_buffer,size_t buflen) {
@@ -443,16 +457,8 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, out:cachefiles_put_object (req->object, cachefiles_obj_put_read_req);/* Remove error request and CLOSE request has no reply */ -if (ret || msg->opcode ==CACHEFILES_OP_CLOSE ) { -xas_reset (&xas); -xas_lock (&xas); -if (xas_load (&xas) == req) { - req->error = ret; -complete (&req->done); -xas_store (&xas,NULL ); - } -xas_unlock (&xas); - } +if (ret || msg->opcode ==CACHEFILES_OP_CLOSE ) +cachefiles_ondemand_finish_req (req, &xas, ret);cachefiles_req_put (req);return ret ? ret : n; }
@@ -544,8 +550,18 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, goto out;wake_up_all (&cache->daemon_pollwq); -wait_for_completion (&req->done); - ret = req->error; +wait: + ret =wait_for_completion_killable (&req->done); +if (!ret) { + ret = req->error; + }else { + ret = -EINTR ; +if (!cachefiles_ondemand_finish_req (req, &xas, ret)) { +/* Someone will complete it soon. */ +cpu_relax (); + goto wait; + } + }cachefiles_req_put (req);return ret; out:
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 7/12 fs/file: fix the check in find_next_fd()
In reply to Sasha LevinFrom: Yuntao Wang <[email protected]>
[ Upstream commit ed8c7fbdfe117abbef81f65428ba263118ef298a ]
The maximum possible return value of find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) is maxbit. This return value, multiplied by BITS_PER_LONG, gives the value of bitbit, which can never be greater than maxfd, it can only be equal to maxfd at most, so the following check 'if (bitbit > maxfd)' will never be true.
Moreover, when bitbit equals maxfd, it indicates that there are no unused fds, and the function can directly return.
Fix this check.
--- fs/file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/file.c +2 -2
@@ -481,12 +481,12 @@ struct files_struct init_files = {static unsigned int find_next_fd (struct fdtable *fdt,unsigned int start) { -unsigned int maxfd = fdt->max_fds; +unsigned int maxfd = fdt->max_fds;/* always multiple of BITS_PER_LONG */ unsigned int maxbit = maxfd /BITS_PER_LONG ;unsigned int bitbit = start /BITS_PER_LONG ; bitbit =find_next_zero_bit (fdt->full_fds_bits, maxbit, bitbit) *BITS_PER_LONG ; -if (bitbit > maxfd) +if (bitbit >= maxfd)return maxfd;if (bitbit > start) start = bitbit;
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 8/12 mei: demote client disconnect warning on suspend to debug
In reply to Sasha LevinFrom: Alexander Usyskin <[email protected]>
[ Upstream commit 1db5322b7e6b58e1b304ce69a50e9dca798ca95b ]
Change level for the "not connected" client message in the write callback from error to debug.
The MEI driver currently disconnects all clients upon system suspend. This behavior is by design and user-space applications with open connections before the suspend are expected to handle errors upon resume, by reopening their handles, reconnecting,
and retrying their operations.
However, the current driver implementation logs an error message every time a write operation is attempted on a disconnected client. Since this is a normal and expected flow after system resume logging this as an error can be misleading.
--- drivers/misc/mei/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/mei/main.c +1 -1
@@ -327,7 +327,7 @@ static ssize_t mei_write(struct file *file, const char __user *ubuf, }if (!mei_cl_is_connected (cl)) { -cl_err (dev, cl,"is not connected" ); +cl_dbg (dev, cl,"is not connected" ); rets = -ENODEV ; goto out; }
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 9/12 iomap: Fix iomap_adjust_read_range for plen calculation
In reply to Sasha LevinFrom: "Ritesh Harjani (IBM)" <[email protected]>
[ Upstream commit f5ceb1bbc98c69536d4673a97315e8427e67de1b ]
If the extent spans the block that contains i_size, we need to handle both halves separately so that we properly zero data in the page cache for blocks that are entirely outside of i_size. But this is needed only when i_size is within the current folio under processing. "orig_pos + length > isize" can be true for all folios if the mapped extent length is greater than the folio size. That is making plen to break for every folio instead of only the last folio.
So use orig_plen for checking if "orig_pos + orig_plen > isize".
--- fs/iomap/buffered-io.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/iomap/buffered-io.c +2 -1
@@ -97,6 +97,7 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,unsigned block_size = (1 << block_bits);size_t poff =offset_in_folio (folio, *pos);size_t plen =min_t (loff_t,folio_size (folio) - poff, length); +size_t orig_plen = plen;unsigned first = poff >> block_bits;unsigned last = (poff + plen -1 ) >> block_bits;
@@ -133,7 +134,7 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,* handle both halves separately so that we properly zero data in the * page cache for blocks that are entirely outside of i_size. */ -if (orig_pos <= isize && orig_pos + length > isize) { +if (orig_pos <= isize && orig_pos + orig_plen > isize) {unsigned end =offset_in_folio (folio, isize -1 ) >> block_bits;if (first <= end && last > end)
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 10/12 drm: panel-orientation-quirks: Add quirk for Aya Neo KUN
In reply to Sasha LevinFrom: Tobias Jakobi <[email protected]>
[ Upstream commit f74fb5df429ebc6a614dc5aa9e44d7194d402e5a ]
Similar to the other Aya Neo devices this one features again a portrait screen, here with a native resolution of 1600x2560.
--- drivers/gpu/drm/drm_panel_orientation_quirks.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/gpu/drm/drm_panel_orientation_quirks.c +6 -0
@@ -202,6 +202,12 @@ static const struct dmi_system_id orientation_data[] = { DMI_MATCH(DMI_BOARD_NAME, "NEXT"), }, .driver_data = (void *)&lcd800x1280_rightside_up, + }, { /* AYA NEO KUN */ + .matches = { +DMI_EXACT_MATCH (DMI_BOARD_VENDOR ,"AYANEO" ), + DMI_MATCH(DMI_BOARD_NAME, "KUN"), + }, + .driver_data = (void *)&lcd1600x2560_rightside_up, }, { /* Chuwi HiBook (CWI514) */ .matches = { DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 11/12 nvme: avoid double free special payload
In reply to Sasha LevinFrom: Chunguang Xu <[email protected]>
[ Upstream commit e5d574ab37f5f2e7937405613d9b1a724811e5ad ]
If a discard request needs to be retried, and that retry may fail before a new special payload is added, a double free will result. Clear the RQF_SPECIAL_LOAD when the request is cleaned.
--- drivers/nvme/host/core.c | 1 + 1 file changed, 1 insertion(+)
drivers/nvme/host/core.c +1 -0
@@ -954,6 +954,7 @@ void nvme_cleanup_cmd(struct request *req)clear_bit_unlock (0 , &ctrl->discard_page_busy); else kfree(bvec_virt(&req->special_vec)); + req->rq_flags &= ~RQF_SPECIAL_PAYLOAD ; } }EXPORT_SYMBOL_GPL (nvme_cleanup_cmd);
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 12/12 nvmet: always initialize cqe.result
In reply to Sasha LevinFrom: Daniel Wagner <[email protected]>
[ Upstream commit cd0c1b8e045a8d2785342b385cb2684d9b48e426 ]
The spec doesn't mandate that the first two double words (aka results) for the command queue entry need to be set to 0 when they are not used (not specified). Though, the target implemention returns 0 for TCP and FC but not for RDMA.
Let's make RDMA behave the same and thus explicitly initializing the result field. This prevents leaking any data from the stack.
--- drivers/nvme/target/core.c | 1 + drivers/nvme/target/fabrics-cmd-auth.c | 3 --- drivers/nvme/target/fabrics-cmd.c | 6 ------ 3 files changed, 1 insertion(+), 9 deletions(-)
drivers/nvme/target/core.c +1 -0
@@ -935,6 +935,7 @@ bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq, req->metadata_sg_cnt =0 ; req->transfer_len =0 ; req->metadata_len =0 ; + req->cqe->result.u64 =0 ; req->cqe->status =0 ; req->cqe->sq_head =0 ; req->ns =NULL ;
drivers/nvme/target/fabrics-cmd-auth.c +0 -3
@@ -336,7 +336,6 @@ void nvmet_execute_auth_send(struct nvmet_req *req)pr_debug ("%s: ctrl %d qid %d nvme status %x error loc %d\n" , __func__, ctrl->cntlid, req->sq->qid, status, req->error_loc); - req->cqe->result.u64 =0 ;if (req->sq->dhchap_step !=NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2 && req->sq->dhchap_step !=NVME_AUTH_DHCHAP_MESSAGE_FAILURE2 ) {unsigned long auth_expire_secs = ctrl->kato ? ctrl->kato :120 ;
@@ -528,8 +527,6 @@ void nvmet_execute_auth_receive(struct nvmet_req *req) status =nvmet_copy_to_sgl (req,0 , d, al);kfree (d); done: - req->cqe->result.u64 =0 ; -if (req->sq->dhchap_step ==NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2 )nvmet_auth_sq_free (req->sq);else if (req->sq->dhchap_step ==NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ) {
drivers/nvme/target/fabrics-cmd.c +0 -6
@@ -225,9 +225,6 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)if (status) goto out; -/* zero out initial completion result, assign values as needed */ - req->cqe->result.u32 =0 ; -if (c->recfmt !=0 ) {pr_warn ("invalid connect version (%d).\n" ,le16_to_cpu (c->recfmt));
@@ -305,9 +302,6 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)if (status) goto out; -/* zero out initial completion result, assign values as needed */ - req->cqe->result.u32 =0 ; -if (c->recfmt !=0 ) {pr_warn ("invalid connect version (%d).\n" ,le16_to_cpu (c->recfmt));
Signature · 1 line
2.43.0