Path: blob/master/tools/testing/selftests/capabilities/validate_cap.c
26285 views
// SPDX-License-Identifier: GPL-2.01#include <cap-ng.h>2#include <linux/capability.h>3#include <stdbool.h>4#include <string.h>5#include <stdio.h>6#include <sys/prctl.h>7#include <sys/auxv.h>89#include "../kselftest.h"1011#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)12# define HAVE_GETAUXVAL13#endif1415static bool bool_arg(char **argv, int i)16{17if (!strcmp(argv[i], "0"))18return false;19else if (!strcmp(argv[i], "1"))20return true;21else {22ksft_exit_fail_msg("wrong argv[%d]\n", i);23return false;24}25}2627int main(int argc, char **argv)28{29const char *atsec = "";30int ret;3132/*33* Be careful just in case a setgid or setcapped copy of this34* helper gets out.35*/3637if (argc != 5)38ksft_exit_fail_msg("wrong argc\n");3940#ifdef HAVE_GETAUXVAL41if (getauxval(AT_SECURE))42atsec = " (AT_SECURE is set)";43else44atsec = " (AT_SECURE is not set)";45#endif4647ret = capng_get_caps_process();48if (ret == -1) {49ksft_print_msg("capng_get_caps_process failed\n");50return 1;51}5253if (capng_have_capability(CAPNG_EFFECTIVE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 1)) {54ksft_print_msg("Wrong effective state%s\n", atsec);55return 1;56}5758if (capng_have_capability(CAPNG_PERMITTED, CAP_NET_BIND_SERVICE) != bool_arg(argv, 2)) {59ksft_print_msg("Wrong permitted state%s\n", atsec);60return 1;61}6263if (capng_have_capability(CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 3)) {64ksft_print_msg("Wrong inheritable state%s\n", atsec);65return 1;66}6768if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != bool_arg(argv, 4)) {69ksft_print_msg("Wrong ambient state%s\n", atsec);70return 1;71}7273ksft_print_msg("%s: Capabilities after execve were correct\n",74"validate_cap:");75return 0;76}777879