syscalls/capget01: Cleanup & convert to new library
PATCH 1/2 syscalls/capget01: Cleanup & convert to new library
Test capget() syscall on three versions and check the result buf. I don't want to use ".need_root", so we only can test Effective set by using ltp capability library code.
--- testcases/kernel/syscalls/capget/capget01.c | 161 ++++++-------------- 1 file changed, 49 insertions(+), 112 deletions(-)
testcases/kernel/syscalls/capget/capget01.c +49 -112
@@ -1,127 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. +* Author: Saji Kumar.V.R <[email protected]> * -* This program is free software; you can redistribute it and/or modify it -* under the terms of version 2 of the GNU General Public License as -* published by the Free Software Foundation. -* -* This program is distributed in the hope that it would be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* -* You should have received a copy of the GNU General Public License along -* with this program; if not, write the Free Software Foundation, Inc., -* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -* +* Description: +* This case tests capget() syscall whether works well on three versions. +* Also, it checks the results buffer. */ -/********************************************************** -* -* TEST IDENTIFIER : capget01 -* -* EXECUTED BY : anyone -* -* TEST TITLE : Basic test for capget(2) -* -* TEST CASE TOTAL : 1 -* -* AUTHOR : Saji Kumar.V.R <[email protected]> -* -* SIGNALS -* Uses SIGUSR1 to pause before test if option set. -* (See the parse_opts(3) man page). -* -* DESCRIPTION -* This is a Phase I test for the capget(2) system call. -* It is intended to provide a limited exposure of the system call. -* -* Setup: -* Setup signal handling. -* Pause for SIGUSR1 if option specified. -* -* Test: -* Loop if the proper options are given. -* call capget() -* if return value == 0 -* Test passed -* Otherwise -* Test failed -* -* Cleanup: -* Print errno log and/or timing stats if options given -* -* USAGE: <for command-line> -* capget01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -h : Show help screen -* -f : Turn off functional testing -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -p : Pause for SIGUSR1 before starting -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -****************************************************************/ - -#include <errno.h> -#include <unistd.h> -#include "test.h" +#include <sys/types.h> +#include "tst_test.h" #include "lapi/syscalls.h" -#include <linux/capability.h> -static void setup (); -static void cleanup (); - -char *TCID ="capget01" ; -int TST_TOTAL =1 ; +static pid_t pid; -static struct __user_cap_header_struct header;/* cap_user_header_t is a pointer -to __user_cap_header_struct */ +static struct tcase { +int version; +char *message; +} tcases[] = { + {0x19980330 ,"Test on LINUX_CAPABILITY_VERSION_1" }, + {0x20071026 ,"Test on LINUX_CAPABILITY_VERSION_2" }, + {0x20080522 ,"Test on LINUX_CAPABILITY_VERSION_3" }, +}; -static struct __user_cap_data_struct data;/* cap_user_data_t is a pointer to -__user_cap_data_struct */ - -int main (int ac,char **av) +static void verify_capget (unsigned int n) { - -int lc; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -/* header.version must be _LINUX_CAPABILITY_VERSION */ - header.version = _LINUX_CAPABILITY_VERSION; - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -TEST (ltp_syscall (__NR_capget, &header, &data)); - -if (TEST_RETURN ==0 ) { -tst_resm (TPASS ,"capget() returned %ld" ,TEST_RETURN ); - }else { -tst_resm (TFAIL |TTERRNO , -"Test Failed, capget() returned %ld" , -TEST_RETURN ); - } - } - -cleanup (); - -tst_exit (); +struct tcase *tc = &tcases[n]; +struct __user_cap_header_struct hdr = { + .version = tc->version, + .pid = pid, + }; + +struct __user_cap_data_struct data; + +tst_res (TINFO ,"%s" , tc->message); + +TEST (tst_syscall (__NR_capget, &hdr, &data)); +if (TST_RET ==0 ) +tst_res (TPASS ,"capget() returned %ld" ,TST_RET ); +else +tst_res (TFAIL |TTERRNO ,"Test Failed, capget() returned %ld" , +TST_RET ); + +if (data.effective &1 <<CAP_NET_RAW ) +tst_res (TFAIL ,"capget() gets CAP_NET_RAW unexpectedly in pE" ); +else +tst_res (TPASS ,"capget() doesn't get CAP_NET_RAW as expected in PE" ); } -void setup (void ) +static void setup (void ) { - -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; - + pid =getpid (); } -void cleanup (void ) -{ -} +static struct tst_test test = { + .setup = setup, + .tcnt =ARRAY_SIZE (tcases), + .test = verify_capget, + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_NET_RAW ), + {} + }, +};
Signature · 1 line
2.18.0
PATCH 2/2 syscalls/capget02: Cleanup & convert to new library
It also adds a check for whether kernel returns preferred linux capability version when we use a bad version.
--- testcases/kernel/syscalls/capget/capget02.c | 245 ++++++-------------- 1 file changed, 73 insertions(+), 172 deletions(-)
testcases/kernel/syscalls/capget/capget02.c +73 -172
@@ -1,199 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of version 2 of the GNU General Public License as -* published by the Free Software Foundation. -* -* This program is distributed in the hope that it would be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* -* You should have received a copy of the GNU General Public License along -* with this program; if not, write the Free Software Foundation, Inc., -* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -* +* Author: Saji Kumar.V.R <[email protected]> +* +* Tests basic error handling of the capget syscall. +* 1) capget() fails with errno set to EFAULT if an invalid address +* is given for header. +* 2) capget() fails with errno set to EFAULT if an invalid address +* is given for data +* 3) capget() fails with errno set to EINVAL if an invalid value +* is given for header->version +* 4) capget() fails with errno set to EINVAL if header->pid < 0 +* 5) capget() fails with errno set to ESRCH if the process with +* pid, header->pid does not exist. */ -/********************************************************** -* -* TEST IDENTIFIER : capget02 -* -* EXECUTED BY : anyone -* -* TEST TITLE : Tests for error conditions. -* -* TEST CASE TOTAL : 5 -* -* AUTHOR : Saji Kumar.V.R <[email protected]> -* -* SIGNALS -* Uses SIGUSR1 to pause before test if option set. -* (See the parse_opts(3) man page). -* -* DESCRIPTION -* Verify that -* 1) capget() fails with errno set to EFAULT if an invalid address -* is given for header -* 2) capget() fails with errno set to EFAULT if an invalid address -* is given for data -* 3) capget() fails with errno set to EINVAL if an invalid value -* is given for header->version -* 4) capget() fails with errno set to EINVAL if header->pid < 0 -* 5) capget() fails with errno set to ESRCH if the process with -* pid, header->pid does not exit -* -* -* Setup: -* Setup signal handling. -* Pause for SIGUSR1 if option specified. -* -* Test: -* Loop if the proper options are given. -* call capget with proper arguments -* if capget() fails with expected errno -* Test passed -* Otherwise -* Test failed -* -* Cleanup: -* Print errno log and/or timing stats if options given -* -* USAGE: <for command-line> -* capget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -h : Show help screen -* -f : Turn off functional testing -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -p : Pause for SIGUSR1 before starting -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* -****************************************************************/ -#include <errno.h> -#include "test.h" +#include <sys/types.h> +#include "tst_test.h" #include "lapi/syscalls.h" - -/**************************************************************************/ -/* */ -/* Some archs do not have the manpage documented sys/capability.h file, */ -/* and require the use of the line below */ -#include <linux/capability.h> -/* If you are having issues with including this file and have the sys/ */ -/* version, then you may want to try switching to it. -Robbie W. */ -/**************************************************************************/ - -static void setup (); -static void cleanup (); -static void test_setup (int ); - -char *TCID ="capget02" ; - -static struct __user_cap_header_struct header; +static struct __user_cap_header_struct header, bad_version_header, bad_pid_header, unused_pid_header;static struct __user_cap_data_struct data; -struct test_case_t { +static struct tcase {cap_user_header_t headerp;cap_user_data_t datap; -int exp_errno; -char *errdesc; -} test_cases[] = { -#ifndef UCLINUX -/* Skip since uClinux does not implement memory protection */ - { - (cap_user_header_t) -1 , &data,EFAULT ,"EFAULT" }, { - &header, (cap_user_data_t) -1 ,EFAULT ,"EFAULT" }, -#endif - { - &header, &data,EINVAL ,"EINVAL" }, { - &header, &data,EINVAL ,"EINVAL" }, { - &header, &data,ESRCH ,"ESRCH" } +int exp_err; +int reset_flag; +char *message; +} tcases[] = { + {NULL , &data,EFAULT ,0 ,"Test bad address header" }, + {&header,NULL ,EFAULT ,0 ,"Test bad address data" }, + {&bad_version_header, &data,EINVAL ,1 ,"Test bad version" }, + {&bad_pid_header, &data,EINVAL ,0 ,"Test bad pid" }, + {&unused_pid_header, &data,ESRCH ,0 ,"Test unused pid" }, }; -int TST_TOTAL =sizeof (test_cases) /sizeof (test_cases[0 ]); - -int main (int ac,char **av) +static void verify_capget (unsigned int n) { +struct tcase *tc = &tcases[n]; -int lc, i; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -for (i =0 ; i <TST_TOTAL ; ++i) { -test_setup (i); -TEST (ltp_syscall (__NR_capget, test_cases[i].headerp, - test_cases[i].datap)); - -if (TEST_RETURN ==-1 && -TEST_ERRNO == test_cases[i].exp_errno) { -tst_resm (TPASS |TTERRNO , -"capget failed as expected" ); - }else { -tst_resm (TFAIL |TTERRNO , -"capget failed unexpectedly (%ld)" , -TEST_RETURN ); - } - } +tst_res (TINFO ,"%s" , tc->message); +TEST (tst_syscall (__NR_capget, tc->headerp, tc->datap)); +if (TST_RET ==0 ) { +tst_res (TFAIL ,"capget() succeed unexpectedly" ); +return ; + } +if (TST_ERR == tc->exp_err) +tst_res (TPASS |TTERRNO ,"capget() failed as expected" ); +else +tst_res (TFAIL |TTERRNO ,"capget() expected %s got " , +tst_strerrno (tc->exp_err)); + +/* +* When an unsupported version value is specified, it will +* return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?. +* Since linux 2.6.26, version 3 is default. We use it. +*/ +if (tc->reset_flag) { +if (tc->headerp->version ==0x20080522 ) + tc->headerp->version =0 ; +else +tst_res (TFAIL ,"kernel doesn't return preferred linux" +" capability version when using bad version" ); } - -cleanup (); - -tst_exit (); - } -/* setup() - performs all ONE TIME setup for this test */ -void setup (void ) +static void setup (void ) { +unsigned int i, pid; -tst_sig (NOFORK ,DEF_HANDLER , cleanup); + pid =getpid (); -TEST_PAUSE ; + header.version =0x19980330 ; + header.pid = pid; -} + bad_version_header.version =0 ; + bad_version_header.pid = pid; -/* -*cleanup() - performs all ONE TIME cleanup for this test at -* completion or premature exit. -*/ -void cleanup (void ) -{ -} + bad_pid_header.version =0x19980330 ; + bad_pid_header.pid =-1 ; -void test_setup (int i) -{ -#ifdef UCLINUX - i = i +2 ; -#endif -switch (i) { + unused_pid_header.version =0x19980330 ; + unused_pid_header.pid =tst_get_unused_pid (); -case 0 : -break ; -case 1 : - header.version = _LINUX_CAPABILITY_VERSION; - header.pid =getpid (); -break ; -case 2 : - header.version =0 ; - header.pid =getpid (); -break ; -case 3 : - header.version = _LINUX_CAPABILITY_VERSION; - header.pid =-1 ; -break ; -case 4 : - header.version = _LINUX_CAPABILITY_VERSION; - header.pid =tst_get_unused_pid (cleanup); -break ; +for (i =0 ; i <ARRAY_SIZE (tcases); i++) { +if (!tcases[i].headerp) + tcases[i].headerp =tst_get_bad_addr (NULL ); +if (!tcases[i].datap) + tcases[i].datap =tst_get_bad_addr (NULL ); } } + +static struct tst_test test = { + .setup = setup, + .tcnt =ARRAY_SIZE (tcases), + .test = verify_capget, +};
Signature · 1 line
2.18.0
PATCH 2/2 syscalls/capget02: Cleanup & convert to new library
Hi!
Pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 syscalls/capget01: Cleanup & convert to new library
In reply to Yang XuHi!
Pushed with following changes:
testcases/kernel/syscalls/capget/capget01.c +3 -3
@@ -31,18 +31,18 @@ static void verify_capget(unsigned int n) .pid = pid, }; -struct __user_cap_data_struct data; +struct __user_cap_data_struct data[2 ];tst_res (TINFO ,"%s" , tc->message); -TEST (tst_syscall (__NR_capget, &hdr, &data)); +TEST (tst_syscall (__NR_capget, &hdr, data));if (TST_RET ==0 )tst_res (TPASS ,"capget() returned %ld" ,TST_RET );else tst_res (TFAIL |TTERRNO ,"Test Failed, capget() returned %ld" ,TST_RET ); -if (data.effective &1 <<CAP_NET_RAW ) +if (data[0 ].effective &1 <<CAP_NET_RAW )tst_res (TFAIL ,"capget() gets CAP_NET_RAW unexpectedly in pE" );else tst_res (TPASS ,"capget() doesn't get CAP_NET_RAW as expected in PE" );
The version 2 and 3 are 64 bit capabilities, hence we have to pass array of cap_struct_data instead.
Otherwise this is a nice improvement on the testcase, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 syscalls/capget01: Cleanup & convert to new library
Hi!
Also btw, we do not cover the case with pid == 0 now, which should return the capabilities for the calling thread. I guess that we can run the capget01 verify function twice, once for pid == getpid() and once for pid == 0.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 syscalls/capget01: Cleanup & convert to new library
Hi Cyril
Quoted 6 lines
Hi!
Also btw, we do not cover the case with pid == 0 now, which should return the capabilities for the calling thread. I guess that we can run the capget01 verify function twice, once for pid ==getpid()and once for pid == 0.
Sorry, I didn't notice this before.
I double read capget/capset man-pages and refer to kernel code. pid = 0 that it indeed returns the the capabilities for the calling thread. I will add it.
Also, for capget syscall, it can get the capabilities for any existed process. I think we should also test this by using child process in a new case like capset04.c.
Best Regards
Yang Xu