Skip to content

Commit 0a79004

Browse files
LiBaokun96brauner
authored andcommitted
cachefiles: add spin_lock for cachefiles_ondemand_info
The following concurrency may cause a read request to fail to be completed and result in a hung: t1 | t2 --------------------------------------------------------- cachefiles_ondemand_copen req = xa_erase(&cache->reqs, id) // Anon fd is maliciously closed. cachefiles_ondemand_fd_release xa_lock(&cache->reqs) cachefiles_ondemand_set_object_close(object) xa_unlock(&cache->reqs) cachefiles_ondemand_set_object_open // No one will ever close it again. cachefiles_ondemand_daemon_read cachefiles_ondemand_select_req // Get a read req but its fd is already closed. // The daemon can't issue a cread ioctl with an closed fd, then hung. So add spin_lock for cachefiles_ondemand_info to protect ondemand_id and state, thus we can avoid the above problem in cachefiles_ondemand_copen() by using ondemand_id to determine if fd has been closed. Fixes: c838305 ("cachefiles: notify the user daemon when looking up cookie") Signed-off-by: Baokun Li <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: Jeff Layton <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent a26dc49 commit 0a79004

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

fs/cachefiles/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct cachefiles_ondemand_info {
5555
int ondemand_id;
5656
enum cachefiles_object_state state;
5757
struct cachefiles_object *object;
58+
spinlock_t lock;
5859
};
5960

6061
/*

fs/cachefiles/ondemand.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ static int cachefiles_ondemand_fd_release(struct inode *inode,
1616
struct cachefiles_object *object = file->private_data;
1717
struct cachefiles_cache *cache = object->volume->cache;
1818
struct cachefiles_ondemand_info *info = object->ondemand;
19-
int object_id = info->ondemand_id;
19+
int object_id;
2020
struct cachefiles_req *req;
2121
XA_STATE(xas, &cache->reqs, 0);
2222

2323
xa_lock(&cache->reqs);
24+
spin_lock(&info->lock);
25+
object_id = info->ondemand_id;
2426
info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
2527
cachefiles_ondemand_set_object_close(object);
28+
spin_unlock(&info->lock);
2629

2730
/* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */
2831
xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
@@ -127,6 +130,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
127130
{
128131
struct cachefiles_req *req;
129132
struct fscache_cookie *cookie;
133+
struct cachefiles_ondemand_info *info;
130134
char *pid, *psize;
131135
unsigned long id;
132136
long size;
@@ -185,6 +189,33 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
185189
goto out;
186190
}
187191

192+
info = req->object->ondemand;
193+
spin_lock(&info->lock);
194+
/*
195+
* The anonymous fd was closed before copen ? Fail the request.
196+
*
197+
* t1 | t2
198+
* ---------------------------------------------------------
199+
* cachefiles_ondemand_copen
200+
* req = xa_erase(&cache->reqs, id)
201+
* // Anon fd is maliciously closed.
202+
* cachefiles_ondemand_fd_release
203+
* xa_lock(&cache->reqs)
204+
* cachefiles_ondemand_set_object_close(object)
205+
* xa_unlock(&cache->reqs)
206+
* cachefiles_ondemand_set_object_open
207+
* // No one will ever close it again.
208+
* cachefiles_ondemand_daemon_read
209+
* cachefiles_ondemand_select_req
210+
*
211+
* Get a read req but its fd is already closed. The daemon can't
212+
* issue a cread ioctl with an closed fd, then hung.
213+
*/
214+
if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) {
215+
spin_unlock(&info->lock);
216+
req->error = -EBADFD;
217+
goto out;
218+
}
188219
cookie = req->object->cookie;
189220
cookie->object_size = size;
190221
if (size)
@@ -194,6 +225,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
194225
trace_cachefiles_ondemand_copen(req->object, id, size);
195226

196227
cachefiles_ondemand_set_object_open(req->object);
228+
spin_unlock(&info->lock);
197229
wake_up_all(&cache->daemon_pollwq);
198230

199231
out:
@@ -596,6 +628,7 @@ int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object,
596628
return -ENOMEM;
597629

598630
object->ondemand->object = object;
631+
spin_lock_init(&object->ondemand->lock);
599632
INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker);
600633
return 0;
601634
}

0 commit comments

Comments
 (0)