1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2018 Xiao Yang <yangx.jy@cn.fujitsu.com>
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
16 /*
17 * DESCRIPTION
18 * 1) Call sysctl(2) as a root user, and attempt to write data
19 * to the kernel_table[]. Since the table does not have write
20 * permissions even for the root, it should fail EPERM.
21 * 2) Call sysctl(2) as a non-root user, and attempt to write data
22 * to the kernel_table[]. Since the table does not have write
23 * permission for the regular user, it should fail with EPERM.
24 *
25 * NOTE: There is a documentation bug in 2.6.33-rc1 where unfortunately
26 * the behavior of sysctl(2) isn't properly documented, as discussed
27 * in detail in the following thread:
28 * http://sourceforge.net/mailarchive/message.php?msg_name=4B7BA24F.2010705%40linux.vnet.ibm.com.
29 *
30 * The documentation bug is filed as:
31 * https://bugzilla.kernel.org/show_bug.cgi?id=15446 . If you want the
32 * message removed, please ask your fellow kernel maintainer to fix their
33 * documentation.
34 *
35 * Thanks!
36 * -Ngie
37 */
38
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <linux/unistd.h>
45 #include <linux/sysctl.h>
46 #include <pwd.h>
47
48 #include "tst_test.h"
49 #include "lapi/syscalls.h"
50
51 static int exp_eno;
52
verify_sysctl(void)53 static void verify_sysctl(void)
54 {
55 char *osname = "Linux";
56 int name[] = {CTL_KERN, KERN_OSTYPE};
57 struct __sysctl_args args = {
58 .name = name,
59 .nlen = ARRAY_SIZE(name),
60 .newval = osname,
61 .newlen = sizeof(osname),
62 };
63
64 TEST(tst_syscall(__NR__sysctl, &args));
65 if (TST_RET != -1) {
66 tst_res(TFAIL, "sysctl(2) succeeded unexpectedly");
67 return;
68 }
69
70 if (TST_ERR == exp_eno) {
71 tst_res(TPASS | TTERRNO, "Got expected error");
72 } else {
73 tst_res(TFAIL | TTERRNO, "Got unexpected error, expected %s",
74 tst_strerrno(exp_eno));
75 }
76 }
77
setup(void)78 static void setup(void)
79 {
80 if ((tst_kvercmp(2, 6, 32)) <= 0) {
81 exp_eno = EPERM;
82 } else {
83 /* Look above this warning. */
84 tst_res(TINFO,
85 "this test's results are based on potentially undocumented behavior in the kernel. read the NOTE in the source file for more details");
86 exp_eno = EACCES;
87 }
88 }
89
do_test(void)90 static void do_test(void)
91 {
92 pid_t pid;
93 struct passwd *ltpuser;
94
95 pid = SAFE_FORK();
96 if (!pid) {
97 ltpuser = SAFE_GETPWNAM("nobody");
98 SAFE_SETUID(ltpuser->pw_uid);
99 verify_sysctl();
100 } else {
101 verify_sysctl();
102 tst_reap_children();
103 }
104 }
105
106 static struct tst_test test = {
107 .needs_root = 1,
108 .forks_child = 1,
109 .setup = setup,
110 .test_all = do_test,
111 };
112