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
37#include <sys/mman.h>
38#include "test.h"
39#include "safe_macros.h"
40#include "lapi/syscalls.h"
41
42#define NULLMODNAME	""
43#define BASEMODNAME	"dummy"
44#define LONGMODNAMECHAR	'm'	/* Arbitrarily selected */
45
46/*
47 * From kernel internal headers: include/linux/module.h
48 * include/linux/moduleparam.h
49 */
50#define MODULE_NAME_LEN	( 64 - sizeof(unsigned long) )
51
52char *TCID = "delete_module02";
53
54static char nobody_uid[] = "nobody";
55struct passwd *ltpuser;
56static char longmodname[MODULE_NAME_LEN];
57static char modname[20];
58
59static void setup(void);
60static void cleanup(void);
61static void setup1(void);
62static void cleanup1(void);
63
64static struct test_case_t {
65	char *modname;
66	int experrno;
67	char *desc;
68	void (*setup) (void);
69	void (*cleanup) (void);
70} tdat[] = {
71	{ modname, ENOENT, "nonexistent module", NULL, NULL},
72	{ NULLMODNAME, ENOENT, "null terminated module name", NULL, NULL},
73	{ (char *)-1, EFAULT, "module name outside program's "
74	  "accessible address space", NULL, NULL},
75	{ longmodname, ENOENT, "long module name", NULL, NULL},
76	{ modname, EPERM, "non-superuser", setup1, cleanup1},
77};
78
79int TST_TOTAL = ARRAY_SIZE(tdat);
80
81int main(int argc, char **argv)
82{
83	int lc;
84	int i;
85
86	tst_parse_opts(argc, argv, NULL, NULL);
87
88	setup();
89
90	for (lc = 0; TEST_LOOPING(lc); lc++) {
91		tst_count = 0;
92
93		for (i = 0; i < TST_TOTAL; ++i) {
94			if (tdat[i].setup)
95				tdat[i].setup();
96
97			tst_resm(TINFO, "test %s", tdat[i].desc);
98			TEST(ltp_syscall(__NR_delete_module,
99			     tdat[i].modname, 0));
100
101			if (TEST_RETURN != -1) {
102				tst_resm(TFAIL, "delete_module() "
103					 "succeeded unexpectedly");
104			} else if (TEST_ERRNO == tdat[i].experrno) {
105				tst_resm(TPASS | TTERRNO,
106					 "delete_module() failed as expected");
107			} else {
108				tst_resm(TFAIL | TTERRNO, "delete_module() "
109					 "failed unexpectedly; expected: "
110					 "%d - %s", tdat[i].experrno,
111					 strerror(tdat[i].experrno));
112			}
113			if (tdat[i].cleanup)
114				tdat[i].cleanup();
115		}
116	}
117
118	cleanup();
119	tst_exit();
120}
121
122static void setup1(void)
123{
124	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
125}
126
127static void cleanup1(void)
128{
129	SAFE_SETEUID(cleanup, 0);
130}
131
132static void setup(void)
133{
134	tst_sig(NOFORK, DEF_HANDLER, cleanup);
135
136	tst_require_root();
137
138	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
139
140	TEST_PAUSE;
141
142	/* Initialize longmodname to LONGMODNAMECHAR character */
143	memset(longmodname, LONGMODNAMECHAR, MODULE_NAME_LEN - 1);
144
145	/* Get unique module name for each child process */
146	if (sprintf(modname, "%s_%d", BASEMODNAME, getpid()) <= 0)
147		tst_brkm(TBROK, NULL, "Failed to initialize module name");
148
149	tdat[2].modname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
150				    MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
151}
152
153void cleanup(void)
154{
155}
156