xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/syslog/syslog12.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) 2022 Petr Vorel <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Madhu T L <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that syslog(2) system call fails with appropriate error number:
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * 1. EINVAL -- invalid type/command
14*49cdfc7eSAndroid Build Coastguard Worker  * 2. EFAULT -- buffer outside program's accessible address space
15*49cdfc7eSAndroid Build Coastguard Worker  * 3. EINVAL -- NULL buffer argument
16*49cdfc7eSAndroid Build Coastguard Worker  * 4. EINVAL -- length argument set to negative value
17*49cdfc7eSAndroid Build Coastguard Worker  * 5. EINVAL -- console level less than 0
18*49cdfc7eSAndroid Build Coastguard Worker  * 6. EINVAL -- console level greater than 8
19*49cdfc7eSAndroid Build Coastguard Worker  * 7. EPERM -- non-root user
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #define syslog(arg1, arg2, arg3) tst_syscall(__NR_syslog, arg1, arg2, arg3)
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static char buf;
32*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
33*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)34*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
setup_nonroot(void)39*49cdfc7eSAndroid Build Coastguard Worker static void setup_nonroot(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEGID(ltpuser->pw_gid);
42*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(ltpuser->pw_uid);
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker 
cleanup_nonroot(void)45*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_nonroot(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(0);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
51*49cdfc7eSAndroid Build Coastguard Worker 	int type;
52*49cdfc7eSAndroid Build Coastguard Worker 	char *buf;
53*49cdfc7eSAndroid Build Coastguard Worker 	int len;
54*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
55*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
56*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
57*49cdfc7eSAndroid Build Coastguard Worker 	{100, &buf, 0, EINVAL, "invalid type/command"},
58*49cdfc7eSAndroid Build Coastguard Worker 	{2, NULL, 0, EINVAL, "NULL buffer argument"},
59*49cdfc7eSAndroid Build Coastguard Worker 	{3, &buf, -1, EINVAL, "negative length argument"},
60*49cdfc7eSAndroid Build Coastguard Worker 	{8, &buf, -1, EINVAL, "console level less than 0"},
61*49cdfc7eSAndroid Build Coastguard Worker 	{8, &buf, 9, EINVAL, "console level greater than 8"},
62*49cdfc7eSAndroid Build Coastguard Worker 	{2, &buf, 0, EPERM, "non-root user"},
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)65*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (n == ARRAY_SIZE(tcases)-1)
70*49cdfc7eSAndroid Build Coastguard Worker 		setup_nonroot();
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(syslog(tc->type, tc->buf, tc->len), tc->exp_errno,
73*49cdfc7eSAndroid Build Coastguard Worker 		"syslog() with %s", tc->desc);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	if (n == ARRAY_SIZE(tcases)-1)
76*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_nonroot();
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
80*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
81*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
82*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
83*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
84*49cdfc7eSAndroid Build Coastguard Worker };
85