blob: 11a67d683869c924b00f51f5587f461f06b81c72 [file] [log] [blame]
Zeng Linggangb8e13bc2014-04-25 10:51:05 +08001/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17/*
18 * DESCRIPTION
19 * The calling process is not privileged and euid is not appropriate,
20 * EPERM should return.
21 */
22
23#include <errno.h>
24#include <pwd.h>
25#include "test.h"
Zeng Linggangb8e13bc2014-04-25 10:51:05 +080026#include "safe_macros.h"
27
28char *TCID = "setegid02";
29int TST_TOTAL = 1;
Zeng Linggangb8e13bc2014-04-25 10:51:05 +080030static void setup(void);
31static void setegid_verify(void);
32static void cleanup(void);
33
34static struct passwd *ltpuser;
35
36int main(int argc, char *argv[])
37{
38 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020039 const char *msg;
Zeng Linggangb8e13bc2014-04-25 10:51:05 +080040
41 msg = parse_opts(argc, argv, NULL, NULL);
42 if (msg != NULL)
43 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
44
45 setup();
46
47 for (lc = 0; TEST_LOOPING(lc); lc++) {
48 tst_count = 0;
49 setegid_verify();
50 }
51
52 cleanup();
53 tst_exit();
54}
55
56static void setup(void)
57{
58 tst_require_root(NULL);
59
60 tst_sig(NOFORK, DEF_HANDLER, cleanup);
61
62 TEST_PAUSE;
63
64 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
65
66 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
Zeng Linggangb8e13bc2014-04-25 10:51:05 +080067}
68
69static void setegid_verify(void)
70{
71 TEST(setegid(ltpuser->pw_gid));
72
73 if (TEST_RETURN != -1) {
74 tst_resm(TFAIL, "setegid(%d) succeeded unexpectedly",
75 ltpuser->pw_gid);
76 return;
77 }
78
79 if (TEST_ERRNO == EPERM) {
80 tst_resm(TPASS | TTERRNO, "setegid failed as expected");
81 } else {
82 tst_resm(TFAIL | TTERRNO,
83 "setegid failed unexpectedly; expected: %d - %s",
84 EPERM, strerror(EPERM));
85 }
86}
87
88static void cleanup(void)
89{
Zeng Linggangb8e13bc2014-04-25 10:51:05 +080090}