1 /* $NetBSD: t_issetugid.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_issetugid.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
42 static bool check(int (*fuid
)(uid_t
), int (*fgid
)(gid_t
));
45 check(int (*fuid
)(uid_t
), int (*fgid
)(gid_t
))
51 pw
= getpwnam("nobody");
63 if (fuid
!= NULL
&& (*fuid
)(pw
->pw_uid
) != 0)
66 if (fgid
!= NULL
&& (*fgid
)(pw
->pw_gid
) != 0)
77 if (WIFEXITED(sta
) == 0 || WEXITSTATUS(sta
) != EXIT_SUCCESS
)
83 ATF_TC(issetugid_egid
);
84 ATF_TC_HEAD(issetugid_egid
, tc
)
86 atf_tc_set_md_var(tc
, "descr", "A test of issetugid(2), eff. GID");
87 atf_tc_set_md_var(tc
, "require.user", "root");
90 ATF_TC_BODY(issetugid_egid
, tc
)
93 if (check(NULL
, setegid
) != true)
94 atf_tc_fail("issetugid(2) failed with effective GID");
97 ATF_TC(issetugid_euid
);
98 ATF_TC_HEAD(issetugid_euid
, tc
)
100 atf_tc_set_md_var(tc
, "descr", "A test of issetugid(2), eff. UID");
101 atf_tc_set_md_var(tc
, "require.user", "root");
104 ATF_TC_BODY(issetugid_euid
, tc
)
107 if (check(seteuid
, NULL
) != true)
108 atf_tc_fail("issetugid(2) failed with effective UID");
111 ATF_TC(issetugid_rgid
);
112 ATF_TC_HEAD(issetugid_rgid
, tc
)
114 atf_tc_set_md_var(tc
, "descr", "A test of issetugid(2), real GID");
115 atf_tc_set_md_var(tc
, "require.user", "root");
118 ATF_TC_BODY(issetugid_rgid
, tc
)
121 if (check(NULL
, setgid
) != true)
122 atf_tc_fail("issetugid(2) failed with real GID");
125 ATF_TC(issetugid_ruid
);
126 ATF_TC_HEAD(issetugid_ruid
, tc
)
128 atf_tc_set_md_var(tc
, "descr", "A test of issetugid(2), real UID");
129 atf_tc_set_md_var(tc
, "require.user", "root");
132 ATF_TC_BODY(issetugid_ruid
, tc
)
135 if (check(setuid
, NULL
) != true)
136 atf_tc_fail("issetugid(2) failed with real UID");
142 ATF_TP_ADD_TC(tp
, issetugid_egid
);
143 ATF_TP_ADD_TC(tp
, issetugid_euid
);
144 ATF_TP_ADD_TC(tp
, issetugid_rgid
);
145 ATF_TP_ADD_TC(tp
, issetugid_ruid
);
147 return atf_no_error();