blob: b0cff43627949b1b91a14469062b193dd4d952a5 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * sysctl03.c
23 *
24 * DESCRIPTION
25 * Testcase to check that sysctl(2) sets errno to EPERM correctly.
26 *
27 * ALGORITHM
28 * a. Call sysctl(2) as a root user, and attempt to write data
29 * to the kernel_table[]. Since the table does not have write
30 * permissions even for the root, it should fail EPERM.
31 * b. Call sysctl(2) as a non-root user, and attempt to write data
32 * to the kernel_table[]. Since the table does not have write
33 * permission for the regular user, it should fail with EPERM.
34 *
Garrett Cooperb49b3752010-08-17 00:03:58 -070035 * NOTE: There is a documentation bug in 2.6.33-rc1 where unfortunately the
36 * behavior of sysctl(2) isn't properly documented, as discussed in detail in
37 * the following thread:
38 * http://sourceforge.net/mailarchive/message.php?msg_name=4B7BA24F.2010705%40linux.vnet.ibm.com.
39 *
40 * The documentation bug is filed as:
41 * https://bugzilla.kernel.org/show_bug.cgi?id=15446 . If you want the message
42 * removed, please ask your fellow kernel maintainer to fix his/her
43 * documentation.
44 *
45 * Thanks!
46 * -Garrett
47 *
plars865695b2001-08-27 22:15:12 +000048 * USAGE: <for command-line>
49 * sysctl03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
50 * where, -c n : Run n copies concurrently.
51 * -e : Turn on errno logging.
52 * -i n : Execute test n times.
53 * -I x : Execute test for x seconds.
54 * -P x : Pause for x seconds between iterations.
55 * -t : Turn on syscall timing.
56 *
57 * HISTORY
58 * 07/2001 Ported by Wayne Boyer
Garrett Cooperb49b3752010-08-17 00:03:58 -070059 * 02/2010 Updated by shiwh@cn.fujitsu.com
plars865695b2001-08-27 22:15:12 +000060 *
61 * RESTRICTIONS
62 * Test must be run as root.
63 */
robbiew9a66de12003-03-27 22:16:14 +000064#include "test.h"
plars74948ad2002-11-14 16:16:14 +000065#include <sys/types.h>
66#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000067#include <stdio.h>
68#include <errno.h>
plars74948ad2002-11-14 16:16:14 +000069#include <unistd.h>
70#include <linux/unistd.h>
robbiew9a66de12003-03-27 22:16:14 +000071#include <linux/sysctl.h>
plars865695b2001-08-27 22:15:12 +000072#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000073
74char *TCID = "sysctl03";
Mike Frysinger75201f12013-02-08 17:12:41 -050075
76/* This is an older/deprecated syscall that newer arches are omitting */
77#ifdef __NR_sysctl
78
robbiew755394c2002-04-10 16:33:40 +000079int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081int sysctl(int *name, int nlen, void *oldval, size_t * oldlenp,
82 void *newval, size_t newlen)
robbiew9a66de12003-03-27 22:16:14 +000083{
subrata_modak56207ce2009-03-23 13:35:39 +000084 struct __sysctl_args args =
85 { name, nlen, oldval, oldlenp, newval, newlen };
vapiere24e35c2006-08-06 00:51:02 +000086 return syscall(__NR__sysctl, &args);
robbiew9a66de12003-03-27 22:16:14 +000087}
88
plars865695b2001-08-27 22:15:12 +000089#define SIZE(x) sizeof(x)/sizeof(x[0])
90#define OSNAMESZ 100
91
92void setup(void);
93void cleanup(void);
94
plars74948ad2002-11-14 16:16:14 +000095int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000096{
Garrett Cooperb49b3752010-08-17 00:03:58 -070097 int exp_eno;
plars865695b2001-08-27 22:15:12 +000098 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020099 const char *msg;
plars865695b2001-08-27 22:15:12 +0000100
101 char osname[OSNAMESZ];
plars74948ad2002-11-14 16:16:14 +0000102 int osnamelth, status;
plars865695b2001-08-27 22:15:12 +0000103 int name[] = { CTL_KERN, KERN_OSTYPE };
104 pid_t pid;
105 struct passwd *ltpuser;
106
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000109 }
110
111 setup();
112
Garrett Cooperb49b3752010-08-17 00:03:58 -0700113 if ((tst_kvercmp(2, 6, 32)) <= 0) {
114 exp_eno = EPERM;
115 } else {
116 /* ^^ Look above this warning. ^^ */
Cyril Hrubisa5cc30c2013-03-06 16:39:03 +0100117 tst_resm(TINFO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 "this test's results are based on potentially undocumented behavior in the kernel. read the NOTE in the source file for more details");
Garrett Cooperb49b3752010-08-17 00:03:58 -0700119 exp_eno = EACCES;
120 exp_enos[0] = EACCES;
121 }
122
plars865695b2001-08-27 22:15:12 +0000123 for (lc = 0; TEST_LOOPING(lc); lc++) {
124
Caspar Zhangd59a6592013-03-07 14:59:12 +0800125 /* reset tst_count in case we are looping */
126 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000127
subrata_modak4bb656a2009-02-26 12:02:09 +0000128 strcpy(osname, "Linux");
plars865695b2001-08-27 22:15:12 +0000129 osnamelth = SIZE(osname);
130
plars74948ad2002-11-14 16:16:14 +0000131 TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth));
plars865695b2001-08-27 22:15:12 +0000132
133 if (TEST_RETURN != -1) {
134 tst_resm(TFAIL, "sysctl(2) succeeded unexpectedly");
135 } else {
Garrett Cooperb49b3752010-08-17 00:03:58 -0700136 if (TEST_ERRNO == exp_eno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 tst_resm(TPASS | TTERRNO, "Got expected error");
Wanlong Gao64aa7222011-11-14 08:37:39 +0800138 } else if (errno == ENOSYS) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TCONF,
140 "You may need to make CONFIG_SYSCTL_SYSCALL=y"
141 " to your kernel config.");
plars865695b2001-08-27 22:15:12 +0000142 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 tst_resm(TFAIL | TTERRNO,
144 "Got unexpected error");
plars865695b2001-08-27 22:15:12 +0000145 }
146 }
147
plars865695b2001-08-27 22:15:12 +0000148 osnamelth = SIZE(osname);
robbiew755394c2002-04-10 16:33:40 +0000149 if ((ltpuser = getpwnam("nobody")) == NULL) {
plars865695b2001-08-27 22:15:12 +0000150 tst_brkm(TBROK, cleanup, "getpwnam() failed");
151 }
152
153 /* set process ID to "ltpuser1" */
154 if (seteuid(ltpuser->pw_uid) == -1) {
plars74948ad2002-11-14 16:16:14 +0000155 tst_brkm(TBROK, cleanup,
156 "seteuid() failed, errno %d", errno);
plars865695b2001-08-27 22:15:12 +0000157 }
158
robbiewd34d5812005-07-11 22:28:09 +0000159 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000160 tst_brkm(TBROK, cleanup, "fork() failed");
161 }
162
Cyril Hrubis182fd102012-06-27 15:39:31 +0200163 if (pid == 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000164 TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth));
plars865695b2001-08-27 22:15:12 +0000165
166 if (TEST_RETURN != -1) {
167 tst_resm(TFAIL, "call succeeded unexpectedly");
168 } else {
Garrett Cooperb49b3752010-08-17 00:03:58 -0700169 if (TEST_ERRNO == exp_eno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 tst_resm(TPASS | TTERRNO,
171 "Got expected error");
Wanlong Gao64aa7222011-11-14 08:37:39 +0800172 } else if (TEST_ERRNO == ENOSYS) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 tst_resm(TCONF,
174 "You may need to make CONFIG_SYSCTL_SYSCALL=y"
175 " to your kernel config.");
plars865695b2001-08-27 22:15:12 +0000176 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 tst_resm(TFAIL | TTERRNO,
178 "Got unexpected error");
plars865695b2001-08-27 22:15:12 +0000179 }
180 }
robbiew51a64192002-07-17 14:58:52 +0000181
182 cleanup();
subrata_modakbdbaec52009-02-26 12:14:51 +0000183
Cyril Hrubis182fd102012-06-27 15:39:31 +0200184 } else {
robbiew51a64192002-07-17 14:58:52 +0000185 /* wait for the child to finish */
186 wait(&status);
plars865695b2001-08-27 22:15:12 +0000187 }
188
189 /* set process ID back to root */
190 if (seteuid(0) == -1) {
191 tst_brkm(TBROK, cleanup, "seteuid() failed");
192 }
193 }
194 cleanup();
Garrett Cooper63dc4bd2010-12-19 08:25:51 -0800195 tst_exit();
plars865695b2001-08-27 22:15:12 +0000196}
197
Cyril Hrubis182fd102012-06-27 15:39:31 +0200198void setup(void)
plars865695b2001-08-27 22:15:12 +0000199{
Cyril Hrubis182fd102012-06-27 15:39:31 +0200200 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000201
plars865695b2001-08-27 22:15:12 +0000202 tst_sig(FORK, DEF_HANDLER, cleanup);
203
plars865695b2001-08-27 22:15:12 +0000204 TEST_PAUSE;
205}
206
Cyril Hrubis182fd102012-06-27 15:39:31 +0200207void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000208{
Wanlong Gao64aa7222011-11-14 08:37:39 +0800209}
Mike Frysinger75201f12013-02-08 17:12:41 -0500210
211#else
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200212int TST_TOTAL = 0;
Mike Frysinger75201f12013-02-08 17:12:41 -0500213
Mike Frysingerc57fba52014-04-09 18:56:30 -0400214int main(void)
Mike Frysinger75201f12013-02-08 17:12:41 -0500215{
216
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100217 tst_brkm(TCONF, NULL,
218 "This test needs a kernel that has sysctl syscall.");
Mike Frysinger75201f12013-02-08 17:12:41 -0500219}
220#endif