cap_proc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 1997-8,2007,2011 Andrew G Morgan <morgan@kernel.org>
  3. *
  4. * This file deals with getting and setting capabilities on processes.
  5. */
  6. #include <sys/prctl.h>
  7. #include "libcap.h"
  8. cap_t cap_get_proc(void)
  9. {
  10. cap_t result;
  11. /* allocate a new capability set */
  12. result = cap_init();
  13. if (result) {
  14. _cap_debug("getting current process' capabilities");
  15. /* fill the capability sets via a system call */
  16. if (capget(&result->head, &result->u[0].set)) {
  17. cap_free(result);
  18. result = NULL;
  19. }
  20. }
  21. return result;
  22. }
  23. int cap_set_proc(cap_t cap_d)
  24. {
  25. int retval;
  26. if (!good_cap_t(cap_d)) {
  27. errno = EINVAL;
  28. return -1;
  29. }
  30. _cap_debug("setting process capabilities");
  31. retval = capset(&cap_d->head, &cap_d->u[0].set);
  32. return retval;
  33. }
  34. /* the following two functions are not required by POSIX */
  35. /* read the caps on a specific process */
  36. int capgetp(pid_t pid, cap_t cap_d)
  37. {
  38. int error;
  39. if (!good_cap_t(cap_d)) {
  40. errno = EINVAL;
  41. return -1;
  42. }
  43. _cap_debug("getting process capabilities for proc %d", pid);
  44. cap_d->head.pid = pid;
  45. error = capget(&cap_d->head, &cap_d->u[0].set);
  46. cap_d->head.pid = 0;
  47. return error;
  48. }
  49. /* allocate space for and return capabilities of target process */
  50. cap_t cap_get_pid(pid_t pid)
  51. {
  52. cap_t result;
  53. result = cap_init();
  54. if (result) {
  55. if (capgetp(pid, result) != 0) {
  56. int my_errno;
  57. my_errno = errno;
  58. cap_free(result);
  59. errno = my_errno;
  60. result = NULL;
  61. }
  62. }
  63. return result;
  64. }
  65. /* set the caps on a specific process/pg etc.. */
  66. int capsetp(pid_t pid, cap_t cap_d)
  67. {
  68. int error;
  69. if (!good_cap_t(cap_d)) {
  70. errno = EINVAL;
  71. return -1;
  72. }
  73. _cap_debug("setting process capabilities for proc %d", pid);
  74. cap_d->head.pid = pid;
  75. error = capset(&cap_d->head, &cap_d->u[0].set);
  76. cap_d->head.version = _LIBCAP_CAPABILITY_VERSION;
  77. cap_d->head.pid = 0;
  78. return error;
  79. }
  80. /* get a capability from the bounding set */
  81. int cap_get_bound(cap_value_t cap)
  82. {
  83. int result;
  84. result = prctl(PR_CAPBSET_READ, cap);
  85. return result;
  86. }
  87. /* drop a capability from the bounding set */
  88. int cap_drop_bound(cap_value_t cap)
  89. {
  90. int result;
  91. result = prctl(PR_CAPBSET_DROP, cap);
  92. return result;
  93. }