Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <cap-ng.h>
0003 #include <linux/capability.h>
0004 #include <stdbool.h>
0005 #include <string.h>
0006 #include <stdio.h>
0007 #include <sys/prctl.h>
0008 #include <sys/auxv.h>
0009 
0010 #include "../kselftest.h"
0011 
0012 #ifndef PR_CAP_AMBIENT
0013 #define PR_CAP_AMBIENT          47
0014 # define PR_CAP_AMBIENT_IS_SET      1
0015 # define PR_CAP_AMBIENT_RAISE       2
0016 # define PR_CAP_AMBIENT_LOWER       3
0017 # define PR_CAP_AMBIENT_CLEAR_ALL   4
0018 #endif
0019 
0020 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)
0021 # define HAVE_GETAUXVAL
0022 #endif
0023 
0024 static bool bool_arg(char **argv, int i)
0025 {
0026     if (!strcmp(argv[i], "0"))
0027         return false;
0028     else if (!strcmp(argv[i], "1"))
0029         return true;
0030     else {
0031         ksft_exit_fail_msg("wrong argv[%d]\n", i);
0032         return false;
0033     }
0034 }
0035 
0036 int main(int argc, char **argv)
0037 {
0038     const char *atsec = "";
0039 
0040     /*
0041      * Be careful just in case a setgid or setcapped copy of this
0042      * helper gets out.
0043      */
0044 
0045     if (argc != 5)
0046         ksft_exit_fail_msg("wrong argc\n");
0047 
0048 #ifdef HAVE_GETAUXVAL
0049     if (getauxval(AT_SECURE))
0050         atsec = " (AT_SECURE is set)";
0051     else
0052         atsec = " (AT_SECURE is not set)";
0053 #endif
0054 
0055     capng_get_caps_process();
0056 
0057     if (capng_have_capability(CAPNG_EFFECTIVE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 1)) {
0058         ksft_print_msg("Wrong effective state%s\n", atsec);
0059         return 1;
0060     }
0061 
0062     if (capng_have_capability(CAPNG_PERMITTED, CAP_NET_BIND_SERVICE) != bool_arg(argv, 2)) {
0063         ksft_print_msg("Wrong permitted state%s\n", atsec);
0064         return 1;
0065     }
0066 
0067     if (capng_have_capability(CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 3)) {
0068         ksft_print_msg("Wrong inheritable state%s\n", atsec);
0069         return 1;
0070     }
0071 
0072     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != bool_arg(argv, 4)) {
0073         ksft_print_msg("Wrong ambient state%s\n", atsec);
0074         return 1;
0075     }
0076 
0077     ksft_print_msg("%s: Capabilities after execve were correct\n",
0078             "validate_cap:");
0079     return 0;
0080 }