From 33ac065b4f33bb4bb43b8ea33f91b6ccbfd400b5 Mon Sep 17 00:00:00 2001 From: liuyuxiu Date: Mon, 25 Nov 2024 13:48:37 +0800 Subject: [PATCH 1/2] qos adtapte FDSAN Signed-off-by: liuyuxiu --- services/src/qos_interface.cpp | 37 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/services/src/qos_interface.cpp b/services/src/qos_interface.cpp index ebabca7..4c23662 100644 --- a/services/src/qos_interface.cpp +++ b/services/src/qos_interface.cpp @@ -31,7 +31,8 @@ static int TrivalOpenRtgNode(void) { char fileName[] = "/proc/self/sched_rtg_ctrl"; - int fd = open(fileName, O_RDWR); + FILE* f = fopen(fileName, "w+"); + int fd = fileno(f); if (fd < 0) { CONCUR_LOGE("[Interface] task %{public}d belong to user %{public}d open rtg node failed, errno = %{public}d", getpid(), getuid(), errno); @@ -42,7 +43,8 @@ static int TrivalOpenRtgNode(void) static int TrivalOpenQosCtrlNode(void) { char fileName[] = "/proc/thread-self/sched_qos_ctrl"; - int fd = open(fileName, O_RDWR); + FILE* f = fopen(fileName, "w+"); + int fd = fileno(f); if (fd < 0) { CONCUR_LOGE("[Interface] task %{public}d belong to user %{public}d open qos node failed, errno = %{public}d", getpid(), getuid(), errno); @@ -67,8 +69,10 @@ int EnableRtg(bool flag) CONCUR_LOGE("set rtg config enable failed."); } - close(fd); - + FILE* f = fdopen(fd, "w+"); + if (f != nullptr) { + fclose(f); + } return 0; }; @@ -95,7 +99,10 @@ int QosApplyForOther(unsigned int level, int tid) if (ret < 0) { CONCUR_LOGE("[Interface] task %{public}d apply qos failed, errno = %{public}d", tid, errno); } - close(fd); + FILE* f = fdopen(fd, "w+"); + if (f != nullptr) { + fclose(f); + } return ret; } @@ -114,7 +121,10 @@ int QosLeave(void) if (ret < 0) { CONCUR_LOGE("[Interface] task %{public}d leave qos failed, errno = %{public}d", gettid(), errno); } - close(fd); + FILE* f = fdopen(fd, "w+"); + if (f != nullptr) { + fclose(f); + } return ret; } @@ -133,7 +143,10 @@ int QosLeaveForOther(int tid) if (ret < 0) { CONCUR_LOGE("[Interface] task %{public}d leave qos failed, errno = %{public}d", tid, errno); } - close(fd); + FILE* f = fdopen(fd, "w+"); + if (f != nullptr) { + fclose(f); + } return ret; } @@ -148,7 +161,10 @@ int QosPolicySet(const struct QosPolicyDatas* policyDatas) if (ret < 0) { CONCUR_LOGE("[Interface] set qos policy failed, errno = %{public}d", errno); } - close(fd); + FILE* f = fdopen(fd, "w+"); + if (f != nullptr) { + fclose(f); + } return ret; } @@ -176,6 +192,9 @@ int QosGetForOther(int tid, int& level) } level = data.qos; - close(fd); + FILE* f = fdopen(fd, "w+"); + if (f != nullptr) { + fclose(f); + } return ret; } \ No newline at end of file -- Gitee From c119f11d23cf79ee770edc7edc2a052cbf9fa99a Mon Sep 17 00:00:00 2001 From: liuyuxiu Date: Tue, 26 Nov 2024 11:11:45 +0800 Subject: [PATCH 2/2] qos adapt FDSAN Signed-off-by: liuyuxiu --- common/include/concurrent_task_utils.h | 30 ++++++++++++++++++++++++++ common/src/concurrent_task_utils.cpp | 29 +++++++++++++++++++++++++ services/BUILD.gn | 1 + services/src/qos_interface.cpp | 21 +++++++++++------- 4 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 common/include/concurrent_task_utils.h create mode 100644 common/src/concurrent_task_utils.cpp diff --git a/common/include/concurrent_task_utils.h b/common/include/concurrent_task_utils.h new file mode 100644 index 0000000..c848f27 --- /dev/null +++ b/common/include/concurrent_task_utils.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONCURRENT_TASK_COMMON_INCLUDE_CONCURRENT_TASK_UTILS_H +#define CONCURRENT_TASK_COMMON_INCLUDE_CONCURRENT_TASK_UTILS_H + +#include +#include + + +namespace OHOS { +namespace ConcurrentTask { + +uint64_t GetAddrTag(void* addr); +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_COMMON_INCLUDE_CONCURRENT_TASK_UTILS_H \ No newline at end of file diff --git a/common/src/concurrent_task_utils.cpp b/common/src/concurrent_task_utils.cpp new file mode 100644 index 0000000..6fd0bb7 --- /dev/null +++ b/common/src/concurrent_task_utils.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "concurrent_task_utils.h" + +namespace OHOS { +namespace ConcurrentTask { + +uint64_t GetAddrTag(void* addr) { + uint64_t tag = 0; + if (addr != nullptr) { + tag = fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, (uint64_t)addr); + } + return tag; +} +} +} \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index 8c23109..a43a3df 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -53,6 +53,7 @@ ohos_shared_library("concurrentsvc") { ldflags = [ "-Wl,--exclude-libs=ALL" ] sources = [ + "../common/src/comcurrent_task_utils.cpp", "src/concurrent_task_controller_interface.cpp", "src/concurrent_task_service.cpp", "src/concurrent_task_service_ability.cpp", diff --git a/services/src/qos_interface.cpp b/services/src/qos_interface.cpp index ebabca7..79e84c4 100644 --- a/services/src/qos_interface.cpp +++ b/services/src/qos_interface.cpp @@ -27,6 +27,7 @@ #include #include "concurrent_task_log.h" +#include "concurrent_task_utils.h" static int TrivalOpenRtgNode(void) { @@ -61,14 +62,13 @@ int EnableRtg(bool flag) if (fd < 0) { return fd; } + fdsan_exchange_owner_tag(fd, 0, GetAddrTag((void*)&fd)); int ret = ioctl(fd, CMD_ID_SET_ENABLE, &enableData); if (ret < 0) { CONCUR_LOGE("set rtg config enable failed."); } - - close(fd); - + fdsan_close_with_tag(fd, GetAddr((void*)&fd)); return 0; }; @@ -85,6 +85,7 @@ int QosApplyForOther(unsigned int level, int tid) if (fd < 0) { return fd; } + fdsan_exchange_owner_tag(fd, 0, GetAddrTag((void*)&fd)); struct QosCtrlData data; data.level = level; @@ -95,7 +96,7 @@ int QosApplyForOther(unsigned int level, int tid) if (ret < 0) { CONCUR_LOGE("[Interface] task %{public}d apply qos failed, errno = %{public}d", tid, errno); } - close(fd); + fdsan_close_with_tag(fd, GetAddr((void*)&fd)); return ret; } @@ -105,6 +106,7 @@ int QosLeave(void) if (fd < 0) { return fd; } + fdsan_exchange_owner_tag(fd, 0, GetAddrTag((void*)&fd)); struct QosCtrlData data; data.type = static_cast(QosManipulateType::QOS_LEAVE); @@ -114,7 +116,7 @@ int QosLeave(void) if (ret < 0) { CONCUR_LOGE("[Interface] task %{public}d leave qos failed, errno = %{public}d", gettid(), errno); } - close(fd); + fdsan_close_with_tag(fd, GetAddr((void*)&fd)); return ret; } @@ -124,6 +126,7 @@ int QosLeaveForOther(int tid) if (fd < 0) { return fd; } + fdsan_exchange_owner_tag(fd, 0, GetAddrTag((void*)&fd)); struct QosCtrlData data; data.type = static_cast(QosManipulateType::QOS_LEAVE); @@ -133,7 +136,7 @@ int QosLeaveForOther(int tid) if (ret < 0) { CONCUR_LOGE("[Interface] task %{public}d leave qos failed, errno = %{public}d", tid, errno); } - close(fd); + fdsan_close_with_tag(fd, GetAddr((void*)&fd)); return ret; } @@ -143,12 +146,13 @@ int QosPolicySet(const struct QosPolicyDatas* policyDatas) if (fd < 0) { return fd; } + fdsan_exchange_owner_tag(fd, 0, GetAddrTag((void*)&fd)); int ret = ioctl(fd, QOS_CTRL_POLICY_OPERATION, policyDatas); if (ret < 0) { CONCUR_LOGE("[Interface] set qos policy failed, errno = %{public}d", errno); } - close(fd); + fdsan_close_with_tag(fd, GetAddr((void*)&fd)); return ret; } @@ -164,6 +168,7 @@ int QosGetForOther(int tid, int& level) if (fd < 0) { return fd; } + fdsan_exchange_owner_tag(fd, 0, GetAddrTag((void*)&fd)); struct QosCtrlData data; data.type = static_cast(QosManipulateType::QOS_GET); @@ -176,6 +181,6 @@ int QosGetForOther(int tid, int& level) } level = data.qos; - close(fd); + fdsan_close_with_tag(fd, GetAddr((void*)&fd)); return ret; } \ No newline at end of file -- Gitee