lsm_syscalls.c 3.6 KB

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