1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**********************************************************
18  *
19  *    TEST IDENTIFIER   : syslog12
20  *
21  *    EXECUTED BY       : root / superuser
22  *
23  *    TEST TITLE        : Checking error conditions for syslog(2)
24  *
25  *    TEST CASE TOTAL   : 7
26  *
27  *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
28  *
29  *    SIGNALS
30  *      Uses SIGUSR1 to pause before test if option set.
31  *      (See the parse_opts(3) man page).
32  *
33  *    DESCRIPTION
34  *	Verify that,
35  *	1. syslog(2) fails with EINVAL for invalid type/command
36  *	2. syslog(2) fails with EFAULT for buffer outside program's  accessible
37  *	   address space.
38  *	3. syslog(2) fails with EINVAL for NULL buffer argument.
39  *	4. syslog(2) fails with EINVAL for length arg. set to negative value.
40  *	5. syslog(2) fails with EPERM for non-root user.
41  *	6. syslog(2) fails with EINVAL for console level less than 0.
42  *	7. syslog(2) fails with EINVAL for console level greater than 8.
43  *
44  *      Setup:
45  *	  Setup signal handling.
46  *	  Test caller is superuser
47  *	  Check existence of user nobody
48  *	  Set expected errnos
49  *	  Pause for SIGUSR1 if option specified.
50  *
51  *	Test:
52  *	 Loop if the proper options are given.
53  *	  Execute system call
54  *	  Check return value and errno, if matching,
55  *		 Issue PASS message
56  *	  Otherwise,
57  *		Issue FAIL message
58  *
59  *	Cleanup:
60  *	  Print errno log and/or timing stats if options given
61  *
62  * USAGE:  <for command-line>
63  *  syslog12 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
64  *		where,  -c n : Run n copies concurrently.
65  *			-e   : Turn on errno logging.
66  *			-f   : Turn off functional testing
67  *			-h   : Show help screen
68  *			-i n : Execute test n times.
69  *			-I x : Execute test for x seconds.
70  *			-p   : Pause for SIGUSR1 before starting
71  *			-P x : Pause for x seconds between iterations.
72  *			-t   : Turn on syscall timing.
73  *
74  ****************************************************************/
75 
76 #include <errno.h>
77 #include <pwd.h>
78 #include <sys/types.h>
79 #include <unistd.h>
80 #include <signal.h>
81 #include <linux/unistd.h>
82 #include <sys/syscall.h>
83 #include "test.h"
84 #include "safe_macros.h"
85 
86 #define EXP_RET_VAL	-1
87 
88 struct test_case_t {		/* test case structure */
89 	int type;		/* 1st arg */
90 	char *buf;		/* 2nd arg */
91 	int len;		/* 3rd arg */
92 	int exp_errno;		/* Expected errno */
93 	int (*setup) (void);	/* Individual setup routine */
94 	void (*cleanup) (void);	/* Individual cleanup routine */
95 	char *desc;		/* Test description */
96 };
97 
98 char *TCID = "syslog12";
99 static int testno;
100 
101 static char buf;
102 static struct passwd *ltpuser;
103 
104 static void setup(void);
105 static void cleanup(void);
106 static int setup1(void);
107 static void cleanup1(void);
108 
109 #define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3)
110 
111 static struct test_case_t tdat[] = {
112 	{100, &buf, 0, EINVAL, NULL, NULL, "invalid type/command"},
113 	{2, NULL, 0, EINVAL, NULL, NULL, "NULL buffer argument"},
114 	{3, &buf, -1, EINVAL, NULL, NULL, "negative length argument"},
115 	{2, &buf, 0, EPERM, setup1, cleanup1, "non-root user"},
116 	{8, &buf, -1, EINVAL, NULL, NULL, "console level less than 0"},
117 	{8, &buf, 9, EINVAL, NULL, NULL, "console level greater than 8"},
118 };
119 
120 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
121 
timeout(int sig)122 void timeout(int sig)
123 {
124 	tst_resm(TWARN, "syslog() timeout after 1s"
125 		 " for %s", tdat[testno].desc);
126 }
127 
main(int argc,char ** argv)128 int main(int argc, char **argv)
129 {
130 	int lc;
131 	struct sigaction sa;
132 	int ret;
133 
134 	tst_parse_opts(argc, argv, NULL, NULL);
135 
136 	setup();
137 
138 	memset(&sa, 0, sizeof(struct sigaction));
139 	sa.sa_handler = timeout;
140 	sa.sa_flags = 0;
141 	sigaction(SIGALRM, &sa, NULL);
142 
143 	for (lc = 0; TEST_LOOPING(lc); lc++) {
144 		/* reset tst_count in case we are looping */
145 		tst_count = 0;
146 
147 		for (testno = 0; testno < TST_TOTAL; ++testno) {
148 
149 			if (tdat[testno].setup && tdat[testno].setup()) {
150 				/* Setup failed, skip this testcase */
151 				continue;
152 			}
153 
154 			alarm(1);
155 
156 			TEST(syslog(tdat[testno].type, tdat[testno].buf,
157 				    tdat[testno].len));
158 
159 			alarm(0);
160 			/* syslog returns an int, so we need to turn the long
161 			 * TEST_RETURN into an int to test with */
162 			ret = TEST_RETURN;
163 			if ((ret == EXP_RET_VAL) &&
164 			    (TEST_ERRNO == tdat[testno].exp_errno)) {
165 				tst_resm(TPASS, "syslog() failed as expected"
166 					 " for %s : errno %d",
167 					 tdat[testno].desc, TEST_ERRNO);
168 			} else {
169 				tst_resm(TFAIL, "syslog() returned "
170 					 "unexpected results for %s ; returned"
171 					 " %d (expected %d), errno %d (expected"
172 					 " %d)", tdat[testno].desc,
173 					 ret, EXP_RET_VAL, TEST_ERRNO,
174 					 tdat[testno].exp_errno);
175 			}
176 
177 			if (tdat[testno].cleanup) {
178 				tdat[testno].cleanup();
179 			}
180 		}
181 	}
182 	cleanup();
183 
184 	tst_exit();
185 }
186 
setup1(void)187 int setup1(void)
188 {
189 	/* Change effective user id to nodody */
190 	if (seteuid(ltpuser->pw_uid) == -1) {
191 		tst_resm(TBROK, "seteuid failed to set the effective"
192 			 " uid to %d", ltpuser->pw_uid);
193 		return 1;
194 	}
195 	return 0;
196 }
197 
cleanup1(void)198 void cleanup1(void)
199 {
200 	/* Change effective user id to root */
201 	SAFE_SETEUID(NULL, 0);
202 }
203 
204 /*
205  * setup()
206  *	performs all ONE TIME setup for this test
207  */
setup(void)208 void setup(void)
209 {
210 	tst_require_root();
211 
212 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
213 
214 	/* Check for nobody_uid user id */
215 	if ((ltpuser = getpwnam("nobody")) == NULL) {
216 		tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
217 	}
218 
219 	/* Pause if that option was specified
220 	 * TEST_PAUSE contains the code to fork the test with the -c option.
221 	 */
222 	TEST_PAUSE;
223 }
224 
225 /*
226  * cleanup()
227  *	performs all ONE TIME cleanup for this test at
228  *	completion or premature exit
229  */
cleanup(void)230 void cleanup(void)
231 {
232 
233 }
234