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  *    AUTHOR: Madhu T L <madhu.tarikere@wipro.com>
20  *
21  *    DESCRIPTION
22  *      Verify that,
23  *      1. delete_module(2) returns -1 and sets errno to ENOENT for nonexistent
24  *	   module entry.
25  *      2. delete_module(2) returns -1 and sets errno to EFAULT, if
26  *         module name parameter is outside program's accessible address space.
27  *      3. delete_module(2) returns -1 and sets errno to EPERM, if effective
28  *         user id of the caller is not superuser.
29  */
30 
31 #include <errno.h>
32 #include <pwd.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #if HAVE_LINUX_MODULE_H
37 #include <linux/module.h>
38 #else
39 /* As per http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/include/linux/moduleparam.h?a=ppc#L17 ... */
40 #define MODULE_NAME_LEN	( 64 - sizeof(unsigned long) )
41 #endif
42 #include <sys/mman.h>
43 #include "test.h"
44 #include "safe_macros.h"
45 #include "linux_syscall_numbers.h"
46 
47 #define NULLMODNAME	""
48 #define BASEMODNAME	"dummy"
49 #define LONGMODNAMECHAR	'm'	/* Arbitrarily selected */
50 
51 char *TCID = "delete_module02";
52 
53 static char nobody_uid[] = "nobody";
54 struct passwd *ltpuser;
55 static char longmodname[MODULE_NAME_LEN];
56 static char modname[20];
57 
58 static void setup(void);
59 static void cleanup(void);
60 static void setup1(void);
61 static void cleanup1(void);
62 
63 static struct test_case_t {
64 	char *modname;
65 	int experrno;
66 	char *desc;
67 	void (*setup) (void);
68 	void (*cleanup) (void);
69 } tdat[] = {
70 	{ modname, ENOENT, "nonexistent module", NULL, NULL},
71 	{ NULLMODNAME, ENOENT, "null terminated module name", NULL, NULL},
72 	{ (char *)-1, EFAULT, "module name outside program's "
73 	  "accessible address space", NULL, NULL},
74 	{ longmodname, ENOENT, "long module name", NULL, NULL},
75 	{ modname, EPERM, "non-superuser", setup1, cleanup1},
76 };
77 
78 int TST_TOTAL = ARRAY_SIZE(tdat);
79 
main(int argc,char ** argv)80 int main(int argc, char **argv)
81 {
82 	int lc;
83 	int i;
84 
85 	tst_parse_opts(argc, argv, NULL, NULL);
86 
87 	setup();
88 
89 	for (lc = 0; TEST_LOOPING(lc); lc++) {
90 		tst_count = 0;
91 
92 		for (i = 0; i < TST_TOTAL; ++i) {
93 			if (tdat[i].setup)
94 				tdat[i].setup();
95 
96 			tst_resm(TINFO, "test %s", tdat[i].desc);
97 			TEST(ltp_syscall(__NR_delete_module,
98 			     tdat[i].modname, 0));
99 
100 			if (TEST_RETURN != -1) {
101 				tst_resm(TFAIL, "delete_module() "
102 					 "succeeded unexpectedly");
103 			} else if (TEST_ERRNO == tdat[i].experrno) {
104 				tst_resm(TPASS | TTERRNO,
105 					 "delete_module() failed as expected");
106 			} else {
107 				tst_resm(TFAIL | TTERRNO, "delete_module() "
108 					 "failed unexpectedly; expected: "
109 					 "%d - %s", tdat[i].experrno,
110 					 strerror(tdat[i].experrno));
111 			}
112 			if (tdat[i].cleanup)
113 				tdat[i].cleanup();
114 		}
115 	}
116 
117 	cleanup();
118 	tst_exit();
119 }
120 
setup1(void)121 static void setup1(void)
122 {
123 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
124 }
125 
cleanup1(void)126 static void cleanup1(void)
127 {
128 	SAFE_SETEUID(cleanup, 0);
129 }
130 
setup(void)131 static void setup(void)
132 {
133 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
134 
135 	tst_require_root();
136 
137 	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
138 
139 	TEST_PAUSE;
140 
141 	/* Initialize longmodname to LONGMODNAMECHAR character */
142 	memset(longmodname, LONGMODNAMECHAR, MODULE_NAME_LEN - 1);
143 
144 	/* Get unique module name for each child process */
145 	if (sprintf(modname, "%s_%d", BASEMODNAME, getpid()) <= 0)
146 		tst_brkm(TBROK, NULL, "Failed to initialize module name");
147 
148 	tdat[2].modname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
149 				    MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
150 }
151 
cleanup(void)152 void cleanup(void)
153 {
154 }
155