blob: e1b74558ac5ee0a0803456c5798c5ba9751f2220 [file] [log] [blame]
robbiewd771da72002-12-27 18:41:54 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiewd771da72002-12-27 18:41:54 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewd771da72002-12-27 18:41:54 +000019 * TEST IDENTIFIER : capget02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiewd771da72002-12-27 18:41:54 +000021 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewd771da72002-12-27 18:41:54 +000023 * TEST TITLE : Tests for error conditions.
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiewd771da72002-12-27 18:41:54 +000025 * TEST CASE TOTAL : 5
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiewd771da72002-12-27 18:41:54 +000027 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiewd771da72002-12-27 18:41:54 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * Verify that
35 * 1) capget() fails with errno set to EFAULT if an invalid address
36 * is given for header
37 * 2) capget() fails with errno set to EFAULT if an invalid address
38 * is given for data
39 * 3) capget() fails with errno set to EINVAL if an invalid value
40 * is given for header->version
41 * 4) capget() fails with errno set to EINVAL if header->pid < 0
42 * 5) capget() fails with errno set to ESRCH if the process with
43 * pid, header->pid does not exit
subrata_modakbdbaec52009-02-26 12:14:51 +000044 *
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
robbiewd771da72002-12-27 18:41:54 +000046 * Setup:
47 * Setup signal handling.
48 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
robbiewd771da72002-12-27 18:41:54 +000050 * Test:
51 * Loop if the proper options are given.
52 * call capget with proper arguments
53 * if capget() fails with expected errno
54 * Test passed
55 * Otherwise
56 * Test failed
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
robbiewd771da72002-12-27 18:41:54 +000058 * Cleanup:
59 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000060 *
robbiewd771da72002-12-27 18:41:54 +000061 * USAGE: <for command-line>
62 * capget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
63 * where, -c n : Run n copies concurrently.
64 * -e : Turn on errno logging.
65 * -h : Show help screen
66 * -f : Turn off functional testing
67 * -i n : Execute test n times.
68 * -I x : Execute test for x seconds.
69 * -p : Pause for SIGUSR1 before starting
70 * -P x : Pause for x seconds between iterations.
71 * -t : Turn on syscall timing.
72 *
73 ****************************************************************/
robbiewd771da72002-12-27 18:41:54 +000074
75#include <errno.h>
robbiewd771da72002-12-27 18:41:54 +000076#include "test.h"
Garrett Cooper17b121b2010-12-17 11:05:05 -080077#include "linux_syscall_numbers.h"
robbiewe7757592003-10-01 17:23:49 +000078
79/**************************************************************************/
robbiewe7757592003-10-01 17:23:49 +000080/* */
81/* Some archs do not have the manpage documented sys/capability.h file, */
robbiew04a8ba52003-10-02 17:38:04 +000082/* and require the use of the line below */
83
84#include <linux/capability.h>
85
86/* If you are having issues with including this file and have the sys/ */
87/* version, then you may want to try switching to it. -Robbie W. */
robbiewe7757592003-10-01 17:23:49 +000088/**************************************************************************/
robbiewd771da72002-12-27 18:41:54 +000089
robbiewd771da72002-12-27 18:41:54 +000090static void setup();
91static void cleanup();
92static void test_setup(int);
93
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020094char *TCID = "capget02";
robbiewd771da72002-12-27 18:41:54 +000095
96static struct __user_cap_header_struct header;
97static struct __user_cap_data_struct data;
98
99struct test_case_t {
100 cap_user_header_t headerp;
101 cap_user_data_t datap;
102 int exp_errno;
103 char *errdesc;
104} test_cases[] = {
vapier7ec19d92006-02-27 04:38:56 +0000105#ifndef UCLINUX
106 /* Skip since uClinux does not implement memory protection */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 {
108 (cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, {
109 &header, (cap_user_data_t) - 1, EFAULT, "EFAULT"},
vapier7ec19d92006-02-27 04:38:56 +0000110#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 {
112 &header, &data, EINVAL, "EINVAL"}, {
113 &header, &data, EINVAL, "EINVAL"}, {
114 &header, &data, ESRCH, "ESRCH"}
robbiewd771da72002-12-27 18:41:54 +0000115};
116
117int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
118
subrata_modak56207ce2009-03-23 13:35:39 +0000119int main(int ac, char **av)
robbiewd771da72002-12-27 18:41:54 +0000120{
121
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200122 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200123 const char *msg;
robbiewd771da72002-12-27 18:41:54 +0000124
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800125 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd771da72002-12-27 18:41:54 +0000127
robbiewd771da72002-12-27 18:41:54 +0000128 setup();
129
robbiewd771da72002-12-27 18:41:54 +0000130 for (lc = 0; TEST_LOOPING(lc); lc++) {
131
Caspar Zhangd59a6592013-03-07 14:59:12 +0800132 tst_count = 0;
robbiewd771da72002-12-27 18:41:54 +0000133
134 for (i = 0; i < TST_TOTAL; ++i) {
135 test_setup(i);
Jan Stancek359980f2013-02-15 10:16:05 +0100136 TEST(ltp_syscall(__NR_capget, test_cases[i].headerp,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 test_cases[i].datap));
robbiewd771da72002-12-27 18:41:54 +0000138
Garrett Cooper17b121b2010-12-17 11:05:05 -0800139 if (TEST_RETURN == -1 &&
140 TEST_ERRNO == test_cases[i].exp_errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_resm(TPASS | TTERRNO,
142 "capget failed as expected");
robbiewd771da72002-12-27 18:41:54 +0000143 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 tst_resm(TFAIL | TTERRNO,
145 "capget failed unexpectedly (%ld)",
146 TEST_RETURN);
robbiewd771da72002-12-27 18:41:54 +0000147 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000148 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800149 }
robbiewd771da72002-12-27 18:41:54 +0000150
robbiewd771da72002-12-27 18:41:54 +0000151 cleanup();
152
Garrett Cooper53740502010-12-16 00:04:01 -0800153 tst_exit();
robbiewd771da72002-12-27 18:41:54 +0000154
Garrett Cooper2c282152010-12-16 00:55:50 -0800155}
robbiewd771da72002-12-27 18:41:54 +0000156
157/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400158void setup(void)
robbiewd771da72002-12-27 18:41:54 +0000159{
Garrett Cooper2c282152010-12-16 00:55:50 -0800160
robbiewd771da72002-12-27 18:41:54 +0000161 tst_sig(NOFORK, DEF_HANDLER, cleanup);
162
robbiewd771da72002-12-27 18:41:54 +0000163 TEST_PAUSE;
164
Garrett Cooper2c282152010-12-16 00:55:50 -0800165}
robbiewd771da72002-12-27 18:41:54 +0000166
167/*
168 *cleanup() - performs all ONE TIME cleanup for this test at
169 * completion or premature exit.
170 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400171void cleanup(void)
robbiewd771da72002-12-27 18:41:54 +0000172{
Garrett Cooper2c282152010-12-16 00:55:50 -0800173}
robbiewd771da72002-12-27 18:41:54 +0000174
subrata_modak56207ce2009-03-23 13:35:39 +0000175void test_setup(int i)
robbiewd771da72002-12-27 18:41:54 +0000176{
vapier7ec19d92006-02-27 04:38:56 +0000177#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000178 i = i + 2;
vapier7ec19d92006-02-27 04:38:56 +0000179#endif
robbiewd771da72002-12-27 18:41:54 +0000180 switch (i) {
181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 case 0:
robbiewd771da72002-12-27 18:41:54 +0000183 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000184 case 1:
robbiewd771da72002-12-27 18:41:54 +0000185 header.version = _LINUX_CAPABILITY_VERSION;
186 header.pid = getpid();
187 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000188 case 2:
robbiewd771da72002-12-27 18:41:54 +0000189 header.version = 0;
190 header.pid = getpid();
191 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000192 case 3:
robbiewd771da72002-12-27 18:41:54 +0000193 header.version = _LINUX_CAPABILITY_VERSION;
194 header.pid = -1;
195 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000196 case 4:
robbiewd771da72002-12-27 18:41:54 +0000197 header.version = _LINUX_CAPABILITY_VERSION;
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400198 header.pid = tst_get_unused_pid(cleanup);
robbiewd771da72002-12-27 18:41:54 +0000199 break;
200 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700201}