landlock.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/compiler.h>
  3. #include <linux/types.h>
  4. #include <unistd.h>
  5. #include "../tests.h"
  6. /* This workload was initially added to test enum augmentation with BTF in perf
  7. * trace because its the only syscall that has an enum argument. Since it is
  8. * a recent addition to the Linux kernel (at the time of the introduction of this
  9. * 'perf test' workload) we just add the required types and defines here instead
  10. * of including linux/landlock, that isn't available in older systems.
  11. *
  12. * We are not interested in the the result of the syscall, just in intercepting
  13. * its arguments.
  14. */
  15. #ifndef __NR_landlock_add_rule
  16. #define __NR_landlock_add_rule 445
  17. #endif
  18. #ifndef LANDLOCK_ACCESS_FS_READ_FILE
  19. #define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2)
  20. #define LANDLOCK_RULE_PATH_BENEATH 1
  21. struct landlock_path_beneath_attr {
  22. __u64 allowed_access;
  23. __s32 parent_fd;
  24. };
  25. #endif
  26. #ifndef LANDLOCK_ACCESS_NET_CONNECT_TCP
  27. #define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
  28. #define LANDLOCK_RULE_NET_PORT 2
  29. struct landlock_net_port_attr {
  30. __u64 allowed_access;
  31. __u64 port;
  32. };
  33. #endif
  34. static int landlock(int argc __maybe_unused, const char **argv __maybe_unused)
  35. {
  36. int fd = 11, flags = 45;
  37. struct landlock_path_beneath_attr path_beneath_attr = {
  38. .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE,
  39. .parent_fd = 14,
  40. };
  41. struct landlock_net_port_attr net_port_attr = {
  42. .port = 19,
  43. .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
  44. };
  45. syscall(__NR_landlock_add_rule, fd, LANDLOCK_RULE_PATH_BENEATH,
  46. &path_beneath_attr, flags);
  47. syscall(__NR_landlock_add_rule, fd, LANDLOCK_RULE_NET_PORT,
  48. &net_port_attr, flags);
  49. return 0;
  50. }
  51. DEFINE_WORKLOAD(landlock);