lsm_syscalls.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * System calls implementing the Linux Security Module API.
  4. *
  5. * Copyright (C) 2022 Casey Schaufler <casey@schaufler-ca.com>
  6. * Copyright (C) 2022 Intel Corporation
  7. */
  8. #include <asm/current.h>
  9. #include <linux/compiler_types.h>
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/security.h>
  13. #include <linux/stddef.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/types.h>
  16. #include <linux/lsm_hooks.h>
  17. #include <uapi/linux/lsm.h>
  18. /**
  19. * lsm_name_to_attr - map an LSM attribute name to its ID
  20. * @name: name of the attribute
  21. *
  22. * Returns the LSM attribute value associated with @name, or 0 if
  23. * there is no mapping.
  24. */
  25. u64 lsm_name_to_attr(const char *name)
  26. {
  27. if (!strcmp(name, "current"))
  28. return LSM_ATTR_CURRENT;
  29. if (!strcmp(name, "exec"))
  30. return LSM_ATTR_EXEC;
  31. if (!strcmp(name, "fscreate"))
  32. return LSM_ATTR_FSCREATE;
  33. if (!strcmp(name, "keycreate"))
  34. return LSM_ATTR_KEYCREATE;
  35. if (!strcmp(name, "prev"))
  36. return LSM_ATTR_PREV;
  37. if (!strcmp(name, "sockcreate"))
  38. return LSM_ATTR_SOCKCREATE;
  39. return LSM_ATTR_UNDEF;
  40. }
  41. /**
  42. * sys_lsm_set_self_attr - Set current task's security module attribute
  43. * @attr: which attribute to set
  44. * @ctx: the LSM contexts
  45. * @size: size of @ctx
  46. * @flags: reserved for future use
  47. *
  48. * Sets the calling task's LSM context. On success this function
  49. * returns 0. If the attribute specified cannot be set a negative
  50. * value indicating the reason for the error is returned.
  51. */
  52. SYSCALL_DEFINE4(lsm_set_self_attr, unsigned int, attr, struct lsm_ctx __user *,
  53. ctx, u32, size, u32, flags)
  54. {
  55. return security_setselfattr(attr, ctx, size, flags);
  56. }
  57. /**
  58. * sys_lsm_get_self_attr - Return current task's security module attributes
  59. * @attr: which attribute to return
  60. * @ctx: the user-space destination for the information, or NULL
  61. * @size: pointer to the size of space available to receive the data
  62. * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
  63. * attributes associated with the LSM identified in the passed @ctx be
  64. * reported.
  65. *
  66. * Returns the calling task's LSM contexts. On success this
  67. * function returns the number of @ctx array elements. This value
  68. * may be zero if there are no LSM contexts assigned. If @size is
  69. * insufficient to contain the return data -E2BIG is returned and
  70. * @size is set to the minimum required size. In all other cases
  71. * a negative value indicating the error is returned.
  72. */
  73. SYSCALL_DEFINE4(lsm_get_self_attr, unsigned int, attr, struct lsm_ctx __user *,
  74. ctx, u32 __user *, size, u32, flags)
  75. {
  76. return security_getselfattr(attr, ctx, size, flags);
  77. }
  78. /**
  79. * sys_lsm_list_modules - Return a list of the active security modules
  80. * @ids: the LSM module ids
  81. * @size: pointer to size of @ids, updated on return
  82. * @flags: reserved for future use, must be zero
  83. *
  84. * Returns a list of the active LSM ids. On success this function
  85. * returns the number of @ids array elements. This value may be zero
  86. * if there are no LSMs active. If @size is insufficient to contain
  87. * the return data -E2BIG is returned and @size is set to the minimum
  88. * required size. In all other cases a negative value indicating the
  89. * error is returned.
  90. */
  91. SYSCALL_DEFINE3(lsm_list_modules, u64 __user *, ids, u32 __user *, size,
  92. u32, flags)
  93. {
  94. u32 total_size = lsm_active_cnt * sizeof(*ids);
  95. u32 usize;
  96. int i;
  97. if (flags)
  98. return -EINVAL;
  99. if (get_user(usize, size))
  100. return -EFAULT;
  101. if (put_user(total_size, size) != 0)
  102. return -EFAULT;
  103. if (usize < total_size)
  104. return -E2BIG;
  105. for (i = 0; i < lsm_active_cnt; i++)
  106. if (put_user(lsm_idlist[i]->id, ids++))
  107. return -EFAULT;
  108. return lsm_active_cnt;
  109. }