plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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 Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 35 | * 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 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * 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 Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 59 | * 02/2010 Updated by shiwh@cn.fujitsu.com |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | * |
| 61 | * RESTRICTIONS |
| 62 | * Test must be run as root. |
| 63 | */ |
robbiew | 9a66de1 | 2003-03-27 22:16:14 +0000 | [diff] [blame] | 64 | #include "test.h" |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 65 | #include <sys/types.h> |
| 66 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | #include <stdio.h> |
| 68 | #include <errno.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 69 | #include <unistd.h> |
| 70 | #include <linux/unistd.h> |
robbiew | 9a66de1 | 2003-03-27 22:16:14 +0000 | [diff] [blame] | 71 | #include <linux/sysctl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | char *TCID = "sysctl03"; |
Mike Frysinger | 75201f1 | 2013-02-08 17:12:41 -0500 | [diff] [blame] | 75 | |
| 76 | /* This is an older/deprecated syscall that newer arches are omitting */ |
| 77 | #ifdef __NR_sysctl |
| 78 | |
robbiew | 755394c | 2002-04-10 16:33:40 +0000 | [diff] [blame] | 79 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | int sysctl(int *name, int nlen, void *oldval, size_t * oldlenp, |
| 82 | void *newval, size_t newlen) |
robbiew | 9a66de1 | 2003-03-27 22:16:14 +0000 | [diff] [blame] | 83 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | struct __sysctl_args args = |
| 85 | { name, nlen, oldval, oldlenp, newval, newlen }; |
vapier | e24e35c | 2006-08-06 00:51:02 +0000 | [diff] [blame] | 86 | return syscall(__NR__sysctl, &args); |
robbiew | 9a66de1 | 2003-03-27 22:16:14 +0000 | [diff] [blame] | 87 | } |
| 88 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | #define SIZE(x) sizeof(x)/sizeof(x[0]) |
| 90 | #define OSNAMESZ 100 |
| 91 | |
| 92 | void setup(void); |
| 93 | void cleanup(void); |
| 94 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 95 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | { |
Garrett Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 97 | int exp_eno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 99 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
| 101 | char osname[OSNAMESZ]; |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 102 | int osnamelth, status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | int name[] = { CTL_KERN, KERN_OSTYPE }; |
| 104 | pid_t pid; |
| 105 | struct passwd *ltpuser; |
| 106 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | setup(); |
| 112 | |
Garrett Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 113 | if ((tst_kvercmp(2, 6, 32)) <= 0) { |
| 114 | exp_eno = EPERM; |
| 115 | } else { |
| 116 | /* ^^ Look above this warning. ^^ */ |
Cyril Hrubis | a5cc30c | 2013-03-06 16:39:03 +0100 | [diff] [blame] | 117 | tst_resm(TINFO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 118 | "this test's results are based on potentially undocumented behavior in the kernel. read the NOTE in the source file for more details"); |
Garrett Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 119 | exp_eno = EACCES; |
| 120 | exp_enos[0] = EACCES; |
| 121 | } |
| 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 124 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 125 | /* reset tst_count in case we are looping */ |
| 126 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 128 | strcpy(osname, "Linux"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | osnamelth = SIZE(osname); |
| 130 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 131 | TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
| 133 | if (TEST_RETURN != -1) { |
| 134 | tst_resm(TFAIL, "sysctl(2) succeeded unexpectedly"); |
| 135 | } else { |
Garrett Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 136 | if (TEST_ERRNO == exp_eno) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | tst_resm(TPASS | TTERRNO, "Got expected error"); |
Wanlong Gao | 64aa722 | 2011-11-14 08:37:39 +0800 | [diff] [blame] | 138 | } else if (errno == ENOSYS) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | tst_resm(TCONF, |
| 140 | "You may need to make CONFIG_SYSCTL_SYSCALL=y" |
| 141 | " to your kernel config."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | tst_resm(TFAIL | TTERRNO, |
| 144 | "Got unexpected error"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | osnamelth = SIZE(osname); |
robbiew | 755394c | 2002-04-10 16:33:40 +0000 | [diff] [blame] | 149 | if ((ltpuser = getpwnam("nobody")) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | tst_brkm(TBROK, cleanup, "getpwnam() failed"); |
| 151 | } |
| 152 | |
| 153 | /* set process ID to "ltpuser1" */ |
| 154 | if (seteuid(ltpuser->pw_uid) == -1) { |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 155 | tst_brkm(TBROK, cleanup, |
| 156 | "seteuid() failed, errno %d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 159 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 161 | } |
| 162 | |
Cyril Hrubis | 182fd10 | 2012-06-27 15:39:31 +0200 | [diff] [blame] | 163 | if (pid == 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
| 166 | if (TEST_RETURN != -1) { |
| 167 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 168 | } else { |
Garrett Cooper | b49b375 | 2010-08-17 00:03:58 -0700 | [diff] [blame] | 169 | if (TEST_ERRNO == exp_eno) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 170 | tst_resm(TPASS | TTERRNO, |
| 171 | "Got expected error"); |
Wanlong Gao | 64aa722 | 2011-11-14 08:37:39 +0800 | [diff] [blame] | 172 | } else if (TEST_ERRNO == ENOSYS) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 173 | tst_resm(TCONF, |
| 174 | "You may need to make CONFIG_SYSCTL_SYSCALL=y" |
| 175 | " to your kernel config."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 177 | tst_resm(TFAIL | TTERRNO, |
| 178 | "Got unexpected error"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
robbiew | 51a6419 | 2002-07-17 14:58:52 +0000 | [diff] [blame] | 181 | |
| 182 | cleanup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 183 | |
Cyril Hrubis | 182fd10 | 2012-06-27 15:39:31 +0200 | [diff] [blame] | 184 | } else { |
robbiew | 51a6419 | 2002-07-17 14:58:52 +0000 | [diff] [blame] | 185 | /* wait for the child to finish */ |
| 186 | wait(&status); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | } |
| 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 Cooper | 63dc4bd | 2010-12-19 08:25:51 -0800 | [diff] [blame] | 195 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Cyril Hrubis | 182fd10 | 2012-06-27 15:39:31 +0200 | [diff] [blame] | 198 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | { |
Cyril Hrubis | 182fd10 | 2012-06-27 15:39:31 +0200 | [diff] [blame] | 200 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | TEST_PAUSE; |
| 205 | } |
| 206 | |
Cyril Hrubis | 182fd10 | 2012-06-27 15:39:31 +0200 | [diff] [blame] | 207 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | { |
Wanlong Gao | 64aa722 | 2011-11-14 08:37:39 +0800 | [diff] [blame] | 209 | } |
Mike Frysinger | 75201f1 | 2013-02-08 17:12:41 -0500 | [diff] [blame] | 210 | |
| 211 | #else |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 212 | int TST_TOTAL = 0; |
Mike Frysinger | 75201f1 | 2013-02-08 17:12:41 -0500 | [diff] [blame] | 213 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 214 | int main(void) |
Mike Frysinger | 75201f1 | 2013-02-08 17:12:41 -0500 | [diff] [blame] | 215 | { |
| 216 | |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 217 | tst_brkm(TCONF, NULL, |
| 218 | "This test needs a kernel that has sysctl syscall."); |
Mike Frysinger | 75201f1 | 2013-02-08 17:12:41 -0500 | [diff] [blame] | 219 | } |
| 220 | #endif |