1 Star 0 Fork 41

haozi007/iSulad-src

forked from src-openEuler/iSulad 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0011-fix-maybe-uwait-use-after-free.patch 5.19 KB
一键复制 编辑 原始数据 按行查看 历史
haozi007 提交于 2022-09-29 19:13 . sync from openEuler
From 95815fe3332197279259ecc4ace08e2e20a174cf Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Wed, 28 Sep 2022 10:57:00 +0800
Subject: [PATCH 11/11] fix maybe uwait use after free
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../graphdriver/devmapper/wrapper_devmapper.c | 77 +++++++++++++++----
.../graphdriver/devmapper/wrapper_devmapper.h | 3 +
2 files changed, 64 insertions(+), 16 deletions(-)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
index 8a1dfff5..2513d64a 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
@@ -359,6 +359,17 @@ out:
return ret;
}
+void free_udev_wait_pth_t(udev_wait_pth_t* uwait)
+{
+ if (uwait->cond_init) {
+ pthread_cond_destroy(&uwait->wait_cond);
+ }
+ if (uwait->mutex_init) {
+ pthread_mutex_destroy(&uwait->udev_mutex);
+ }
+ free(uwait);
+}
+
static void *udev_wait_process(void *data)
{
int ret = 0;
@@ -376,13 +387,49 @@ static void *udev_wait_process(void *data)
} else {
uwait->state = DEV_OK;
}
+ if (pthread_cond_wait(&uwait->wait_cond, &uwait->udev_mutex) != 0) {
+ CRIT("Udev wait condition failed");
+ }
pthread_mutex_unlock(&uwait->udev_mutex);
+ free_udev_wait_pth_t(uwait);
+
out:
DAEMON_CLEAR_ERRMSG();
return NULL;
}
+udev_wait_pth_t *init_udev_wait_pth_t(uint32_t cookie)
+{
+ udev_wait_pth_t *uwait = NULL;
+
+ uwait = util_common_calloc_s(sizeof(udev_wait_pth_t));
+ if (uwait == NULL) {
+ ERROR("Out of memory");
+ return NULL;
+ }
+ uwait->cookie = cookie;
+ uwait->state = DEV_INIT;
+ uwait->mutex_init = false;
+ uwait->cond_init = false;
+
+ if (pthread_mutex_init(&uwait->udev_mutex, NULL) != 0) {
+ ERROR("Udev mutex initialized failed");
+ free(uwait);
+ return NULL;
+ }
+ uwait->mutex_init = true;
+
+ if (pthread_cond_init(&uwait->wait_cond, NULL) != 0) {
+ ERROR("Udev condition initialized failed");
+ free_udev_wait_pth_t(uwait);
+ return NULL;
+ }
+ uwait->cond_init = true;
+
+ return uwait;
+}
+
// UdevWait waits for any processes that are waiting for udev to complete the specified cookie.
void dev_udev_wait(uint32_t cookie)
{
@@ -396,51 +443,49 @@ void dev_udev_wait(uint32_t cookie)
return;
}
- uwait = util_common_calloc_s(sizeof(udev_wait_pth_t));
+ // free in udev_wait_process
+ uwait = init_udev_wait_pth_t(cookie);
if (uwait == NULL) {
- ERROR("Out of memory");
return;
}
- uwait->cookie = cookie;
- uwait->state = DEV_INIT;
-
- if (pthread_mutex_init(&uwait->udev_mutex, NULL) != 0) {
- ERROR("Udev mutex initialized failed");
- goto free_out;
- }
if (pthread_create(&tid, NULL, udev_wait_process, uwait) != 0) {
ERROR("devmapper: create udev wait process thread error:%s", strerror(errno));
- goto free_out;
+ free_udev_wait_pth_t(uwait);
+ return;
}
while (true) {
pthread_mutex_lock(&uwait->udev_mutex);
if (uwait->state != DEV_INIT) {
pthread_mutex_unlock(&uwait->udev_mutex);
- goto free_out;
+ goto out;
}
pthread_mutex_unlock(&uwait->udev_mutex);
if (gettimeofday(&end, NULL) != 0) {
ERROR("devmapper: get time failed");
- goto free_out;
+ goto out;
}
timeout = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000; // seconds
if (timeout >= (float)dm_udev_wait_timeout) {
if (dm_udev_complete(cookie) != 1) {
ERROR("Failed to complete udev cookie %u on udev wait timeout", cookie);
- goto free_out;
+ goto out;
}
ERROR("Wait on udev cookie time out");
break;
}
}
-free_out:
- pthread_mutex_destroy(&uwait->udev_mutex);
- free(uwait);
+out:
+ pthread_mutex_lock(&uwait->udev_mutex);
+ if (pthread_cond_broadcast(&uwait->wait_cond) != 0) {
+ ERROR("Failed to broadcast wait conditio");
+ }
+ pthread_mutex_unlock(&uwait->udev_mutex);
+ return;
}
int dev_delete_device_force(const char *name)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.h b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.h
index 6a45db58..5a692980 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.h
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.h
@@ -74,7 +74,10 @@ typedef enum {
typedef struct {
uint32_t cookie;
pthread_mutex_t udev_mutex;
+ bool mutex_init;
int state; // 0: ok 1:err_udev_wait 2: err_udev_wait_timeout
+ pthread_cond_t wait_cond;
+ bool cond_init;
} udev_wait_pth_t;
char *dev_strerror(int errnum);
--
2.25.1
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/duguhaotian/iSulad-src.git
git@gitee.com:duguhaotian/iSulad-src.git
duguhaotian
iSulad-src
iSulad-src
master

搜索帮助

Cb406eda 1850385 E526c682 1850385