xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/capabilities/validate_cap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <cap-ng.h>
3*4882a593Smuzhiyun #include <linux/capability.h>
4*4882a593Smuzhiyun #include <stdbool.h>
5*4882a593Smuzhiyun #include <string.h>
6*4882a593Smuzhiyun #include <stdio.h>
7*4882a593Smuzhiyun #include <sys/prctl.h>
8*4882a593Smuzhiyun #include <sys/auxv.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "../kselftest.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #ifndef PR_CAP_AMBIENT
13*4882a593Smuzhiyun #define PR_CAP_AMBIENT			47
14*4882a593Smuzhiyun # define PR_CAP_AMBIENT_IS_SET		1
15*4882a593Smuzhiyun # define PR_CAP_AMBIENT_RAISE		2
16*4882a593Smuzhiyun # define PR_CAP_AMBIENT_LOWER		3
17*4882a593Smuzhiyun # define PR_CAP_AMBIENT_CLEAR_ALL	4
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)
21*4882a593Smuzhiyun # define HAVE_GETAUXVAL
22*4882a593Smuzhiyun #endif
23*4882a593Smuzhiyun 
bool_arg(char ** argv,int i)24*4882a593Smuzhiyun static bool bool_arg(char **argv, int i)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	if (!strcmp(argv[i], "0"))
27*4882a593Smuzhiyun 		return false;
28*4882a593Smuzhiyun 	else if (!strcmp(argv[i], "1"))
29*4882a593Smuzhiyun 		return true;
30*4882a593Smuzhiyun 	else {
31*4882a593Smuzhiyun 		ksft_exit_fail_msg("wrong argv[%d]\n", i);
32*4882a593Smuzhiyun 		return false;
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
main(int argc,char ** argv)36*4882a593Smuzhiyun int main(int argc, char **argv)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	const char *atsec = "";
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	/*
41*4882a593Smuzhiyun 	 * Be careful just in case a setgid or setcapped copy of this
42*4882a593Smuzhiyun 	 * helper gets out.
43*4882a593Smuzhiyun 	 */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	if (argc != 5)
46*4882a593Smuzhiyun 		ksft_exit_fail_msg("wrong argc\n");
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #ifdef HAVE_GETAUXVAL
49*4882a593Smuzhiyun 	if (getauxval(AT_SECURE))
50*4882a593Smuzhiyun 		atsec = " (AT_SECURE is set)";
51*4882a593Smuzhiyun 	else
52*4882a593Smuzhiyun 		atsec = " (AT_SECURE is not set)";
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	capng_get_caps_process();
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (capng_have_capability(CAPNG_EFFECTIVE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 1)) {
58*4882a593Smuzhiyun 		ksft_print_msg("Wrong effective state%s\n", atsec);
59*4882a593Smuzhiyun 		return 1;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (capng_have_capability(CAPNG_PERMITTED, CAP_NET_BIND_SERVICE) != bool_arg(argv, 2)) {
63*4882a593Smuzhiyun 		ksft_print_msg("Wrong permitted state%s\n", atsec);
64*4882a593Smuzhiyun 		return 1;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (capng_have_capability(CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 3)) {
68*4882a593Smuzhiyun 		ksft_print_msg("Wrong inheritable state%s\n", atsec);
69*4882a593Smuzhiyun 		return 1;
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != bool_arg(argv, 4)) {
73*4882a593Smuzhiyun 		ksft_print_msg("Wrong ambient state%s\n", atsec);
74*4882a593Smuzhiyun 		return 1;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	ksft_print_msg("%s: Capabilities after execve were correct\n",
78*4882a593Smuzhiyun 			"validate_cap:");
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81