blob: fc9894ab7419686ddcea971dcc8ded52b99b6107 [file] [log] [blame]
plars5f30b5d2003-03-12 20:40:32 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars5f30b5d2003-03-12 20:40:32 +000015 *
16 */
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080017
18/*
19 * AUTHOR: Madhu T L <madhu.tarikere@wipro.com>
plars5f30b5d2003-03-12 20:40:32 +000020 *
21 * DESCRIPTION
22 * Verify that,
yaberauneyaef772532009-10-09 17:55:43 +000023 * 1. delete_module(2) returns -1 and sets errno to ENOENT for nonexistent
24 * module entry.
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080025 * 2. delete_module(2) returns -1 and sets errno to EFAULT, if
plars5f30b5d2003-03-12 20:40:32 +000026 * module name parameter is outside program's accessible address space.
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080027 * 3. delete_module(2) returns -1 and sets errno to EPERM, if effective
plars5f30b5d2003-03-12 20:40:32 +000028 * user id of the caller is not superuser.
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080029 */
plars5f30b5d2003-03-12 20:40:32 +000030
31#include <errno.h>
32#include <pwd.h>
33#include <sys/types.h>
subrata_modak024988b2008-09-24 12:55:33 +000034#include <unistd.h>
35#include <limits.h>
yaberauneyaef772532009-10-09 17:55:43 +000036#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
plars5f30b5d2003-03-12 20:40:32 +000042#include <sys/mman.h>
43#include "test.h"
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080044#include "safe_macros.h"
45#include "linux_syscall_numbers.h"
plars5f30b5d2003-03-12 20:40:32 +000046
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080047#define NULLMODNAME ""
48#define BASEMODNAME "dummy"
49#define LONGMODNAMECHAR 'm' /* Arbitrarily selected */
plars5f30b5d2003-03-12 20:40:32 +000050
51char *TCID = "delete_module02";
Wanlong Gao354ebb42012-12-07 10:10:04 +080052
plars5f30b5d2003-03-12 20:40:32 +000053static char nobody_uid[] = "nobody";
54struct passwd *ltpuser;
yaberauneyaef772532009-10-09 17:55:43 +000055static char longmodname[MODULE_NAME_LEN];
yaberauneyaef772532009-10-09 17:55:43 +000056static char modname[20];
plars5f30b5d2003-03-12 20:40:32 +000057
plars5f30b5d2003-03-12 20:40:32 +000058static void setup(void);
59static void cleanup(void);
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080060static void setup1(void);
plars5f30b5d2003-03-12 20:40:32 +000061static void cleanup1(void);
62
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080063static 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},
plars5f30b5d2003-03-12 20:40:32 +000076};
77
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080078int TST_TOTAL = ARRAY_SIZE(tdat);
plars5f30b5d2003-03-12 20:40:32 +000079
Wanlong Gao354ebb42012-12-07 10:10:04 +080080int main(int argc, char **argv)
plars5f30b5d2003-03-12 20:40:32 +000081{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020082 int lc;
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080083 int i;
84
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020085 const char *msg;
plars5f30b5d2003-03-12 20:40:32 +000086
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080087 msg = parse_opts(argc, argv, NULL, NULL);
88 if (msg != NULL)
Garrett Cooper53740502010-12-16 00:04:01 -080089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars5f30b5d2003-03-12 20:40:32 +000090
yaberauneyaef772532009-10-09 17:55:43 +000091 setup();
plars5f30b5d2003-03-12 20:40:32 +000092
yaberauneyaef772532009-10-09 17:55:43 +000093 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 tst_count = 0;
plars5f30b5d2003-03-12 20:40:32 +000095
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +080096 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));
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800103
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");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 } else {
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800111 tst_resm(TFAIL | TTERRNO, "delete_module() "
112 "failed unexpectedly; expected: "
113 "%d - %s", tdat[i].experrno,
114 strerror(tdat[i].experrno));
yaberauneyaef772532009-10-09 17:55:43 +0000115 }
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800116 if (tdat[i].cleanup)
117 tdat[i].cleanup();
yaberauneyaef772532009-10-09 17:55:43 +0000118 }
119 }
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800120
yaberauneyaef772532009-10-09 17:55:43 +0000121 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800122 tst_exit();
plars5f30b5d2003-03-12 20:40:32 +0000123}
124
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800125static void setup1(void)
plars5f30b5d2003-03-12 20:40:32 +0000126{
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800127 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
plars5f30b5d2003-03-12 20:40:32 +0000128}
129
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800130static void cleanup1(void)
plars5f30b5d2003-03-12 20:40:32 +0000131{
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800132 SAFE_SETEUID(cleanup, 0);
plars5f30b5d2003-03-12 20:40:32 +0000133}
134
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800135static void setup(void)
plars5f30b5d2003-03-12 20:40:32 +0000136{
yaberauneyaef772532009-10-09 17:55:43 +0000137 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars5f30b5d2003-03-12 20:40:32 +0000138
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800139 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800140
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800141 ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
plars5f30b5d2003-03-12 20:40:32 +0000142
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800143 TEST_PAUSE;
plars5f30b5d2003-03-12 20:40:32 +0000144
yaberauneyaef772532009-10-09 17:55:43 +0000145 /* Initialize longmodname to LONGMODNAMECHAR character */
146 memset(longmodname, LONGMODNAMECHAR, MODULE_NAME_LEN - 1);
plars5f30b5d2003-03-12 20:40:32 +0000147
yaberauneyaef772532009-10-09 17:55:43 +0000148 /* Get unique module name for each child process */
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800149 if (sprintf(modname, "%s_%d", BASEMODNAME, getpid()) <= 0)
Garrett Cooper53740502010-12-16 00:04:01 -0800150 tst_brkm(TBROK, NULL, "Failed to initialize module name");
plars5f30b5d2003-03-12 20:40:32 +0000151
Xiaoguang Wangc2a4cd92014-02-11 16:08:39 +0800152 tdat[2].modname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
153 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
plars5f30b5d2003-03-12 20:40:32 +0000154}
155
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156void cleanup(void)
plars5f30b5d2003-03-12 20:40:32 +0000157{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700158}