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