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