From 9b7348dcea0ccbabc5773e7136083e061bb12c9a Mon Sep 17 00:00:00 2001 From: huadongqin Date: Thu, 13 Jan 2022 10:24:55 +0800 Subject: [PATCH 1/5] Signed-off-by: huadongqin Change-Id: I35ccad6dcd68f1ec3b0c6c2a66bc1afc2c7713de --- BUILD.gn | 2 + interfaces/innerkits/token_setproc/BUILD.gn | 44 +++++++ .../token_setproc/include/token_setproc.h | 37 ++++++ .../token_setproc/src/token_setproc.c | 114 ++++++++++++++++++ .../innerkits/token_setproc/test/BUILD.gn | 43 +++++++ .../cpp/src/tokensetproc_kit_test.cpp | 33 +++++ .../unittest/cpp/src/tokensetproc_kit_test.h | 35 ++++++ 7 files changed, 308 insertions(+) create mode 100644 interfaces/innerkits/token_setproc/BUILD.gn create mode 100644 interfaces/innerkits/token_setproc/include/token_setproc.h create mode 100644 interfaces/innerkits/token_setproc/src/token_setproc.c create mode 100644 interfaces/innerkits/token_setproc/test/BUILD.gn create mode 100644 interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp create mode 100644 interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h diff --git a/BUILD.gn b/BUILD.gn index b9397ed3d..1a7fc27f6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -19,6 +19,7 @@ group("accesstoken_build_module") { deps = [ "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", "//base/security/access_token/interfaces/innerkits/nativetoken:libaccesstoken_lib", + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", "//base/security/access_token/services/accesstokenmanager:accesstoken_manager_service", "//base/security/access_token/services/accesstokenmanager/main/sa_profile:accesstoken_sa_profile_standard", ] @@ -43,6 +44,7 @@ group("accesstoken_build_module_test") { "//base/security/access_token/interfaces/innerkits/accesstoken/test:unittest", "//base/security/access_token/interfaces/innerkits/nativetoken/test:unittest", "//base/security/access_token/interfaces/innerkits/tokensync/test:unittest", + "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", ] } diff --git a/interfaces/innerkits/token_setproc/BUILD.gn b/interfaces/innerkits/token_setproc/BUILD.gn new file mode 100644 index 000000000..a214169fd --- /dev/null +++ b/interfaces/innerkits/token_setproc/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright (C) 2021 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. + +import("//build/ohos.gni") + +################################################################ +# C, Main, source file here. +################################################################ +config("token_setproc") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +ohos_static_library("libtoken_setproc") { + subsystem_name = "security" + part_name = "access_token" + + output_name = "libtoken_setproc" + + public_configs = [ ":token_setproc" ] + + cflags = [ "-Wall" ] + + include_dirs = [ + "include", + "src", + ] + + sources = [ + "src/token_setproc.c", + ] + + deps = [] +} diff --git a/interfaces/innerkits/token_setproc/include/token_setproc.h b/interfaces/innerkits/token_setproc/include/token_setproc.h new file mode 100644 index 000000000..f95ae7147 --- /dev/null +++ b/interfaces/innerkits/token_setproc/include/token_setproc.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 TOKEN_setproc_H +#define TOKEN_setproc_H +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +uint64_t GetSelfTokenID(); + +int SetSelfTokenID(uint64_t tokenID); + +uint64_t GetFirstCallerTokenID(); + +int SetFirstCallerTokenID(uint64_t tokenID); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/src/token_setproc.c b/interfaces/innerkits/token_setproc/src/token_setproc.c new file mode 100644 index 000000000..59420d191 --- /dev/null +++ b/interfaces/innerkits/token_setproc/src/token_setproc.c @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2021 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 "token_setproc.h" +#include +#include +#include +#include +#include + +#define ACCESS_TOKEN_ID_IOCTL_BASE 'A' + +enum { + GET_TOKEN_ID = 1, + SET_TOKEN_ID, + GET_FTOKEN_ID, + SET_FTOKEN_ID, + ACCESS_TOKENID_MAX_NR, +}; + +#define ACCESS_TOKENID_GET_TOKENID \ + _IOR(ACCESS_TOKEN_ID_IOCTL_BASE,GET_TOKEN_ID,unsigned long long) +#define ACCESS_TOKENID_SET_TOKENID \ + _IOW(ACCESS_TOKEN_ID_IOCTL_BASE,SET_TOKEN_ID,unsigned long long) +#define ACCESS_TOKENID_GET_FTOKENID\ + _IOR(ACCESS_TOKEN_ID_IOCTL_BASE,GET_FTOKEN_ID,unsigned long long) +#define ACCESS_TOKENID_SET_FTOKENID \ + _IOW(ACCESS_TOKEN_ID_IOCTL_BASE,SET_FTOKEN_ID,unsigned long long) + +#define ACCESS_TOKEN_OK 0 +#define ACCESS_TOKEN_ERROR (-1) + +#define INVAL_TOKEN_ID 0x0 +#define TOKEN_ID_LOWMASK 0xffffffff + +#define TOKENID_DEVNODE "/dev/access_token_id" + +uint64_t GetSelfTokenID() +{ + uint64_t token = INVAL_TOKEN_ID; + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return INVAL_TOKEN_ID; + } + int ret =ioctl(fd,ACCESS_TOKENID_GET_TOKENID,&token); + if (ret) { + close(fd); + return INVAL_TOKEN_ID; + } + + close(fd); + return token; +} + +int SetSelfTokenID(uint64_t tokenID) +{ + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return ACCESS_TOKEN_ERROR; + } + int ret = ioctl(fd,ACCESS_TOKENID_SET_TOKENID,&tokenID); + if (ret) { + close(fd); + return ACCESS_TOKEN_ERROR; + } + + close(fd); + return ACCESS_TOKEN_OK; +} + +uint64_t GetFirstCallerTokenID() +{ + uint64_t token = INVAL_TOKEN_ID; + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return INVAL_TOKEN_ID; + } + int ret = ioctl(fd,ACCESS_TOKENID_GET_FTOKENID,&token); + if (ret) { + close(fd); + return INVAL_TOKEN_ID; + } + + close(fd); + return token; +} + +int SetFirstCallerTokenID(uint64_t tokenID) +{ + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return ACCESS_TOKEN_ERROR; + } + int ret = ioctl(fd,ACCESS_TOKENID_SET_FTOKENID,&tokenID); + if (ret) { + close(fd); + return ACCESS_TOKEN_ERROR; + } + + close(fd); + return ACCESS_TOKEN_OK; +} \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/test/BUILD.gn b/interfaces/innerkits/token_setproc/test/BUILD.gn new file mode 100644 index 000000000..de0c63587 --- /dev/null +++ b/interfaces/innerkits/token_setproc/test/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (C) 2021 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. + +import("//build/test.gni") + +ohos_unittest("libtoken_setproc_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//base/security/access_token/interfaces/innerkits/token_setproc/include", + ] + + sources = [ + "unittest/cpp/src/tokensetproc_kit_test.cpp", + ] + + cflags_cc = [ "-fexceptions" ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + ] +} + +group("unittest") { + testonly = true + deps = [ + ":libtoken_setproc_test", + ] +} diff --git a/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp new file mode 100644 index 000000000..4463b0954 --- /dev/null +++ b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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 "tokensetproc_kit_test.h" + +#include "token_setproc.h" + +using namespace testing::ext; +using namespace OHOS::Security; + +void TokensetprocKitTest::SetUpTestCase() +{} + +void TokensetprocKitTest::TearDownTestCase() +{} + +void TokensetprocKitTest::SetUp() +{} + +void TokensetprocKitTest::TearDown() +{} \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h new file mode 100644 index 000000000..c215f7a96 --- /dev/null +++ b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 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 TOKENSYNC_KIT_TEST_H +#define TOKENSYNC_KIT_TEST_H + +#include + +namespace OHOS { +namespace Security { +class TokensetprocKitTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace Security +} // namespace OHOS +#endif \ No newline at end of file -- Gitee From ab6a1f7729cb992f6841009143c9121595f6e14a Mon Sep 17 00:00:00 2001 From: huadongqin Date: Thu, 13 Jan 2022 10:39:27 +0800 Subject: [PATCH 2/5] Signed-off-by: huadongqin Change-Id: I35ccad6dcd68f1ec3b0c6c2a66bc1afc2c7713de --- BUILD.gn | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 1a7fc27f6..d0e402760 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -43,13 +43,12 @@ group("accesstoken_build_module_test") { deps += [ "//base/security/access_token/interfaces/innerkits/accesstoken/test:unittest", "//base/security/access_token/interfaces/innerkits/nativetoken/test:unittest", - "//base/security/access_token/interfaces/innerkits/tokensync/test:unittest", "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", + "//base/security/access_token/interfaces/innerkits/tokensync/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", ] } } - #group("distributed_permission") { # if (is_standard_system) { # deps = [ -- Gitee From f0bee9f46519c9599cbbd531f377516d6391c168 Mon Sep 17 00:00:00 2001 From: huadongqin Date: Thu, 13 Jan 2022 10:54:54 +0800 Subject: [PATCH 3/5] Signed-off-by: huadongqin Change-Id: I35ccad6dcd68f1ec3b0c6c2a66bc1afc2c7713de --- .../token_setproc/include/token_setproc.h | 6 ++-- .../token_setproc/src/token_setproc.c | 30 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/interfaces/innerkits/token_setproc/include/token_setproc.h b/interfaces/innerkits/token_setproc/include/token_setproc.h index f95ae7147..2a6b47574 100644 --- a/interfaces/innerkits/token_setproc/include/token_setproc.h +++ b/interfaces/innerkits/token_setproc/include/token_setproc.h @@ -19,14 +19,14 @@ #include #ifdef __cplusplus -extern "C"{ +extern "C" { #endif -uint64_t GetSelfTokenID(); +uint64_t GetSelfTokenID(void); int SetSelfTokenID(uint64_t tokenID); -uint64_t GetFirstCallerTokenID(); +uint64_t GetFirstCallerTokenID(void); int SetFirstCallerTokenID(uint64_t tokenID); diff --git a/interfaces/innerkits/token_setproc/src/token_setproc.c b/interfaces/innerkits/token_setproc/src/token_setproc.c index 59420d191..8a793c25d 100644 --- a/interfaces/innerkits/token_setproc/src/token_setproc.c +++ b/interfaces/innerkits/token_setproc/src/token_setproc.c @@ -31,13 +31,13 @@ enum { }; #define ACCESS_TOKENID_GET_TOKENID \ - _IOR(ACCESS_TOKEN_ID_IOCTL_BASE,GET_TOKEN_ID,unsigned long long) + _IOR(ACCESS_TOKEN_ID_IOCTL_BASE, GET_TOKEN_ID, unsigned long long) #define ACCESS_TOKENID_SET_TOKENID \ - _IOW(ACCESS_TOKEN_ID_IOCTL_BASE,SET_TOKEN_ID,unsigned long long) -#define ACCESS_TOKENID_GET_FTOKENID\ - _IOR(ACCESS_TOKEN_ID_IOCTL_BASE,GET_FTOKEN_ID,unsigned long long) + _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_TOKEN_ID, unsigned long long) +#define ACCESS_TOKENID_GET_FTOKENID \ + _IOR(ACCESS_TOKEN_ID_IOCTL_BASE, GET_FTOKEN_ID, unsigned long long) #define ACCESS_TOKENID_SET_FTOKENID \ - _IOW(ACCESS_TOKEN_ID_IOCTL_BASE,SET_FTOKEN_ID,unsigned long long) + _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_FTOKEN_ID, unsigned long long) #define ACCESS_TOKEN_OK 0 #define ACCESS_TOKEN_ERROR (-1) @@ -47,14 +47,14 @@ enum { #define TOKENID_DEVNODE "/dev/access_token_id" -uint64_t GetSelfTokenID() +uint64_t GetSelfTokenID(void) { uint64_t token = INVAL_TOKEN_ID; - int fd = open(TOKENID_DEVNODE,O_RDWR); + int fd = open(TOKENID_DEVNODE, O_RDWR); if (fd < 0) { return INVAL_TOKEN_ID; } - int ret =ioctl(fd,ACCESS_TOKENID_GET_TOKENID,&token); + int ret =ioctl(fd, ACCESS_TOKENID_GET_TOKENID, &token); if (ret) { close(fd); return INVAL_TOKEN_ID; @@ -66,11 +66,11 @@ uint64_t GetSelfTokenID() int SetSelfTokenID(uint64_t tokenID) { - int fd = open(TOKENID_DEVNODE,O_RDWR); + int fd = open(TOKENID_DEVNODE, O_RDWR); if (fd < 0) { return ACCESS_TOKEN_ERROR; } - int ret = ioctl(fd,ACCESS_TOKENID_SET_TOKENID,&tokenID); + int ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, &tokenID); if (ret) { close(fd); return ACCESS_TOKEN_ERROR; @@ -80,14 +80,14 @@ int SetSelfTokenID(uint64_t tokenID) return ACCESS_TOKEN_OK; } -uint64_t GetFirstCallerTokenID() +uint64_t GetFirstCallerTokenID(void) { uint64_t token = INVAL_TOKEN_ID; - int fd = open(TOKENID_DEVNODE,O_RDWR); + int fd = open(TOKENID_DEVNODE, O_RDWR); if (fd < 0) { return INVAL_TOKEN_ID; } - int ret = ioctl(fd,ACCESS_TOKENID_GET_FTOKENID,&token); + int ret = ioctl(fd, ACCESS_TOKENID_GET_FTOKENID, &token); if (ret) { close(fd); return INVAL_TOKEN_ID; @@ -99,11 +99,11 @@ uint64_t GetFirstCallerTokenID() int SetFirstCallerTokenID(uint64_t tokenID) { - int fd = open(TOKENID_DEVNODE,O_RDWR); + int fd = open(TOKENID_DEVNODE, O_RDWR); if (fd < 0) { return ACCESS_TOKEN_ERROR; } - int ret = ioctl(fd,ACCESS_TOKENID_SET_FTOKENID,&tokenID); + int ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, &tokenID); if (ret) { close(fd); return ACCESS_TOKEN_ERROR; -- Gitee From 26d6c19ed41d117161f982067624a6d335b634ff Mon Sep 17 00:00:00 2001 From: huadongqin Date: Thu, 13 Jan 2022 11:11:24 +0800 Subject: [PATCH 4/5] Signed-off-by: huadongqin Change-Id: I35ccad6dcd68f1ec3b0c6c2a66bc1afc2c7713de --- BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index d0e402760..5f5cae979 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -13,7 +13,6 @@ #import("//base/security/permission/permission.gni") import("//build/ohos.gni") - group("accesstoken_build_module") { if (is_standard_system) { deps = [ -- Gitee From 8f7adb134e3e087d51d8b4346970ccce20840323 Mon Sep 17 00:00:00 2001 From: huadongqin Date: Thu, 13 Jan 2022 11:30:42 +0800 Subject: [PATCH 5/5] Signed-off-by: huadongqin Change-Id: I35ccad6dcd68f1ec3b0c6c2a66bc1afc2c7713de --- interfaces/innerkits/token_setproc/BUILD.gn | 4 +--- interfaces/innerkits/token_setproc/test/BUILD.gn | 8 ++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/interfaces/innerkits/token_setproc/BUILD.gn b/interfaces/innerkits/token_setproc/BUILD.gn index a214169fd..74e429ff1 100644 --- a/interfaces/innerkits/token_setproc/BUILD.gn +++ b/interfaces/innerkits/token_setproc/BUILD.gn @@ -36,9 +36,7 @@ ohos_static_library("libtoken_setproc") { "src", ] - sources = [ - "src/token_setproc.c", - ] + sources = [ "src/token_setproc.c" ] deps = [] } diff --git a/interfaces/innerkits/token_setproc/test/BUILD.gn b/interfaces/innerkits/token_setproc/test/BUILD.gn index de0c63587..14c1dbf80 100644 --- a/interfaces/innerkits/token_setproc/test/BUILD.gn +++ b/interfaces/innerkits/token_setproc/test/BUILD.gn @@ -22,9 +22,7 @@ ohos_unittest("libtoken_setproc_test") { "//base/security/access_token/interfaces/innerkits/token_setproc/include", ] - sources = [ - "unittest/cpp/src/tokensetproc_kit_test.cpp", - ] + sources = [ "unittest/cpp/src/tokensetproc_kit_test.cpp" ] cflags_cc = [ "-fexceptions" ] @@ -37,7 +35,5 @@ ohos_unittest("libtoken_setproc_test") { group("unittest") { testonly = true - deps = [ - ":libtoken_setproc_test", - ] + deps = [ ":libtoken_setproc_test" ] } -- Gitee