• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************/
2 /*                                                                            */
3 /* Copyright (c) International Business Machines  Corp., 2007, 2008           */
4 /*                                                                            */
5 /* This program is free software;  you can redistribute it and/or modify      */
6 /* it under the terms of the GNU General Public License as published by       */
7 /* the Free Software Foundation; either version 2 of the License, or          */
8 /* (at your option) any later version.                                        */
9 /*                                                                            */
10 /* This program is distributed in the hope that it will be useful,            */
11 /* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
13 /* the GNU General Public License for more details.                           */
14 /*                                                                            */
15 /* You should have received a copy of the GNU General Public License          */
16 /* along with this program;  if not, write to the Free Software               */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
18 /*                                                                            */
19 /******************************************************************************/
20 /*
21  * File: exec_without_inh.c
22  * Author: Serge Hallyn
23  * Make sure that CAP_SYS_ADMIN is not in pI
24  * drop CAP_SYS_ADMIN from bounding set
25  * Then exec "check_pe 0"
26  * check_pe will return PASS if it does not have CAP_SYS_ADMIN in pE.
27  */
28 
29 #include <errno.h>
30 #include "config.h"
31 #if HAVE_SYS_CAPABILITY_H
32 #include <linux/types.h>
33 #include <sys/capability.h>
34 #endif
35 #include <sys/prctl.h>
36 #include "test.h"
37 
38 char *TCID = "exec_without_inh";
39 int TST_TOTAL = 1;
40 
main(void)41 int main(void)
42 {
43 #if HAVE_SYS_CAPABILITY_H
44 #if HAVE_DECL_PR_CAPBSET_DROP
45 #ifdef HAVE_LIBCAP
46 	int ret = 1;
47 	cap_flag_value_t f;
48 	cap_value_t v[1];
49 	cap_t cur;
50 
51 	/* Make sure CAP_SYS_ADMIN is not in pI */
52 	cur = cap_get_proc();
53 	ret = cap_get_flag(cur, CAP_SYS_ADMIN, CAP_INHERITABLE, &f);
54 	if (f == CAP_SET) {
55 		v[0] = CAP_SYS_ADMIN;
56 		ret = cap_set_flag(cur, CAP_INHERITABLE, 1, v, CAP_CLEAR);
57 		if (!ret)
58 			ret = cap_set_proc(cur);
59 		if (ret) {
60 			tst_brkm(TBROK,
61 				 NULL,
62 				 "Failed to drop cap_sys_admin from pI");
63 		}
64 	} else if (ret) {
65 		tst_brkm(TBROK | TERRNO, NULL, "Failed to add \
66 			CAP_SYS_ADMIN to pI");
67 	}
68 	cap_free(cur);
69 
70 	/* drop the capability from bounding set */
71 	ret = prctl(PR_CAPBSET_DROP, CAP_SYS_ADMIN);
72 	if (ret) {
73 		tst_resm(TFAIL,
74 			 "Failed to drop CAP_SYS_ADMIN from bounding set.");
75 		tst_resm(TINFO, "(ret=%d, errno %d)", ret, errno);
76 		tst_exit();
77 	}
78 
79 	/* execute "check_pe 0" */
80 	execl("check_pe", "check_pe", "0", NULL);
81 	tst_resm(TBROK, "Failed to execute check_pe (errno %d)", errno);
82 #else /* libcap */
83 	tst_resm(TCONF, "System doesn't have POSIX capabilities.");
84 #endif
85 #else /* HAVE_DECL_PR_CAPBSET_DROP */
86 	tst_resm(TCONF, "System doesn't have CAPBSET prctls");
87 #endif
88 #else /* capability_h */
89 	tst_resm(TCONF, "System doesn't have sys/capability.h.");
90 #endif
91 	tst_exit();
92 }
93