delete_module02.c revision aabb8340f63ed31afe995fd97795e542dc68b93c
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
51char *TCID = "delete_module02";
52
53static char nobody_uid[] = "nobody";
54struct passwd *ltpuser;
55static char longmodname[MODULE_NAME_LEN];
56static char modname[20];
57
58static void setup(void);
59static void cleanup(void);
60static void setup1(void);
61static void cleanup1(void);
62
63static 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
78int TST_TOTAL = ARRAY_SIZE(tdat);
79
80int main(int argc, char **argv)
81{
82	int lc;
83	int i;
84
85	const char *msg;
86
87	msg = parse_opts(argc, argv, NULL, NULL);
88	if (msg != NULL)
89		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
90
91	setup();
92
93	for (lc = 0; TEST_LOOPING(lc); lc++) {
94		tst_count = 0;
95
96		for (i = 0; i < TST_TOTAL; ++i) {
97			if (tdat[i].setup)
98				tdat[i].setup();
99
100			tst_resm(TINFO, "test %s", tdat[i].desc);
101			TEST(ltp_syscall(__NR_delete_module,
102			     tdat[i].modname, 0));
103
104			if (TEST_RETURN != -1) {
105				tst_resm(TFAIL, "delete_module() "
106					 "succeeded unexpectedly");
107			} else if (TEST_ERRNO == tdat[i].experrno) {
108				tst_resm(TPASS | TTERRNO,
109					 "delete_module() failed as expected");
110			} else {
111				tst_resm(TFAIL | TTERRNO, "delete_module() "
112					 "failed unexpectedly; expected: "
113					 "%d - %s", tdat[i].experrno,
114					 strerror(tdat[i].experrno));
115			}
116			if (tdat[i].cleanup)
117				tdat[i].cleanup();
118		}
119	}
120
121	cleanup();
122	tst_exit();
123}
124
125static void setup1(void)
126{
127	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
128}
129
130static void cleanup1(void)
131{
132	SAFE_SETEUID(cleanup, 0);
133}
134
135static void setup(void)
136{
137	tst_sig(NOFORK, DEF_HANDLER, cleanup);
138
139	tst_require_root(NULL);
140
141	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
142
143	TEST_PAUSE;
144
145	/* Initialize longmodname to LONGMODNAMECHAR character */
146	memset(longmodname, LONGMODNAMECHAR, MODULE_NAME_LEN - 1);
147
148	/* Get unique module name for each child process */
149	if (sprintf(modname, "%s_%d", BASEMODNAME, getpid()) <= 0)
150		tst_brkm(TBROK, NULL, "Failed to initialize module name");
151
152	tdat[2].modname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
153				    MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
154}
155
156void cleanup(void)
157{
158}
159