From c9d69e2d1bde57c67068cc36f6a79327d5db5daf Mon Sep 17 00:00:00 2001 From: wcc0 <917033401@qq.com> Date: Thu, 1 Jul 2021 16:44:26 +0800 Subject: [PATCH] fix: add capability and amend smoke testcase add setrlimit,gethostname,gethostid and capability Change-Id: I0d5f23cb87ec2731fb79e7c5cfbe1ce109ac158a --- compat/posix/src/misc.c | 46 +++++++++++++++++ kernel/base/include/los_process_pri.h | 2 + security/cap/capability_type.h | 2 +- syscall/los_syscall.h | 3 ++ syscall/misc_syscall.c | 51 +++++++++++++++++++ syscall/syscall_lookup.h | 3 ++ testsuites/unittest/misc/BUILD.gn | 6 +-- .../misc/{smoke => full}/misc_test_008.cpp | 0 .../misc/{smoke => full}/misc_test_009.cpp | 0 testsuites/unittest/misc/misc_test.cpp | 37 +++++++++++++- 10 files changed, 144 insertions(+), 6 deletions(-) rename testsuites/unittest/misc/{smoke => full}/misc_test_008.cpp (100%) rename testsuites/unittest/misc/{smoke => full}/misc_test_009.cpp (100%) diff --git a/compat/posix/src/misc.c b/compat/posix/src/misc.c index e740930f..becef6c4 100644 --- a/compat/posix/src/misc.c +++ b/compat/posix/src/misc.c @@ -30,12 +30,14 @@ */ #include "sys/types.h" +#include "sys/resource.h" #include "unistd.h" #include "stdio.h" #include "pthread.h" #include "sys/utsname.h" #include "mqueue.h" #include "semaphore.h" +#include "los_process_pri.h" /* @@ -140,3 +142,47 @@ pid_t getpid(void) return ((LosTaskCB *)(OsCurrTaskGet()))->taskID; } +int getrlimit(int resource, struct rlimit *rlim) +{ + LosProcessCB *pcb = OsCurrProcessGet(); + + switch (resource) { + case RLIMIT_NOFILE: + case RLIMIT_FSIZE: + break; + default: + return -EINVAL; + } + rlim->rlim_cur = pcb->pl_rlimit[resource].rlim_cur; + rlim->rlim_max = pcb->pl_rlimit[resource].rlim_max; + + return 0; +} + +#define FSIZE_RLIMIT 0XFFFFFFFF +int setrlimit(int resource, const struct rlimit *rlim) +{ + LosProcessCB *pcb = OsCurrProcessGet(); + + if (rlim->rlim_cur > rlim->rlim_max) { + return -EINVAL; + } + switch (resource) { + case RLIMIT_NOFILE: + if (rlim->rlim_max > NR_OPEN_DEFAULT) { + return -EPERM; + } + break; + case RLIMIT_FSIZE: + if (rlim->rlim_max > FSIZE_RLIMIT) { + return -EPERM; + } + break; + default: + return -EINVAL; + } + pcb->pl_rlimit[resource].rlim_cur = rlim->rlim_cur; + pcb->pl_rlimit[resource].rlim_max = rlim->rlim_max; + + return 0; +} \ No newline at end of file diff --git a/kernel/base/include/los_process_pri.h b/kernel/base/include/los_process_pri.h index e07ea820..2f987fe6 100644 --- a/kernel/base/include/los_process_pri.h +++ b/kernel/base/include/los_process_pri.h @@ -45,6 +45,7 @@ #ifdef LOSCFG_SECURITY_VID #include "vid_type.h" #endif +#include "sys/resource.h" #ifdef __cplusplus #if __cplusplus @@ -126,6 +127,7 @@ typedef struct ProcessCB { #ifdef LOSCFG_KERNEL_CPUP OsCpupBase processCpup; /**< Process cpu usage */ #endif + struct rlimit pl_rlimit[RLIM_NLIMITS]; } LosProcessCB; #define CLONE_VM 0x00000100 diff --git a/security/cap/capability_type.h b/security/cap/capability_type.h index b5163325..dc239e34 100644 --- a/security/cap/capability_type.h +++ b/security/cap/capability_type.h @@ -66,4 +66,4 @@ #define CAP_REBOOT 18 // self deined privileged syscalls #define CAP_SHELL_EXEC 19 -#endif \ No newline at end of file +#endif diff --git a/syscall/los_syscall.h b/syscall/los_syscall.h index 14995d43..1d7a8c16 100644 --- a/syscall/los_syscall.h +++ b/syscall/los_syscall.h @@ -287,5 +287,8 @@ extern int SysUmask(int mask); extern int SysShellExec(const char *msgName, const char *cmdString); extern int SysReboot(int magic, int magic2, int type); extern int SysGetrusage(int what, struct rusage *ru); +extern long SysSysconf(int name); +extern int SysUgetrlimit(int resource, unsigned long long k_rlim[2]); +extern int SysSetrlimit(int resource, unsigned long long k_rlim[2]); #endif #endif /* _LOS_SYSCALL_H */ diff --git a/syscall/misc_syscall.c b/syscall/misc_syscall.c index 29b571d9..e27afd32 100644 --- a/syscall/misc_syscall.c +++ b/syscall/misc_syscall.c @@ -45,6 +45,10 @@ #include "shmsg.h" #endif #include "user_copy.h" +#include "los_strncpy_from_user.h" +#include "capability_type.h" +#include "capability_api.h" +#include "unistd.h" int SysUname(struct utsname *name) @@ -193,3 +197,50 @@ int SysGetrusage(int what, struct rusage *ru) return 0; } +long SysSysconf(int name) +{ + long ret; + + ret = sysconf(name); + if (ret == -1) { + return -get_errno(); + } + + return ret; +} + +int SysUgetrlimit(int resource, unsigned long long k_rlim[2]) +{ + int ret; + struct rlimit lim; + + ret = getrlimit(resource, &lim); + if (ret < 0) { + return ret; + } + + ret = LOS_ArchCopyToUser(k_rlim, &lim, sizeof(struct rlimit)); + if (ret != 0) { + return -EFAULT; + } + + return ret; +} + +int SysSetrlimit(int resource, unsigned long long k_rlim[2]) +{ + int ret; + struct rlimit lim; + + if(!IsCapPermit(CAP_CAPSET)) { + return -EPERM; + } + + ret = LOS_ArchCopyFromUser(&lim, k_rlim, sizeof(struct rlimit)); + if (ret != 0) { + return -EFAULT; + } + ret = setrlimit(resource, &lim); + + return ret; +} diff --git a/syscall/syscall_lookup.h b/syscall/syscall_lookup.h index 236f7a25..8206d735 100644 --- a/syscall/syscall_lookup.h +++ b/syscall/syscall_lookup.h @@ -246,3 +246,6 @@ SYSCALL_HAND_DEF(__NR_pthread_join, SysThreadJoin, int, ARG_NUM_1) SYSCALL_HAND_DEF(__NR_pthread_deatch, SysUserThreadDetach, int, ARG_NUM_1) SYSCALL_HAND_DEF(__NR_creat_user_thread, SysCreateUserThread, unsigned int, ARG_NUM_3) SYSCALL_HAND_DEF(__NR_getrusage, SysGetrusage, int, ARG_NUM_2) +SYSCALL_HAND_DEF(__NR_sysconf, SysSysconf, long, ARG_NUM_1) +SYSCALL_HAND_DEF(__NR_ugetrlimit, SysUgetrlimit, int, ARG_NUM_2) +SYSCALL_HAND_DEF(__NR_setrlimit, SysSetrlimit, int, ARG_NUM_2) \ No newline at end of file diff --git a/testsuites/unittest/misc/BUILD.gn b/testsuites/unittest/misc/BUILD.gn index ca9be5e1..96b1af96 100644 --- a/testsuites/unittest/misc/BUILD.gn +++ b/testsuites/unittest/misc/BUILD.gn @@ -44,6 +44,8 @@ sources_entry = [ sources_full = [ "full/misc_test_006.cpp", "full/misc_test_007.cpp", + "full/misc_test_008.cpp", + "full/misc_test_009.cpp", "full/misc_test_010.cpp", "full/misc_test_011.cpp", "full/misc_test_012.cpp", @@ -56,8 +58,6 @@ sources_smoke = [ "smoke/misc_test_003.cpp", "smoke/misc_test_004.cpp", "smoke/misc_test_005.cpp", - "smoke/misc_test_008.cpp", - "smoke/misc_test_009.cpp", "smoke/misc_test_014.cpp", ] @@ -86,4 +86,4 @@ if (LOSCFG_USER_TEST_LEVEL >= TEST_LEVEL_MIDDLE) { configs = [ "..:public_config_for_all" ] deps = [ "//third_party/bounds_checking_function:libsec_shared" ] } -} \ No newline at end of file +} diff --git a/testsuites/unittest/misc/smoke/misc_test_008.cpp b/testsuites/unittest/misc/full/misc_test_008.cpp similarity index 100% rename from testsuites/unittest/misc/smoke/misc_test_008.cpp rename to testsuites/unittest/misc/full/misc_test_008.cpp diff --git a/testsuites/unittest/misc/smoke/misc_test_009.cpp b/testsuites/unittest/misc/full/misc_test_009.cpp similarity index 100% rename from testsuites/unittest/misc/smoke/misc_test_009.cpp rename to testsuites/unittest/misc/full/misc_test_009.cpp diff --git a/testsuites/unittest/misc/misc_test.cpp b/testsuites/unittest/misc/misc_test.cpp index 2bcc66e1..bd49e9e1 100644 --- a/testsuites/unittest/misc/misc_test.cpp +++ b/testsuites/unittest/misc/misc_test.cpp @@ -128,10 +128,32 @@ HWTEST_F(MiscTest, ItTestMisc006, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000EEMQ9 */ -/*HWTEST_F(MiscTest, ItTestMisc007, TestSize.Level0) +HWTEST_F(MiscTest, ItTestMisc007, TestSize.Level0) { ItTestMisc007(); -}*/ +} + +/* * + * @tc.name: IT_TEST_MISC_008 + * @tc.desc: function for MiscTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(MiscTest, ItTestMisc008, TestSize.Level0) +{ + ItTestMisc008(); +} + +/* * + * @tc.name: IT_TEST_MISC_009 + * @tc.desc: function for MiscTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(MiscTest, ItTestMisc009, TestSize.Level0) +{ + ItTestMisc009(); +} /* * * @tc.name: IT_TEST_MISC_010 @@ -155,6 +177,17 @@ HWTEST_F(MiscTest, ItTestMisc006, TestSize.Level0) ItTestMisc011(); }*/ +/* * + * @tc.name: IT_TEST_MISC_012 + * @tc.desc: function for MiscTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(MiscTest, ItTestMisc012, TestSize.Level0) +{ + ItTestMisc012(); +} + /* * * @tc.name: IT_TEST_MISC_013 * @tc.desc: function for MiscTest -- GitLab