Skip to content

Commit 3fb3079

Browse files
cjihrigcodebytere
authored andcommittedMar 17, 2020
deps: uvwasi: cherry-pick eea4508
Original commit message: prevent race conditions with uvwasi_fd_close() uvwasi_fd_close() performed the following operations: - lock the file descriptor mutex - close the file - release the file descriptor mutex - call the file table's remove() function Once the fd's mutex is released, another thread could acquire it before the fd is removed from the file table. If this happens, remove() could destroy a held mutex. This commit updates uvwasi_fd_close() to perform the entire sequence while holding the file table's lock, preventing new acquisitions of the fd's mutex. Fixes: nodejs/uvwasi#88 PR-URL: #31432 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
1 parent 3bd1c02 commit 3fb3079

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed
 

‎deps/uvwasi/include/fd_table.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ uvwasi_errno_t uvwasi_fd_table_get_nolock(struct uvwasi_fd_table_t* table,
5656
struct uvwasi_fd_wrap_t** wrap,
5757
uvwasi_rights_t rights_base,
5858
uvwasi_rights_t rights_inheriting);
59-
uvwasi_errno_t uvwasi_fd_table_remove(struct uvwasi_s* uvwasi,
60-
struct uvwasi_fd_table_t* table,
61-
const uvwasi_fd_t id);
59+
uvwasi_errno_t uvwasi_fd_table_remove_nolock(struct uvwasi_s* uvwasi,
60+
struct uvwasi_fd_table_t* table,
61+
const uvwasi_fd_t id);
6262
uvwasi_errno_t uvwasi_fd_table_renumber(struct uvwasi_s* uvwasi,
6363
struct uvwasi_fd_table_t* table,
6464
const uvwasi_fd_t dst,

‎deps/uvwasi/src/fd_table.c

+8-18
Original file line numberDiff line numberDiff line change
@@ -306,37 +306,27 @@ uvwasi_errno_t uvwasi_fd_table_get_nolock(struct uvwasi_fd_table_t* table,
306306
}
307307

308308

309-
uvwasi_errno_t uvwasi_fd_table_remove(uvwasi_t* uvwasi,
310-
struct uvwasi_fd_table_t* table,
311-
const uvwasi_fd_t id) {
309+
uvwasi_errno_t uvwasi_fd_table_remove_nolock(uvwasi_t* uvwasi,
310+
struct uvwasi_fd_table_t* table,
311+
const uvwasi_fd_t id) {
312312
struct uvwasi_fd_wrap_t* entry;
313-
uvwasi_errno_t err;
314313

315314
if (table == NULL)
316315
return UVWASI_EINVAL;
317316

318-
uv_rwlock_wrlock(&table->rwlock);
319-
320-
if (id >= table->size) {
321-
err = UVWASI_EBADF;
322-
goto exit;
323-
}
317+
if (id >= table->size)
318+
return UVWASI_EBADF;
324319

325320
entry = table->fds[id];
326321

327-
if (entry == NULL || entry->id != id) {
328-
err = UVWASI_EBADF;
329-
goto exit;
330-
}
322+
if (entry == NULL || entry->id != id)
323+
return UVWASI_EBADF;
331324

332325
uv_mutex_destroy(&entry->mutex);
333326
uvwasi__free(uvwasi, entry);
334327
table->fds[id] = NULL;
335328
table->used--;
336-
err = UVWASI_ESUCCESS;
337-
exit:
338-
uv_rwlock_wrunlock(&table->rwlock);
339-
return err;
329+
return UVWASI_ESUCCESS;
340330
}
341331

342332

‎deps/uvwasi/src/uvwasi.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -878,18 +878,26 @@ uvwasi_errno_t uvwasi_fd_close(uvwasi_t* uvwasi, uvwasi_fd_t fd) {
878878
if (uvwasi == NULL)
879879
return UVWASI_EINVAL;
880880

881-
err = uvwasi_fd_table_get(&uvwasi->fds, fd, &wrap, 0, 0);
881+
uvwasi_fd_table_lock(&uvwasi->fds);
882+
883+
err = uvwasi_fd_table_get_nolock(&uvwasi->fds, fd, &wrap, 0, 0);
882884
if (err != UVWASI_ESUCCESS)
883-
return err;
885+
goto exit;
884886

885887
r = uv_fs_close(NULL, &req, wrap->fd, NULL);
886888
uv_mutex_unlock(&wrap->mutex);
887889
uv_fs_req_cleanup(&req);
888890

889-
if (r != 0)
890-
return uvwasi__translate_uv_error(r);
891+
if (r != 0) {
892+
err = uvwasi__translate_uv_error(r);
893+
goto exit;
894+
}
895+
896+
err = uvwasi_fd_table_remove_nolock(uvwasi, &uvwasi->fds, fd);
891897

892-
return uvwasi_fd_table_remove(uvwasi, &uvwasi->fds, fd);
898+
exit:
899+
uvwasi_fd_table_unlock(&uvwasi->fds);
900+
return err;
893901
}
894902

895903

0 commit comments

Comments
 (0)
Please sign in to comment.